fix: scroll state of trends not preserved

This commit is contained in:
Bnyro 2024-07-23 17:58:53 +02:00
parent 315aa4f5c5
commit d74d7c62ba

View File

@ -1,14 +1,14 @@
package com.github.libretube.ui.fragments package com.github.libretube.ui.fragments
import android.content.Intent import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver import androidx.recyclerview.widget.RecyclerView
import androidx.lifecycle.LifecycleOwner
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.FragmentTrendsBinding import com.github.libretube.databinding.FragmentTrendsBinding
import com.github.libretube.ui.activities.SettingsActivity import com.github.libretube.ui.activities.SettingsActivity
@ -41,8 +41,9 @@ class TrendsFragment : DynamicLayoutManagerFragment() {
viewModel.trendingVideos.observe(viewLifecycleOwner) { videos -> viewModel.trendingVideos.observe(viewLifecycleOwner) { videos ->
if (videos == null) return@observe if (videos == null) return@observe
binding.recview.layoutManager?.onRestoreInstanceState(viewModel.recyclerViewState)
binding.recview.adapter = VideosAdapter(videos.toMutableList()) binding.recview.adapter = VideosAdapter(videos.toMutableList())
binding.recview.layoutManager?.onRestoreInstanceState(viewModel.recyclerViewState)
binding.homeRefresh.isRefreshing = false binding.homeRefresh.isRefreshing = false
binding.progressBar.isGone = true binding.progressBar.isGone = true
@ -61,8 +62,9 @@ class TrendsFragment : DynamicLayoutManagerFragment() {
viewModel.fetchTrending(requireContext()) viewModel.fetchTrending(requireContext())
} }
viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver { binding.recview.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onDestroy(owner: LifecycleOwner) { override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
viewModel.recyclerViewState = _binding?.recview?.layoutManager?.onSaveInstanceState() viewModel.recyclerViewState = _binding?.recview?.layoutManager?.onSaveInstanceState()
} }
}) })
@ -70,6 +72,12 @@ class TrendsFragment : DynamicLayoutManagerFragment() {
viewModel.fetchTrending(requireContext()) 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() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
_binding = null _binding = null