From a8146dab60f16042b0be5ae2085e0d6f117e8958 Mon Sep 17 00:00:00 2001 From: sspanak Date: Wed, 29 Jan 2025 18:01:53 +0200 Subject: [PATCH] themes code cleanup --- app/src/main/AndroidManifest.xml | 9 ++-- .../sspanak/tt9/ui/dialogs/AddWordDialog.java | 15 +++++- .../sspanak/tt9/ui/main/BaseMainLayout.java | 28 +++------- .../tt9/util/ThemedContextBuilder.java | 53 +++++++++++++++++++ app/src/main/res/values-night-v31/colors.xml | 14 +++++ app/src/main/res/values-night-v31/styles.xml | 40 -------------- app/src/main/res/values-night/colors.xml | 4 ++ app/src/main/res/values-night/styles.xml | 11 ---- app/src/main/res/values-v31/colors.xml | 14 +++++ app/src/main/res/values-v31/styles.xml | 38 ++++++------- app/src/main/res/values/colors.xml | 4 ++ app/src/main/res/values/styles.xml | 7 ++- 12 files changed, 137 insertions(+), 100 deletions(-) create mode 100644 app/src/main/java/io/github/sspanak/tt9/util/ThemedContextBuilder.java create mode 100644 app/src/main/res/values-night-v31/colors.xml create mode 100644 app/src/main/res/values-v31/colors.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1fb1184c..e4c92225 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -48,7 +48,8 @@ + android:exported="true" + android:theme="@style/TTheme.Preferences"> @@ -58,7 +59,8 @@ + android:exported="true" + android:theme="@style/TTheme.Preferences"> @@ -68,6 +70,7 @@ + android:name="io.github.sspanak.tt9.ui.dialogs.RequestPermissionDialog" + android:theme="@style/TTheme.Preferences" /> diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/dialogs/AddWordDialog.java b/app/src/main/java/io/github/sspanak/tt9/ui/dialogs/AddWordDialog.java index da8a1fca..564f590f 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/dialogs/AddWordDialog.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/dialogs/AddWordDialog.java @@ -3,6 +3,7 @@ package io.github.sspanak.tt9.ui.dialogs; import android.content.Context; import android.content.Intent; import android.inputmethodservice.InputMethodService; +import android.os.Build; import androidx.annotation.NonNull; @@ -11,7 +12,9 @@ import io.github.sspanak.tt9.db.DataStore; import io.github.sspanak.tt9.db.entities.AddWordResult; import io.github.sspanak.tt9.languages.Language; import io.github.sspanak.tt9.languages.LanguageCollection; +import io.github.sspanak.tt9.preferences.settings.SettingsStore; import io.github.sspanak.tt9.util.ConsumerCompat; +import io.github.sspanak.tt9.util.ThemedContextBuilder; public class AddWordDialog extends PopupDialog { public static final String TYPE = "tt9.popup_dialog.add_word"; @@ -23,7 +26,17 @@ public class AddWordDialog extends PopupDialog { AddWordDialog(@NonNull Context context, @NonNull Intent intent, ConsumerCompat activityFinisher) { - super(context, activityFinisher); + super( + new ThemedContextBuilder() + .setConfiguration(context.getApplicationContext().getResources().getConfiguration()) + .setContext(context) + .setSettings(new SettingsStore(context)) + // The main theme does not work on Android <= 11 and the _AddWord theme does not work on 12+. + // Not sure why since they inherit from the same parent, but it is what it is. + .setTheme(Build.VERSION.SDK_INT < Build.VERSION_CODES.S ? R.style.TTheme_AddWord : R.style.TTheme) + .build(), + activityFinisher + ); title = context.getResources().getString(R.string.add_word_title); OKLabel = context.getResources().getString(R.string.add_word_add); diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/BaseMainLayout.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/BaseMainLayout.java index 513f8cb2..738c4f3a 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/BaseMainLayout.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/BaseMainLayout.java @@ -1,6 +1,5 @@ package io.github.sspanak.tt9.ui.main; -import android.content.res.Configuration; import android.os.Build; import android.view.ContextThemeWrapper; import android.view.View; @@ -14,6 +13,7 @@ import java.util.ArrayList; import io.github.sspanak.tt9.R; import io.github.sspanak.tt9.ime.TraditionalT9; import io.github.sspanak.tt9.ui.main.keys.SoftKey; +import io.github.sspanak.tt9.util.ThemedContextBuilder; abstract class BaseMainLayout { protected final TraditionalT9 tt9; @@ -37,27 +37,15 @@ abstract class BaseMainLayout { @NonNull protected ArrayList getKeys() { return keys; } - /** - * getThemedContext - * 1. Overrides the system dark/light them with the one in our settings. - * 2. Fixes this error log: "View class SoftKeyXXX is an AppCompat widget that can only be used - * with a Theme.AppCompat theme (or descendant)." - */ - private ContextThemeWrapper getThemedContext() { - int nightModeFlag = tt9.getSettings().getDarkTheme() ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO; - Configuration config = new Configuration(tt9.getResources().getConfiguration()); - config.uiMode = nightModeFlag | (config.uiMode & ~Configuration.UI_MODE_NIGHT_MASK); - - ContextThemeWrapper themedCtx = new ContextThemeWrapper(tt9, R.style.TTheme); - themedCtx.applyOverrideConfiguration(config); - - return themedCtx; - } - - protected View getView() { if (view == null) { - view = View.inflate(getThemedContext(), xml, null); + ContextThemeWrapper themedContext = new ThemedContextBuilder() + .setConfiguration(tt9.getResources().getConfiguration()) + .setContext(tt9) + .setSettings(tt9.getSettings()) + .setTheme(R.style.TTheme) + .build(); + view = View.inflate(themedContext, xml, null); } return view; diff --git a/app/src/main/java/io/github/sspanak/tt9/util/ThemedContextBuilder.java b/app/src/main/java/io/github/sspanak/tt9/util/ThemedContextBuilder.java new file mode 100644 index 00000000..f34d2295 --- /dev/null +++ b/app/src/main/java/io/github/sspanak/tt9/util/ThemedContextBuilder.java @@ -0,0 +1,53 @@ +package io.github.sspanak.tt9.util; + +import android.content.Context; +import android.content.res.Configuration; +import android.view.ContextThemeWrapper; + +import androidx.annotation.NonNull; + +import io.github.sspanak.tt9.preferences.settings.SettingsStore; + +public class ThemedContextBuilder { + private Configuration configuration; + private Context context; + private int nightModeFlag; + private int theme; + + + public ThemedContextBuilder setConfiguration(@NonNull Configuration configuration) { + this.configuration = new Configuration(configuration); + return this; + } + + + public ThemedContextBuilder setContext(@NonNull Context context) { + this.context = context; + return this; + } + + + public ThemedContextBuilder setSettings(@NonNull SettingsStore settings) { + nightModeFlag = settings.getDarkTheme() ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO; + return this; + } + + public ThemedContextBuilder setTheme(int theme) { + this.theme = theme; + return this; + } + + /** + * getThemedContext + * 1. Overrides the system dark/light them with the one in our settings. + * 2. Fixes this error log: "View class SoftKeyXXX is an AppCompat widget that can only be used + * with a Theme.AppCompat theme (or descendant)." + */ + public ContextThemeWrapper build() { + configuration.uiMode = nightModeFlag | (configuration.uiMode & ~Configuration.UI_MODE_NIGHT_MASK); + ContextThemeWrapper themedCtx = new ContextThemeWrapper(context, theme); + themedCtx.applyOverrideConfiguration(configuration); + + return themedCtx; + } +} diff --git a/app/src/main/res/values-night-v31/colors.xml b/app/src/main/res/values-night-v31/colors.xml new file mode 100644 index 00000000..d20407c0 --- /dev/null +++ b/app/src/main/res/values-night-v31/colors.xml @@ -0,0 +1,14 @@ + + + + + + @color/material_dynamic_neutral_variant30 + @color/material_dynamic_neutral90 + + + @color/material_dynamic_neutral10 + @color/material_dynamic_primary70 + diff --git a/app/src/main/res/values-night-v31/styles.xml b/app/src/main/res/values-night-v31/styles.xml index 4df64d9b..045e125f 100644 --- a/app/src/main/res/values-night-v31/styles.xml +++ b/app/src/main/res/values-night-v31/styles.xml @@ -1,43 +1,3 @@ - - - - - - - - - - - - diff --git a/app/src/main/res/values-night/colors.xml b/app/src/main/res/values-night/colors.xml index b6109a6a..72fb023e 100644 --- a/app/src/main/res/values-night/colors.xml +++ b/app/src/main/res/values-night/colors.xml @@ -1,5 +1,9 @@ + + #292e33 #d4d5d6 diff --git a/app/src/main/res/values-night/styles.xml b/app/src/main/res/values-night/styles.xml index 1b137a30..045e125f 100644 --- a/app/src/main/res/values-night/styles.xml +++ b/app/src/main/res/values-night/styles.xml @@ -1,14 +1,3 @@ - - - - - - - - - - - diff --git a/app/src/main/res/values-v31/colors.xml b/app/src/main/res/values-v31/colors.xml new file mode 100644 index 00000000..97eecd91 --- /dev/null +++ b/app/src/main/res/values-v31/colors.xml @@ -0,0 +1,14 @@ + + + + + + @color/material_dynamic_neutral_variant90 + @color/material_dynamic_neutral20 + + + @color/material_dynamic_neutral95 + @color/material_dynamic_primary40 + diff --git a/app/src/main/res/values-v31/styles.xml b/app/src/main/res/values-v31/styles.xml index 830a9bad..a9075922 100644 --- a/app/src/main/res/values-v31/styles.xml +++ b/app/src/main/res/values-v31/styles.xml @@ -1,17 +1,24 @@ - + + - - @@ -42,15 +49,4 @@ - - diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index eb242abc..db13ded8 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,6 +3,10 @@ What colors look good together: https://www.canva.com/colors/color-wheel/ --> + + #e8eaed #3d3d3f diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 45905daa..ff0adb02 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,15 +1,14 @@ +