remove unnecessary settings reloads during adjusting additional subtype

This commit is contained in:
Helium314 2025-03-08 17:55:02 +01:00
parent 2b8c39b125
commit e9a2a7ebb1

View file

@ -67,28 +67,29 @@ object SubtypeUtilsAdditional {
val isSelected = prefs.getString(Settings.PREF_SELECTED_SUBTYPE, Defaults.PREF_SELECTED_SUBTYPE)!!.toSettingsSubtype() == from val isSelected = prefs.getString(Settings.PREF_SELECTED_SUBTYPE, Defaults.PREF_SELECTED_SUBTYPE)!!.toSettingsSubtype() == from
val isEnabled = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!.split(Separators.SETS) val isEnabled = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!.split(Separators.SETS)
.any { it.toSettingsSubtype() == from } .any { it.toSettingsSubtype() == from }
val new = prefs.getString(Settings.PREF_ADDITIONAL_SUBTYPES, Defaults.PREF_ADDITIONAL_SUBTYPES)!! val additionalSubtypes = SubtypeSettings.createSettingsSubtypes(prefs.getString(Settings.PREF_ADDITIONAL_SUBTYPES, Defaults.PREF_ADDITIONAL_SUBTYPES)!!)
.split(Separators.SETS).mapNotNullTo(sortedSetOf()) { .toMutableList()
if (it == from.toPref()) null else it additionalSubtypes.remove(from)
} + to.toPref() if (SubtypeSettings.getResourceSubtypesForLocale(to.locale).none { it.toSettingsSubtype() == to }) {
prefs.edit().putString(Settings.PREF_ADDITIONAL_SUBTYPES, new.joinToString(Separators.SETS)).apply() // We only add the "to" subtype if it's not equal to a resource subtype.
// This means we make additional subtype disappear as magically as it was added if all settings are default.
val fromSubtype = from.toAdditionalSubtype() // If we don't do this, enabling the base subtype will result in the additional subtype being enabled,
val toSubtype = to.toAdditionalSubtype() // as both have the same settingsSubtype.
if (SubtypeSettings.getResourceSubtypesForLocale(to.locale).any { it.toSettingsSubtype() == to }) { additionalSubtypes.add(to)
// all settings changed to default -> make additional subtype disappear as magically as it was added
// if we don't do this, enabling the base subtype will result in the additional subtype being enabled,
// as both have the same settingsSubtype
removeAdditionalSubtype(context, toSubtype)
} }
val editor = prefs.edit()
editor.putString(Settings.PREF_ADDITIONAL_SUBTYPES, SubtypeSettings.createPrefSubtypes(additionalSubtypes))
if (isSelected) { if (isSelected) {
SubtypeSettings.setSelectedSubtype(prefs, toSubtype) editor.putString(Settings.PREF_SELECTED_SUBTYPE, to.toPref())
} }
if (isEnabled) { if (isEnabled) {
SubtypeSettings.removeEnabledSubtype(context, fromSubtype) val enabled = SubtypeSettings.createSettingsSubtypes(prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!)
SubtypeSettings.addEnabledSubtype(prefs, toSubtype) .toMutableList()
enabled.remove(from)
enabled.add(to)
editor.putString(Settings.PREF_ENABLED_SUBTYPES, SubtypeSettings.createPrefSubtypes(enabled))
} }
// reloading is often unnecessary, but fast enough to not care about calling it only when necessary editor.apply()
SubtypeSettings.reloadEnabledSubtypes(context) SubtypeSettings.reloadEnabledSubtypes(context)
} }