make all colors setting independent for day and night mode

This commit is contained in:
Helium314 2024-06-30 18:21:23 +02:00
parent b26ac87ece
commit 7dac19cec7
4 changed files with 23 additions and 12 deletions

View file

@ -19,7 +19,6 @@ import helium314.keyboard.latin.common.DynamicColors
import helium314.keyboard.latin.common.readAllColorsMap import helium314.keyboard.latin.common.readAllColorsMap
import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.DeviceProtectedUtils import helium314.keyboard.latin.utils.DeviceProtectedUtils
import helium314.keyboard.latin.utils.ResourceUtils
class KeyboardTheme // Note: The themeId should be aligned with "themeId" attribute of Keyboard style in values/themes-<style>.xml. class KeyboardTheme // Note: The themeId should be aligned with "themeId" attribute of Keyboard style in values/themes-<style>.xml.
private constructor(val themeId: Int, @JvmField val mStyleId: Int) { private constructor(val themeId: Int, @JvmField val mStyleId: Int) {
@ -101,12 +100,12 @@ private constructor(val themeId: Int, @JvmField val mStyleId: Int) {
} }
@JvmStatic @JvmStatic
fun getThemeColors(themeColors: String, themeStyle: String, context: Context, prefs: SharedPreferences): Colors { fun getThemeColors(themeColors: String, themeStyle: String, context: Context, prefs: SharedPreferences, isNight: Boolean): Colors {
val hasBorders = prefs.getBoolean(Settings.PREF_THEME_KEY_BORDERS, false) val hasBorders = prefs.getBoolean(Settings.PREF_THEME_KEY_BORDERS, false)
val useNightMode = Settings.readDayNightPref(prefs, context.resources) && ResourceUtils.isNight(context.resources) val backgroundImage = Settings.readUserBackgroundImage(context, isNight)
val backgroundImage = Settings.readUserBackgroundImage(context, useNightMode)
return when (themeColors) { return when (themeColors) {
THEME_USER -> if (prefs.getInt(Settings.PREF_SHOW_MORE_COLORS, 0) == 2) AllColors(readAllColorsMap(prefs, false), themeStyle, hasBorders, backgroundImage) THEME_USER -> if (prefs.getInt(Settings.getColorPref(Settings.PREF_SHOW_MORE_COLORS, isNight), 0) == 2)
AllColors(readAllColorsMap(prefs, false), themeStyle, hasBorders, backgroundImage)
else DefaultColors( else DefaultColors(
themeStyle, themeStyle,
hasBorders, hasBorders,
@ -122,7 +121,8 @@ private constructor(val themeId: Int, @JvmField val mStyleId: Int) {
Settings.readUserColor(prefs, context, Settings.PREF_COLOR_GESTURE_SUFFIX, false), Settings.readUserColor(prefs, context, Settings.PREF_COLOR_GESTURE_SUFFIX, false),
keyboardBackground = backgroundImage keyboardBackground = backgroundImage
) )
THEME_USER_NIGHT -> if (prefs.getInt(Settings.PREF_SHOW_MORE_COLORS, 0) == 2) AllColors(readAllColorsMap(prefs, true), themeStyle, hasBorders, backgroundImage) THEME_USER_NIGHT -> if (prefs.getInt(Settings.getColorPref(Settings.PREF_SHOW_MORE_COLORS, isNight), 0) == 2)
AllColors(readAllColorsMap(prefs, true), themeStyle, hasBorders, backgroundImage)
else DefaultColors( else DefaultColors(
themeStyle, themeStyle,
hasBorders, hasBorders,
@ -140,7 +140,7 @@ private constructor(val themeId: Int, @JvmField val mStyleId: Int) {
) )
THEME_DYNAMIC -> { THEME_DYNAMIC -> {
if (Build.VERSION.SDK_INT >= VERSION_CODES.S) DynamicColors(context, themeStyle, hasBorders, backgroundImage) if (Build.VERSION.SDK_INT >= VERSION_CODES.S) DynamicColors(context, themeStyle, hasBorders, backgroundImage)
else getThemeColors(THEME_LIGHT, themeStyle, context, prefs) else getThemeColors(THEME_LIGHT, themeStyle, context, prefs, isNight)
} }
THEME_DARK -> DefaultColors( THEME_DARK -> DefaultColors(
themeStyle, themeStyle,

View file

@ -93,6 +93,17 @@ fun checkVersionUpgrade(context: Context) {
if (prefs.contains(Settings.PREF_LANGUAGE_SWITCH_KEY) && prefs.getString(Settings.PREF_LANGUAGE_SWITCH_KEY, "") != "off") if (prefs.contains(Settings.PREF_LANGUAGE_SWITCH_KEY) && prefs.getString(Settings.PREF_LANGUAGE_SWITCH_KEY, "") != "off")
prefs.edit { putBoolean(Settings.PREF_SHOW_LANGUAGE_SWITCH_KEY, true) } prefs.edit { putBoolean(Settings.PREF_SHOW_LANGUAGE_SWITCH_KEY, true) }
} }
if (oldVersion <= 2100) {
if (prefs.contains(Settings.PREF_SHOW_MORE_COLORS)) {
val moreColors = prefs.getInt(Settings.PREF_SHOW_MORE_COLORS, 0)
prefs.edit {
putInt(Settings.getColorPref(Settings.PREF_SHOW_MORE_COLORS, false), moreColors)
if (prefs.getBoolean(Settings.PREF_THEME_DAY_NIGHT, false))
putInt(Settings.getColorPref(Settings.PREF_SHOW_MORE_COLORS, true), moreColors)
remove(Settings.PREF_SHOW_MORE_COLORS)
}
}
}
upgradeToolbarPrefs(prefs) upgradeToolbarPrefs(prefs)
onCustomLayoutFileListChanged() // just to be sure onCustomLayoutFileListChanged() // just to be sure
prefs.edit { putInt(Settings.PREF_VERSION_CODE, BuildConfig.VERSION_CODE) } prefs.edit { putInt(Settings.PREF_VERSION_CODE, BuildConfig.VERSION_CODE) }

View file

@ -42,8 +42,8 @@ open class ColorsSettingsFragment : Fragment(R.layout.color_settings), MenuProvi
// 1 for more colors // 1 for more colors
// 2 for all colors // 2 for all colors
private var moreColors: Int private var moreColors: Int
get() = prefs.getInt(Settings.PREF_SHOW_MORE_COLORS, 0) get() = prefs.getInt(Settings.getColorPref(Settings.PREF_SHOW_MORE_COLORS, isNight), 0)
set(value) { prefs.edit().putInt(Settings.PREF_SHOW_MORE_COLORS, value).apply() } set(value) { prefs.edit().putInt(Settings.getColorPref(Settings.PREF_SHOW_MORE_COLORS, isNight), value).apply() }
private val prefs by lazy { DeviceProtectedUtils.getSharedPreferences(requireContext()) } private val prefs by lazy { DeviceProtectedUtils.getSharedPreferences(requireContext()) }

View file

@ -187,7 +187,6 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
add(PREF_LAST_SHOWN_EMOJI_CATEGORY_ID); add(PREF_LAST_SHOWN_EMOJI_CATEGORY_ID);
add(PREF_EMOJI_RECENT_KEYS); add(PREF_EMOJI_RECENT_KEYS);
add(PREF_DONT_SHOW_MISSING_DICTIONARY_DIALOG); add(PREF_DONT_SHOW_MISSING_DICTIONARY_DIALOG);
add(PREF_SHOW_MORE_COLORS);
add(PREF_SELECTED_SUBTYPE); add(PREF_SELECTED_SUBTYPE);
}}; }};
@ -566,12 +565,13 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static Colors getColorsForCurrentTheme(final Context context, final SharedPreferences prefs) { public static Colors getColorsForCurrentTheme(final Context context, final SharedPreferences prefs) {
boolean isNight = ResourceUtils.isNight(context.getResources()); boolean isNight = ResourceUtils.isNight(context.getResources());
if (ColorsSettingsFragment.Companion.getForceOppositeTheme()) isNight = !isNight; if (ColorsSettingsFragment.Companion.getForceOppositeTheme()) isNight = !isNight;
final String themeColors = (isNight && readDayNightPref(prefs, context.getResources())) else isNight = isNight && readDayNightPref(prefs, context.getResources());
final String themeColors = (isNight)
? prefs.getString(Settings.PREF_THEME_COLORS_NIGHT, KeyboardTheme.THEME_DARK) ? prefs.getString(Settings.PREF_THEME_COLORS_NIGHT, KeyboardTheme.THEME_DARK)
: prefs.getString(Settings.PREF_THEME_COLORS, KeyboardTheme.THEME_LIGHT); : prefs.getString(Settings.PREF_THEME_COLORS, KeyboardTheme.THEME_LIGHT);
final String themeStyle = prefs.getString(Settings.PREF_THEME_STYLE, KeyboardTheme.STYLE_MATERIAL); final String themeStyle = prefs.getString(Settings.PREF_THEME_STYLE, KeyboardTheme.STYLE_MATERIAL);
return KeyboardTheme.getThemeColors(themeColors, themeStyle, context, prefs); return KeyboardTheme.getThemeColors(themeColors, themeStyle, context, prefs, isNight);
} }
public static int readUserColor(final SharedPreferences prefs, final Context context, final String colorName, final boolean isNight) { public static int readUserColor(final SharedPreferences prefs, final Context context, final String colorName, final boolean isNight) {