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 not preserved after rotation change at some places #5950

Merged
merged 1 commit into from
May 1, 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
@@ -1,6 +1,8 @@
package com.github.libretube.ui.fragments

import android.content.res.Configuration

Check failure on line 3 in app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Imports must be ordered in lexicographic order without any empty lines in-between with "java", "javax", "kotlin" and aliases in the end Raw Output: app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt:3:1: error: Imports must be ordered in lexicographic order without any empty lines in-between with "java", "javax", "kotlin" and aliases in the end (standard:import-ordering)
import android.os.Bundle
import android.os.Parcelable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
Expand All @@ -12,6 +14,7 @@
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.ChannelTab
Expand Down Expand Up @@ -61,6 +64,7 @@
private var searchChannelAdapter: SearchChannelAdapter? = null

private var isAppBarFullyExpanded: Boolean = true
private var recyclerViewState: Parcelable? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -109,6 +113,13 @@
loadNextPage()
}

binding.channelRecView.addOnScrollListener(object : RecyclerView.OnScrollListener() {

Check failure on line 116 in app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt:116:52: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {

Check failure on line 117 in app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected after opening parenthesis Raw Output: app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt:117:47: error: Newline expected after opening parenthesis (standard:function-signature)

Check failure on line 117 in app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt:117:75: error: Parameter should start on a newline (standard:function-signature)

Check failure on line 117 in app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected before closing parenthesis Raw Output: app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt:117:88: error: Newline expected before closing parenthesis (standard:function-signature)
super.onScrollStateChanged(recyclerView, newState)
recyclerViewState = binding.channelRecView.layoutManager?.onSaveInstanceState()
}
})

fetchChannel()
}

Expand Down Expand Up @@ -338,4 +349,11 @@

return newContent.nextpage
}


Check failure on line 353 in app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Needless blank line(s) Raw Output: app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt:353:1: error: Needless blank line(s) (standard:no-consecutive-blank-lines)
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.channelRecView.layoutManager?.onRestoreInstanceState(recyclerViewState)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.libretube.ui.fragments

import android.annotation.SuppressLint
import android.content.res.Configuration
import android.os.Bundle
import android.os.Parcelable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -72,6 +74,7 @@
field = value
}
private val sortOptions by lazy { resources.getStringArray(R.array.playlistSortOptions) }
private var recyclerViewState: Parcelable? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -106,6 +109,14 @@
binding.playlistRecView.updatePadding(bottom = if (it) 64f.dpToPx() else 0)
}

// manually restore the recyclerview state due to https://github.com/material-components/material-components-android/issues/3473
binding.playlistRecView.addOnScrollListener(object : RecyclerView.OnScrollListener() {

Check failure on line 113 in app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt:113:53: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {

Check failure on line 114 in app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected after opening parenthesis Raw Output: app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt:114:47: error: Newline expected after opening parenthesis (standard:function-signature)

Check failure on line 114 in app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt:114:75: error: Parameter should start on a newline (standard:function-signature)
super.onScrollStateChanged(recyclerView, newState)
recyclerViewState = binding.playlistRecView.layoutManager?.onSaveInstanceState()
}
})

fetchPlaylist()
}

Expand Down Expand Up @@ -402,4 +413,10 @@
isLoading = false
}
}

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.playlistRecView.layoutManager?.onRestoreInstanceState(recyclerViewState)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.libretube.ui.fragments

import android.annotation.SuppressLint
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 @@ -16,6 +18,7 @@ import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.api.obj.Subscription
Expand Down Expand Up @@ -72,6 +75,9 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
field = value
}

private var subChannelsRecyclerViewState: Parcelable? = null
private var subFeedRecyclerViewState: Parcelable? = null

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -177,6 +183,21 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
ChannelGroupsSheet().show(childFragmentManager, null)
}

// manually restore the recyclerview state due to https://github.com/material-components/material-components-android/issues/3473
binding.subChannels.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
subChannelsRecyclerViewState = binding.subChannels.layoutManager?.onSaveInstanceState()
}
})

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

lifecycleScope.launch(Dispatchers.IO) {
val groups = DatabaseHolder.Database.subscriptionGroupsDao().getAll()
.sortedBy { it.index }
Expand Down Expand Up @@ -379,4 +400,11 @@ class SubscriptionsFragment : DynamicLayoutManagerFragment() {
val subCount = subscriptions.size.toLong().formatShort()
binding.toggleSubs.text = "${getString(R.string.subscriptions)} ($subCount)"
}

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.subChannels.layoutManager?.onRestoreInstanceState(subChannelsRecyclerViewState)
binding.subFeed.layoutManager?.onRestoreInstanceState(subFeedRecyclerViewState)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.github.libretube.ui.fragments

import android.content.res.Configuration
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Parcelable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -46,6 +49,7 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
private val handler = Handler(Looper.getMainLooper())
private val playerViewModel: PlayerViewModel by activityViewModels()
private var isLoading = false
private var recyclerViewState: Parcelable? = null

private var selectedStatusFilter = PreferenceHelper.getInt(
PreferenceKeys.SELECTED_HISTORY_STATUS_FILTER,
Expand Down Expand Up @@ -143,6 +147,14 @@ class WatchHistoryFragment : DynamicLayoutManagerFragment() {
}.show(childFragmentManager)
}

// manually restore the recyclerview state due to https://github.com/material-components/material-components-android/issues/3473
binding.watchHistoryRecView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
recyclerViewState = binding.watchHistoryRecView.layoutManager?.onSaveInstanceState()
}
})

showWatchHistory(allHistory)
}

Expand Down Expand Up @@ -256,6 +268,12 @@ class WatchHistoryFragment : 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.watchHistoryRecView.layoutManager?.onRestoreInstanceState(recyclerViewState)
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_watch_history.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
android:visibility="gone">

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

<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/playlist_collapsing_tb"
android:id="@+id/watch_history_collapsing_tb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll"
Expand Down
Loading