Skip to content

Commit

Permalink
Merge pull request #6033 from Bnyro/master
Browse files Browse the repository at this point in the history
fix: search results scroll state not preserved when rotating
  • Loading branch information
Bnyro authored May 14, 2024
2 parents 7a9809b + 6981433 commit 4ecd751
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.libretube.ui.fragments

import android.content.res.Configuration
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -13,6 +15,7 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.navArgs
import androidx.paging.LoadState
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentSearchResultBinding
Expand All @@ -36,6 +39,8 @@ class SearchResultFragment : DynamicLayoutManagerFragment() {
private val args by navArgs<SearchResultFragmentArgs>()
private val viewModel by viewModels<SearchResultViewModel>()

private var recyclerViewState: Parcelable? = null

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -83,6 +88,13 @@ class SearchResultFragment : DynamicLayoutManagerFragment() {
val searchResultsAdapter = SearchResultsAdapter(timeStamp ?: 0)
binding.searchRecycler.adapter = searchResultsAdapter

binding.searchRecycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
recyclerViewState = binding.searchRecycler.layoutManager?.onSaveInstanceState()
}
})

viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
searchResultsAdapter.loadStateFlow.collect {
Expand Down Expand Up @@ -112,6 +124,12 @@ class SearchResultFragment : DynamicLayoutManagerFragment() {
}
}

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.searchRecycler.layoutManager?.onRestoreInstanceState(recyclerViewState)
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down

0 comments on commit 4ecd751

Please sign in to comment.