From 3658fc423b373efb512deff4c17c951ca24b2ac0 Mon Sep 17 00:00:00 2001 From: sk Date: Mon, 19 Dec 2022 20:59:07 -0300 Subject: [PATCH] simplify themes --- .../android/GlobalUserPreferences.java | 24 +- .../android/ui/utils/ColorPalette.java | 68 ++ .../android/ui/utils/UiUtils.java | 102 +-- .../src/main/res/values-night/palettes.xml | 4 + mastodon/src/main/res/values-night/styles.xml | 21 - .../src/main/res/values-notnight/palettes.xml | 3 + mastodon/src/main/res/values-v27/colors.xml | 5 - mastodon/src/main/res/values/attrs.xml | 26 + mastodon/src/main/res/values/colors.xml | 190 +---- mastodon/src/main/res/values/palettes.xml | 228 +++++ mastodon/src/main/res/values/styles.xml | 802 ++---------------- 11 files changed, 460 insertions(+), 1013 deletions(-) create mode 100644 mastodon/src/main/java/org/joinmastodon/android/ui/utils/ColorPalette.java create mode 100644 mastodon/src/main/res/values-night/palettes.xml create mode 100644 mastodon/src/main/res/values-notnight/palettes.xml create mode 100644 mastodon/src/main/res/values/palettes.xml diff --git a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java index 285b619c9..9ebcf2e54 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java +++ b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java @@ -4,7 +4,6 @@ import static org.joinmastodon.android.api.MastodonAPIController.gson; import android.content.Context; import android.content.SharedPreferences; -import android.os.Build; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; @@ -32,7 +31,6 @@ public class GlobalUserPreferences{ private final static Type recentLanguagesType = new TypeToken>>() {}.getType(); public static Map> recentLanguages; - public static Map defaultLanguages; private static SharedPreferences getPrefs(){ return MastodonApp.context.getSharedPreferences("global", Context.MODE_PRIVATE); @@ -59,12 +57,13 @@ public class GlobalUserPreferences{ voteButtonForSingleChoice=prefs.getBoolean("voteButtonForSingleChoice", true); theme=ThemePreference.values()[prefs.getInt("theme", 0)]; recentLanguages=fromJson(prefs.getString("recentLanguages", "{}"), recentLanguagesType, new HashMap<>()); - if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){ - color=ColorPreference.values()[prefs.getInt("color", 0)]; - }else{ - color=ColorPreference.values()[prefs.getInt("color", 1)]; - } + try { + color=ColorPreference.valueOf(prefs.getString("color", ColorPreference.PINK.name())); + } catch (IllegalArgumentException|ClassCastException ignored) { + // invalid color name or color was previously saved as integer + color=ColorPreference.PINK; + } } public static void save(){ @@ -81,20 +80,20 @@ public class GlobalUserPreferences{ .putBoolean("disableMarquee", disableMarquee) .putBoolean("disableSwipe", disableSwipe) .putInt("theme", theme.ordinal()) + .putString("color", color.name()) .putString("recentLanguages", gson.toJson(recentLanguages)) - .putInt("color", color.ordinal()) .apply(); } public enum ColorPreference{ MATERIAL3, - PURPLE, PINK, + PURPLE, GREEN, BLUE, - ORANGE, - YELLOW, - RED + BROWN, + RED, + YELLOW } public enum ThemePreference{ @@ -103,3 +102,4 @@ public class GlobalUserPreferences{ DARK } } + diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/utils/ColorPalette.java b/mastodon/src/main/java/org/joinmastodon/android/ui/utils/ColorPalette.java new file mode 100644 index 000000000..e4edf2796 --- /dev/null +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/utils/ColorPalette.java @@ -0,0 +1,68 @@ +package org.joinmastodon.android.ui.utils; + +import static org.joinmastodon.android.GlobalUserPreferences.ColorPreference; +import static org.joinmastodon.android.GlobalUserPreferences.ThemePreference; +import static org.joinmastodon.android.GlobalUserPreferences.theme; +import static org.joinmastodon.android.GlobalUserPreferences.trueBlackTheme; + +import android.content.Context; +import android.content.res.Resources; + +import androidx.annotation.StyleRes; + +import org.joinmastodon.android.GlobalUserPreferences; +import org.joinmastodon.android.R; + +import java.util.Map; + +public class ColorPalette { + public static final Map palettes = Map.of( + ColorPreference.MATERIAL3, new ColorPalette(R.style.ColorPalette_Material3) + .dark(R.style.ColorPalette_Material3_Dark, R.style.ColorPalette_Material3_AutoLightDark), + ColorPreference.PINK, new ColorPalette(R.style.ColorPalette_Pink), + ColorPreference.PURPLE, new ColorPalette(R.style.ColorPalette_Purple), + ColorPreference.GREEN, new ColorPalette(R.style.ColorPalette_Green), + ColorPreference.BLUE, new ColorPalette(R.style.ColorPalette_Blue), + ColorPreference.BROWN, new ColorPalette(R.style.ColorPalette_Brown), + ColorPreference.RED, new ColorPalette(R.style.ColorPalette_Red), + ColorPreference.YELLOW, new ColorPalette(R.style.ColorPalette_Yellow) + ); + + private @StyleRes int base; + private @StyleRes int autoDark; + private @StyleRes int light; + private @StyleRes int dark; + private @StyleRes int black; + private @StyleRes int autoBlack; + + public ColorPalette(@StyleRes int baseRes) { base = baseRes; } + + public ColorPalette(@StyleRes int lightRes, @StyleRes int darkRes, @StyleRes int autoDarkRes, @StyleRes int blackRes, @StyleRes int autoBlackRes) { + light = lightRes; + dark = darkRes; + autoDark = autoDarkRes; + black = blackRes; + autoBlack = autoBlackRes; + } + + public ColorPalette light(@StyleRes int res) { light = res; return this; } + public ColorPalette dark(@StyleRes int res, @StyleRes int auto) { dark = res; autoDark = auto; return this; } + public ColorPalette black(@StyleRes int res, @StyleRes int auto) { dark = res; autoBlack = auto; return this; } + + public void apply(Context context) { + if (!((dark != 0 && autoDark != 0) || (black != 0 && autoBlack != 0) || light != 0 || base != 0)) { + throw new IllegalStateException("Invalid color scheme definition"); + } + + Resources.Theme t = context.getTheme(); + if (base != 0) t.applyStyle(base, true); + if (light != 0 && theme.equals(ThemePreference.LIGHT)) t.applyStyle(light, true); + else if (theme.equals(ThemePreference.DARK)) { + if (dark != 0 && !trueBlackTheme) t.applyStyle(dark, true); + else if (black != 0 && trueBlackTheme) t.applyStyle(black, true); + } else if (theme.equals(ThemePreference.AUTO)) { + if (autoDark != 0 && !trueBlackTheme) t.applyStyle(autoDark, true); + else if (autoBlack != 0 && trueBlackTheme) t.applyStyle(autoBlack, true); + } + } +} diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java b/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java index a56b94830..bea5dacc9 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java @@ -1,5 +1,8 @@ package org.joinmastodon.android.ui.utils; +import static org.joinmastodon.android.GlobalUserPreferences.theme; +import static org.joinmastodon.android.GlobalUserPreferences.trueBlackTheme; + import android.annotation.SuppressLint; import android.app.Activity; import android.content.ActivityNotFoundException; @@ -24,7 +27,6 @@ import android.os.Looper; import android.os.VibrationEffect; import android.os.Vibrator; import android.provider.OpenableColumns; -import android.provider.Settings; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextUtils; @@ -57,8 +59,6 @@ import org.joinmastodon.android.events.NotificationDeletedEvent; import org.joinmastodon.android.events.RemoveAccountPostsEvent; import org.joinmastodon.android.events.StatusDeletedEvent; import org.joinmastodon.android.events.StatusUnpinnedEvent; -import org.joinmastodon.android.fragments.BaseStatusListFragment; -import org.joinmastodon.android.fragments.ComposeFragment; import org.joinmastodon.android.fragments.HashtagTimelineFragment; import org.joinmastodon.android.fragments.ListTimelineFragment; import org.joinmastodon.android.fragments.ProfileFragment; @@ -661,95 +661,19 @@ public class UiUtils{ } public static void setUserPreferredTheme(Context context){ -// boolean isDarkTheme = isDarkTheme(); - switch(GlobalUserPreferences.color){ - case PINK: - context.setTheme(switch(GlobalUserPreferences.theme){ - case AUTO -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_AutoLightDark_TrueBlack : R.style.Theme_Mastodon_AutoLightDark; - case LIGHT -> - R.style.Theme_Mastodon_Light; - case DARK -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_Dark_TrueBlack : R.style.Theme_Mastodon_Dark; - }); - break; - case PURPLE: - context.setTheme(switch(GlobalUserPreferences.theme){ - case AUTO -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_AutoLightDark_TrueBlack_Original : R.style.Theme_Mastodon_AutoLightDark_Original; - case LIGHT -> - R.style.Theme_Mastodon_Light_Original; - case DARK -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_Dark_TrueBlack_Original : R.style.Theme_Mastodon_Dark_Original; - }); - break; - case GREEN: - context.setTheme(switch(GlobalUserPreferences.theme){ - case AUTO -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_AutoLightDark_TrueBlack_Green : R.style.Theme_Mastodon_AutoLightDark_Green; - case LIGHT -> - R.style.Theme_Mastodon_Light_Green; - case DARK -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_Dark_TrueBlack_Green : R.style.Theme_Mastodon_Dark_Green; - }); - break; - case BLUE: - context.setTheme(switch(GlobalUserPreferences.theme){ - case AUTO -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_AutoLightDark_TrueBlack_Blue : R.style.Theme_Mastodon_AutoLightDark_Blue; - case LIGHT -> - R.style.Theme_Mastodon_Light_Blue; - case DARK -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_Dark_TrueBlack_Blue : R.style.Theme_Mastodon_Dark_Blue; - }); - break; - case ORANGE: - context.setTheme(switch(GlobalUserPreferences.theme){ - case AUTO -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_AutoLightDark_TrueBlack_Orange : R.style.Theme_Mastodon_AutoLightDark_Orange; - case LIGHT -> - R.style.Theme_Mastodon_Light_Orange; - case DARK -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_Dark_TrueBlack_Orange : R.style.Theme_Mastodon_Dark_Orange; - }); - break; - case YELLOW: - context.setTheme(switch(GlobalUserPreferences.theme){ - case AUTO -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_AutoLightDark_TrueBlack_Yellow : R.style.Theme_Mastodon_AutoLightDark_Yellow; - case LIGHT -> - R.style.Theme_Mastodon_Light_Yellow; - case DARK -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_Dark_TrueBlack_Yellow : R.style.Theme_Mastodon_Dark_Yellow; - }); - break; - case RED: - context.setTheme(switch(GlobalUserPreferences.theme){ - case AUTO -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_AutoLightDark_TrueBlack_Red : R.style.Theme_Mastodon_AutoLightDark_Red; - case LIGHT -> - R.style.Theme_Mastodon_Light_Red; - case DARK -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_Dark_TrueBlack_Red : R.style.Theme_Mastodon_Dark_Red; - }); - break; - case MATERIAL3: - context.setTheme(switch(GlobalUserPreferences.theme){ - case AUTO -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_AutoLightDark_TrueBlack_Material3 : R.style.Theme_Mastodon_AutoLightDark_Material3; - case LIGHT -> - R.style.Theme_Mastodon_Light_Material3; - case DARK -> - GlobalUserPreferences.trueBlackTheme ? R.style.Theme_Mastodon_Dark_TrueBlack_Material3 : R.style.Theme_Mastodon_Dark_Material3; - }); - break; - } + context.setTheme(switch (theme) { + case LIGHT -> R.style.Theme_Mastodon_Light; + case DARK -> trueBlackTheme ? R.style.Theme_Mastodon_Dark_TrueBlack : R.style.Theme_Mastodon_Dark; + default -> trueBlackTheme ? R.style.Theme_Mastodon_AutoLightDark_TrueBlack : R.style.Theme_Mastodon_AutoLightDark; + }); + ColorPalette palette = ColorPalette.palettes.get(GlobalUserPreferences.color); + if (palette != null) palette.apply(context); } public static boolean isDarkTheme(){ - if(GlobalUserPreferences.theme==GlobalUserPreferences.ThemePreference.AUTO) + if(theme==GlobalUserPreferences.ThemePreference.AUTO) return (MastodonApp.context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK)==Configuration.UI_MODE_NIGHT_YES; - return GlobalUserPreferences.theme==GlobalUserPreferences.ThemePreference.DARK; + return theme==GlobalUserPreferences.ThemePreference.DARK; } public static void openURL(Context context, @Nullable String accountID, String url){ @@ -791,7 +715,7 @@ public class UiUtils{ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) vibrator.vibrate(VibrationEffect.createOneShot(50, VibrationEffect.DEFAULT_AMPLITUDE)); else vibrator.vibrate(50); } - + private static String getSystemProperty(String key){ try{ Class props=Class.forName("android.os.SystemProperties"); diff --git a/mastodon/src/main/res/values-night/palettes.xml b/mastodon/src/main/res/values-night/palettes.xml new file mode 100644 index 000000000..95b8f137e --- /dev/null +++ b/mastodon/src/main/res/values-night/palettes.xml @@ -0,0 +1,4 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mastodon/src/main/res/values/styles.xml b/mastodon/src/main/res/values/styles.xml index 1a659be15..c03f83788 100644 --- a/mastodon/src/main/res/values/styles.xml +++ b/mastodon/src/main/res/values/styles.xml @@ -13,30 +13,30 @@ @style/Widget.Mastodon.Button.Secondary_DarkOnLight @style/Widget.Mastodon.Button.Large.Primary_DarkOnLight @style/Widget.Mastodon.Button.Large.Secondary_DarkOnLight - @color/primary_700 - @color/custom_gray_800 - @color/gray_100 - @color/custom_gray_800 - @color/custom_gray_500 - @color/gray_50 + ?colorPrimary700 + ?colorGray800 + ?colorGray100 + ?colorGray800 + ?colorGray500 + ?colorGray50 #E9EDF2 - @color/gray_50 - @color/gray_25 + ?colorGray50 + ?colorGray25 ?colorBackgroundLightest - @color/custom_gray_900 + ?colorGray900 @color/white - @color/gray_50 - @color/navigation_bar_bg + ?colorGray50 + ?android:statusBarColor @style/Theme.Mastodon.Toolbar @style/Theme.Mastodon.Dialog.Alert - @color/primary_500 - @color/gray_300 - @color/primary_600 - @color/gray_200 - @color/custom_gray_600 - @color/gray_400 - @color/primary_100 - @color/custom_gray_500 + ?colorPrimary500 + ?colorGray300 + ?colorPrimary600 + ?colorGray200 + ?colorGray600 + ?colorGray400 + ?colorPrimary100 + ?colorGray500 @drawable/bg_button_primary_dark_on_light @drawable/bg_edittext_light @@ -45,7 +45,7 @@ true @style/Widget.Mastodon.PopupMenu @style/Widget.Mastodon.PopupMenu - + @color/m3_sys_light_primary @color/m3_sys_light_on_primary @@ -74,6 +74,7 @@ #410E0B + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -