From d74d7c62bae3a865a6c617876e71c0282a423a35 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 23 Jul 2024 17:58:53 +0200 Subject: [PATCH] fix: scroll state of trends not preserved --- .../libretube/ui/fragments/TrendsFragment.kt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/TrendsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/TrendsFragment.kt index c61377044..fd1c878e1 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/TrendsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/TrendsFragment.kt @@ -1,14 +1,14 @@ package com.github.libretube.ui.fragments import android.content.Intent +import android.content.res.Configuration import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.view.isGone import androidx.fragment.app.activityViewModels -import androidx.lifecycle.DefaultLifecycleObserver -import androidx.lifecycle.LifecycleOwner +import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R import com.github.libretube.databinding.FragmentTrendsBinding import com.github.libretube.ui.activities.SettingsActivity @@ -41,8 +41,9 @@ class TrendsFragment : DynamicLayoutManagerFragment() { viewModel.trendingVideos.observe(viewLifecycleOwner) { videos -> if (videos == null) return@observe - binding.recview.layoutManager?.onRestoreInstanceState(viewModel.recyclerViewState) binding.recview.adapter = VideosAdapter(videos.toMutableList()) + binding.recview.layoutManager?.onRestoreInstanceState(viewModel.recyclerViewState) + binding.homeRefresh.isRefreshing = false binding.progressBar.isGone = true @@ -61,8 +62,9 @@ class TrendsFragment : DynamicLayoutManagerFragment() { viewModel.fetchTrending(requireContext()) } - viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver { - override fun onDestroy(owner: LifecycleOwner) { + binding.recview.addOnScrollListener(object : RecyclerView.OnScrollListener() { + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + super.onScrollStateChanged(recyclerView, newState) viewModel.recyclerViewState = _binding?.recview?.layoutManager?.onSaveInstanceState() } }) @@ -70,6 +72,12 @@ class TrendsFragment : DynamicLayoutManagerFragment() { viewModel.fetchTrending(requireContext()) } + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + // manually restore the recyclerview state due to https://github.com/material-components/material-components-android/issues/3473 + binding.recview.layoutManager?.onRestoreInstanceState(viewModel.recyclerViewState) + } + override fun onDestroyView() { super.onDestroyView() _binding = null