From 0ecd9fbf287f76b520e60fa2e02314808422e24d Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 21 Dec 2022 12:00:58 +0100 Subject: [PATCH] Add option to hide watched videos from the feed --- .../libretube/constants/PreferenceKeys.kt | 1 + .../libretube/ui/adapters/VideosAdapter.kt | 35 +++++++++++-------- .../ui/extensions/SetWatchProgressLength.kt | 23 +++++++----- .../ui/fragments/SubscriptionsFragment.kt | 3 +- app/src/main/res/drawable/ic_invisible.xml | 10 ++++++ app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/advanced_settings.xml | 8 ++++- 7 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 app/src/main/res/drawable/ic_invisible.xml diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index 927b1a53b..978a7da9d 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -109,6 +109,7 @@ object PreferenceKeys { */ const val LAST_STREAM_VIDEO_ID = "last_stream_video_id" const val LAST_WATCHED_FEED_TIME = "last_watched_feed_time" + const val HIDE_WATCHED_FROM_FEED = "hide_watched_from_feed" /** * Advanced diff --git a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt index 349e75397..e547988a1 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt @@ -32,7 +32,8 @@ import com.github.libretube.util.TextUtils class VideosAdapter( private val streamItems: MutableList, private val showAllAtOnce: Boolean = true, - private val forceMode: ForceMode = ForceMode.NONE + private val forceMode: ForceMode = ForceMode.NONE, + private val hideWatched: Boolean = false ) : RecyclerView.Adapter() { var index = 10 @@ -81,17 +82,33 @@ class VideosAdapter( } } + private fun hideItemView(holder: VideosViewHolder) { + holder.itemView.visibility = View.GONE + holder.itemView.layoutParams = RecyclerView.LayoutParams(0, 0) + } + @SuppressLint("SetTextI18n") override fun onBindViewHolder(holder: VideosViewHolder, position: Int) { val video = streamItems[position] + val videoId = video.url?.toID() + val videoName = video.title + // hide the item if there was an extractor error if (video.title == null && video.type != "caught") { - holder.itemView.visibility = View.GONE - holder.itemView.layoutParams = RecyclerView.LayoutParams(0, 0) + hideItemView(holder) return } + videoId?.let { + val shouldHide = (holder.trendingRowBinding?.watchProgress ?: holder.videoRowBinding!!.watchProgress) + .setWatchProgressLength(it, video.duration ?: 0L) + if (hideWatched && shouldHide) { + hideItemView(holder) + return + } + } + // Trending layout holder.trendingRowBinding?.apply { // set a fixed width for better visuals @@ -120,8 +137,6 @@ class VideosAdapter( root.setOnClickListener { NavigationHelper.navigateVideo(root.context, video.url) } - val videoId = video.url?.toID() - val videoName = video.title root.setOnLongClickListener { if (videoId == null || videoName == null) return@setOnLongClickListener true @@ -133,9 +148,6 @@ class VideosAdapter( true } - if (videoId != null) { - watchProgress.setWatchProgressLength(videoId, video.duration ?: 0L) - } } // Normal videos row layout @@ -167,8 +179,6 @@ class VideosAdapter( NavigationHelper.navigateVideo(root.context, video.url) } - val videoId = video.url?.toID() - val videoName = video.title root.setOnLongClickListener { if (videoId == null || videoName == null) return@setOnLongClickListener true VideoOptionsBottomSheet(videoId, videoName) @@ -176,13 +186,8 @@ class VideosAdapter( (root.context as BaseActivity).supportFragmentManager, VideoOptionsBottomSheet::class.java.name ) - true } - - if (videoId != null) { - watchProgress.setWatchProgressLength(videoId, video.duration ?: 0L) - } } } diff --git a/app/src/main/java/com/github/libretube/ui/extensions/SetWatchProgressLength.kt b/app/src/main/java/com/github/libretube/ui/extensions/SetWatchProgressLength.kt index fd9b3436d..a079b5b45 100644 --- a/app/src/main/java/com/github/libretube/ui/extensions/SetWatchProgressLength.kt +++ b/app/src/main/java/com/github/libretube/ui/extensions/SetWatchProgressLength.kt @@ -7,9 +7,12 @@ import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.extensions.awaitQuery /** - * shows the already watched time under the video + * Shows the already watched time under the video + * @param videoId The id of the video to inspect + * @param duration The duration of the video in seconds + * @return Whether the video is already watched more than 90% */ -fun View?.setWatchProgressLength(videoId: String, duration: Long) { +fun View?.setWatchProgressLength(videoId: String, duration: Long): Boolean { val view = this!! val progress = try { @@ -17,23 +20,27 @@ fun View?.setWatchProgressLength(videoId: String, duration: Long) { Database.watchPositionDao().findById(videoId)?.position } } catch (e: Exception) { - return + return false + } // divide by 1000 to convert ms to seconds + ?.toFloat()?.div(1000) + + if (progress == null || duration == 0L) { + view.visibility = View.GONE + return false } view.viewTreeObserver .addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { this@setWatchProgressLength.viewTreeObserver.removeOnGlobalLayoutListener(this) - if (progress == null || duration == 0L) { - view.visibility = View.GONE - return - } val fullWidth = (parent as LinearLayout).width - val newWidth = (fullWidth * (progress / duration)) / 1000 + val newWidth = fullWidth * (progress / duration.toFloat()) val lp = view.layoutParams lp.width = newWidth.toInt() view.layoutParams = lp view.visibility = View.VISIBLE } }) + + return (progress ?: 0f) / duration.toFloat() > 0.9 } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt index 55226058d..5536fec9c 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SubscriptionsFragment.kt @@ -167,7 +167,8 @@ class SubscriptionsFragment : BaseFragment() { binding.subProgress.visibility = View.GONE subscriptionAdapter = VideosAdapter( sortedFeed.toMutableList(), - showAllAtOnce = false + showAllAtOnce = false, + hideWatched = PreferenceHelper.getBoolean(PreferenceKeys.HIDE_WATCHED_FROM_FEED, false) ) binding.subFeed.adapter = subscriptionAdapter diff --git a/app/src/main/res/drawable/ic_invisible.xml b/app/src/main/res/drawable/ic_invisible.xml new file mode 100644 index 000000000..2a2ff9a76 --- /dev/null +++ b/app/src/main/res/drawable/ic_invisible.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9979b3d99..a4306749d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -418,6 +418,8 @@ Nothing selected! Versatile Violet Failed to fetch available instances. + Hide watched videos from feed + Don\'t show videos being watched more than 90% in the subscriptions tab. Download Service diff --git a/app/src/main/res/xml/advanced_settings.xml b/app/src/main/res/xml/advanced_settings.xml index c9a298603..cf028a26d 100644 --- a/app/src/main/res/xml/advanced_settings.xml +++ b/app/src/main/res/xml/advanced_settings.xml @@ -18,7 +18,7 @@ app:key="image_cache_size" app:title="@string/maximum_image_cache" app:useSimpleSummaryProvider="true" /> - + + +