sort extra value when creating SettingsSubtype

This commit is contained in:
Helium314 2025-03-01 08:40:50 +01:00
parent c431f7043f
commit 7ec9e1a8cf
2 changed files with 29 additions and 20 deletions

View file

@ -83,8 +83,12 @@ data class SettingsSubtype(val locale: Locale, val extraValues: String) {
.split(Separators.SETS).contains(toPref()) .split(Separators.SETS).contains(toPref())
companion object { companion object {
fun String.toSettingsSubtype() = fun String.toSettingsSubtype(): SettingsSubtype =
SettingsSubtype(substringBefore(Separators.SET).constructLocale(), substringAfter(Separators.SET)) SettingsSubtype(
substringBefore(Separators.SET).constructLocale(),
// we want a sorted string for reliable comparison
substringAfter(Separators.SET).split(",").sorted().joinToString(",")
)
fun String.getExtraValueOf(extraValueKey: String) = split(",") fun String.getExtraValueOf(extraValueKey: String) = split(",")
.firstOrNull { it.startsWith("$extraValueKey=") }?.substringAfter("$extraValueKey=") .firstOrNull { it.startsWith("$extraValueKey=") }?.substringAfter("$extraValueKey=")
@ -108,7 +112,7 @@ data class SettingsSubtype(val locale: Locale, val extraValues: String) {
|| it == ExtraValue.EMOJI_CAPABLE || it == ExtraValue.EMOJI_CAPABLE
|| it == ExtraValue.IS_ADDITIONAL_SUBTYPE || it == ExtraValue.IS_ADDITIONAL_SUBTYPE
|| it.startsWith(ExtraValue.UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME) || it.startsWith(ExtraValue.UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME)
}.joinToString(",") }.sorted().joinToString(",")
require(!filteredExtraValue.contains(Separators.SETS) && !filteredExtraValue.contains(Separators.SET)) require(!filteredExtraValue.contains(Separators.SETS) && !filteredExtraValue.contains(Separators.SET))
{ "extra value contains not allowed characters $filteredExtraValue" } { "extra value contains not allowed characters $filteredExtraValue" }
return SettingsSubtype(locale(), filteredExtraValue) return SettingsSubtype(locale(), filteredExtraValue)

View file

@ -17,6 +17,7 @@ import helium314.keyboard.latin.common.LocaleUtils
import helium314.keyboard.latin.define.DebugFlags import helium314.keyboard.latin.define.DebugFlags
import helium314.keyboard.latin.settings.Defaults import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.settings.SettingsSubtype
import helium314.keyboard.latin.settings.SettingsSubtype.Companion.toSettingsSubtype import helium314.keyboard.latin.settings.SettingsSubtype.Companion.toSettingsSubtype
import helium314.keyboard.latin.utils.ScriptUtils.script import helium314.keyboard.latin.utils.ScriptUtils.script
import java.util.Locale import java.util.Locale
@ -50,9 +51,10 @@ object SubtypeSettings {
} }
fun addEnabledSubtype(prefs: SharedPreferences, newSubtype: InputMethodSubtype) { fun addEnabledSubtype(prefs: SharedPreferences, newSubtype: InputMethodSubtype) {
val subtypeString = newSubtype.toSettingsSubtype().toPref() val subtype = newSubtype.toSettingsSubtype()
val oldSubtypeStrings = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!.split(Separators.SETS) val subtypes = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!
val newString = (oldSubtypeStrings + subtypeString).filter { it.isNotBlank() }.toSortedSet().joinToString(Separators.SETS) .split(Separators.SETS).filter { it.isNotBlank() }.map { it.toSettingsSubtype() } + subtype
val newString = subtypes.map { it.toPref() }.toSortedSet().joinToString(Separators.SETS)
prefs.edit { putString(Settings.PREF_ENABLED_SUBTYPES, newString) } prefs.edit { putString(Settings.PREF_ENABLED_SUBTYPES, newString) }
if (newSubtype !in enabledSubtypes) { if (newSubtype !in enabledSubtypes) {
@ -64,7 +66,7 @@ object SubtypeSettings {
/** @return whether subtype was actually removed */ /** @return whether subtype was actually removed */
fun removeEnabledSubtype(context: Context, subtype: InputMethodSubtype): Boolean { fun removeEnabledSubtype(context: Context, subtype: InputMethodSubtype): Boolean {
if (!removeEnabledSubtype(context.prefs(), subtype.toSettingsSubtype().toPref())) return false if (!removeEnabledSubtype(context.prefs(), subtype.toSettingsSubtype())) return false
if (!enabledSubtypes.remove(subtype)) reloadEnabledSubtypes(context) if (!enabledSubtypes.remove(subtype)) reloadEnabledSubtypes(context)
else RichInputMethodManager.getInstance().refreshSubtypeCaches() else RichInputMethodManager.getInstance().refreshSubtypeCaches()
return true return true
@ -93,10 +95,12 @@ object SubtypeSettings {
} }
fun setSelectedSubtype(prefs: SharedPreferences, subtype: InputMethodSubtype) { fun setSelectedSubtype(prefs: SharedPreferences, subtype: InputMethodSubtype) {
val subtypeString = subtype.toSettingsSubtype().toPref() val settingsSubtype = subtype.toSettingsSubtype()
if (subtype.locale().toLanguageTag().isEmpty() || prefs.getString(Settings.PREF_SELECTED_SUBTYPE, Defaults.PREF_SELECTED_SUBTYPE) == subtypeString) if (settingsSubtype.locale.toLanguageTag().isEmpty()) {
Log.w(TAG, "tried to set subtype with empty locale: $settingsSubtype")
return return
prefs.edit { putString(Settings.PREF_SELECTED_SUBTYPE, subtypeString) } }
prefs.edit { putString(Settings.PREF_SELECTED_SUBTYPE, settingsSubtype.toPref()) }
} }
fun isAdditionalSubtype(subtype: InputMethodSubtype): Boolean = subtype in additionalSubtypes fun isAdditionalSubtype(subtype: InputMethodSubtype): Boolean = subtype in additionalSubtypes
@ -235,7 +239,7 @@ object SubtypeSettings {
if (DebugFlags.DEBUG_ENABLED) if (DebugFlags.DEBUG_ENABLED)
Toast.makeText(context, message, Toast.LENGTH_LONG).show() Toast.makeText(context, message, Toast.LENGTH_LONG).show()
else // don't remove in debug mode else // don't remove in debug mode
removeEnabledSubtype(prefs, settingsSubtype.toPref()) removeEnabledSubtype(prefs, settingsSubtype)
continue continue
} }
@ -246,7 +250,7 @@ object SubtypeSettings {
if (DebugFlags.DEBUG_ENABLED) if (DebugFlags.DEBUG_ENABLED)
Toast.makeText(context, message, Toast.LENGTH_LONG).show() Toast.makeText(context, message, Toast.LENGTH_LONG).show()
else // don't remove in debug mode else // don't remove in debug mode
removeEnabledSubtype(prefs, settingsSubtype.toPref()) removeEnabledSubtype(prefs, settingsSubtype)
continue continue
} }
@ -255,17 +259,18 @@ object SubtypeSettings {
} }
/** @return whether pref was changed */ /** @return whether pref was changed */
private fun removeEnabledSubtype(prefs: SharedPreferences, subtypeString: String): Boolean { private fun removeEnabledSubtype(prefs: SharedPreferences, subtype: SettingsSubtype): Boolean {
val oldSubtypeString = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!! val oldSubtypes = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!
val newString = (oldSubtypeString.split(Separators.SETS) - subtypeString).joinToString(Separators.SETS) .split(Separators.SETS).filter { it.isNotEmpty() }.map { it.toSettingsSubtype() }
if (newString == oldSubtypeString) val newSubtypes = oldSubtypes - subtype
return false// already removed if (oldSubtypes == newSubtypes)
prefs.edit { putString(Settings.PREF_ENABLED_SUBTYPES, newString) } return false // already removed
if (subtypeString == prefs.getString(Settings.PREF_SELECTED_SUBTYPE, Defaults.PREF_SELECTED_SUBTYPE)) { prefs.edit { putString(Settings.PREF_ENABLED_SUBTYPES, newSubtypes.joinToString(Separators.SETS) { it.toPref() }) }
if (subtype == prefs.getString(Settings.PREF_SELECTED_SUBTYPE, Defaults.PREF_SELECTED_SUBTYPE)!!.toSettingsSubtype()) {
// switch subtype if the currently used one has been disabled // switch subtype if the currently used one has been disabled
try { try {
val nextSubtype = RichInputMethodManager.getInstance().getNextSubtypeInThisIme(true) val nextSubtype = RichInputMethodManager.getInstance().getNextSubtypeInThisIme(true)
if (subtypeString == nextSubtype?.toSettingsSubtype()?.toPref()) if (subtype == nextSubtype?.toSettingsSubtype())
KeyboardSwitcher.getInstance().switchToSubtype(getDefaultEnabledSubtypes().first()) KeyboardSwitcher.getInstance().switchToSubtype(getDefaultEnabledSubtypes().first())
else else
KeyboardSwitcher.getInstance().switchToSubtype(nextSubtype) KeyboardSwitcher.getInstance().switchToSubtype(nextSubtype)