1
0
Fork 0

themes code cleanup

This commit is contained in:
sspanak 2025-01-29 18:01:53 +02:00 committed by Dimo Karaivanov
parent 38121ba796
commit a8146dab60
12 changed files with 137 additions and 100 deletions

View file

@ -48,7 +48,8 @@
<activity
android:label="@string/pref_help"
android:name="io.github.sspanak.tt9.preferences.HelpActivity"
android:exported="true">
android:exported="true"
android:theme="@style/TTheme.Preferences">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -58,7 +59,8 @@
<activity
android:label="Log Messages"
android:name="io.github.sspanak.tt9.preferences.LogsActivity"
android:exported="true">
android:exported="true"
android:theme="@style/TTheme.Preferences">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -68,6 +70,7 @@
<activity
android:excludeFromRecents="true"
android:label=""
android:name="io.github.sspanak.tt9.ui.dialogs.RequestPermissionDialog" />
android:name="io.github.sspanak.tt9.ui.dialogs.RequestPermissionDialog"
android:theme="@style/TTheme.Preferences" />
</application>
</manifest>

View file

@ -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<String> 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);

View file

@ -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<SoftKey> 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;

View file

@ -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;
}
}

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--*******************************************
Preferences
*******************************************-->
<!-- title bar -->
<color name="prefs_title_background">@color/material_dynamic_neutral_variant30</color>
<color name="prefs_title_text_color">@color/material_dynamic_neutral90</color>
<!-- page -->
<color name="prefs_page_background">@color/material_dynamic_neutral10</color>
<color name="prefs_category_title_color">@color/material_dynamic_primary70</color>
</resources>

View file

@ -1,43 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TTheme" parent="Theme.Material3.DynamicColors.Dark" />
<style name="TTheme.Preferences" parent="Theme.Material3.DynamicColors.Dark">
<item name="preferenceTheme">@style/TTheme.PreferenceOverlay</item>
<!-- title bar -->
<item name="tint">@color/material_dynamic_neutral90</item> <!-- back button color -->
<item name="colorOnSurface">@color/material_dynamic_neutral90</item> <!-- title text color -->
<item name="colorSurfaceContainer">@color/material_dynamic_neutral_variant30</item> <!-- title background -->
<!-- page -->
<item name="android:windowBackground">@color/material_dynamic_neutral10</item> <!-- page background -->
<item name="colorSecondary">@color/material_dynamic_primary70</item> <!-- category title -->
<item name="android:textAppearanceListItem">@style/TextAppearance.Material3.TitleLarge</item> <!-- preference title -->
<!-- <item name="android:popupMenuStyle">@style/AppDropDownStyle</item> &lt;!&ndash; dropdown background &ndash;&gt;-->
</style>
<style name="TTheme.PreferenceOverlay" parent="@style/PreferenceThemeOverlay">
<item name="switchPreferenceCompatStyle">@style/TTheme.Switch</item>
</style>
<!-- <style name="AppDropDownStyle" parent="Widget.AppCompat.ListPopupWindow">-->
<!-- <item name="android:background">@color/material_dynamic_neutral_variant30</item>-->
<!-- <item name="popupMenuBackground">@color/material_dynamic_neutral_variant30</item>-->
<!-- </style>-->
<style name="TTheme.Switch" parent="@style/Preference.SwitchPreferenceCompat.Material">
<item name="widgetLayout">@layout/pref_switch_v31</item>
</style>
<style name="TTheme.AddWord" parent="Theme.Material3.Dark.Dialog.Alert">
<item name="windowNoTitle">true</item> <!-- hide some weird floating rectangle above the dialog -->
<item name="android:textColor">@color/material_dynamic_neutral99</item> <!-- headline (title) text color -->
<item name="android:textColorPrimary">@color/material_dynamic_neutral_variant95</item> <!-- supporting text (body text) color -->
<item name="android:background">@color/material_dynamic_neutral20</item> <!-- container background -->
<item name="colorPrimary">@color/material_dynamic_primary90</item> <!-- label text (button text) color -->
<item name="textAppearanceBodyMedium">@style/TextAppearance.AppCompat.Widget.PopupMenu.Large</item> <!-- body text size -->
<item name="textAppearanceLabelLarge">@style/TextAppearance.MaterialComponents.Button</item> <!-- button text size -->
</style>
</resources>

View file

@ -1,5 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--*******************************************
Keyboard
*******************************************-->
<color name="keyboard_background">#292e33</color>
<color name="keyboard_text_color">#d4d5d6</color>

View file

@ -1,14 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- <style name="TTheme" parent="Theme.AppCompat" />-->
<!-- <style name="TTheme.Colored.Key" parent="TTheme.Key">-->
<!-- <item name="android:textColor">@color/button_text</item>-->
<!-- <item name="android:color">@color/dark_button_color</item>-->
<!-- <item name="android:textColor">@color/dark_button_color</item>-->
<!-- <item name="android:textColorPrimary">@color/dark_button_color</item>-->
<!-- <item name="android:tint">@color/dark_button_color</item>-->
<!-- <item name="tint">@color/dark_button_color</item>-->
<!-- </style>-->
</resources>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--*******************************************
Preferences
*******************************************-->
<!-- title bar -->
<color name="prefs_title_background">@color/material_dynamic_neutral_variant90</color>
<color name="prefs_title_text_color">@color/material_dynamic_neutral20</color>
<!-- page -->
<color name="prefs_page_background">@color/material_dynamic_neutral95</color>
<color name="prefs_category_title_color">@color/material_dynamic_primary40</color>
</resources>

View file

@ -1,17 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TTheme" parent="Theme.Material3.DynamicColors.Light" />
<style name="TTheme" parent="Theme.Material3.DynamicColors.DayNight" />
<style name="TTheme.Preferences" parent="Theme.Material3.DynamicColors.Light">
<item name="preferenceTheme">@style/TTheme.PreferenceOverlay</item>
<style name="TTheme.AddWord" parent="Theme.Material3.DayNight.Dialog.Alert">
<item name="windowNoTitle">true</item> <!-- hide some weird floating rectangle above the dialog -->
<item name="textAppearanceBodyMedium">@style/TextAppearance.AppCompat.Widget.PopupMenu.Large</item> <!-- body text size -->
<item name="textAppearanceLabelLarge">@style/TextAppearance.MaterialComponents.Button</item> <!-- button text size -->
</style>
<style name="TTheme.Preferences" parent="TTheme">
<item name="preferenceTheme">@style/TTheme.Preferences.Overlay</item>
<!-- title bar -->
<item name="tint">@color/material_dynamic_neutral20</item> <!-- back button color -->
<item name="colorSurfaceContainer">@color/material_dynamic_neutral_variant90</item> <!-- title bar background -->
<item name="tint">@color/prefs_title_text_color</item>
<item name="colorOnSurface">@color/prefs_title_text_color</item>
<item name="colorSurfaceContainer">@color/prefs_title_background</item>
<!-- page -->
<item name="android:windowBackground">@color/material_dynamic_neutral95</item> <!-- page background -->
<item name="colorSecondary">@color/material_dynamic_primary40</item> <!-- category title -->
<item name="android:windowBackground">@color/prefs_page_background</item> <!-- page background -->
<item name="colorSecondary">@color/prefs_category_title_color</item> <!-- category title -->
<item name="android:textAppearanceListItem">@style/TextAppearance.Material3.TitleLarge</item> <!-- preference title -->
<!-- <item name="android:popupWindowStyle">@style/AppDropDownStyle</item> &lt;!&ndash; dropdown background &ndash;&gt;-->
@ -30,11 +37,11 @@
-->
</style>
<style name="TTheme.PreferenceOverlay" parent="@style/PreferenceThemeOverlay">
<item name="switchPreferenceCompatStyle">@style/TTheme.Switch</item>
<style name="TTheme.Preferences.Overlay" parent="@style/PreferenceThemeOverlay">
<item name="switchPreferenceCompatStyle">@style/TTheme.Preferences.Switch</item>
</style>
<style name="TTheme.Switch" parent="@style/Preference.SwitchPreferenceCompat.Material">
<style name="TTheme.Preferences.Switch" parent="@style/Preference.SwitchPreferenceCompat.Material">
<item name="widgetLayout">@layout/pref_switch_v31</item>
</style>
@ -42,15 +49,4 @@
<!-- <item name="android:background">@color/material_dynamic_neutral_variant90</item>-->
<!-- <item name="popupMenuBackground">@color/material_dynamic_neutral_variant90</item>-->
<!-- </style>-->
<style name="TTheme.AddWord" parent="Theme.Material3.Light.Dialog.Alert">
<item name="windowNoTitle">true</item> <!-- hide some weird floating rectangle above the dialog -->
<item name="android:textColor">@color/material_dynamic_neutral10</item> <!-- headline (title) text color -->
<item name="android:textColorPrimary">@color/material_dynamic_neutral_variant10</item> <!-- supporting text (body text) color -->
<item name="android:background">@color/material_dynamic_neutral95</item> <!-- container background -->
<item name="colorPrimary">@color/material_dynamic_primary20</item> <!-- label text (button text) color -->
<item name="textAppearanceBodyMedium">@style/TextAppearance.AppCompat.Widget.PopupMenu.Large</item> <!-- body text size -->
<item name="textAppearanceLabelLarge">@style/TextAppearance.MaterialComponents.Button</item> <!-- button text size -->
</style>
</resources>

View file

@ -3,6 +3,10 @@
What colors look good together: https://www.canva.com/colors/color-wheel/
-->
<resources>
<!--*******************************************
Keyboard
*******************************************-->
<color name="keyboard_background">#e8eaed</color>
<color name="keyboard_text_color">#3d3d3f</color>

View file

@ -1,15 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TTheme" parent="Theme.MaterialComponents.DayNight" />
<style name="TTheme.Preferences" parent="Theme.AppCompat.DayNight" />
<style name="TTheme.AddWord" parent="Theme.AppCompat.Dialog.Alert">
<style name="TTheme.AddWord" parent="Theme.AppCompat.DayNight.Dialog.Alert">
<item name="windowNoTitle">true</item> <!-- hide some weird floating rectangle above the dialog -->
</style>
<style name="TTheme.Preferences" parent="Theme.AppCompat.DayNight" />
<style name="TTheme.Keyboard">
<style name="TTheme.Keyboard" parent="TTheme">
<item name="android:background">@color/keyboard_background</item>
</style>