Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stuttering when scrolling in watch history #5657

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.ui.adapters.WatchHistoryAdapter
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
import com.github.libretube.ui.extensions.addOnBottomReachedListener
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.BaseBottomSheet
import com.github.libretube.util.PlayingQueue
Expand Down Expand Up @@ -73,7 +74,8 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
}

override fun setLayoutManagers(gridItems: Int) {
_binding?.watchHistoryRecView?.layoutManager = GridLayoutManager(context, gridItems.ceilHalf())
_binding?.watchHistoryRecView?.layoutManager =
GridLayoutManager(context, gridItems.ceilHalf())
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -89,8 +91,10 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {

if (allHistory.isEmpty()) return

binding.filterTypeTV.text = resources.getStringArray(R.array.filterOptions)[selectedTypeFilter]
binding.filterStatusTV.text = resources.getStringArray(R.array.filterStatusOptions)[selectedStatusFilter]
binding.filterTypeTV.text =
resources.getStringArray(R.array.filterOptions)[selectedTypeFilter]
binding.filterStatusTV.text =
resources.getStringArray(R.array.filterStatusOptions)[selectedStatusFilter]

val watchPositionItem = arrayOf(getString(R.string.also_clear_watch_positions))
val selected = booleanArrayOf(false)
Expand All @@ -102,7 +106,7 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
selected[index] = newValue
}
.setPositiveButton(R.string.okay) { _, _ ->
binding.historyScrollView.isGone = true
binding.historyContainer.isGone = true
binding.historyEmpty.isVisible = true
lifecycleScope.launch(Dispatchers.IO) {
Database.withTransaction {
Expand Down Expand Up @@ -179,7 +183,7 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {

binding.watchHistoryRecView.adapter = watchHistoryAdapter
binding.historyEmpty.isGone = true
binding.historyScrollView.isVisible = true
binding.historyContainer.isVisible = true

val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(
0,
Expand Down Expand Up @@ -207,7 +211,7 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
if (watchHistoryAdapter.itemCount == 0) {
binding.historyScrollView.isGone = true
binding.historyContainer.isGone = true
binding.historyEmpty.isVisible = true
}
}
Expand All @@ -216,14 +220,13 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
// add a listener for scroll end, delay needed to prevent loading new ones the first time
handler.postDelayed(200) {
if (_binding == null) return@postDelayed
binding.historyScrollView.viewTreeObserver.addOnScrollChangedListener {
if (_binding?.historyScrollView?.canScrollVertically(1) == false &&
!isLoading
) {
isLoading = true
watchHistoryAdapter.showMoreItems()
isLoading = false
}

binding.watchHistoryRecView.addOnBottomReachedListener {
if (isLoading) return@addOnBottomReachedListener

isLoading = true
watchHistoryAdapter.showMoreItems()
isLoading = false
}
}
}
Expand Down
162 changes: 89 additions & 73 deletions app/src/main/res/layout/fragment_watch_history.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,90 +28,106 @@
android:textStyle="bold" />
</LinearLayout>

<androidx.core.widget.NestedScrollView
android:id="@+id/historyScrollView"
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/historyContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:visibility="gone">

<LinearLayout
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/playlist_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal">

<com.google.android.material.button.MaterialButton
android:id="@+id/play_all"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:maxLines="1"
android:text="@string/play_all"
app:icon="@drawable/ic_playlist" />

<com.google.android.material.button.MaterialButton
android:id="@+id/clear"
style="@style/Widget.Material3.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:maxLines="1"
android:text="@string/clear_history"
app:icon="@drawable/ic_delete" />
android:layout_height="wrap_content">

</LinearLayout>

<FrameLayout
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/playlist_collapsing_tb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
app:layout_scrollFlags="scroll"
app:titleCollapseMode="scale">

<TextView
android:id="@+id/filterTypeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginStart="5dp"
android:drawablePadding="5dp"
android:paddingHorizontal="10dp"
android:text="@string/all"
android:textSize="16sp"
android:tooltipText="@string/tooltip_filter"
app:drawableEndCompat="@drawable/ic_filter" />

<TextView
android:id="@+id/filterStatusTV"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="5dp"
android:drawablePadding="5dp"
android:paddingHorizontal="10dp"
android:text="@string/watched"
android:textSize="16sp"
android:tooltipText="@string/tooltip_filter"
app:drawableEndCompat="@drawable/ic_filter" />

</FrameLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/watchHistoryRecView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:nestedScrollingEnabled="false" />

</LinearLayout>
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal">

<com.google.android.material.button.MaterialButton
android:id="@+id/play_all"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:maxLines="1"
android:text="@string/play_all"
app:icon="@drawable/ic_playlist" />

<com.google.android.material.button.MaterialButton
android:id="@+id/clear"
style="@style/Widget.Material3.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:maxLines="1"
android:text="@string/clear_history"
app:icon="@drawable/ic_delete" />

</LinearLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">

<TextView
android:id="@+id/filterTypeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginStart="5dp"
android:drawablePadding="5dp"
android:paddingHorizontal="10dp"
android:text="@string/all"
android:textSize="16sp"
android:tooltipText="@string/tooltip_filter"
app:drawableEndCompat="@drawable/ic_filter" />

<TextView
android:id="@+id/filterStatusTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="5dp"
android:drawablePadding="5dp"
android:paddingHorizontal="10dp"
android:text="@string/watched"
android:textSize="16sp"
android:tooltipText="@string/tooltip_filter"
app:drawableEndCompat="@drawable/ic_filter" />

</FrameLayout>

</LinearLayout>

</com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/watchHistoryRecView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

</FrameLayout>
Loading