From be514f1a4551f00b6272621e4289c99013477a55 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 23 Jan 2023 18:21:51 +0100 Subject: [PATCH] Don't load the whole watch history at once --- .../ui/adapters/WatchHistoryAdapter.kt | 15 ++++++--- .../ui/fragments/WatchHistoryFragment.kt | 31 ++++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt index 1a8930306..92336e22f 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt @@ -21,6 +21,10 @@ class WatchHistoryAdapter( ) : RecyclerView.Adapter() { + var visibleCount = minOf(10, watchHistory.size) + + override fun getItemCount(): Int = visibleCount + fun removeFromWatchHistory(position: Int) { val history = watchHistory[position] query { @@ -31,6 +35,13 @@ class WatchHistoryAdapter( notifyItemRangeChanged(position, itemCount) } + fun showMoreItems() { + val oldSize = visibleCount + visibleCount += minOf(10, watchHistory.size - oldSize) + if (visibleCount == oldSize) return + notifyItemRangeInserted(oldSize, visibleCount) + } + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WatchHistoryViewHolder { val layoutInflater = LayoutInflater.from(parent.context) val binding = VideoRowBinding.inflate(layoutInflater, parent, false) @@ -71,8 +82,4 @@ class WatchHistoryAdapter( watchProgress.setWatchProgressLength(video.videoId, video.duration) } } - - override fun getItemCount(): Int { - return watchHistory.size - } } 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 8a3976bb3..1fc8395c0 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 @@ -1,6 +1,8 @@ package com.github.libretube.ui.fragments import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -28,6 +30,7 @@ class WatchHistoryFragment : BaseFragment() { private lateinit var binding: FragmentWatchHistoryBinding private val playerViewModel: PlayerViewModel by activityViewModels() + private var isLoading = false override fun onCreateView( inflater: LayoutInflater, @@ -49,7 +52,7 @@ class WatchHistoryFragment : BaseFragment() { val watchHistory = awaitQuery { Database.watchHistoryDao().getAll() - } + }.reversed() if (watchHistory.isEmpty()) return @@ -96,16 +99,15 @@ class WatchHistoryFragment : BaseFragment() { ) } - // reversed order - binding.watchHistoryRecView.layoutManager = LinearLayoutManager(requireContext()).apply { - reverseLayout = true - stackFromEnd = true - } - val watchHistoryAdapter = WatchHistoryAdapter( watchHistory.toMutableList() ) + binding.watchHistoryRecView.layoutManager = LinearLayoutManager(context) + binding.watchHistoryRecView.adapter = watchHistoryAdapter + binding.historyEmpty.visibility = View.GONE + binding.historyScrollView.visibility = View.VISIBLE + val itemTouchCallback = object : ItemTouchHelper.SimpleCallback( 0, ItemTouchHelper.LEFT @@ -130,7 +132,7 @@ class WatchHistoryFragment : BaseFragment() { val itemTouchHelper = ItemTouchHelper(itemTouchCallback) itemTouchHelper.attachToRecyclerView(binding.watchHistoryRecView) - // observe changes + // observe changes to indicate if the history is empty watchHistoryAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { @@ -141,8 +143,15 @@ class WatchHistoryFragment : BaseFragment() { } }) - binding.watchHistoryRecView.adapter = watchHistoryAdapter - binding.historyEmpty.visibility = View.GONE - binding.historyScrollView.visibility = View.VISIBLE + // add a listener for scroll end, delay needed to prevent loading new ones the first time + Handler(Looper.getMainLooper()).postDelayed({ + binding.historyScrollView.viewTreeObserver.addOnScrollChangedListener { + if (!binding.historyScrollView.canScrollVertically(1) && !isLoading) { + isLoading = true + watchHistoryAdapter.showMoreItems() + isLoading = false + } + } + }, 200) } }