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.settings.Defaults
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.ResourceUtils
import helium314.keyboard.latin.utils.brightenOrDarken
import helium314.keyboard.latin.utils.isBrightColor
import helium314.keyboard.latin.utils.isGoodContrast
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.keyboardNeedsReload
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
@ -128,7 +130,20 @@ private constructor(val themeId: Int, @JvmField val mStyleId: Int) {
}
@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 backgroundImage = Settings.readUserBackgroundImage(context, isNight)
return when (themeName) {

View file

@ -22,11 +22,9 @@ import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import helium314.keyboard.keyboard.KeyboardActionListener;
import helium314.keyboard.keyboard.KeyboardTheme;
import helium314.keyboard.latin.AudioAndHapticFeedbackManager;
import helium314.keyboard.latin.InputAttributes;
import helium314.keyboard.latin.R;
import helium314.keyboard.latin.common.Colors;
import helium314.keyboard.latin.utils.DeviceProtectedUtils;
import helium314.keyboard.latin.utils.KtxKt;
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.StatsUtils;
import helium314.keyboard.latin.utils.SubtypeSettings;
import helium314.keyboard.latin.utils.SubtypeUtilsAdditional;
import helium314.keyboard.latin.utils.ToolbarKey;
import helium314.keyboard.latin.utils.ToolbarUtilsKt;
import helium314.keyboard.settings.SettingsActivity;
import java.io.File;
import java.util.Arrays;
@ -460,18 +456,6 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
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) {
final boolean isNight = ResourceUtils.isNight(context.getResources());
if (isNight == wantNight)

View file

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

View file

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

View file

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

View file

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

View file

@ -70,6 +70,7 @@ import kotlinx.serialization.json.Json
@Composable
fun ColorsScreen(
isNight: Boolean,
theme: String?,
onClickBack: () -> Unit
) {
val ctx = LocalContext.current
@ -78,15 +79,15 @@ fun ColorsScreen(
// 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)?.setForceTheme(null, null)
}
}
(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))
if (lifecycleState == Lifecycle.State.RESUMED) {
(ctx.getActivity() as? SettingsActivity)?.setForceTheme(theme, isNight)
}
}
val prefs = ctx.prefs()
@ -94,7 +95,7 @@ fun ColorsScreen(
if ((b?.value ?: 0) < 0)
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)!!
val moreColors = KeyboardTheme.readUserMoreColors(prefs, themeName)
val userColors = KeyboardTheme.readUserColors(prefs, themeName)
@ -136,8 +137,9 @@ fun ColorsScreen(
nameField = it
},
isError = !nameValid,
// 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) }
// 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) },
singleLine = true,
)
},
menu = listOf(
@ -233,7 +235,7 @@ data class SaveThoseColors(val name: String? = null, val moreColors: Int, val co
private fun Preview() {
Theme(true) {
Surface {
ColorsScreen(false) { }
ColorsScreen(false, null) { }
}
}
}