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.
This commit is contained in:
OxygenCobalt 2022-01-23 10:53:39 -07:00
parent 50170f202e
commit c98d131316
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
7 changed files with 88 additions and 11 deletions

View file

@ -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
}
}
}
}

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorOnPrimary" android:state_checked="true" />
<item android:color="?attr/colorSurfaceVariant" />
</selector>

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_checked="true" />
<item android:color="?attr/colorOnSurfaceVariant" />
</selector>

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp">
<shape android:shape="oval">
<solid android:color="#000000" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
</item>
</layer-list>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
<corners android:radius="56dp" />
<size
android:width="64dp"
android:height="28dp" />
</shape>
</item>
</layer-list>

View file

@ -72,7 +72,7 @@
</style> </style>
<style name="TextAppearance.Auxio.TitleSmallish" parent="TextAppearance.Material3.TitleSmall"> <style name="TextAppearance.Auxio.TitleSmallish" parent="TextAppearance.Material3.TitleSmall">
<item name="android:textSize">16sp</item> <item name="android:textSize">18sp</item>
</style> </style>
<style name="TextAppearance.Auxio.LabelLarger" parent="TextAppearance.Auxio.LabelLarge"> <style name="TextAppearance.Auxio.LabelLarger" parent="TextAppearance.Auxio.LabelLarge">

View file

@ -20,7 +20,7 @@
app:key="auxio_accent" app:key="auxio_accent"
app:title="@string/set_accent" /> app:title="@string/set_accent" />
<SwitchPreferenceCompat <org.oxycblt.auxio.settings.pref.M3SwitchPreference
app:allowDividerBelow="false" app:allowDividerBelow="false"
app:defaultValue="false" app:defaultValue="false"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
@ -40,14 +40,14 @@
app:summary="@string/set_lib_tabs_desc" app:summary="@string/set_lib_tabs_desc"
app:title="@string/set_lib_tabs" /> app:title="@string/set_lib_tabs" />
<SwitchPreferenceCompat <org.oxycblt.auxio.settings.pref.M3SwitchPreference
app:defaultValue="true" app:defaultValue="true"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
app:key="KEY_SHOW_COVERS" app:key="KEY_SHOW_COVERS"
app:summary="@string/set_show_covers_desc" app:summary="@string/set_show_covers_desc"
app:title="@string/set_show_covers" /> app:title="@string/set_show_covers" />
<SwitchPreferenceCompat <org.oxycblt.auxio.settings.pref.M3SwitchPreference
app:defaultValue="false" app:defaultValue="false"
app:dependency="KEY_SHOW_COVERS" app:dependency="KEY_SHOW_COVERS"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
@ -55,7 +55,7 @@
app:summary="@string/set_quality_covers_desc" app:summary="@string/set_quality_covers_desc"
app:title="@string/set_quality_covers" /> app:title="@string/set_quality_covers" />
<SwitchPreferenceCompat <org.oxycblt.auxio.settings.pref.M3SwitchPreference
app:defaultValue="false" app:defaultValue="false"
app:dependency="KEY_SHOW_COVERS" app:dependency="KEY_SHOW_COVERS"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
@ -63,7 +63,7 @@
app:summary="@string/set_round_covers_desc" app:summary="@string/set_round_covers_desc"
app:title="@string/set_round_covers" /> app:title="@string/set_round_covers" />
<SwitchPreferenceCompat <org.oxycblt.auxio.settings.pref.M3SwitchPreference
app:allowDividerBelow="false" app:allowDividerBelow="false"
app:defaultValue="false" app:defaultValue="false"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
@ -78,14 +78,14 @@
app:layout="@layout/item_header" app:layout="@layout/item_header"
app:title="@string/set_audio"> app:title="@string/set_audio">
<SwitchPreferenceCompat <org.oxycblt.auxio.settings.pref.M3SwitchPreference
app:defaultValue="true" app:defaultValue="true"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
app:key="KEY_AUDIO_FOCUS" app:key="KEY_AUDIO_FOCUS"
app:summary="@string/set_focus_desc" app:summary="@string/set_focus_desc"
app:title="@string/set_focus" /> app:title="@string/set_focus" />
<SwitchPreferenceCompat <org.oxycblt.auxio.settings.pref.M3SwitchPreference
app:defaultValue="true" app:defaultValue="true"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
app:key="KEY_PLUG_MGT" app:key="KEY_PLUG_MGT"
@ -116,14 +116,14 @@
app:title="@string/set_song_mode" app:title="@string/set_song_mode"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
<SwitchPreferenceCompat <org.oxycblt.auxio.settings.pref.M3SwitchPreference
app:defaultValue="true" app:defaultValue="true"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
app:key="KEY_KEEP_SHUFFLE" app:key="KEY_KEEP_SHUFFLE"
app:summary="@string/set_keep_shuffle_desc" app:summary="@string/set_keep_shuffle_desc"
app:title="@string/set_keep_shuffle" /> app:title="@string/set_keep_shuffle" />
<SwitchPreferenceCompat <org.oxycblt.auxio.settings.pref.M3SwitchPreference
app:allowDividerBelow="false" app:allowDividerBelow="false"
app:defaultValue="true" app:defaultValue="true"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
@ -131,7 +131,7 @@
app:summary="@string/set_rewind_prev_desc" app:summary="@string/set_rewind_prev_desc"
app:title="@string/set_rewind_prev" /> app:title="@string/set_rewind_prev" />
<SwitchPreferenceCompat <org.oxycblt.auxio.settings.pref.M3SwitchPreference
app:allowDividerBelow="false" app:allowDividerBelow="false"
app:defaultValue="false" app:defaultValue="false"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"