make sure the right theme is shown in colors screen

proobably there is a way to do it more straightforward...
This commit is contained in:
Helium314 2025-02-11 21:42:31 +01:00
parent f8bdd7dd1c
commit 9bb087b422
3 changed files with 47 additions and 3 deletions

View file

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

View file

@ -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?) {

View file

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