mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-04-22 07:09:10 +00:00
remove unnecessary settings reloads during adjusting additional subtype
This commit is contained in:
parent
2b8c39b125
commit
e9a2a7ebb1
1 changed files with 18 additions and 17 deletions
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue