editing a theme does not automatically enabled it (outside edit screen)

This commit is contained in:
Helium314 2025-02-23 16:24:59 +01:00
parent b41ad29e55
commit e3fa210031
7 changed files with 44 additions and 42 deletions

View file

@ -21,10 +21,12 @@ import helium314.keyboard.latin.common.DefaultColors
import helium314.keyboard.latin.common.DynamicColors import helium314.keyboard.latin.common.DynamicColors
import helium314.keyboard.latin.settings.Defaults import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.ResourceUtils
import helium314.keyboard.latin.utils.brightenOrDarken import helium314.keyboard.latin.utils.brightenOrDarken
import helium314.keyboard.latin.utils.isBrightColor import helium314.keyboard.latin.utils.isBrightColor
import helium314.keyboard.latin.utils.isGoodContrast import helium314.keyboard.latin.utils.isGoodContrast
import helium314.keyboard.latin.utils.prefs import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.keyboardNeedsReload import helium314.keyboard.settings.keyboardNeedsReload
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ -128,7 +130,20 @@ private constructor(val themeId: Int, @JvmField val mStyleId: Int) {
} }
@JvmStatic @JvmStatic
fun getThemeColors(themeName: String, themeStyle: String, context: Context, prefs: SharedPreferences, isNight: Boolean): Colors { fun getColorsForCurrentTheme(context: Context): Colors {
val prefs = context.prefs()
val isNight = SettingsActivity.forceNight
?: (ResourceUtils.isNight(context.resources) && prefs.getBoolean(Settings.PREF_THEME_DAY_NIGHT, Defaults.PREF_THEME_DAY_NIGHT))
val themeName = SettingsActivity.forceTheme ?: if (isNight)
prefs.getString(Settings.PREF_THEME_COLORS_NIGHT, Defaults.PREF_THEME_COLORS_NIGHT)
else
prefs.getString(Settings.PREF_THEME_COLORS, Defaults.PREF_THEME_COLORS)
val themeStyle = prefs.getString(Settings.PREF_THEME_STYLE, Defaults.PREF_THEME_STYLE)
return getThemeColors(themeName!!, themeStyle!!, context, prefs, isNight)
}
private fun getThemeColors(themeName: String, themeStyle: String, context: Context, prefs: SharedPreferences, isNight: Boolean): Colors {
val hasBorders = prefs.getBoolean(Settings.PREF_THEME_KEY_BORDERS, Defaults.PREF_THEME_KEY_BORDERS) val hasBorders = prefs.getBoolean(Settings.PREF_THEME_KEY_BORDERS, Defaults.PREF_THEME_KEY_BORDERS)
val backgroundImage = Settings.readUserBackgroundImage(context, isNight) val backgroundImage = Settings.readUserBackgroundImage(context, isNight)
return when (themeName) { return when (themeName) {

View file

@ -22,11 +22,9 @@ import androidx.annotation.Nullable;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import helium314.keyboard.keyboard.KeyboardActionListener; import helium314.keyboard.keyboard.KeyboardActionListener;
import helium314.keyboard.keyboard.KeyboardTheme;
import helium314.keyboard.latin.AudioAndHapticFeedbackManager; import helium314.keyboard.latin.AudioAndHapticFeedbackManager;
import helium314.keyboard.latin.InputAttributes; import helium314.keyboard.latin.InputAttributes;
import helium314.keyboard.latin.R; import helium314.keyboard.latin.R;
import helium314.keyboard.latin.common.Colors;
import helium314.keyboard.latin.utils.DeviceProtectedUtils; import helium314.keyboard.latin.utils.DeviceProtectedUtils;
import helium314.keyboard.latin.utils.KtxKt; import helium314.keyboard.latin.utils.KtxKt;
import helium314.keyboard.latin.utils.LayoutType; import helium314.keyboard.latin.utils.LayoutType;
@ -35,10 +33,8 @@ import helium314.keyboard.latin.utils.ResourceUtils;
import helium314.keyboard.latin.utils.RunInLocaleKt; import helium314.keyboard.latin.utils.RunInLocaleKt;
import helium314.keyboard.latin.utils.StatsUtils; import helium314.keyboard.latin.utils.StatsUtils;
import helium314.keyboard.latin.utils.SubtypeSettings; import helium314.keyboard.latin.utils.SubtypeSettings;
import helium314.keyboard.latin.utils.SubtypeUtilsAdditional;
import helium314.keyboard.latin.utils.ToolbarKey; import helium314.keyboard.latin.utils.ToolbarKey;
import helium314.keyboard.latin.utils.ToolbarUtilsKt; import helium314.keyboard.latin.utils.ToolbarUtilsKt;
import helium314.keyboard.settings.SettingsActivity;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
@ -460,18 +456,6 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
Arrays.fill(sCachedBackgroundImages, null); Arrays.fill(sCachedBackgroundImages, null);
} }
public static Colors getColorsForCurrentTheme(final Context context, final SharedPreferences prefs) {
boolean isNight = ResourceUtils.isNight(context.getResources());
if (SettingsActivity.Companion.getForceOppositeTheme()) isNight = !isNight;
else isNight = isNight && prefs.getBoolean(PREF_THEME_DAY_NIGHT, Defaults.PREF_THEME_DAY_NIGHT);
final String themeName = (isNight)
? prefs.getString(Settings.PREF_THEME_COLORS_NIGHT, Defaults.PREF_THEME_COLORS_NIGHT)
: prefs.getString(Settings.PREF_THEME_COLORS, Defaults.PREF_THEME_COLORS);
final String themeStyle = prefs.getString(Settings.PREF_THEME_STYLE, Defaults.PREF_THEME_STYLE);
return KeyboardTheme.getThemeColors(themeName, themeStyle, context, prefs, isNight);
}
public static Context getDayNightContext(final Context context, final boolean wantNight) { public static Context getDayNightContext(final Context context, final boolean wantNight) {
final boolean isNight = ResourceUtils.isNight(context.getResources()); final boolean isNight = ResourceUtils.isNight(context.getResources());
if (isNight == wantNight) if (isNight == wantNight)

View file

@ -19,6 +19,7 @@ import androidx.annotation.Nullable;
import androidx.core.util.TypedValueCompat; import androidx.core.util.TypedValueCompat;
import helium314.keyboard.compat.ConfigurationCompatKt; import helium314.keyboard.compat.ConfigurationCompatKt;
import helium314.keyboard.keyboard.KeyboardTheme;
import helium314.keyboard.keyboard.internal.keyboard_parser.LocaleKeyboardInfosKt; import helium314.keyboard.keyboard.internal.keyboard_parser.LocaleKeyboardInfosKt;
import helium314.keyboard.latin.InputAttributes; import helium314.keyboard.latin.InputAttributes;
import helium314.keyboard.latin.R; import helium314.keyboard.latin.R;
@ -252,7 +253,7 @@ public class SettingsValues {
mShowMorePopupKeys = selectedSubtype.isAsciiCapable() mShowMorePopupKeys = selectedSubtype.isAsciiCapable()
? SubtypeUtilsKt.getMoreKeys(selectedSubtype, prefs) ? SubtypeUtilsKt.getMoreKeys(selectedSubtype, prefs)
: LocaleKeyboardInfosKt.POPUP_KEYS_NORMAL; : LocaleKeyboardInfosKt.POPUP_KEYS_NORMAL;
mColors = Settings.getColorsForCurrentTheme(context, prefs); mColors = KeyboardTheme.getColorsForCurrentTheme(context);
mPopupKeyTypes = SubtypeUtilsKt.getPopupKeyTypes(selectedSubtype, prefs); mPopupKeyTypes = SubtypeUtilsKt.getPopupKeyTypes(selectedSubtype, prefs);
mPopupKeyLabelSources = SubtypeUtilsKt.getPopupKeyLabelSources(selectedSubtype, prefs); mPopupKeyLabelSources = SubtypeUtilsKt.getPopupKeyLabelSources(selectedSubtype, prefs);

View file

@ -150,8 +150,9 @@ class SettingsActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferen
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
paused = true paused = true
if (forceOppositeTheme) keyboardNeedsReload = true if (forceNight != null || forceTheme != null) keyboardNeedsReload = true
forceOppositeTheme = false forceNight = false
forceTheme = null
} }
override fun onResume() { override fun onResume() {
@ -160,12 +161,13 @@ class SettingsActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferen
} }
private var paused = true private var paused = true
fun setForceOppositeTheme(opposite: Boolean) { fun setForceTheme(theme: String?, night: Boolean?) {
if (paused) return if (paused) return
if (forceOppositeTheme != opposite) { if (forceTheme != theme || forceNight != night) {
keyboardNeedsReload = true keyboardNeedsReload = true
} }
forceOppositeTheme = opposite forceTheme = theme
forceNight = night
} }
private fun findCrashReports(): List<File> { private fun findCrashReports(): List<File> {
@ -205,7 +207,8 @@ class SettingsActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferen
// but it's small enough to not care // but it's small enough to not care
lateinit var settingsContainer: SettingsContainer lateinit var settingsContainer: SettingsContainer
var forceOppositeTheme = false var forceNight: Boolean? = null
var forceTheme: String? = null
} }
override fun onSharedPreferenceChanged(prefereces: SharedPreferences?, key: String?) { override fun onSharedPreferenceChanged(prefereces: SharedPreferences?, key: String?) {

View file

@ -113,11 +113,11 @@ fun SettingsNavHost(
composable(SettingsDestination.Layouts) { composable(SettingsDestination.Layouts) {
SecondaryLayoutScreen(onClickBack = ::goBack) SecondaryLayoutScreen(onClickBack = ::goBack)
} }
composable(SettingsDestination.Colors) { composable(SettingsDestination.Colors + "{theme}") {
ColorsScreen(isNight = false, onClickBack = ::goBack) ColorsScreen(isNight = false, theme = it.arguments?.getString("theme"), onClickBack = ::goBack)
} }
composable(SettingsDestination.ColorsNight) { composable(SettingsDestination.ColorsNight + "{theme}") {
ColorsScreen(isNight = true, onClickBack = ::goBack) ColorsScreen(isNight = true, theme = it.arguments?.getString("theme"), onClickBack = ::goBack)
} }
} }
if (target.value != SettingsDestination.Settings/* && target.value != navController.currentBackStackEntry?.destination?.route*/) if (target.value != SettingsDestination.Settings/* && target.value != navController.currentBackStackEntry?.destination?.route*/)
@ -134,8 +134,8 @@ object SettingsDestination {
const val Advanced = "advanced" const val Advanced = "advanced"
const val Debug = "debug" const val Debug = "debug"
const val Appearance = "appearance" const val Appearance = "appearance"
const val Colors = "colors" const val Colors = "colors/"
const val ColorsNight = "colors_night" const val ColorsNight = "colors_night/"
const val PersonalDictionaries = "personal_dictionaries" const val PersonalDictionaries = "personal_dictionaries"
const val PersonalDictionary = "personal_dictionary/" const val PersonalDictionary = "personal_dictionary/"
const val Languages = "languages" const val Languages = "languages"

View file

@ -213,10 +213,7 @@ private fun ColorItemRow(onDismissRequest: () -> Unit, item: String, isSelected:
IconButton( IconButton(
onClick = { onClick = {
onDismissRequest() onDismissRequest()
// todo: maybe no need to set it as default when using the navigation specials SettingsDestination.navigateTo(targetScreen + item)
prefs.edit().putString(prefKey, item).apply()
SettingsDestination.navigateTo(targetScreen)
keyboardNeedsReload = true
} }
) { Icon(painterResource(R.drawable.ic_edit), null) } ) { Icon(painterResource(R.drawable.ic_edit), null) }
if (showDialog) if (showDialog)

View file

@ -70,6 +70,7 @@ import kotlinx.serialization.json.Json
@Composable @Composable
fun ColorsScreen( fun ColorsScreen(
isNight: Boolean, isNight: Boolean,
theme: String?,
onClickBack: () -> Unit onClickBack: () -> Unit
) { ) {
val ctx = LocalContext.current val ctx = LocalContext.current
@ -78,15 +79,15 @@ fun ColorsScreen(
// lifecycle stuff is weird, there is no pause and similar when activity is paused // lifecycle stuff is weird, there is no pause and similar when activity is paused
DisposableEffect(isNight) { DisposableEffect(isNight) {
onDispose { // works on pressing back onDispose { // works on pressing back
(ctx.getActivity() as? SettingsActivity)?.setForceOppositeTheme(false) (ctx.getActivity() as? SettingsActivity)?.setForceTheme(null, null)
} }
} }
(ctx.getActivity() as? SettingsActivity)?.setForceOppositeTheme(isNight != ResourceUtils.isNight(ctx.resources))
val lifecycleOwner = LocalLifecycleOwner.current val lifecycleOwner = LocalLifecycleOwner.current
val lifecycleState by lifecycleOwner.lifecycle.currentStateFlow.collectAsState() val lifecycleState by lifecycleOwner.lifecycle.currentStateFlow.collectAsState()
LaunchedEffect(lifecycleState) { LaunchedEffect(lifecycleState) {
if (lifecycleState == Lifecycle.State.RESUMED) if (lifecycleState == Lifecycle.State.RESUMED) {
(ctx.getActivity() as? SettingsActivity)?.setForceOppositeTheme(isNight != ResourceUtils.isNight(ctx.resources)) (ctx.getActivity() as? SettingsActivity)?.setForceTheme(theme, isNight)
}
} }
val prefs = ctx.prefs() val prefs = ctx.prefs()
@ -94,7 +95,7 @@ fun ColorsScreen(
if ((b?.value ?: 0) < 0) if ((b?.value ?: 0) < 0)
Log.v("irrelevant", "stupid way to trigger recomposition on preference change") Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
val themeName = if (isNight) prefs.getString(Settings.PREF_THEME_COLORS_NIGHT, Defaults.PREF_THEME_COLORS_NIGHT)!! val themeName = theme ?: if (isNight) prefs.getString(Settings.PREF_THEME_COLORS_NIGHT, Defaults.PREF_THEME_COLORS_NIGHT)!!
else prefs.getString(Settings.PREF_THEME_COLORS, Defaults.PREF_THEME_COLORS)!! else prefs.getString(Settings.PREF_THEME_COLORS, Defaults.PREF_THEME_COLORS)!!
val moreColors = KeyboardTheme.readUserMoreColors(prefs, themeName) val moreColors = KeyboardTheme.readUserMoreColors(prefs, themeName)
val userColors = KeyboardTheme.readUserColors(prefs, themeName) val userColors = KeyboardTheme.readUserColors(prefs, themeName)
@ -136,8 +137,9 @@ fun ColorsScreen(
nameField = it nameField = it
}, },
isError = !nameValid, isError = !nameValid,
// supportingText = { if (!nameValid) Text(stringResource(R.string.name_invalid) } // todo: this is cutting off bottom half of the actual text... // supportingText = { if (!nameValid) Text(stringResource(R.string.name_invalid)) } // todo: this is cutting off bottom half of the actual text...
trailingIcon = { if (!nameValid) Icon(painterResource(R.drawable.ic_close), null) } trailingIcon = { if (!nameValid) Icon(painterResource(R.drawable.ic_close), null) },
singleLine = true,
) )
}, },
menu = listOf( menu = listOf(
@ -233,7 +235,7 @@ data class SaveThoseColors(val name: String? = null, val moreColors: Int, val co
private fun Preview() { private fun Preview() {
Theme(true) { Theme(true) {
Surface { Surface {
ColorsScreen(false) { } ColorsScreen(false, null) { }
} }
} }
} }