also rename layout in preferences when renaming the file

This commit is contained in:
Helium314 2025-02-20 18:11:23 +01:00
parent d03801f22e
commit 31f7ef6182
2 changed files with 24 additions and 1 deletions

View file

@ -125,6 +125,25 @@ object SubtypeSettings {
fun getAvailableSubtypeLocales(): List<Locale> = resourceSubtypesByLocale.keys.toList()
fun onRenameLayout(type: LayoutType, from: String, to: String, context: Context) {
val prefs = context.prefs()
listOf(
Settings.PREF_ADDITIONAL_SUBTYPES to Defaults.PREF_ADDITIONAL_SUBTYPES,
Settings.PREF_ENABLED_SUBTYPES to Defaults.PREF_ENABLED_SUBTYPES,
Settings.PREF_SELECTED_SUBTYPE to Defaults.PREF_SELECTED_SUBTYPE
).forEach { (key, default) ->
val new = prefs.getString(key, default)!!.split(Separators.SETS).joinToString(Separators.SETS) {
val subtype = it.toSettingsSubtype()
if (subtype.layoutName(type) == from) subtype.withLayout(type, to).toPref()
else subtype.toPref()
}
prefs.edit().putString(key, new).apply()
}
if (Settings.readDefaultLayoutName(type, prefs) == from)
Settings.writeDefaultLayoutName(to, type, prefs)
reloadEnabledSubtypes(context)
}
fun reloadEnabledSubtypes(context: Context) {
enabledSubtypes.clear()
removeInvalidCustomSubtypes(context)

View file

@ -24,6 +24,7 @@ import helium314.keyboard.latin.R
import helium314.keyboard.latin.utils.LayoutType
import helium314.keyboard.latin.utils.LayoutUtilsCustom
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.SubtypeSettings
import helium314.keyboard.latin.utils.getStringResourceOrName
import helium314.keyboard.settings.keyboardNeedsReload
import kotlinx.coroutines.Job
@ -61,8 +62,10 @@ fun LayoutEditDialog(
},
onConfirmed = {
val newLayoutName = LayoutUtilsCustom.getSecondaryLayoutName(displayNameValue.text)
if (startIsCustom && initialLayoutName != newLayoutName)
if (startIsCustom && initialLayoutName != newLayoutName) {
LayoutUtilsCustom.getLayoutFile(initialLayoutName, layoutType, ctx).delete()
SubtypeSettings.onRenameLayout(layoutType, initialLayoutName, newLayoutName, ctx)
}
LayoutUtilsCustom.getLayoutFile(newLayoutName, layoutType, ctx).writeText(it)
LayoutUtilsCustom.onLayoutFileChanged()
keyboardNeedsReload = true
@ -84,6 +87,7 @@ fun LayoutEditDialog(
job?.cancel()
if (!valid) {
job = scope.launch {
// todo: this can end up in toast-spam
val message = Log.getLog(10)
.lastOrNull { it.tag == "LayoutUtilsCustom" }?.message
?.split("\n")?.take(2)?.joinToString("\n")