Added MD3 switch

This commit is contained in:
Prathamesh More 2022-06-06 21:35:58 +05:30
parent 33a0d113b8
commit b28d5678bc
9 changed files with 46 additions and 50 deletions

View file

@ -14,7 +14,7 @@ android {
vectorDrawables.useSupportLibrary = true vectorDrawables.useSupportLibrary = true
applicationId "code.name.monkey.retromusic" applicationId "code.name.monkey.retromusic"
versionCode 10588 versionCode 10590
versionName '6.0.0' versionName '6.0.0'
buildConfigField("String", "GOOGLE_PLAY_LICENSING_KEY", "\"${getProperty(getProperties('../public.properties'), 'GOOGLE_PLAY_LICENSE_KEY')}\"") buildConfigField("String", "GOOGLE_PLAY_LICENSING_KEY", "\"${getProperty(getProperties('../public.properties'), 'GOOGLE_PLAY_LICENSE_KEY')}\"")

View file

@ -135,7 +135,9 @@ fun Button.accentTextColor() {
fun MaterialButton.accentBackgroundColor() { fun MaterialButton.accentBackgroundColor() {
if (materialYou) return if (materialYou) return
backgroundTintList = ColorStateList.valueOf(context.accentColor()) backgroundTintList = ColorStateList(
arrayOf(intArrayOf(android.R.attr.state_enabled), intArrayOf()),
intArrayOf(context.accentColor(), context.accentColor().addAlpha(0.12f)))
} }
fun MaterialButton.accentOutlineColor() { fun MaterialButton.accentOutlineColor() {

View file

@ -79,8 +79,7 @@
app:showText="false" app:showText="false"
android:clickable="false" android:clickable="false"
android:focusable="false" android:focusable="false"
app:track="@drawable/switch_track" style="@style/Widget.Material3.CompoundButton.MaterialSwitch"
android:thumb="@drawable/switch_thumb"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />

View file

@ -65,8 +65,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:showText="false" app:showText="false"
app:track="@drawable/switch_track" style="@style/Widget.Material3.CompoundButton.MaterialSwitch"
android:thumb="@drawable/switch_thumb"
android:clickable="false" android:clickable="false"
android:focusable="false" android:focusable="false"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"

View file

@ -343,7 +343,7 @@ private constructor(private val mContext: Context) : ThemeStorePrefKeys, ThemeSt
return true return true
} }
private fun isMD3Enabled(context: Context): Boolean { fun isMD3Enabled(context: Context): Boolean {
return PreferenceManager.getDefaultSharedPreferences(context) return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(ThemeStorePrefKeys.KEY_MATERIAL_YOU, VersionUtils.hasS()) .getBoolean(ThemeStorePrefKeys.KEY_MATERIAL_YOU, VersionUtils.hasS())
} }

View file

@ -26,9 +26,6 @@ class ATEAccentTextView @JvmOverloads constructor(
) : AppCompatTextView(context, attrs, defStyleAttr) { ) : AppCompatTextView(context, attrs, defStyleAttr) {
init { init {
setTextColor( setTextColor(ThemeStore.accentColor(context))
// Set MD3 accent if MD3 is enabled or in-app accent otherwise
ThemeStore.accentColor(context)
)
} }
} }

View file

@ -2,35 +2,25 @@ package code.name.monkey.appthemehelper.common.views
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import androidx.appcompat.widget.SwitchCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
import code.name.monkey.appthemehelper.ATH import code.name.monkey.appthemehelper.ATH
import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.appthemehelper.ThemeStore
import com.google.android.material.materialswitch.MaterialSwitch
/** /**
* @author Aidan Follestad (afollestad) * @author Aidan Follestad (afollestad)
*/ */
class ATESwitch : SwitchCompat { class ATESwitch @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = -1,
) : MaterialSwitch(context, attrs, defStyleAttr) {
constructor(context: Context) : super(context) { init {
init(context) if (!isInEditMode && !ThemeStore.isMD3Enabled(context)) {
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init(context)
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
init(context)
}
private fun init(context: Context) {
ATH.setTint(this, ThemeStore.accentColor(context)) ATH.setTint(this, ThemeStore.accentColor(context))
} }
}
override fun isShown(): Boolean { override fun isShown(): Boolean {
return parent != null && isVisible return parent != null && isVisible

View file

@ -3,6 +3,7 @@ package code.name.monkey.appthemehelper.util;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable; import android.graphics.drawable.RippleDrawable;
@ -14,7 +15,6 @@ import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.RadioButton; import android.widget.RadioButton;
import android.widget.SeekBar; import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.CheckResult; import androidx.annotation.CheckResult;
@ -28,6 +28,7 @@ import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.graphics.drawable.DrawableCompat;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.materialswitch.MaterialSwitch;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -210,17 +211,6 @@ public final class TintHelper {
image.setColorFilter(color, PorterDuff.Mode.SRC_ATOP); image.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
} }
public static void setTint(@NonNull Switch switchView, @ColorInt int color, boolean useDarker) {
if (switchView.getTrackDrawable() != null) {
switchView.setTrackDrawable(modifySwitchDrawable(switchView.getContext(),
switchView.getTrackDrawable(), color, false, false, useDarker));
}
if (switchView.getThumbDrawable() != null) {
switchView.setThumbDrawable(modifySwitchDrawable(switchView.getContext(),
switchView.getThumbDrawable(), color, true, false, useDarker));
}
}
public static void setTint(@NonNull SwitchCompat switchView, @ColorInt int color, boolean useDarker) { public static void setTint(@NonNull SwitchCompat switchView, @ColorInt int color, boolean useDarker) {
if (switchView.getTrackDrawable() != null) { if (switchView.getTrackDrawable() != null) {
switchView.setTrackDrawable(modifySwitchDrawable(switchView.getContext(), switchView.setTrackDrawable(modifySwitchDrawable(switchView.getContext(),
@ -232,6 +222,15 @@ public final class TintHelper {
} }
} }
public static void setTint(@NonNull MaterialSwitch switchView, @ColorInt int color, boolean useDarker) {
if (switchView.getTrackDrawable() != null) {
switchView.setTrackTintList(createSwitchDrawableTintList(switchView.getContext(), color, false, true, useDarker));
}
if (switchView.getThumbDrawable() != null) {
switchView.setThumbTintList(createSwitchDrawableTintList(switchView.getContext(), color, true, true, useDarker));
}
}
public static void setTintAuto(final @NonNull View view, final @ColorInt int color, public static void setTintAuto(final @NonNull View view, final @ColorInt int color,
boolean background) { boolean background) {
setTintAuto(view, color, background, ATHUtil.INSTANCE.isWindowBackgroundDark(view.getContext())); setTintAuto(view, color, background, ATHUtil.INSTANCE.isWindowBackgroundDark(view.getContext()));
@ -254,8 +253,8 @@ public final class TintHelper {
setTint((CheckBox) view, color, isDark); setTint((CheckBox) view, color, isDark);
} else if (view instanceof ImageView) { } else if (view instanceof ImageView) {
setTint((ImageView) view, color); setTint((ImageView) view, color);
} else if (view instanceof Switch) { } else if (view instanceof MaterialSwitch) {
setTint((Switch) view, color, isDark); setTint((MaterialSwitch) view, color, isDark);
} else if (view instanceof SwitchCompat) { } else if (view instanceof SwitchCompat) {
setTint((SwitchCompat) view, color, isDark); setTint((SwitchCompat) view, color, isDark);
} else { } else {
@ -388,12 +387,15 @@ public final class TintHelper {
}); });
} }
private static Drawable modifySwitchDrawable(@NonNull Context context, @NonNull Drawable from, @ColorInt int tint, private static ColorStateList createSwitchDrawableTintList(@NonNull Context context, @ColorInt int tint,
boolean thumb, boolean compatSwitch, boolean useDarker) { boolean thumb, boolean compatSwitch, boolean useDarker) {
int lighterTint = ColorUtil.INSTANCE.blendColors(tint, Color.WHITE, 0.4f);
int darkerTint = ColorUtil.INSTANCE.shiftColor(tint, 0.8f);
if (useDarker) { if (useDarker) {
tint = ColorUtil.INSTANCE.shiftColor(tint, 1.1f); tint = (compatSwitch && !thumb) ? lighterTint : darkerTint;
} else {
tint = (compatSwitch && !thumb) ? darkerTint : Color.WHITE;
} }
tint = ColorUtil.INSTANCE.adjustAlpha(tint, (compatSwitch && !thumb) ? 0.5f : 1.0f);
int disabled; int disabled;
int normal; int normal;
if (thumb) { if (thumb) {
@ -413,7 +415,7 @@ public final class TintHelper {
normal = ColorUtil.INSTANCE.stripAlpha(normal); normal = ColorUtil.INSTANCE.stripAlpha(normal);
} }
final ColorStateList sl = new ColorStateList( return new ColorStateList(
new int[][]{ new int[][]{
new int[]{-android.R.attr.state_enabled}, new int[]{-android.R.attr.state_enabled},
new int[]{android.R.attr.state_enabled, -android.R.attr.state_activated, new int[]{android.R.attr.state_enabled, -android.R.attr.state_activated,
@ -428,6 +430,12 @@ public final class TintHelper {
tint tint
} }
); );
}
private static Drawable modifySwitchDrawable(@NonNull Context context, @NonNull Drawable from, @ColorInt int tint,
boolean thumb, boolean compatSwitch, boolean useDarker) {
ColorStateList sl = createSwitchDrawableTintList(context, tint, thumb, compatSwitch, useDarker);
return createTintedDrawable(from, sl); return createTintedDrawable(from, sl);
} }

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<code.name.monkey.appthemehelper.common.views.ATESwitch xmlns:android="http://schemas.android.com/apk/res/android" <code.name.monkey.appthemehelper.common.views.ATESwitch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/checkbox" android:id="@android:id/checkbox"
style="@style/Widget.Material3.CompoundButton.MaterialSwitch"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:clickable="false" android:clickable="false"