mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-04-20 06:09:09 +00:00
fix some issues with renaming color schemes
fixes GH-1449 and some more unreported bugs
This commit is contained in:
parent
8932fc84e1
commit
ac805a9286
2 changed files with 38 additions and 40 deletions
|
@ -405,14 +405,7 @@ private constructor(val themeId: Int, @JvmField val mStyleId: Int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getUnusedThemeName(initialName: String, prefs: SharedPreferences): String {
|
fun getUnusedThemeName(initialName: String, prefs: SharedPreferences): String {
|
||||||
val existingNames = prefs.all.keys.mapNotNull {
|
val existingNames = getExistingThemeNames(prefs)
|
||||||
when {
|
|
||||||
it.startsWith(Settings.PREF_USER_COLORS_PREFIX) -> it.substringAfter(Settings.PREF_USER_COLORS_PREFIX)
|
|
||||||
it.startsWith(Settings.PREF_USER_ALL_COLORS_PREFIX) -> it.substringAfter(Settings.PREF_USER_ALL_COLORS_PREFIX)
|
|
||||||
it.startsWith(Settings.PREF_USER_MORE_COLORS_PREFIX) -> it.substringAfter(Settings.PREF_USER_MORE_COLORS_PREFIX)
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
}.toSortedSet()
|
|
||||||
if (initialName !in existingNames) return initialName
|
if (initialName !in existingNames) return initialName
|
||||||
var i = 1
|
var i = 1
|
||||||
while ("$initialName$i" in existingNames)
|
while ("$initialName$i" in existingNames)
|
||||||
|
@ -420,11 +413,8 @@ private constructor(val themeId: Int, @JvmField val mStyleId: Int) {
|
||||||
return "$initialName$i"
|
return "$initialName$i"
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns false if not renamed due to invalid name or collision
|
private fun getExistingThemeNames(prefs: SharedPreferences) =
|
||||||
fun renameUserColors(from: String, to: String, prefs: SharedPreferences): Boolean {
|
prefs.all.keys.mapNotNull {
|
||||||
if (to.isBlank()) return false // don't want that
|
|
||||||
if (to == from) return true // nothing to do
|
|
||||||
val existingNames = prefs.all.keys.mapNotNull {
|
|
||||||
when {
|
when {
|
||||||
it.startsWith(Settings.PREF_USER_COLORS_PREFIX) -> it.substringAfter(Settings.PREF_USER_COLORS_PREFIX)
|
it.startsWith(Settings.PREF_USER_COLORS_PREFIX) -> it.substringAfter(Settings.PREF_USER_COLORS_PREFIX)
|
||||||
it.startsWith(Settings.PREF_USER_ALL_COLORS_PREFIX) -> it.substringAfter(Settings.PREF_USER_ALL_COLORS_PREFIX)
|
it.startsWith(Settings.PREF_USER_ALL_COLORS_PREFIX) -> it.substringAfter(Settings.PREF_USER_ALL_COLORS_PREFIX)
|
||||||
|
@ -432,6 +422,12 @@ private constructor(val themeId: Int, @JvmField val mStyleId: Int) {
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
}.toSortedSet()
|
}.toSortedSet()
|
||||||
|
|
||||||
|
// returns false if not renamed due to invalid name or collision
|
||||||
|
fun renameUserColors(from: String, to: String, prefs: SharedPreferences): Boolean {
|
||||||
|
if (to.isBlank()) return false // don't want that
|
||||||
|
if (to == from) return true // nothing to do
|
||||||
|
val existingNames = getExistingThemeNames(prefs)
|
||||||
if (to in existingNames) return false
|
if (to in existingNames) return false
|
||||||
// all good, now rename
|
// all good, now rename
|
||||||
prefs.edit {
|
prefs.edit {
|
||||||
|
|
|
@ -71,6 +71,14 @@ fun ColorsScreen(
|
||||||
onClickBack: () -> Unit
|
onClickBack: () -> Unit
|
||||||
) {
|
) {
|
||||||
val ctx = LocalContext.current
|
val ctx = LocalContext.current
|
||||||
|
val prefs = ctx.prefs()
|
||||||
|
val b = (ctx.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
|
||||||
|
if ((b?.value ?: 0) < 0)
|
||||||
|
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
|
||||||
|
|
||||||
|
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)!!
|
||||||
|
var newThemeName by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue(themeName)) }
|
||||||
|
|
||||||
// is there really no better way of only setting forceOpposite while the screen is shown (and not paused)?
|
// 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
|
// lifecycle stuff is weird, there is no pause and similar when activity is paused
|
||||||
|
@ -83,21 +91,14 @@ fun ColorsScreen(
|
||||||
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)?.setForceTheme(theme, isNight)
|
(ctx.getActivity() as? SettingsActivity)?.setForceTheme(newThemeName.text, isNight)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val prefs = ctx.prefs()
|
val moreColors = KeyboardTheme.readUserMoreColors(prefs, newThemeName.text)
|
||||||
val b = (ctx.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
|
val userColors = KeyboardTheme.readUserColors(prefs, newThemeName.text)
|
||||||
if ((b?.value ?: 0) < 0)
|
|
||||||
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
|
|
||||||
|
|
||||||
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)
|
|
||||||
val shownColors = if (moreColors == 2) {
|
val shownColors = if (moreColors == 2) {
|
||||||
val allColors = KeyboardTheme.readUserAllColors(prefs, themeName)
|
val allColors = KeyboardTheme.readUserAllColors(prefs, newThemeName.text)
|
||||||
ColorType.entries.map {
|
ColorType.entries.map {
|
||||||
ColorSetting(it.name, null, allColors[it] ?: it.default())
|
ColorSetting(it.name, null, allColors[it] ?: it.default())
|
||||||
}
|
}
|
||||||
|
@ -114,12 +115,11 @@ fun ColorsScreen(
|
||||||
fun ColorSetting.displayColor() = if (auto == true) KeyboardTheme.determineUserColor(userColors, ctx, name, isNight)
|
fun ColorSetting.displayColor() = if (auto == true) KeyboardTheme.determineUserColor(userColors, ctx, name, isNight)
|
||||||
else color ?: KeyboardTheme.determineUserColor(userColors, ctx, name, isNight)
|
else color ?: KeyboardTheme.determineUserColor(userColors, ctx, name, isNight)
|
||||||
|
|
||||||
var newThemeName by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue(themeName)) }
|
|
||||||
var chosenColorString: String by rememberSaveable { mutableStateOf("") }
|
var chosenColorString: String by rememberSaveable { mutableStateOf("") }
|
||||||
val chosenColor = runCatching { Json.decodeFromString<ColorSetting?>(chosenColorString) }.getOrNull()
|
val chosenColor = runCatching { Json.decodeFromString<ColorSetting?>(chosenColorString) }.getOrNull()
|
||||||
val saveLauncher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
val saveLauncher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||||
if (it.resultCode != Activity.RESULT_OK) return@rememberLauncherForActivityResult
|
if (result.resultCode != Activity.RESULT_OK) return@rememberLauncherForActivityResult
|
||||||
val uri = it.data?.data ?: return@rememberLauncherForActivityResult
|
val uri = result.data?.data ?: return@rememberLauncherForActivityResult
|
||||||
ctx.getActivity()?.contentResolver?.openOutputStream(uri)?.writer()?.use { it.write(getColorString(prefs, newThemeName.text)) }
|
ctx.getActivity()?.contentResolver?.openOutputStream(uri)?.writer()?.use { it.write(getColorString(prefs, newThemeName.text)) }
|
||||||
}
|
}
|
||||||
SearchScreen(
|
SearchScreen(
|
||||||
|
@ -130,8 +130,10 @@ fun ColorsScreen(
|
||||||
value = nameField,
|
value = nameField,
|
||||||
onValueChange = {
|
onValueChange = {
|
||||||
nameValid = KeyboardTheme.renameUserColors(newThemeName.text, it.text, prefs)
|
nameValid = KeyboardTheme.renameUserColors(newThemeName.text, it.text, prefs)
|
||||||
if (nameValid)
|
if (nameValid) {
|
||||||
newThemeName = it
|
newThemeName = it
|
||||||
|
SettingsActivity.forceTheme = newThemeName.text
|
||||||
|
}
|
||||||
nameField = it
|
nameField = it
|
||||||
},
|
},
|
||||||
isError = !nameValid,
|
isError = !nameValid,
|
||||||
|
@ -195,11 +197,11 @@ fun ColorsScreen(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (colorSetting.auto != null)
|
if (colorSetting.auto != null)
|
||||||
Switch(colorSetting.auto, onCheckedChange = {
|
Switch(colorSetting.auto, onCheckedChange = { checked ->
|
||||||
val oldUserColors = KeyboardTheme.readUserColors(prefs, themeName)
|
val oldUserColors = KeyboardTheme.readUserColors(prefs, newThemeName.text)
|
||||||
val newUserColors = (oldUserColors + ColorSetting(colorSetting.name, it, colorSetting.color))
|
val newUserColors = (oldUserColors + ColorSetting(colorSetting.name, checked, colorSetting.color))
|
||||||
.reversed().distinctBy { it.displayName }
|
.reversed().distinctBy { it.displayName }
|
||||||
KeyboardTheme.writeUserColors(prefs, themeName, newUserColors)
|
KeyboardTheme.writeUserColors(prefs, newThemeName.text, newUserColors)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -209,16 +211,16 @@ fun ColorsScreen(
|
||||||
onDismissRequest = { chosenColorString = "" },
|
onDismissRequest = { chosenColorString = "" },
|
||||||
initialColor = chosenColor.displayColor(),
|
initialColor = chosenColor.displayColor(),
|
||||||
title = chosenColor.displayName,
|
title = chosenColor.displayName,
|
||||||
) {
|
) { color ->
|
||||||
if (moreColors == 2) {
|
if (moreColors == 2) {
|
||||||
val oldColors = KeyboardTheme.readUserAllColors(prefs, themeName)
|
val oldColors = KeyboardTheme.readUserAllColors(prefs, newThemeName.text)
|
||||||
oldColors[ColorType.valueOf(chosenColor.name)] = it
|
oldColors[ColorType.valueOf(chosenColor.name)] = color
|
||||||
KeyboardTheme.writeUserAllColors(prefs, themeName, oldColors)
|
KeyboardTheme.writeUserAllColors(prefs, newThemeName.text, oldColors)
|
||||||
} else {
|
} else {
|
||||||
val oldUserColors = KeyboardTheme.readUserColors(prefs, themeName)
|
val oldUserColors = KeyboardTheme.readUserColors(prefs, newThemeName.text)
|
||||||
val newUserColors = (oldUserColors + ColorSetting(chosenColor.name, false, it))
|
val newUserColors = (oldUserColors + ColorSetting(chosenColor.name, false, color))
|
||||||
.reversed().distinctBy { it.displayName }
|
.reversed().distinctBy { it.displayName }
|
||||||
KeyboardTheme.writeUserColors(prefs, themeName, newUserColors)
|
KeyboardTheme.writeUserColors(prefs, newThemeName.text, newUserColors)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue