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

View File

@ -20,7 +20,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" android:layout_centerInParent="true"
android:orientation="vertical" android:orientation="vertical"
android:visibility="gone"> android:visibility="gone"
tools:visibility="visible">
<ImageView <ImageView
android:layout_width="100dp" android:layout_width="100dp"
@ -44,132 +45,126 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<ScrollView <androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/scrollview_sub" android:id="@+id/sub_coordinator"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<LinearLayout <com.google.android.material.appbar.AppBarLayout
android:id="@+id/subscriptions_app_bar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content">
android:animateLayoutChanges="true"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout <com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent" android:id="@+id/subscriptions_collapsing_tb"
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"/>
<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_marginBottom="6dp"
app:layout_constraintEnd_toStartOf="@id/filter_sort"
app:layout_constraintStart_toStartOf="parent"
android:text="@string/subscriptions"
android:textAlignment="viewStart"
android:textColor="?colorPrimary"
app:drawableEndCompat="@drawable/ic_arrow_up_down"
app:drawableTint="?colorPrimary" />
</androidx.constraintlayout.widget.ConstraintLayout>
<RelativeLayout
android:id="@+id/sub_channels_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants" app:layout_scrollFlags="scroll"
android:visibility="gone"> app:titleCollapseMode="scale">
<androidx.recyclerview.widget.RecyclerView <LinearLayout
android:id="@+id/sub_channels"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:nestedScrollingEnabled="false" /> android:animateLayoutChanges="true"
android:orientation="vertical">
</RelativeLayout>
<LinearLayout
android:id="@+id/sub_feed_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="3dp"
android:scrollbars="none">
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">
<ImageView <com.google.android.material.button.MaterialButton
android:id="@+id/edit_groups" android:id="@+id/toggle_subs"
android:layout_width="wrap_content" style="@style/PlayerActionsButton"
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp" android:layout_marginStart="12dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="6dp" android:layout_marginEnd="6dp"
android:background="?attr/selectableItemBackgroundBorderless" android:layout_marginBottom="6dp"
android:src="@drawable/ic_edit" android:layout_weight="1"
android:tooltipText="@string/tooltip_edit_groups" /> android:text="@string/subscriptions"
android:textAlignment="viewStart"
android:textColor="?colorPrimary"
app:drawableEndCompat="@drawable/ic_arrow_up_down"
app:drawableTint="?colorPrimary" />
<com.google.android.material.chip.ChipGroup <ImageView
android:id="@+id/channel_groups" android:id="@+id/filter_sort"
android:layout_width="wrap_content" android:layout_width="40dp"
android:layout_height="wrap_content" android:layout_height="40dp"
android:layout_marginStart="5dp" android:layout_gravity="center"
app:checkedChip="@id/chip_all" android:layout_marginEnd="7dp"
app:selectionRequired="true" android:alpha="0.7"
app:singleLine="true" android:contentDescription="@string/tooltip_filter"
app:singleSelection="true"> android:padding="6dp"
android:src="@drawable/ic_filter_sort" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_all"
style="@style/ElevatedFilterChip"
android:text="@string/all" />
</com.google.android.material.chip.ChipGroup>
</LinearLayout> </LinearLayout>
</HorizontalScrollView> <HorizontalScrollView
android:id="@+id/channel_groups_container"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/sub_feed"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:nestedScrollingEnabled="false" /> android:layout_marginVertical="3dp"
android:scrollbars="none">
</RelativeLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout> <ImageView
</LinearLayout> android:id="@+id/edit_groups"
</ScrollView> android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginEnd="6dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/ic_edit"
android:tooltipText="@string/tooltip_edit_groups" />
<com.google.android.material.chip.ChipGroup
android:id="@+id/channel_groups"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
app:checkedChip="@id/chip_all"
app:selectionRequired="true"
app:singleLine="true"
app:singleSelection="true">
<com.google.android.material.chip.Chip
android:id="@+id/chip_all"
style="@style/ElevatedFilterChip"
android:text="@string/all" />
</com.google.android.material.chip.ChipGroup>
</LinearLayout>
</HorizontalScrollView>
</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"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<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> </com.github.libretube.ui.views.CustomSwipeToRefresh>
</RelativeLayout> </RelativeLayout>