fix switches

This commit is contained in:
gdlbo
2023-03-18 03:14:42 +03:00
parent db95b53d41
commit 125c2d3d69
4 changed files with 43 additions and 38 deletions
@@ -1,5 +1,6 @@
package ru.vtosters.lite.hooks;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.widget.CompoundButton;
@@ -8,46 +9,50 @@ import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.widget.CompoundButtonCompat;
import com.vk.core.ui.themes.VKThemeHelper;
import com.vk.core.util.ColorUtils;
import ru.vtosters.lite.utils.AndroidUtils;
import ru.vtosters.lite.utils.Preferences;
import ru.vtosters.lite.utils.ThemesUtils;
import static com.vk.core.ui.themes.VKThemeHelper.r;
import static com.vtosters.lite.R.color.*;
public class SwitchHook {
private static final int[] SWITCH_COLORS = {
getSwitchColor(),
getDisabledSwitchColor(),
ContextCompat.getColor(VKThemeHelper.u(), gray_20),
ThemesUtils.getAccentColor()
};
private static final int[] TRACK_COLORS = {
ColorUtils.b(ThemesUtils.getColorFromAttr(com.vtosters.lite.R.attr.loader_track_fill), 0.4f),
ColorUtils.b(ThemesUtils.getAccentColor(), 0.12f),
ThemesUtils.getColorFromAttr(com.vtosters.lite.R.attr.loader_track_fill),
ColorUtils.b(ThemesUtils.getAccentColor(), 0.48f)
};
private static int[] getSwitchColors(Context ctx) {
return new int[] {
ContextCompat.getColor(ctx, ThemesUtils.isDarkTheme() ? gray_800 : white),
ContextCompat.getColor(ctx, ThemesUtils.isDarkTheme() ? switch_disabled_on_dark : switch_disabled_on_light),
ContextCompat.getColor(ctx, gray_20),
ThemesUtils.getAccentColor()
};
}
public static void setSwitchCompatColors(SwitchCompat switchCompat) {
private static int[] getTrackColors() {
return new int[] {
ColorUtils.b(ThemesUtils.getColorFromAttr(com.vtosters.lite.R.attr.loader_track_fill), 0.4f),
ColorUtils.b(ThemesUtils.getAccentColor(), 0.12f),
ThemesUtils.getColorFromAttr(com.vtosters.lite.R.attr.loader_track_fill),
ColorUtils.b(ThemesUtils.getAccentColor(), 0.48f)
};
}
public static void setSwitchCompatColors(SwitchCompat switchCompat, Context ctx) {
if (Preferences.getBoolValue("useCustomPrefsStyle", false)) {
switchCompat.setThumbDrawable(setTintList(getCustomThumb(), new ColorStateList(createTrackColorStateList(), SWITCH_COLORS)));
switchCompat.setTrackDrawable(setTintList(getCustomTrack(), new ColorStateList(createTrackColorStateList(), TRACK_COLORS)));
switchCompat.setThumbDrawable(setTintList(getCustomThumb(), new ColorStateList(createColorStateList(), getSwitchColors(ctx))));
switchCompat.setTrackDrawable(setTintList(getCustomTrack(), new ColorStateList(createColorStateList(), getTrackColors())));
} else {
setTintList(switchCompat.getThumbDrawable(), new ColorStateList(createTrackColorStateList(), SWITCH_COLORS));
setTintList(switchCompat.getTrackDrawable(), new ColorStateList(createTrackColorStateList(), TRACK_COLORS));
setTintList(switchCompat.getThumbDrawable(), new ColorStateList(createColorStateList(), getSwitchColors(ctx)));
setTintList(switchCompat.getTrackDrawable(), new ColorStateList(createColorStateList(), getTrackColors()));
}
}
public static void setSwitchColors(Switch drawableCompat) {
public static void setSwitchColors(Switch drawableCompat, Context ctx) {
if (Preferences.getBoolValue("useCustomPrefsStyle", false)) {
drawableCompat.setThumbDrawable(setTintList(getCustomThumb(), new ColorStateList(createTrackColorStateList(), SWITCH_COLORS)));
drawableCompat.setTrackDrawable(setTintList(getCustomTrack(), new ColorStateList(createTrackColorStateList(), TRACK_COLORS)));
drawableCompat.setThumbDrawable(setTintList(getCustomThumb(), new ColorStateList(createColorStateList(), getSwitchColors(ctx))));
drawableCompat.setTrackDrawable(setTintList(getCustomTrack(), new ColorStateList(createColorStateList(), getTrackColors())));
} else {
setTintList(drawableCompat.getThumbDrawable(), new ColorStateList(createTrackColorStateList(), SWITCH_COLORS));
setTintList(drawableCompat.getTrackDrawable(), new ColorStateList(createTrackColorStateList(), TRACK_COLORS));
setTintList(drawableCompat.getThumbDrawable(), new ColorStateList(createColorStateList(), getSwitchColors(ctx)));
setTintList(drawableCompat.getTrackDrawable(), new ColorStateList(createColorStateList(), getTrackColors()));
}
}
@@ -55,7 +60,7 @@ public class SwitchHook {
CompoundButtonCompat.setButtonTintList(
compoundButton,
new ColorStateList(
createTrackColorStateList(),
createColorStateList(),
new int[]{
ColorUtils.b(ThemesUtils.getColorFromAttr(com.vtosters.lite.R.attr.selection_off_icon), 0.4f),
ColorUtils.b(ThemesUtils.getAccentColor(), 0.4f),
@@ -63,7 +68,7 @@ public class SwitchHook {
ThemesUtils.getAccentColor()}));
}
private static int[][] createTrackColorStateList() {
private static int[][] createColorStateList() {
return new int[][]{
new int[]{-android.R.attr.state_enabled, -android.R.attr.state_checked},
new int[]{-android.R.attr.state_enabled, android.R.attr.state_checked},
@@ -78,10 +83,6 @@ public class SwitchHook {
return wrappedDrawable;
}
private static int getSwitchColor() {
return ContextCompat.getColor(VKThemeHelper.u(), r() ? gray_800 : white);
}
private static Drawable getCustomTrack() {
return AndroidUtils.getGlobalContext().getDrawable(AndroidUtils.getIdentifier("custom_track_selector", "drawable"));
}
@@ -89,8 +90,4 @@ public class SwitchHook {
private static Drawable getCustomThumb() {
return AndroidUtils.getGlobalContext().getDrawable(AndroidUtils.getIdentifier("custom_thumb_selector", "drawable"));
}
private static int getDisabledSwitchColor() {
return ContextCompat.getColor(VKThemeHelper.u(), r() ? switch_disabled_on_dark : switch_disabled_on_light);
}
}
@@ -141,7 +141,7 @@ public class StickerPackAdapter extends RecyclerView.Adapter<StickerPackAdapter.
mSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> sService.setPackEnabled(pack, isChecked, true));
mStickersCount.setText(pack.stickersCount + " " + mStickersCount.getContext().getString(R.string.stickerscount));
SwitchHook.setSwitchCompatColors(mSwitch);
SwitchHook.setSwitchCompatColors(mSwitch, mSwitch.getContext());
// Color костыль
int color = getTextAttr();
@@ -94,7 +94,7 @@ public class MessageSettings {
silentSwitch.setPadding(dp2px(24f), dp2px(12f), dp2px(18f), dp2px(12f));
SwitchHook.setSwitchCompatColors(silentSwitch);
SwitchHook.setSwitchCompatColors(silentSwitch, silentSwitch.getContext());
mContainer.setLayoutParams(LayoutUtils.createLinear(-1, -1));
mContainer.setOrientation(LinearLayout.VERTICAL);