mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-10 12:47:26 +01:00
Retain scroll position when selecting app theme preference
Co-authored-by: Jays2Kings <Jays2Kings@users.noreply.github.com>
This commit is contained in:
parent
069f4e12d8
commit
0de3558ab3
@ -1,6 +1,8 @@
|
|||||||
package eu.kanade.tachiyomi.ui.setting
|
package eu.kanade.tachiyomi.ui.setting
|
||||||
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.View
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.preference.PreferenceScreen
|
import androidx.preference.PreferenceScreen
|
||||||
import eu.kanade.tachiyomi.R
|
import eu.kanade.tachiyomi.R
|
||||||
@ -23,6 +25,8 @@ import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
|
|||||||
|
|
||||||
class SettingsAppearanceController : SettingsController() {
|
class SettingsAppearanceController : SettingsController() {
|
||||||
|
|
||||||
|
private var themesPreference: ThemesPreference? = null
|
||||||
|
|
||||||
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
|
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
|
||||||
titleRes = R.string.pref_category_appearance
|
titleRes = R.string.pref_category_appearance
|
||||||
|
|
||||||
@ -59,7 +63,7 @@ class SettingsAppearanceController : SettingsController() {
|
|||||||
|
|
||||||
summary = "%s"
|
summary = "%s"
|
||||||
}
|
}
|
||||||
initThenAdd(ThemesPreference(context)) {
|
themesPreference = initThenAdd(ThemesPreference(context)) {
|
||||||
key = Keys.appTheme
|
key = Keys.appTheme
|
||||||
titleRes = R.string.pref_app_theme
|
titleRes = R.string.pref_app_theme
|
||||||
|
|
||||||
@ -158,4 +162,23 @@ class SettingsAppearanceController : SettingsController() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onSaveViewState(view: View, outState: Bundle) {
|
||||||
|
themesPreference?.let {
|
||||||
|
outState.putInt(THEMES_SCROLL_POSITION, it.lastScrollPosition ?: 0)
|
||||||
|
}
|
||||||
|
super.onSaveInstanceState(outState)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onRestoreViewState(view: View, savedViewState: Bundle) {
|
||||||
|
super.onRestoreViewState(view, savedViewState)
|
||||||
|
themesPreference?.lastScrollPosition = savedViewState.getInt(THEMES_SCROLL_POSITION, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroyView(view: View) {
|
||||||
|
super.onDestroyView(view)
|
||||||
|
themesPreference = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private const val THEMES_SCROLL_POSITION = "themesScrollPosition"
|
||||||
|
@ -8,13 +8,23 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
|||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import eu.kanade.tachiyomi.R
|
import eu.kanade.tachiyomi.R
|
||||||
import eu.kanade.tachiyomi.data.preference.PreferenceValues
|
import eu.kanade.tachiyomi.data.preference.PreferenceValues
|
||||||
|
import eu.kanade.tachiyomi.util.system.dpToPx
|
||||||
|
|
||||||
class ThemesPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
|
class ThemesPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
|
||||||
ListPreference(context, attrs),
|
ListPreference(context, attrs),
|
||||||
ThemesPreferenceAdapter.OnItemClickListener {
|
ThemesPreferenceAdapter.OnItemClickListener {
|
||||||
|
|
||||||
|
private var recycler: RecyclerView? = null
|
||||||
private val adapter = ThemesPreferenceAdapter(this)
|
private val adapter = ThemesPreferenceAdapter(this)
|
||||||
|
|
||||||
|
var lastScrollPosition: Int? = null
|
||||||
|
|
||||||
|
var entries: List<PreferenceValues.AppTheme> = emptyList()
|
||||||
|
set(value) {
|
||||||
|
field = value
|
||||||
|
adapter.setItems(value)
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
layoutResource = R.layout.pref_themes_list
|
layoutResource = R.layout.pref_themes_list
|
||||||
}
|
}
|
||||||
@ -22,9 +32,18 @@ class ThemesPreference @JvmOverloads constructor(context: Context, attrs: Attrib
|
|||||||
override fun onBindViewHolder(holder: PreferenceViewHolder) {
|
override fun onBindViewHolder(holder: PreferenceViewHolder) {
|
||||||
super.onBindViewHolder(holder)
|
super.onBindViewHolder(holder)
|
||||||
|
|
||||||
val themesList = holder.findViewById(R.id.themes_list) as RecyclerView
|
recycler = holder.findViewById(R.id.themes_list) as RecyclerView
|
||||||
themesList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
recycler?.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
||||||
themesList.adapter = adapter
|
recycler?.adapter = adapter
|
||||||
|
|
||||||
|
// Retain scroll position on activity recreate after changing theme
|
||||||
|
recycler?.addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
||||||
|
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
||||||
|
super.onScrolled(recyclerView, dx, dy)
|
||||||
|
lastScrollPosition = recyclerView.computeHorizontalScrollOffset()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
lastScrollPosition?.let { scrollToOffset(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onItemClick(position: Int) {
|
override fun onItemClick(position: Int) {
|
||||||
@ -36,9 +55,16 @@ class ThemesPreference @JvmOverloads constructor(context: Context, attrs: Attrib
|
|||||||
// no-op; not actually a DialogPreference
|
// no-op; not actually a DialogPreference
|
||||||
}
|
}
|
||||||
|
|
||||||
var entries: List<PreferenceValues.AppTheme> = emptyList()
|
private fun scrollToOffset(lX: Int) {
|
||||||
set(value) {
|
recycler?.let {
|
||||||
field = value
|
(it.layoutManager as LinearLayoutManager).apply {
|
||||||
adapter.setItems(value)
|
scrollToPositionWithOffset(
|
||||||
|
// 118dp is the width of the pref_theme_item layout
|
||||||
|
lX / 118.dpToPx,
|
||||||
|
-lX % 118.dpToPx
|
||||||
|
)
|
||||||
|
}
|
||||||
|
lastScrollPosition = it.computeHorizontalScrollOffset()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user