From 9bb087b422249868670b5ef9af36f3507fc7c6af Mon Sep 17 00:00:00 2001 From: Helium314 Date: Tue, 11 Feb 2025 21:42:31 +0100 Subject: [PATCH] make sure the right theme is shown in colors screen proobably there is a way to do it more straightforward... --- .../keyboard/latin/settings/Settings.java | 3 ++- .../keyboard/settings/SettingsActivity.kt | 23 ++++++++++++++++++ .../keyboard/settings/screens/ColorsScreen.kt | 24 +++++++++++++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/settings/Settings.java b/app/src/main/java/helium314/keyboard/latin/settings/Settings.java index 15b5f3987..0590a8187 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/Settings.java +++ b/app/src/main/java/helium314/keyboard/latin/settings/Settings.java @@ -39,6 +39,7 @@ import helium314.keyboard.latin.utils.StatsUtils; import helium314.keyboard.latin.utils.SubtypeSettingsKt; import helium314.keyboard.latin.utils.ToolbarKey; import helium314.keyboard.latin.utils.ToolbarUtilsKt; +import helium314.keyboard.settings.SettingsActivity; import java.io.File; import java.util.ArrayList; @@ -502,7 +503,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang public static Colors getColorsForCurrentTheme(final Context context, final SharedPreferences prefs) { boolean isNight = ResourceUtils.isNight(context.getResources()); - if (ColorsSettingsFragment.Companion.getForceOppositeTheme()) isNight = !isNight; + 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) diff --git a/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt b/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt index 677eca4a8..5d857478d 100644 --- a/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt +++ b/app/src/main/java/helium314/keyboard/settings/SettingsActivity.kt @@ -77,11 +77,34 @@ class SettingsActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferen super.onStop() } + override fun onPause() { + super.onPause() + paused = true + if (forceOppositeTheme) keyboardNeedsReload = true + forceOppositeTheme = false + } + + override fun onResume() { + super.onResume() + paused = false + } + + private var paused = true + fun setForceOppositeTheme(opposite: Boolean) { + if (paused) return + if (forceOppositeTheme != opposite) { + keyboardNeedsReload = true + } + forceOppositeTheme = opposite + } + companion object { // public write so compose previews can show the screens // having it in a companion object is not ideal as it will stay in memory even after settings are closed // but it's small enough to not care lateinit var settingsContainer: SettingsContainer + + var forceOppositeTheme = false } override fun onSharedPreferenceChanged(prefereces: SharedPreferences?, key: String?) { diff --git a/app/src/main/java/helium314/keyboard/settings/screens/ColorsScreen.kt b/app/src/main/java/helium314/keyboard/settings/screens/ColorsScreen.kt index 1f143eb17..617683169 100644 --- a/app/src/main/java/helium314/keyboard/settings/screens/ColorsScreen.kt +++ b/app/src/main/java/helium314/keyboard/settings/screens/ColorsScreen.kt @@ -20,6 +20,8 @@ import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -35,6 +37,8 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.compose.LocalLifecycleOwner import helium314.keyboard.keyboard.ColorSetting import helium314.keyboard.keyboard.KeyboardTheme import helium314.keyboard.latin.R @@ -45,6 +49,7 @@ import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.settings.colorPrefsAndResIds import helium314.keyboard.latin.settings.getColorPrefsToHideInitially import helium314.keyboard.latin.utils.Log +import helium314.keyboard.latin.utils.ResourceUtils import helium314.keyboard.latin.utils.getActivity import helium314.keyboard.latin.utils.prefs import helium314.keyboard.settings.SearchScreen @@ -58,13 +63,28 @@ fun ColorsScreen( onClickBack: () -> Unit ) { // todo: - // need to force opposite theme if necessary! // allow save (load should be in theme selector, maybe here too) // import/export should now also store theme name // handle name collisions on load by simply appending a number // make sure import of old colors works val ctx = LocalContext.current + + // is there really no better way of only setting forceOpposite while the screen is shown (and not paused)? + // lifecycle stuff is weird, there is no pause and similar when activity is paused + DisposableEffect(isNight) { + onDispose { // works on pressing back + (ctx.getActivity() as? SettingsActivity)?.setForceOppositeTheme(false) + } + } + (ctx.getActivity() as? SettingsActivity)?.setForceOppositeTheme(isNight != ResourceUtils.isNight(ctx.resources)) + val lifecycleOwner = LocalLifecycleOwner.current + val lifecycleState by lifecycleOwner.lifecycle.currentStateFlow.collectAsState() + LaunchedEffect(lifecycleState) { + if (lifecycleState == Lifecycle.State.RESUMED) + (ctx.getActivity() as? SettingsActivity)?.setForceOppositeTheme(isNight != ResourceUtils.isNight(ctx.resources)) + } + val prefs = ctx.prefs() val b = (ctx.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState() if ((b?.value ?: 0) < 0) @@ -107,7 +127,7 @@ fun ColorsScreen( newThemeName = it nameField = it }, - // modifier = Modifier.weight(1f) + modifier = Modifier.weight(1f) ) CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.secondary) { // todo: this should indicate whether name is saved, but looks like a button