From 53a76d2bc97e9ce961bfc9eae9362ac6247e086e Mon Sep 17 00:00:00 2001 From: FineFindus <63370021+FineFindus@users.noreply.github.com> Date: Mon, 31 Mar 2025 16:30:22 +0200 Subject: [PATCH] fix(WatchHistory): always show filter bar (#7258) * fix(WatchHistory): always show filter bar Fixes an issue, where the users could get soft-locked, by selecting a filter, which had no items, leading to the filter bar being hidden, and thus making the user unable to deselect the filter. This is fixed, by only hiding recyclerView/No items views. * feat(WatchHistory): disable 'Play all' if history is empty * feat(WatchHistory): disable clear button, if history is empty --- .../ui/fragments/WatchHistoryFragment.kt | 18 ++++++++++++--- .../sources/WatchHistoryPagingSource.kt | 22 ------------------- .../res/layout/fragment_watch_history.xml | 4 +--- 3 files changed, 16 insertions(+), 28 deletions(-) delete mode 100644 app/src/main/java/com/github/libretube/ui/models/sources/WatchHistoryPagingSource.kt diff --git a/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt index 4973b22ca..b60326b08 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/WatchHistoryFragment.kt @@ -31,8 +31,10 @@ import com.github.libretube.ui.models.WatchHistoryModel import com.github.libretube.ui.sheets.BaseBottomSheet import com.github.libretube.util.PlayingQueue import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class WatchHistoryFragment : DynamicLayoutManagerFragment(R.layout.fragment_watch_history) { private var _binding: FragmentWatchHistoryBinding? = null @@ -67,7 +69,7 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment(R.layout.fragment_watc RecyclerView.AdapterDataObserver() { override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { if (watchHistoryAdapter.itemCount == 0) { - binding.historyContainer.isGone = true + binding.watchHistoryRecView.isGone = true binding.historyEmpty.isVisible = true } } @@ -98,7 +100,7 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment(R.layout.fragment_watc selected[index] = newValue } .setPositiveButton(R.string.okay) { _, _ -> - binding.historyContainer.isGone = true + binding.watchHistoryRecView.isGone = true binding.historyEmpty.isVisible = true lifecycleScope.launch(Dispatchers.IO) { Database.withTransaction { @@ -149,7 +151,8 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment(R.layout.fragment_watc viewModel.filteredWatchHistory.observe(viewLifecycleOwner) { history -> binding.historyEmpty.isGone = history.isNotEmpty() - binding.historyContainer.isVisible = history.isNotEmpty() + binding.playAll.isEnabled = history.isNotEmpty() + binding.watchHistoryRecView.isVisible = history.isNotEmpty() watchHistoryAdapter.submitList(history) } @@ -163,6 +166,15 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment(R.layout.fragment_watc if (NavBarHelper.getStartFragmentId(requireContext()) != R.id.watchHistoryFragment) { setupFragmentAnimation(binding.root) } + + CoroutineScope(Dispatchers.IO).launch { + val hasItems = Database.watchHistoryDao().getSize() != 0 + + withContext(Dispatchers.Main) { + binding.clear.isEnabled = hasItems + } + } + } override fun onConfigurationChanged(newConfig: Configuration) { diff --git a/app/src/main/java/com/github/libretube/ui/models/sources/WatchHistoryPagingSource.kt b/app/src/main/java/com/github/libretube/ui/models/sources/WatchHistoryPagingSource.kt deleted file mode 100644 index dccb4237a..000000000 --- a/app/src/main/java/com/github/libretube/ui/models/sources/WatchHistoryPagingSource.kt +++ /dev/null @@ -1,22 +0,0 @@ -package com.github.libretube.ui.models.sources - -import androidx.paging.PagingSource -import androidx.paging.PagingState -import com.github.libretube.db.DatabaseHelper -import com.github.libretube.db.obj.WatchHistoryItem -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext - -class WatchHistoryPagingSource( - private val shouldIncludeItemPredicate: suspend (WatchHistoryItem) -> Boolean -): PagingSource() { - override fun getRefreshKey(state: PagingState) = null - - override suspend fun load(params: LoadParams): LoadResult { - val newHistory = withContext(Dispatchers.IO) { - DatabaseHelper.getWatchHistoryPage( params.key ?: 0, params.loadSize) - }.filter { shouldIncludeItemPredicate(it) } - - return LoadResult.Page(newHistory, params.key ?: 0, params.key?.plus(1) ?: 0) - } -} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_watch_history.xml b/app/src/main/res/layout/fragment_watch_history.xml index 69aac96aa..2efec9a40 100644 --- a/app/src/main/res/layout/fragment_watch_history.xml +++ b/app/src/main/res/layout/fragment_watch_history.xml @@ -29,11 +29,9 @@ + android:scrollbars="vertical">