fix: stuttering when scrolling in watch history

This commit is contained in:
Bnyro 2024-02-25 14:08:56 +01:00
parent c493ada148
commit 1257ed44b9
2 changed files with 110 additions and 91 deletions

View File

@ -30,6 +30,7 @@ import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.ui.adapters.WatchHistoryAdapter
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
import com.github.libretube.ui.extensions.addOnBottomReachedListener
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.BaseBottomSheet
import com.github.libretube.util.PlayingQueue
@ -73,7 +74,8 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
}
override fun setLayoutManagers(gridItems: Int) {
_binding?.watchHistoryRecView?.layoutManager = GridLayoutManager(context, gridItems.ceilHalf())
_binding?.watchHistoryRecView?.layoutManager =
GridLayoutManager(context, gridItems.ceilHalf())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -89,8 +91,10 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
if (allHistory.isEmpty()) return
binding.filterTypeTV.text = resources.getStringArray(R.array.filterOptions)[selectedTypeFilter]
binding.filterStatusTV.text = resources.getStringArray(R.array.filterStatusOptions)[selectedStatusFilter]
binding.filterTypeTV.text =
resources.getStringArray(R.array.filterOptions)[selectedTypeFilter]
binding.filterStatusTV.text =
resources.getStringArray(R.array.filterStatusOptions)[selectedStatusFilter]
val watchPositionItem = arrayOf(getString(R.string.also_clear_watch_positions))
val selected = booleanArrayOf(false)
@ -102,7 +106,7 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
selected[index] = newValue
}
.setPositiveButton(R.string.okay) { _, _ ->
binding.historyScrollView.isGone = true
binding.historyContainer.isGone = true
binding.historyEmpty.isVisible = true
lifecycleScope.launch(Dispatchers.IO) {
Database.withTransaction {
@ -179,7 +183,7 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
binding.watchHistoryRecView.adapter = watchHistoryAdapter
binding.historyEmpty.isGone = true
binding.historyScrollView.isVisible = true
binding.historyContainer.isVisible = true
val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(
0,
@ -207,7 +211,7 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
if (watchHistoryAdapter.itemCount == 0) {
binding.historyScrollView.isGone = true
binding.historyContainer.isGone = true
binding.historyEmpty.isVisible = true
}
}
@ -216,17 +220,16 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
// add a listener for scroll end, delay needed to prevent loading new ones the first time
handler.postDelayed(200) {
if (_binding == null) return@postDelayed
binding.historyScrollView.viewTreeObserver.addOnScrollChangedListener {
if (_binding?.historyScrollView?.canScrollVertically(1) == false &&
!isLoading
) {
binding.watchHistoryRecView.addOnBottomReachedListener {
if (isLoading) return@addOnBottomReachedListener
isLoading = true
watchHistoryAdapter.showMoreItems()
isLoading = false
}
}
}
}
private fun List<WatchHistoryItem>.filterByStatusAndWatchPosition(): List<WatchHistoryItem> {
val watchHistoryItem = this.filter {

View File

@ -28,13 +28,25 @@
android:textStyle="bold" />
</LinearLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/historyScrollView"
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/historyContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:visibility="gone">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/playlist_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/playlist_collapsing_tb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll"
app:titleCollapseMode="scale">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -103,15 +115,19 @@
</FrameLayout>
</LinearLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/watchHistoryRecView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:nestedScrollingEnabled="false" />
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</FrameLayout>