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: scroll state of trends not preserved #6271

Merged
merged 1 commit into from
Jul 23, 2024
Merged
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
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -61,15 +62,22 @@ 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()
}
})

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
Expand Down
Loading