From c98d1313160f112d5645c4b82d2bea98a906a77b Mon Sep 17 00:00:00 2001 From: OxygenCobalt Date: Sun, 23 Jan 2022 10:53:39 -0700 Subject: [PATCH] style: implement m3 switches Implement a facimile of the new Material 3/You switches in the settings menu. There's no defined spec for this yet, so I just shamelessly ripped the implementation from Doodle. --- .../auxio/settings/pref/M3SwitchPreference.kt | 40 +++++++++++++++++++ .../main/res/color/sel_m3_switch_thumb.xml | 5 +++ .../main/res/color/sel_m3_switch_track.xml | 5 +++ .../main/res/drawable/ui_m3_switch_thumb.xml | 15 +++++++ .../main/res/drawable/ui_m3_switch_track.xml | 12 ++++++ app/src/main/res/values/typography.xml | 2 +- app/src/main/res/xml/prefs_main.xml | 20 +++++----- 7 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/org/oxycblt/auxio/settings/pref/M3SwitchPreference.kt create mode 100644 app/src/main/res/color/sel_m3_switch_thumb.xml create mode 100644 app/src/main/res/color/sel_m3_switch_track.xml create mode 100644 app/src/main/res/drawable/ui_m3_switch_thumb.xml create mode 100644 app/src/main/res/drawable/ui_m3_switch_track.xml diff --git a/app/src/main/java/org/oxycblt/auxio/settings/pref/M3SwitchPreference.kt b/app/src/main/java/org/oxycblt/auxio/settings/pref/M3SwitchPreference.kt new file mode 100644 index 000000000..c3017a072 --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/settings/pref/M3SwitchPreference.kt @@ -0,0 +1,40 @@ +package org.oxycblt.auxio.settings.pref + +import android.content.Context +import android.os.Build +import android.util.AttributeSet +import androidx.appcompat.widget.SwitchCompat +import androidx.preference.PreferenceViewHolder +import androidx.preference.SwitchPreferenceCompat +import org.oxycblt.auxio.R +import org.oxycblt.auxio.util.resolveDrawable +import org.oxycblt.auxio.util.resolveStateList + +class M3SwitchPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = R.attr.switchPreferenceCompatStyle, + defStyleRes: Int = 0 +) : 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) { + super.onBindViewHolder(holder) + + if (needToUpdateSwitch) { + val switch = holder.findViewById(androidx.preference.R.id.switchWidget) + + if (switch is SwitchCompat) { + switch.apply { + trackDrawable = R.drawable.ui_m3_switch_track.resolveDrawable(context) + trackTintList = R.color.sel_m3_switch_track.resolveStateList(context) + thumbDrawable = R.drawable.ui_m3_switch_thumb.resolveDrawable(context) + thumbTintList = R.color.sel_m3_switch_thumb.resolveStateList(context) + } + + needToUpdateSwitch = false + } + } + } +} diff --git a/app/src/main/res/color/sel_m3_switch_thumb.xml b/app/src/main/res/color/sel_m3_switch_thumb.xml new file mode 100644 index 000000000..61c22f9ad --- /dev/null +++ b/app/src/main/res/color/sel_m3_switch_thumb.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/color/sel_m3_switch_track.xml b/app/src/main/res/color/sel_m3_switch_track.xml new file mode 100644 index 000000000..5d2e6df2f --- /dev/null +++ b/app/src/main/res/color/sel_m3_switch_track.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ui_m3_switch_thumb.xml b/app/src/main/res/drawable/ui_m3_switch_thumb.xml new file mode 100644 index 000000000..04e5221cf --- /dev/null +++ b/app/src/main/res/drawable/ui_m3_switch_thumb.xml @@ -0,0 +1,15 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ui_m3_switch_track.xml b/app/src/main/res/drawable/ui_m3_switch_track.xml new file mode 100644 index 000000000..f89e5de3a --- /dev/null +++ b/app/src/main/res/drawable/ui_m3_switch_track.xml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/typography.xml b/app/src/main/res/values/typography.xml index 07cead865..e76ae0924 100644 --- a/app/src/main/res/values/typography.xml +++ b/app/src/main/res/values/typography.xml @@ -72,7 +72,7 @@