From a4d96a12a990636408e58aa6019b8be4fca8612a Mon Sep 17 00:00:00 2001 From: Helium314 Date: Sat, 8 Mar 2025 09:05:33 +0100 Subject: [PATCH] fix some issues with subtypes being disabled or not shown as enabled after editing --- .../latin/settings/SettingsSubtype.kt | 2 +- .../latin/utils/SubtypeUtilsAdditional.kt | 27 ++++++++++--------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/settings/SettingsSubtype.kt b/app/src/main/java/helium314/keyboard/latin/settings/SettingsSubtype.kt index 62899ddb..ca18b7fd 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/SettingsSubtype.kt +++ b/app/src/main/java/helium314/keyboard/latin/settings/SettingsSubtype.kt @@ -38,7 +38,7 @@ data class SettingsSubtype(val locale: Locale, val extraValues: String) { val newList = extraValues.split(",") .filterNot { it.isBlank() || it.startsWith("$extraValueKey=") || it == extraValueKey } val newValue = if (extraValue == null) extraValueKey else "$extraValueKey=$extraValue" - val newValues = (newList + newValue).joinToString(",") + val newValues = (newList + newValue).sorted().joinToString(",") return copy(extraValues = newValues) } diff --git a/app/src/main/java/helium314/keyboard/latin/utils/SubtypeUtilsAdditional.kt b/app/src/main/java/helium314/keyboard/latin/utils/SubtypeUtilsAdditional.kt index f110d59d..53ac40e1 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/SubtypeUtilsAdditional.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/SubtypeUtilsAdditional.kt @@ -74,6 +74,12 @@ object SubtypeUtilsAdditional { val fromSubtype = from.toAdditionalSubtype() val toSubtype = to.toAdditionalSubtype() + if (SubtypeSettings.getResourceSubtypesForLocale(to.locale).any { it.toSettingsSubtype() == 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) + } if (isSelected) { SubtypeSettings.setSelectedSubtype(prefs, toSubtype) } @@ -81,21 +87,18 @@ object SubtypeUtilsAdditional { SubtypeSettings.removeEnabledSubtype(context, fromSubtype) SubtypeSettings.addEnabledSubtype(prefs, toSubtype) } - if (!isEnabled && !isSelected) - SubtypeSettings.reloadEnabledSubtypes(context) + // reloading is often unnecessary, but fast enough to not care about calling it only when necessary + SubtypeSettings.reloadEnabledSubtypes(context) } - fun createAdditionalSubtypes(prefSubtypes: String): List { - if (prefSubtypes.isEmpty()) - return emptyList() - return prefSubtypes.split(Separators.SETS).map { it.toSettingsSubtype().toAdditionalSubtype() } - } + fun createAdditionalSubtypes(prefSubtypes: String): List = + prefSubtypes.split(Separators.SETS).mapNotNull { + if (it.isEmpty()) null + else it.toSettingsSubtype().toAdditionalSubtype() + } - fun createPrefSubtypes(subtypes: Collection): String { - if (subtypes.isEmpty()) - return "" - return subtypes.joinToString(Separators.SETS) { it.toSettingsSubtype().toPref() } - } + fun createPrefSubtypes(subtypes: Collection): String = + subtypes.joinToString(Separators.SETS) { it.toSettingsSubtype().toPref() } private fun getNameResId(locale: Locale, mainLayoutName: String): Int { val nameId = SubtypeLocaleUtils.getSubtypeNameResId(locale, mainLayoutName)