fix issues when renaming or deleting layouts

This commit is contained in:
Helium314 2025-04-03 17:09:15 +02:00
parent 57deb82ca7
commit d79c84d7df

View file

@ -130,12 +130,14 @@ object SubtypeSettings {
*/ */
fun onRenameLayout(type: LayoutType, from: String, to: String?, context: Context) { fun onRenameLayout(type: LayoutType, from: String, to: String?, context: Context) {
val prefs = context.prefs() val prefs = context.prefs()
val editor = prefs.edit() // calling apply for each separate setting would result in an invalid intermediate state
listOf( listOf(
Settings.PREF_ADDITIONAL_SUBTYPES to Defaults.PREF_ADDITIONAL_SUBTYPES, Settings.PREF_ADDITIONAL_SUBTYPES to Defaults.PREF_ADDITIONAL_SUBTYPES,
Settings.PREF_ENABLED_SUBTYPES to Defaults.PREF_ENABLED_SUBTYPES, Settings.PREF_ENABLED_SUBTYPES to Defaults.PREF_ENABLED_SUBTYPES,
Settings.PREF_SELECTED_SUBTYPE to Defaults.PREF_SELECTED_SUBTYPE Settings.PREF_SELECTED_SUBTYPE to Defaults.PREF_SELECTED_SUBTYPE
).forEach { (key, default) -> ).forEach { (key, default) ->
val new = prefs.getString(key, default)!!.split(Separators.SETS).mapNotNullTo(mutableSetOf()) { val new = prefs.getString(key, default)!!.split(Separators.SETS).mapNotNullTo(mutableSetOf()) {
if (it.isEmpty()) return@mapNotNullTo null
val subtype = it.toSettingsSubtype() val subtype = it.toSettingsSubtype()
if (subtype.layoutName(type) == from) { if (subtype.layoutName(type) == from) {
if (to == null) { if (to == null) {
@ -146,7 +148,7 @@ object SubtypeSettings {
val newSubtype = if (defaultLayout == null) subtype.withoutLayout(type) val newSubtype = if (defaultLayout == null) subtype.withoutLayout(type)
else subtype.withLayout(type, defaultLayout) else subtype.withLayout(type, defaultLayout)
if (newSubtype.isSameAsDefault() && key == Settings.PREF_ADDITIONAL_SUBTYPES) null if (newSubtype.isSameAsDefault() && key == Settings.PREF_ADDITIONAL_SUBTYPES) null
else subtype.withoutLayout(type).toPref() else newSubtype.toPref()
} }
else subtype.withLayout(type, to).toPref() else subtype.withLayout(type, to).toPref()
} }
@ -154,6 +156,7 @@ object SubtypeSettings {
}.joinToString(Separators.SETS) }.joinToString(Separators.SETS)
prefs.edit().putString(key, new).apply() prefs.edit().putString(key, new).apply()
} }
editor.apply()
if (Settings.readDefaultLayoutName(type, prefs) == from) if (Settings.readDefaultLayoutName(type, prefs) == from)
Settings.writeDefaultLayoutName(to, type, prefs) Settings.writeDefaultLayoutName(to, type, prefs)
reloadEnabledSubtypes(context) reloadEnabledSubtypes(context)