pref: fix application of m3 switches

Fix an issue with M3SwitchPreference where the switch would not update
properly.

When reacing M3SwitchPreference, I wanted to make an optimization
regarding updating the switch to M3, so I decided to make the
preference check if they have already applied the switch, and then
ignore it if that's the case. However, I ended up forgetting that
ViewHolders tend to need to be re-bound, which resulted in this
optimization leading to inconsistent application of the M3 switches.
Fix this by removing that optimization.
This commit is contained in:
OxygenCobalt 2022-02-06 19:17:49 -07:00
parent d9e2d7f07a
commit a51f1aa5b9
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47

View file

@ -20,13 +20,11 @@ class M3SwitchPreference @JvmOverloads constructor(
defStyleAttr: Int = R.attr.switchPreferenceCompatStyle, defStyleAttr: Int = R.attr.switchPreferenceCompatStyle,
defStyleRes: Int = 0 defStyleRes: Int = 0
) : SwitchPreferenceCompat(context, attrs, defStyleAttr, defStyleRes) { ) : SwitchPreferenceCompat(context, attrs, defStyleAttr, defStyleRes) {
// Lollipop cannot into ColorStateList, disable this feature on that version
private var needToUpdateSwitch = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder) super.onBindViewHolder(holder)
if (needToUpdateSwitch) { // Lollipop cannot into ColorStateList, disable this feature on that version
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val switch = holder.findViewById(androidx.preference.R.id.switchWidget) val switch = holder.findViewById(androidx.preference.R.id.switchWidget)
if (switch is SwitchCompat) { if (switch is SwitchCompat) {
@ -36,8 +34,6 @@ class M3SwitchPreference @JvmOverloads constructor(
thumbDrawable = context.getDrawableSafe(R.drawable.ui_m3_switch_thumb) thumbDrawable = context.getDrawableSafe(R.drawable.ui_m3_switch_thumb)
thumbTintList = context.getColorStateListSafe(R.color.sel_m3_switch_thumb) thumbTintList = context.getColorStateListSafe(R.color.sel_m3_switch_thumb)
} }
needToUpdateSwitch = false
} }
} }
} }