-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ukrainefield/feature/bookmarks
Feature/bookmarks
- Loading branch information
Showing
13 changed files
with
288 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/app/src/main/java/nl/gardensnakes/ukrainefield/BookmarkFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package nl.gardensnakes.ukrainefield | ||
|
||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.google.firebase.analytics.FirebaseAnalytics | ||
import nl.gardensnakes.ukrainefield.data.remote.SavedPreferences | ||
import nl.gardensnakes.ukrainefield.helper.BookmarkHelper | ||
import nl.gardensnakes.ukrainefield.view.adapter.FeedCardAdapter | ||
|
||
class BookmarkFragment : Fragment() { | ||
|
||
private lateinit var feedRecyclerView: RecyclerView | ||
private lateinit var noBookmarksText: TextView | ||
|
||
private lateinit var feedCardAdapter: FeedCardAdapter | ||
private var useProxyServer: Boolean = false | ||
private lateinit var mFirebaseAnalytics: FirebaseAnalytics | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
// Inflate the layout for this fragment | ||
val view = inflater.inflate(R.layout.fragment_bookmark, container, false) | ||
|
||
mFirebaseAnalytics = FirebaseAnalytics.getInstance(view.context); | ||
|
||
useProxyServer = SavedPreferences.useProxyServer(requireContext()) | ||
feedRecyclerView = view.findViewById(R.id.bookmark_recycler_view) | ||
noBookmarksText = view.findViewById(R.id.bookmark_no_bookmark_text) | ||
|
||
val bookmarkedItems = BookmarkHelper().getAll(view.context) | ||
|
||
if(bookmarkedItems == null || bookmarkedItems.isEmpty()){ | ||
noBookmarksText.visibility = View.VISIBLE | ||
feedRecyclerView.visibility = View.GONE | ||
} | ||
else { | ||
noBookmarksText.visibility = View.GONE | ||
feedRecyclerView.visibility = View.VISIBLE | ||
feedCardAdapter = FeedCardAdapter(bookmarkedItems.sortedByDescending { it.epochTime}, true) | ||
feedRecyclerView.adapter = feedCardAdapter | ||
feedRecyclerView.layoutManager = LinearLayoutManager(view.context); | ||
} | ||
|
||
return view | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
app/app/src/main/java/nl/gardensnakes/ukrainefield/helper/BookmarkHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package nl.gardensnakes.ukrainefield.helper | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import nl.gardensnakes.ukrainefield.data.remote.dto.feed.FeedMessageResponse | ||
import java.io.* | ||
|
||
class BookmarkHelper { | ||
private var bookmarkedFeed: ArrayList<FeedMessageResponse>? = null | ||
|
||
fun bookmark(feedItem: FeedMessageResponse, context: Context) { | ||
val feedMessages = getAll(context) as ArrayList<FeedMessageResponse> | ||
|
||
val found = feedMessages.firstOrNull { | ||
it.messageURL == feedItem.messageURL | ||
} | ||
|
||
if(found == null) { | ||
feedMessages.add(feedItem) | ||
} else { | ||
feedMessages.remove(found) | ||
} | ||
|
||
val json = extractToJson(feedMessages); | ||
|
||
if(json != null) saveToJsonFile(json, context) | ||
} | ||
|
||
|
||
fun isFavorite(messageUrl: String, context: Context): Boolean { | ||
return getAll(context)?.any { | ||
it.messageURL == messageUrl | ||
} ?: false | ||
} | ||
|
||
fun getAll(context: Context?): List<FeedMessageResponse>? { | ||
return if(bookmarkedFeed != null) { | ||
return bookmarkedFeed | ||
} else { | ||
context?.let { | ||
readJsonFile(it) | ||
}?.let { | ||
extractToBookmark(it) | ||
} | ||
} | ||
} | ||
|
||
private fun extractToBookmark(videos: String): List<FeedMessageResponse>? { | ||
val listOfBookmarks = Gson().fromJson<List<FeedMessageResponse>>( | ||
videos, | ||
object : TypeToken<ArrayList<FeedMessageResponse>>() {}.type | ||
) ?: ArrayList(0) | ||
|
||
this.bookmarkedFeed = listOfBookmarks as ArrayList<FeedMessageResponse> | ||
|
||
return listOfBookmarks | ||
} | ||
|
||
private fun extractToJson(bookmarks: List<FeedMessageResponse>): String? { | ||
return Gson().toJson( | ||
bookmarks, | ||
List::class.java | ||
) | ||
} | ||
|
||
private fun saveToJsonFile(json: String, context: Context) { | ||
try { | ||
val outputStreamWriter = OutputStreamWriter( | ||
context.openFileOutput( | ||
"bookmarks.txt", | ||
Context.MODE_PRIVATE | ||
) | ||
) | ||
|
||
outputStreamWriter.write(json) | ||
outputStreamWriter.close() | ||
} catch (e: IOException) { | ||
Log.e("Exception", "File write failed: $e") | ||
} | ||
} | ||
|
||
private fun readJsonFile(context: Context): String { | ||
var ret = "" | ||
|
||
try { | ||
val inputStream: InputStream? = context.openFileInput("bookmarks.txt") | ||
if (inputStream != null) { | ||
val inputStreamReader = InputStreamReader(inputStream) | ||
val bufferedReader = BufferedReader(inputStreamReader) | ||
var receiveString: String? = "" | ||
val stringBuilder = StringBuilder() | ||
while (bufferedReader.readLine().also { receiveString = it } != null) { | ||
stringBuilder.append("\n").append(receiveString) | ||
} | ||
inputStream.close() | ||
ret = stringBuilder.toString() | ||
} | ||
} catch (e: FileNotFoundException) { | ||
Log.e("BookmarkHelper", "File not found: $e") | ||
} catch (e: IOException) { | ||
Log.e("BookmarkHelper", "Can not read file: $e") | ||
} | ||
|
||
return ret | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24" | ||
android:tint="?attr/colorControlNormal"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M17,3H7c-1.1,0 -1.99,0.9 -1.99,2L5,21l7,-3 7,3V5c0,-1.1 -0.9,-2 -2,-2z"/> | ||
</vector> |
10 changes: 10 additions & 0 deletions
10
app/app/src/main/res/drawable/ic_baseline_no_bookmark_24.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24" | ||
android:tint="?attr/colorControlNormal"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M17,3L7,3c-1.1,0 -1.99,0.9 -1.99,2L5,21l7,-3 7,3L19,5c0,-1.1 -0.9,-2 -2,-2zM17,18l-5,-2.18L7,18L7,5h10v13z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".BookmarkFragment"> | ||
|
||
<TextView | ||
android:id="@+id/bookmark_no_bookmark_text" | ||
android:visibility="gone" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:fontFamily="@font/roboto_medium" | ||
android:text="Nothing bookmarked yet. \nBookmark some messages" | ||
android:textAlignment="center" | ||
android:textSize="24sp" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
|
||
<androidx.recyclerview.widget.RecyclerView | ||
android:id="@+id/bookmark_recycler_view" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:itemCount="5" | ||
tools:listitem="@layout/feed_card" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
Oops, something went wrong.