|
@@ -8,13 +8,23 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
import eu.kanade.tachiyomi.R
|
|
|
import eu.kanade.tachiyomi.data.preference.PreferenceValues
|
|
|
+import eu.kanade.tachiyomi.util.system.dpToPx
|
|
|
|
|
|
class ThemesPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
|
|
|
ListPreference(context, attrs),
|
|
|
ThemesPreferenceAdapter.OnItemClickListener {
|
|
|
|
|
|
+ private var recycler: RecyclerView? = null
|
|
|
private val adapter = ThemesPreferenceAdapter(this)
|
|
|
|
|
|
+ var lastScrollPosition: Int? = null
|
|
|
+
|
|
|
+ var entries: List<PreferenceValues.AppTheme> = emptyList()
|
|
|
+ set(value) {
|
|
|
+ field = value
|
|
|
+ adapter.setItems(value)
|
|
|
+ }
|
|
|
+
|
|
|
init {
|
|
|
layoutResource = R.layout.pref_themes_list
|
|
|
}
|
|
@@ -22,9 +32,18 @@ class ThemesPreference @JvmOverloads constructor(context: Context, attrs: Attrib
|
|
|
override fun onBindViewHolder(holder: PreferenceViewHolder) {
|
|
|
super.onBindViewHolder(holder)
|
|
|
|
|
|
- val themesList = holder.findViewById(R.id.themes_list) as RecyclerView
|
|
|
- themesList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
|
|
- themesList.adapter = adapter
|
|
|
+ recycler = holder.findViewById(R.id.themes_list) as RecyclerView
|
|
|
+ recycler?.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
|
|
+ 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) {
|
|
@@ -36,9 +55,16 @@ class ThemesPreference @JvmOverloads constructor(context: Context, attrs: Attrib
|
|
|
// no-op; not actually a DialogPreference
|
|
|
}
|
|
|
|
|
|
- var entries: List<PreferenceValues.AppTheme> = emptyList()
|
|
|
- set(value) {
|
|
|
- field = value
|
|
|
- adapter.setItems(value)
|
|
|
+ private fun scrollToOffset(lX: Int) {
|
|
|
+ recycler?.let {
|
|
|
+ (it.layoutManager as LinearLayoutManager).apply {
|
|
|
+ scrollToPositionWithOffset(
|
|
|
+ // 118dp is the width of the pref_theme_item layout
|
|
|
+ lX / 118.dpToPx,
|
|
|
+ -lX % 118.dpToPx
|
|
|
+ )
|
|
|
+ }
|
|
|
+ lastScrollPosition = it.computeHorizontalScrollOffset()
|
|
|
}
|
|
|
+ }
|
|
|
}
|