Don't load the whole watch history at once

This commit is contained in:
Bnyro 2023-01-23 18:21:51 +01:00
parent f6a42cc8a0
commit be514f1a45
2 changed files with 31 additions and 15 deletions

View File

@ -21,6 +21,10 @@ class WatchHistoryAdapter(
) : ) :
RecyclerView.Adapter<WatchHistoryViewHolder>() { RecyclerView.Adapter<WatchHistoryViewHolder>() {
var visibleCount = minOf(10, watchHistory.size)
override fun getItemCount(): Int = visibleCount
fun removeFromWatchHistory(position: Int) { fun removeFromWatchHistory(position: Int) {
val history = watchHistory[position] val history = watchHistory[position]
query { query {
@ -31,6 +35,13 @@ class WatchHistoryAdapter(
notifyItemRangeChanged(position, itemCount) 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 { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WatchHistoryViewHolder {
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)
val binding = VideoRowBinding.inflate(layoutInflater, parent, false) val binding = VideoRowBinding.inflate(layoutInflater, parent, false)
@ -71,8 +82,4 @@ class WatchHistoryAdapter(
watchProgress.setWatchProgressLength(video.videoId, video.duration) watchProgress.setWatchProgressLength(video.videoId, video.duration)
} }
} }
override fun getItemCount(): Int {
return watchHistory.size
}
} }

View File

@ -1,6 +1,8 @@
package com.github.libretube.ui.fragments package com.github.libretube.ui.fragments
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -28,6 +30,7 @@ class WatchHistoryFragment : BaseFragment() {
private lateinit var binding: FragmentWatchHistoryBinding private lateinit var binding: FragmentWatchHistoryBinding
private val playerViewModel: PlayerViewModel by activityViewModels() private val playerViewModel: PlayerViewModel by activityViewModels()
private var isLoading = false
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -49,7 +52,7 @@ class WatchHistoryFragment : BaseFragment() {
val watchHistory = awaitQuery { val watchHistory = awaitQuery {
Database.watchHistoryDao().getAll() Database.watchHistoryDao().getAll()
} }.reversed()
if (watchHistory.isEmpty()) return 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( val watchHistoryAdapter = WatchHistoryAdapter(
watchHistory.toMutableList() 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( val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(
0, 0,
ItemTouchHelper.LEFT ItemTouchHelper.LEFT
@ -130,7 +132,7 @@ class WatchHistoryFragment : BaseFragment() {
val itemTouchHelper = ItemTouchHelper(itemTouchCallback) val itemTouchHelper = ItemTouchHelper(itemTouchCallback)
itemTouchHelper.attachToRecyclerView(binding.watchHistoryRecView) itemTouchHelper.attachToRecyclerView(binding.watchHistoryRecView)
// observe changes // observe changes to indicate if the history is empty
watchHistoryAdapter.registerAdapterDataObserver(object : watchHistoryAdapter.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() { RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
@ -141,8 +143,15 @@ class WatchHistoryFragment : BaseFragment() {
} }
}) })
binding.watchHistoryRecView.adapter = watchHistoryAdapter // add a listener for scroll end, delay needed to prevent loading new ones the first time
binding.historyEmpty.visibility = View.GONE Handler(Looper.getMainLooper()).postDelayed({
binding.historyScrollView.visibility = View.VISIBLE binding.historyScrollView.viewTreeObserver.addOnScrollChangedListener {
if (!binding.historyScrollView.canScrollVertically(1) && !isLoading) {
isLoading = true
watchHistoryAdapter.showMoreItems()
isLoading = false
}
}
}, 200)
} }
} }