mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
Merge pull request #2845 from Bnyro/master
Fix unresponsiveness when opening watch history
This commit is contained in:
commit
dbd6516b62
@ -26,7 +26,7 @@ object DatabaseHelper {
|
||||
query {
|
||||
Database.watchHistoryDao().insertAll(watchHistoryItem)
|
||||
val maxHistorySize =
|
||||
PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "unlimited")
|
||||
PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "100")
|
||||
if (maxHistorySize == "unlimited") return@query
|
||||
|
||||
// delete the first watch history entry if the limit is reached
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -91,9 +91,7 @@ class ChannelFragment : BaseFragment() {
|
||||
|
||||
binding.channelScrollView.viewTreeObserver
|
||||
.addOnScrollChangedListener {
|
||||
if (binding.channelScrollView.getChildAt(0).bottom
|
||||
== (binding.channelScrollView.height + binding.channelScrollView.scrollY)
|
||||
) {
|
||||
if (!binding.channelScrollView.canScrollVertically(1)) {
|
||||
try {
|
||||
onScrollEnd.invoke()
|
||||
} catch (e: Exception) {
|
||||
|
@ -212,9 +212,7 @@ class PlaylistFragment : BaseFragment() {
|
||||
binding.playlistRecView.adapter = playlistAdapter
|
||||
binding.playlistScrollview.viewTreeObserver
|
||||
.addOnScrollChangedListener {
|
||||
if (binding.playlistScrollview.getChildAt(0).bottom
|
||||
== (binding.playlistScrollview.height + binding.playlistScrollview.scrollY)
|
||||
) {
|
||||
if (!binding.playlistScrollview.canScrollVertically(1)) {
|
||||
if (isLoading) return@addOnScrollChangedListener
|
||||
|
||||
// append more playlists to the recycler view
|
||||
|
@ -137,9 +137,7 @@ class SubscriptionsFragment : BaseFragment() {
|
||||
|
||||
binding.scrollviewSub.viewTreeObserver
|
||||
.addOnScrollChangedListener {
|
||||
if (binding.scrollviewSub.getChildAt(0).bottom
|
||||
== (binding.scrollviewSub.height + binding.scrollviewSub.scrollY)
|
||||
) {
|
||||
if (!binding.scrollviewSub.canScrollVertically(1)) {
|
||||
// scroll view is at bottom
|
||||
if (viewModel.videoFeed.value == null) return@addOnScrollChangedListener
|
||||
binding.subRefresh.isRefreshing = true
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@
|
||||
app:title="@string/watch_history" />
|
||||
|
||||
<ListPreference
|
||||
android:defaultValue="unlimited"
|
||||
android:defaultValue="100"
|
||||
android:entries="@array/historySize"
|
||||
android:entryValues="@array/historySizeValues"
|
||||
android:icon="@drawable/ic_list"
|
||||
|
Loading…
x
Reference in New Issue
Block a user