Added MD3 switch
This commit is contained in:
parent
33a0d113b8
commit
b28d5678bc
9 changed files with 46 additions and 50 deletions
|
@ -14,7 +14,7 @@ android {
|
|||
vectorDrawables.useSupportLibrary = true
|
||||
|
||||
applicationId "code.name.monkey.retromusic"
|
||||
versionCode 10588
|
||||
versionCode 10590
|
||||
versionName '6.0.0'
|
||||
|
||||
buildConfigField("String", "GOOGLE_PLAY_LICENSING_KEY", "\"${getProperty(getProperties('../public.properties'), 'GOOGLE_PLAY_LICENSE_KEY')}\"")
|
||||
|
|
|
@ -135,7 +135,9 @@ fun Button.accentTextColor() {
|
|||
|
||||
fun MaterialButton.accentBackgroundColor() {
|
||||
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() {
|
||||
|
|
|
@ -79,8 +79,7 @@
|
|||
app:showText="false"
|
||||
android:clickable="false"
|
||||
android:focusable="false"
|
||||
app:track="@drawable/switch_track"
|
||||
android:thumb="@drawable/switch_thumb"
|
||||
style="@style/Widget.Material3.CompoundButton.MaterialSwitch"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
|
|
@ -65,8 +65,7 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:showText="false"
|
||||
app:track="@drawable/switch_track"
|
||||
android:thumb="@drawable/switch_thumb"
|
||||
style="@style/Widget.Material3.CompoundButton.MaterialSwitch"
|
||||
android:clickable="false"
|
||||
android:focusable="false"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
|
|
|
@ -343,7 +343,7 @@ private constructor(private val mContext: Context) : ThemeStorePrefKeys, ThemeSt
|
|||
return true
|
||||
}
|
||||
|
||||
private fun isMD3Enabled(context: Context): Boolean {
|
||||
fun isMD3Enabled(context: Context): Boolean {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context)
|
||||
.getBoolean(ThemeStorePrefKeys.KEY_MATERIAL_YOU, VersionUtils.hasS())
|
||||
}
|
||||
|
|
|
@ -26,9 +26,6 @@ class ATEAccentTextView @JvmOverloads constructor(
|
|||
) : AppCompatTextView(context, attrs, defStyleAttr) {
|
||||
|
||||
init {
|
||||
setTextColor(
|
||||
// Set MD3 accent if MD3 is enabled or in-app accent otherwise
|
||||
ThemeStore.accentColor(context)
|
||||
)
|
||||
setTextColor(ThemeStore.accentColor(context))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,35 +2,25 @@ package code.name.monkey.appthemehelper.common.views
|
|||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import androidx.appcompat.widget.SwitchCompat
|
||||
import androidx.core.view.isVisible
|
||||
import code.name.monkey.appthemehelper.ATH
|
||||
import code.name.monkey.appthemehelper.ThemeStore
|
||||
import com.google.android.material.materialswitch.MaterialSwitch
|
||||
|
||||
/**
|
||||
* @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(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) {
|
||||
init {
|
||||
if (!isInEditMode && !ThemeStore.isMD3Enabled(context)) {
|
||||
ATH.setTint(this, ThemeStore.accentColor(context))
|
||||
}
|
||||
}
|
||||
|
||||
override fun isShown(): Boolean {
|
||||
return parent != null && isVisible
|
||||
|
|
|
@ -3,6 +3,7 @@ package code.name.monkey.appthemehelper.util;
|
|||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.RippleDrawable;
|
||||
|
@ -14,7 +15,6 @@ import android.widget.ImageView;
|
|||
import android.widget.ProgressBar;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.CheckResult;
|
||||
|
@ -28,6 +28,7 @@ import androidx.core.content.ContextCompat;
|
|||
import androidx.core.graphics.drawable.DrawableCompat;
|
||||
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.google.android.material.materialswitch.MaterialSwitch;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
|
@ -210,17 +211,6 @@ public final class TintHelper {
|
|||
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) {
|
||||
if (switchView.getTrackDrawable() != null) {
|
||||
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,
|
||||
boolean background) {
|
||||
setTintAuto(view, color, background, ATHUtil.INSTANCE.isWindowBackgroundDark(view.getContext()));
|
||||
|
@ -254,8 +253,8 @@ public final class TintHelper {
|
|||
setTint((CheckBox) view, color, isDark);
|
||||
} else if (view instanceof ImageView) {
|
||||
setTint((ImageView) view, color);
|
||||
} else if (view instanceof Switch) {
|
||||
setTint((Switch) view, color, isDark);
|
||||
} else if (view instanceof MaterialSwitch) {
|
||||
setTint((MaterialSwitch) view, color, isDark);
|
||||
} else if (view instanceof SwitchCompat) {
|
||||
setTint((SwitchCompat) view, color, isDark);
|
||||
} 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) {
|
||||
int lighterTint = ColorUtil.INSTANCE.blendColors(tint, Color.WHITE, 0.4f);
|
||||
int darkerTint = ColorUtil.INSTANCE.shiftColor(tint, 0.8f);
|
||||
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 normal;
|
||||
if (thumb) {
|
||||
|
@ -413,7 +415,7 @@ public final class TintHelper {
|
|||
normal = ColorUtil.INSTANCE.stripAlpha(normal);
|
||||
}
|
||||
|
||||
final ColorStateList sl = new ColorStateList(
|
||||
return new ColorStateList(
|
||||
new int[][]{
|
||||
new int[]{-android.R.attr.state_enabled},
|
||||
new int[]{android.R.attr.state_enabled, -android.R.attr.state_activated,
|
||||
|
@ -428,6 +430,12 @@ public final class TintHelper {
|
|||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<code.name.monkey.appthemehelper.common.views.ATESwitch xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@android:id/checkbox"
|
||||
style="@style/Widget.Material3.CompoundButton.MaterialSwitch"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:clickable="false"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue