fix: add pagination to subscriptions list

This commit is contained in:
Bnyro 2023-09-08 17:45:42 +02:00
parent caef216c96
commit b22e026ec0
3 changed files with 21 additions and 7 deletions

View File

@ -16,8 +16,9 @@ import com.github.libretube.ui.viewholders.SubscriptionChannelViewHolder
class SubscriptionChannelAdapter(
private val subscriptions: MutableList<Subscription>
) : RecyclerView.Adapter<SubscriptionChannelViewHolder>() {
private var visibleCount = 20
override fun getItemCount() = subscriptions.size
override fun getItemCount() = minOf(visibleCount, subscriptions.size)
override fun onCreateViewHolder(
parent: ViewGroup,
@ -28,6 +29,13 @@ class SubscriptionChannelAdapter(
return SubscriptionChannelViewHolder(binding)
}
fun updateItems() {
val oldSize = visibleCount
visibleCount += minOf(10, subscriptions.size - oldSize)
if (visibleCount == oldSize) return
notifyItemRangeInserted(oldSize, visibleCount)
}
override fun onBindViewHolder(holder: SubscriptionChannelViewHolder, position: Int) {
val subscription = subscriptions[position]

View File

@ -49,7 +49,8 @@ class SubscriptionsFragment : Fragment() {
private var selectedFilterGroup = 0
private var isCurrentTabSubChannels = false
var subscriptionsAdapter: VideosAdapter? = null
var feedAdapter: VideosAdapter? = null
private var channelsAdapter: SubscriptionChannelAdapter? = null
private var selectedSortOrder = PreferenceHelper.getInt(PreferenceKeys.FEED_SORT_ORDER, 0)
set(value) {
PreferenceHelper.putInt(PreferenceKeys.FEED_SORT_ORDER, value)
@ -155,7 +156,11 @@ class SubscriptionsFragment : Fragment() {
viewModel.videoFeed.value != null // scroll view is at bottom
) {
binding.subRefresh.isRefreshing = true
subscriptionsAdapter?.updateItems()
if (isCurrentTabSubChannels) {
channelsAdapter?.updateItems()
} else {
feedAdapter?.updateItems()
}
binding.subRefresh.isRefreshing = false
}
}
@ -315,11 +320,11 @@ class SubscriptionsFragment : Fragment() {
binding.subFeedContainer.isGone = notLoaded
binding.emptyFeed.isVisible = notLoaded
subscriptionsAdapter = VideosAdapter(
feedAdapter = VideosAdapter(
sortedFeed.toMutableList(),
showAllAtOnce = false
)
binding.subFeed.adapter = subscriptionsAdapter
binding.subFeed.adapter = feedAdapter
binding.toggleSubsText.text = getString(R.string.subscriptions)
PreferenceHelper.updateLastFeedWatchedTime()
@ -345,7 +350,8 @@ class SubscriptionsFragment : Fragment() {
binding.subChannels.adapter = LegacySubscriptionAdapter(subscriptions)
} else {
binding.subChannels.layoutManager = LinearLayoutManager(context)
binding.subChannels.adapter = SubscriptionChannelAdapter(subscriptions.toMutableList())
channelsAdapter = SubscriptionChannelAdapter(subscriptions.toMutableList())
binding.subChannels.adapter = channelsAdapter
}
binding.subRefresh.isRefreshing = false

View File

@ -132,7 +132,7 @@ class VideoOptionsBottomSheet(
// get the current fragment
val fragment = navHostFragment?.childFragmentManager?.fragments
?.firstOrNull() as? SubscriptionsFragment
fragment?.subscriptionsAdapter?.removeItemById(videoId)
fragment?.feedAdapter?.removeItemById(videoId)
}
onVideoChanged()
}