mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-13 22:00:30 +05:30
Don't load the whole watch history at once
This commit is contained in:
parent
f6a42cc8a0
commit
be514f1a45
@ -21,6 +21,10 @@ class WatchHistoryAdapter(
|
||||
) :
|
||||
RecyclerView.Adapter<WatchHistoryViewHolder>() {
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user