From 7fc855aaef7b155105c33880c73098772b7d217a Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 29 Jul 2022 08:47:37 +0200 Subject: [PATCH] improve watch history --- .../libretube/adapters/WatchHistoryAdapter.kt | 19 +-- .../fragments/WatchHistoryFragment.kt | 10 +- .../libretube/preferences/PreferenceHelper.kt | 53 +++---- app/src/main/res/layout/fragment_channel.xml | 3 +- app/src/main/res/layout/fragment_player.xml | 1 - app/src/main/res/layout/fragment_playlist.xml | 1 - .../res/layout/fragment_watch_history.xml | 56 ++++---- app/src/main/res/layout/playlists_row.xml | 4 +- app/src/main/res/layout/watch_history_row.xml | 129 ++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 10 files changed, 189 insertions(+), 88 deletions(-) create mode 100644 app/src/main/res/layout/watch_history_row.xml diff --git a/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt index 4bd70b516..8da51363e 100644 --- a/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt @@ -5,9 +5,10 @@ import android.view.LayoutInflater import android.view.ViewGroup import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.RecyclerView -import com.github.libretube.databinding.VideoRowBinding +import com.github.libretube.databinding.WatchHistoryRowBinding import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.obj.WatchHistoryItem +import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.NavigationHelper import com.github.libretube.util.setWatchProgressLength @@ -19,15 +20,9 @@ class WatchHistoryAdapter( RecyclerView.Adapter() { private val TAG = "WatchHistoryAdapter" - fun clear() { - val size = watchHistory.size - watchHistory.clear() - notifyItemRangeRemoved(0, size) - } - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WatchHistoryViewHolder { val layoutInflater = LayoutInflater.from(parent.context) - val binding = VideoRowBinding.inflate(layoutInflater, parent, false) + val binding = WatchHistoryRowBinding.inflate(layoutInflater, parent, false) return WatchHistoryViewHolder(binding) } @@ -45,6 +40,12 @@ class WatchHistoryAdapter( NavigationHelper.navigateChannel(root.context, video.uploaderUrl) } + deleteBTN.setOnClickListener { + PreferenceHelper.removeFromWatchHistory(video.videoId!!) + watchHistory.removeAt(position) + notifyItemRemoved(position) + } + root.setOnClickListener { NavigationHelper.navigateVideo(root.context, video.videoId) } @@ -63,5 +64,5 @@ class WatchHistoryAdapter( } } -class WatchHistoryViewHolder(val binding: VideoRowBinding) : +class WatchHistoryViewHolder(val binding: WatchHistoryRowBinding) : RecyclerView.ViewHolder(binding.root) diff --git a/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt b/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt index 03f00c576..43c5e3a4d 100644 --- a/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt @@ -27,12 +27,12 @@ class WatchHistoryFragment : Fragment() { super.onViewCreated(view, savedInstanceState) val watchHistory = PreferenceHelper.getWatchHistory() - val watchHistoryAdapter = WatchHistoryAdapter(watchHistory, childFragmentManager) - binding.watchHistoryRecView.adapter = watchHistoryAdapter - binding.clearHistory.setOnClickListener { - PreferenceHelper.removePreference("watch_history") - watchHistoryAdapter.clear() + if (watchHistory.isNotEmpty()) { + val watchHistoryAdapter = WatchHistoryAdapter(watchHistory, childFragmentManager) + binding.watchHistoryRecView.adapter = watchHistoryAdapter + binding.historyEmpty.visibility = View.GONE + binding.watchHistoryRecView.visibility = View.VISIBLE } // reverse order diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt index 89522853d..d2c038768 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt @@ -25,38 +25,10 @@ object PreferenceHelper { editor = settings.edit() } - fun setString(key: String?, value: String?) { - editor.putString(key, value) - editor.apply() - } - - fun setInt(key: String?, value: Int) { - editor.putInt(key, value) - editor.apply() - } - - fun setLong(key: String?, value: Long) { - editor.putLong(key, value) - editor.apply() - } - - fun setBoolean(key: String?, value: Boolean) { - editor.putBoolean(key, value) - editor.apply() - } - fun getString(key: String?, defValue: String?): String { return settings.getString(key, defValue)!! } - fun getInt(key: String?, defValue: Int): Int { - return settings.getInt(key, defValue) - } - - fun getLong(key: String?, defValue: Long): Long { - return settings.getLong(key, defValue) - } - fun getBoolean(key: String?, defValue: Boolean): Boolean { return settings.getBoolean(key, defValue) } @@ -153,7 +125,7 @@ object PreferenceHelper { } fun addToWatchHistory(videoId: String, streams: Streams) { - val mapper = ObjectMapper() + removeFromWatchHistory(videoId) val watchHistoryItem = WatchHistoryItem( videoId, @@ -166,21 +138,30 @@ object PreferenceHelper { streams.duration ) + val mapper = ObjectMapper() val watchHistory = getWatchHistory() - // delete entries that have the same videoId - var indexToRemove: Int? = null - watchHistory.forEachIndexed { index, item -> - if (item.videoId == videoId) indexToRemove = index - } - if (indexToRemove != null) watchHistory.removeAt(indexToRemove!!) - watchHistory += watchHistoryItem val json = mapper.writeValueAsString(watchHistory) editor.putString("watch_history", json).apply() } + fun removeFromWatchHistory(videoId: String) { + val mapper = ObjectMapper() + val watchHistory = getWatchHistory() + + var indexToRemove: Int? = null + watchHistory.forEachIndexed { index, item -> + if (item.videoId == videoId) indexToRemove = index + } + if (indexToRemove != null) { + watchHistory.removeAt(indexToRemove!!) + val json = mapper.writeValueAsString(watchHistory) + editor.putString("watch_history", json).commit() + } + } + fun getWatchHistory(): ArrayList { val mapper = ObjectMapper() diff --git a/app/src/main/res/layout/fragment_channel.xml b/app/src/main/res/layout/fragment_channel.xml index cb5501d22..14a75275a 100644 --- a/app/src/main/res/layout/fragment_channel.xml +++ b/app/src/main/res/layout/fragment_channel.xml @@ -97,8 +97,7 @@ android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:layout_marginBottom="15dp" - android:autoLink="web" - android:text="" /> + android:autoLink="web" /> diff --git a/app/src/main/res/layout/fragment_playlist.xml b/app/src/main/res/layout/fragment_playlist.xml index c3e29c5e9..e4b2c1201 100644 --- a/app/src/main/res/layout/fragment_playlist.xml +++ b/app/src/main/res/layout/fragment_playlist.xml @@ -34,7 +34,6 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" - android:text="" android:textSize="24sp" android:textStyle="bold" /> diff --git a/app/src/main/res/layout/fragment_watch_history.xml b/app/src/main/res/layout/fragment_watch_history.xml index 3e522b29d..da62ec3cc 100644 --- a/app/src/main/res/layout/fragment_watch_history.xml +++ b/app/src/main/res/layout/fragment_watch_history.xml @@ -1,43 +1,37 @@ - - + + - - - - - - - - - + android:layout_marginHorizontal="10dp" + android:gravity="center" + android:text="@string/history_empty" + android:textSize="20sp" + android:textStyle="bold" /> - \ No newline at end of file + + + \ No newline at end of file diff --git a/app/src/main/res/layout/playlists_row.xml b/app/src/main/res/layout/playlists_row.xml index 2d5695c9d..c7be905ae 100644 --- a/app/src/main/res/layout/playlists_row.xml +++ b/app/src/main/res/layout/playlists_row.xml @@ -42,7 +42,6 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" - android:text="" app:layout_constraintEnd_toStartOf="@+id/delete_playlist" app:layout_constraintStart_toEndOf="@+id/card_playlist_thumbnail" app:layout_constraintTop_toTopOf="parent" /> @@ -52,8 +51,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" - android:text="" - app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintEnd_toStartOf="@id/delete_playlist" app:layout_constraintStart_toEndOf="@+id/card_playlist_thumbnail" app:layout_constraintTop_toBottomOf="@+id/playlist_title" /> diff --git a/app/src/main/res/layout/watch_history_row.xml b/app/src/main/res/layout/watch_history_row.xml new file mode 100644 index 000000000..a15034ebd --- /dev/null +++ b/app/src/main/res/layout/watch_history_row.xml @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 41dcfbc47..ee9a0bd40 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -266,4 +266,5 @@ Worst quality Default subtitle language Are you sure? This can\'t be undone! + History is empty. \ No newline at end of file