From 1876fa643ccc5ab7a211bbfba79d62d97f1c9371 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 15 Jan 2024 18:00:43 +0100 Subject: [PATCH] feat: add 'hide watched videos' to feed filter options --- .../github/libretube/constants/IntentData.kt | 1 + .../ui/fragments/SubscriptionsFragment.kt | 45 +++++++++++-------- .../ui/sheets/FilterSortBottomSheet.kt | 28 +++++++++--- app/src/main/res/layout/filter_sort_sheet.xml | 7 +++ app/src/main/res/values/strings.xml | 2 +- app/src/main/res/xml/advanced_settings.xml | 6 --- 6 files changed, 59 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/github/libretube/constants/IntentData.kt b/app/src/main/java/com/github/libretube/constants/IntentData.kt index f5bd35433..c981bceaf 100644 --- a/app/src/main/java/com/github/libretube/constants/IntentData.kt +++ b/app/src/main/java/com/github/libretube/constants/IntentData.kt @@ -35,4 +35,5 @@ object IntentData { const val isCurrentlyPlaying = "isCurrentlyPlaying" const val isSubscribed = "isSubscribed" const val sortOptions = "sortOptions" + const val hideWatched = "hideWatched" } 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 296f7a2cf..557719eb0 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 @@ -40,7 +40,6 @@ import com.github.libretube.ui.models.SubscriptionsViewModel import com.github.libretube.ui.sheets.ChannelGroupsSheet import com.github.libretube.ui.sheets.FilterSortBottomSheet import com.github.libretube.ui.sheets.FilterSortBottomSheet.Companion.FILTER_SORT_REQUEST_KEY -import com.github.libretube.ui.sheets.FilterSortBottomSheet.Companion.SELECTED_SORT_OPTION_KEY import com.github.libretube.util.PlayingQueue import com.google.android.material.chip.Chip import kotlinx.coroutines.Dispatchers @@ -65,6 +64,13 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() { field = value } + private var hideWatched = + PreferenceHelper.getBoolean(PreferenceKeys.HIDE_WATCHED_FROM_FEED, false) + set(value) { + PreferenceHelper.putBoolean(PreferenceKeys.HIDE_WATCHED_FROM_FEED, value) + field = value + } + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -173,19 +179,25 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() { } private fun setupSortAndFilter() { - binding.filterSort.setOnClickListener { + binding.filterSort.setOnClickListener { val activityCompat = context as AppCompatActivity val fragManager = activityCompat .supportFragmentManager .apply { setFragmentResultListener(FILTER_SORT_REQUEST_KEY, activityCompat) { _, resultBundle -> - selectedSortOrder = resultBundle.getInt(SELECTED_SORT_OPTION_KEY) + selectedSortOrder = resultBundle.getInt(IntentData.sortOptions) + hideWatched = resultBundle.getBoolean(IntentData.hideWatched) showFeed() } } FilterSortBottomSheet() - .apply { arguments = bundleOf(IntentData.sortOptions to fetchSortOptions()) } + .apply { + arguments = bundleOf( + IntentData.sortOptions to fetchSortOptions(), + IntentData.hideWatched to hideWatched + ) + } .show(fragManager) } } @@ -215,7 +227,11 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() { PlayingQueue.clear() PlayingQueue.add(*streams.toTypedArray()) - NavigationHelper.navigateVideo(requireContext(), videoUrlOrId = streams.first().url, keepQueue = true) + NavigationHelper.navigateVideo( + requireContext(), + videoUrlOrId = streams.first().url, + keepQueue = true + ) } @SuppressLint("InflateParams") @@ -266,23 +282,16 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() { val isVideo = !it.isShort && !it.isLive return@filter when { - !ContentFilter.SHORTS.isEnabled() && it.isShort -> false - !ContentFilter.VIDEOS.isEnabled() && isVideo -> false + !ContentFilter.SHORTS.isEnabled() && it.isShort -> false + !ContentFilter.VIDEOS.isEnabled() && isVideo -> false !ContentFilter.LIVESTREAMS.isEnabled() && it.isLive -> false - else -> true + else -> true } - } - if (!PreferenceHelper.getBoolean( - PreferenceKeys.HIDE_WATCHED_FROM_FEED, - false - ) - ) { - return streamItems - } - - return runBlocking { DatabaseHelper.filterUnwatched(streamItems) } + return if (hideWatched) runBlocking { + DatabaseHelper.filterUnwatched(streamItems) + } else streamItems } private fun List.sortedBySelectedOrder() = when (selectedSortOrder) { diff --git a/app/src/main/java/com/github/libretube/ui/sheets/FilterSortBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/FilterSortBottomSheet.kt index da6a83ea1..621f021ac 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/FilterSortBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/FilterSortBottomSheet.kt @@ -1,5 +1,6 @@ package com.github.libretube.ui.sheets +import android.os.Build import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -19,10 +20,17 @@ class FilterSortBottomSheet: ExpandedBottomSheet() { private lateinit var sortOptions: Array - private var selectedIndex: Int = 0 + private var selectedIndex = 0 + private var hideWatched = false override fun onCreate(savedInstanceState: Bundle?) { - sortOptions = requireArguments().getParcelableArray(IntentData.sortOptions) as Array + sortOptions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + requireArguments().getParcelableArray(IntentData.sortOptions, SelectableOption::class.java)!! + } else { + @Suppress("DEPRECATION") + requireArguments().getParcelableArray(IntentData.sortOptions) as Array + } + hideWatched = requireArguments().getBoolean(IntentData.hideWatched) super.onCreate(savedInstanceState) } @@ -38,6 +46,7 @@ class FilterSortBottomSheet: ExpandedBottomSheet() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { addSortOptions() observeSortChanges() + observeHideWatchedChanges() setInitialFiltersState() observeFiltersChanges() } @@ -71,10 +80,18 @@ class FilterSortBottomSheet: ExpandedBottomSheet() { } } + private fun observeHideWatchedChanges() { + binding.hideWatchedCheckbox.setOnCheckedChangeListener { _, checked -> + hideWatched = checked + notifyChange() + } + } + private fun setInitialFiltersState() { binding.filterVideos.isChecked = ContentFilter.VIDEOS.isEnabled() binding.filterShorts.isChecked = ContentFilter.SHORTS.isEnabled() binding.filterLivestreams.isChecked = ContentFilter.LIVESTREAMS.isEnabled() + binding.hideWatchedCheckbox.isChecked = hideWatched } private fun observeFiltersChanges() { @@ -89,7 +106,10 @@ class FilterSortBottomSheet: ExpandedBottomSheet() { private fun notifyChange() { setFragmentResult( requestKey = FILTER_SORT_REQUEST_KEY, - result = bundleOf(SELECTED_SORT_OPTION_KEY to selectedIndex) + result = bundleOf( + IntentData.sortOptions to selectedIndex, + IntentData.hideWatched to hideWatched + ) ) } @@ -100,7 +120,5 @@ class FilterSortBottomSheet: ExpandedBottomSheet() { companion object { const val FILTER_SORT_REQUEST_KEY = "filter_sort_request_key" - const val SELECTED_SORT_OPTION_KEY = "selected_sort_option_key" } - } \ No newline at end of file diff --git a/app/src/main/res/layout/filter_sort_sheet.xml b/app/src/main/res/layout/filter_sort_sheet.xml index 89c2495af..6883156ba 100644 --- a/app/src/main/res/layout/filter_sort_sheet.xml +++ b/app/src/main/res/layout/filter_sort_sheet.xml @@ -89,6 +89,13 @@ android:layout_height="wrap_content" android:paddingHorizontal="16dp"/> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5a4c82c3c..60c935a34 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -375,7 +375,7 @@ Nothing selected! Versatile Violet Could not fetch available instances. - Hide watched videos from feed + Hide already watched videos Don\'t show videos being watched more than 90% in the subscriptions tab Playlist URL Pause on quit diff --git a/app/src/main/res/xml/advanced_settings.xml b/app/src/main/res/xml/advanced_settings.xml index 2a1c6a7c3..6590585fd 100644 --- a/app/src/main/res/xml/advanced_settings.xml +++ b/app/src/main/res/xml/advanced_settings.xml @@ -56,12 +56,6 @@ app:key="confirm_unsubscribing" app:title="@string/confirm_unsubscribing" /> - -