Merge pull request #5655 from Bnyro/master

fix: bad performance when scrolling in subscriptions feed
This commit is contained in:
Bnyro 2024-02-25 13:54:27 +01:00 committed by GitHub
commit 9f16a27b74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 133 additions and 118 deletions

View File

@ -55,6 +55,7 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
private val channelGroupsModel: EditChannelGroupsModel by activityViewModels()
private var selectedFilterGroup = 0
private var isCurrentTabSubChannels = false
private var isAppBarFullyExpanded = true
var feedAdapter: VideosAdapter? = null
private var channelsAdapter: SubscriptionChannelAdapter? = null
@ -89,6 +90,16 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
setupSortAndFilter()
// Check if the AppBarLayout is fully expanded
binding.subscriptionsAppBar.addOnOffsetChangedListener { _, verticalOffset ->
isAppBarFullyExpanded = verticalOffset == 0
}
// Determine if the child can scroll up
binding.subRefresh.setOnChildScrollUpCallback { _, _ ->
!isAppBarFullyExpanded
}
binding.subRefresh.isEnabled = true
binding.subProgress.isVisible = true
@ -119,21 +130,30 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
if (isCurrentTabSubChannels) showSubscriptions() else showFeed()
binding.subChannelsContainer.isVisible = isCurrentTabSubChannels
binding.subFeedContainer.isGone = isCurrentTabSubChannels
binding.subChannels.isVisible = isCurrentTabSubChannels
binding.subFeed.isGone = isCurrentTabSubChannels
}
binding.scrollviewSub.viewTreeObserver.addOnScrollChangedListener {
binding.subChannels.viewTreeObserver.addOnScrollChangedListener {
val binding = _binding
if (binding?.scrollviewSub?.canScrollVertically(1) == false &&
viewModel.videoFeed.value != null // scroll view is at bottom
if (binding?.subChannels?.canScrollVertically(1) == false &&
viewModel.subscriptions.value != null && // scroll view is at bottom
isCurrentTabSubChannels
) {
binding.subRefresh.isRefreshing = true
if (isCurrentTabSubChannels) {
channelsAdapter?.updateItems()
} else {
feedAdapter?.updateItems()
binding.subRefresh.isRefreshing = false
}
}
binding.subFeed.viewTreeObserver.addOnScrollChangedListener {
val binding = _binding
if (binding?.subFeed?.canScrollVertically(1) == false &&
viewModel.videoFeed.value != null && // scroll view is at bottom
!isCurrentTabSubChannels
) {
binding.subRefresh.isRefreshing = true
feedAdapter?.updateItems()
binding.subRefresh.isRefreshing = false
}
}
@ -141,7 +161,7 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
// add some extra margin to the subscribed channels while the mini player is visible
// otherwise the last channel would be invisible
playerModel.isMiniPlayerVisible.observe(viewLifecycleOwner) {
binding.subChannelsContainer.updateLayoutParams<MarginLayoutParams> {
binding.subChannels.updateLayoutParams<MarginLayoutParams> {
bottomMargin = (if (it) 64f else 0f).dpToPx()
}
}
@ -314,11 +334,11 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
}
}
binding.subChannelsContainer.isGone = true
binding.subChannels.isGone = true
binding.subProgress.isGone = true
val notLoaded = viewModel.videoFeed.value.isNullOrEmpty()
binding.subFeedContainer.isGone = notLoaded
binding.subFeed.isGone = notLoaded
binding.emptyFeed.isVisible = notLoaded
feedAdapter = VideosAdapter(
@ -357,10 +377,10 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
binding.subRefresh.isRefreshing = false
binding.subProgress.isGone = true
binding.subFeedContainer.isGone = true
binding.subFeed.isGone = true
val notLoaded = viewModel.subscriptions.value.isNullOrEmpty()
binding.subChannelsContainer.isGone = notLoaded
binding.subChannels.isGone = notLoaded
binding.emptyFeed.isVisible = notLoaded
val subCount = subscriptions.size.toLong().formatShort()

View File

@ -20,7 +20,8 @@
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:visibility="gone">
android:visibility="gone"
tools:visibility="visible">
<ImageView
android:layout_width="100dp"
@ -44,75 +45,66 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollview_sub"
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/sub_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/subscriptions_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/subscriptions_collapsing_tb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll"
app:titleCollapseMode="scale">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/filter_sort"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ic_filter_sort"
android:contentDescription="@string/tooltip_filter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="6dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="7dp"
android:alpha="0.7"/>
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/toggle_subs"
style="@style/PlayerActionsButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="6dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="6dp"
android:layout_marginBottom="6dp"
app:layout_constraintEnd_toStartOf="@id/filter_sort"
app:layout_constraintStart_toStartOf="parent"
android:layout_weight="1"
android:text="@string/subscriptions"
android:textAlignment="viewStart"
android:textColor="?colorPrimary"
app:drawableEndCompat="@drawable/ic_arrow_up_down"
app:drawableTint="?colorPrimary" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="@+id/filter_sort"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginEnd="7dp"
android:alpha="0.7"
android:contentDescription="@string/tooltip_filter"
android:padding="6dp"
android:src="@drawable/ic_filter_sort" />
<RelativeLayout
android:id="@+id/sub_channels_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:visibility="gone">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/sub_channels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false" />
</RelativeLayout>
<LinearLayout
android:id="@+id/sub_feed_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<HorizontalScrollView
android:id="@+id/channel_groups_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="3dp"
@ -155,21 +147,24 @@
</HorizontalScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
</LinearLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/sub_feed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false" />
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/sub_channels"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</com.github.libretube.ui.views.CustomSwipeToRefresh>
</RelativeLayout>