Improve theme chooser

This commit is contained in:
Koitharu
2023-08-22 13:13:01 +03:00
parent 2f9c2d9ab6
commit 99b698ad12
3 changed files with 25 additions and 16 deletions

View File

@@ -10,7 +10,6 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewTreeObserver import android.view.ViewTreeObserver
import android.widget.HorizontalScrollView import android.widget.HorizontalScrollView
import android.widget.LinearLayout
import androidx.appcompat.view.ContextThemeWrapper import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.customview.view.AbsSavedState import androidx.customview.view.AbsSavedState
@@ -19,7 +18,9 @@ import androidx.preference.PreferenceViewHolder
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.prefs.ColorScheme import org.koitharu.kotatsu.core.prefs.ColorScheme
import org.koitharu.kotatsu.databinding.ItemColorSchemeBinding import org.koitharu.kotatsu.databinding.ItemColorSchemeBinding
import org.koitharu.kotatsu.databinding.PreferenceThemeBinding
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import com.google.android.material.R as materialR
class ThemeChooserPreference @JvmOverloads constructor( class ThemeChooserPreference @JvmOverloads constructor(
context: Context, context: Context,
@@ -43,38 +44,44 @@ class ThemeChooserPreference @JvmOverloads constructor(
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder) super.onBindViewHolder(holder)
val layout = holder.findViewById(R.id.linear) as? LinearLayout ?: return val binding = PreferenceThemeBinding.bind(holder.itemView)
val scrollView = holder.findViewById(R.id.scrollView) as? HorizontalScrollView ?: return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
scrollView.suppressLayout(true) binding.scrollView.suppressLayout(true)
layout.suppressLayout(true) binding.linear.suppressLayout(true)
} }
layout.removeAllViews() binding.linear.removeAllViews()
for (theme in entries) { for (theme in entries) {
val context = ContextThemeWrapper(context, theme.styleResId) val context = ContextThemeWrapper(context, theme.styleResId)
val item = ItemColorSchemeBinding.inflate(LayoutInflater.from(context), layout, false) val item = ItemColorSchemeBinding.inflate(LayoutInflater.from(context), binding.linear, false)
item.card.isChecked = theme == currentValue val isSelected = theme == currentValue
item.card.isChecked = isSelected
item.card.strokeWidth = if (isSelected) context.resources.getDimensionPixelSize(
materialR.dimen.m3_comp_outlined_card_outline_width,
) else 0
item.textViewTitle.setText(theme.titleResId) item.textViewTitle.setText(theme.titleResId)
item.root.tag = theme item.root.tag = theme
item.card.tag = theme item.card.tag = theme
item.imageViewCheck.isVisible = theme == currentValue item.imageViewCheck.isVisible = theme == currentValue
item.root.setOnClickListener(itemClickListener) item.root.setOnClickListener(itemClickListener)
item.card.setOnClickListener(itemClickListener) item.card.setOnClickListener(itemClickListener)
layout.addView(item.root) binding.linear.addView(item.root)
if (isSelected) {
item.root.requestFocus()
}
} }
if (lastScrollPosition[0] >= 0) { if (lastScrollPosition[0] >= 0) {
val scroller = Scroller(scrollView, lastScrollPosition[0]) val scroller = Scroller(binding.scrollView, lastScrollPosition[0])
scroller.run() scroller.run()
scrollView.post(scroller) binding.scrollView.post(scroller)
} }
scrollView.viewTreeObserver.run { binding.scrollView.viewTreeObserver.run {
scrollPersistListener?.let { removeOnScrollChangedListener(it) } scrollPersistListener?.let { removeOnScrollChangedListener(it) }
scrollPersistListener = ScrollPersistListener(WeakReference(scrollView), lastScrollPosition) scrollPersistListener = ScrollPersistListener(WeakReference(binding.scrollView), lastScrollPosition)
addOnScrollChangedListener(scrollPersistListener) addOnScrollChangedListener(scrollPersistListener)
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
layout.suppressLayout(false) binding.linear.suppressLayout(false)
scrollView.suppressLayout(false) binding.scrollView.suppressLayout(false)
} }
} }

View File

@@ -15,7 +15,8 @@
style="?materialCardViewFilledStyle" style="?materialCardViewFilledStyle"
android:layout_width="@dimen/widget_cover_width" android:layout_width="@dimen/widget_cover_width"
android:layout_height="@dimen/widget_cover_height" android:layout_height="@dimen/widget_cover_height"
android:focusableInTouchMode="false"> android:focusableInTouchMode="false"
app:strokeColor="?colorOutline">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@@ -60,6 +60,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="2dp" android:layout_marginTop="2dp"
android:clipToPadding="false" android:clipToPadding="false"
android:descendantFocusability="blocksDescendants"
android:paddingStart="0dp" android:paddingStart="0dp"
android:paddingEnd="16dp" android:paddingEnd="16dp"
android:scrollbars="none" android:scrollbars="none"