consistent way of for settingsSubtypes <-> string

This commit is contained in:
Helium314 2025-03-08 09:34:33 +01:00
parent e1f02dab31
commit c47da4203f
4 changed files with 23 additions and 22 deletions

View file

@ -161,7 +161,7 @@ fun checkVersionUpgrade(context: Context) {
split[1] = newName
split.joinToString(":")
}
Settings.writePrefAdditionalSubtypes(prefs, newSubtypeStrings.joinToString(";"))
prefs.edit().putString(Settings.PREF_ADDITIONAL_SUBTYPES, newSubtypeStrings.joinToString(";")).apply()
}
// rename other custom layouts
LayoutUtilsCustom.onLayoutFileChanged()
@ -630,7 +630,7 @@ private fun upgradesWhenComingFromOldAppName(context: Context) {
val localeString = it.substringBefore(":")
additionalSubtypes.add(it.replace(localeString, localeString.constructLocale().toLanguageTag()))
}
Settings.writePrefAdditionalSubtypes(prefs, additionalSubtypes.joinToString(";"))
prefs.edit().putString(Settings.PREF_ADDITIONAL_SUBTYPES, additionalSubtypes.joinToString(";")).apply()
}
// move pinned clips to credential protected storage if device is not locked (should never happen)
if (!prefs.contains(Settings.PREF_PINNED_CLIPS)) return

View file

@ -312,10 +312,6 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
mPrefs.edit().putBoolean(Settings.PREF_ALWAYS_INCOGNITO_MODE, !oldValue).apply();
}
public static void writePrefAdditionalSubtypes(final SharedPreferences prefs, final String prefSubtypes) {
prefs.edit().putString(PREF_ADDITIONAL_SUBTYPES, prefSubtypes).apply();
}
public static int readHorizontalSpaceSwipe(final SharedPreferences prefs) {
return switch (prefs.getString(PREF_SPACE_HORIZONTAL_SWIPE, Defaults.PREF_SPACE_HORIZONTAL_SWIPE)) {
case "move_cursor" -> KeyboardActionListener.SWIPE_MOVE_CURSOR;

View file

@ -52,9 +52,8 @@ object SubtypeSettings {
fun addEnabledSubtype(prefs: SharedPreferences, newSubtype: InputMethodSubtype) {
val subtype = newSubtype.toSettingsSubtype()
val subtypes = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!
.split(Separators.SETS).filter { it.isNotBlank() }.map { it.toSettingsSubtype() } + subtype
val newString = subtypes.map { it.toPref() }.toSortedSet().joinToString(Separators.SETS)
val subtypes = createSettingsSubtypes(prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!) + subtype
val newString = createPrefSubtypes(subtypes)
prefs.edit { putString(Settings.PREF_ENABLED_SUBTYPES, newString) }
if (newSubtype !in enabledSubtypes) {
@ -155,6 +154,15 @@ object SubtypeSettings {
RichInputMethodManager.getInstance().refreshSubtypeCaches()
}
fun createSettingsSubtypes(prefSubtypes: String): List<SettingsSubtype> =
prefSubtypes.split(Separators.SETS).mapNotNull {
if (it.isEmpty()) null
else it.toSettingsSubtype()
}
fun createPrefSubtypes(subtypes: Collection<SettingsSubtype>): String =
subtypes.map { it.toPref() }.toSortedSet().joinToString(Separators.SETS)
fun init(context: Context) {
SubtypeLocaleUtils.init(context) // necessary to get the correct getKeyboardLayoutSetName
@ -205,7 +213,8 @@ object SubtypeSettings {
}
if (subtypesToRemove.isEmpty()) return
Log.w(TAG, "removing custom subtypes without main layout files: $subtypesToRemove")
Settings.writePrefAdditionalSubtypes(prefs, additionalSubtypes.filterNot { it in subtypesToRemove }.joinToString(Separators.SETS))
// todo: now we have a qwerty fallback anyway, consider removing this method (makes bugs more obvious to users)
prefs.edit().putString(Settings.PREF_ADDITIONAL_SUBTYPES, additionalSubtypes.filterNot { it in subtypesToRemove }.joinToString(Separators.SETS)).apply()
}
private fun loadAdditionalSubtypes(prefs: SharedPreferences) {
@ -218,8 +227,7 @@ object SubtypeSettings {
// requires loadResourceSubtypes to be called before
private fun loadEnabledSubtypes(context: Context) {
val prefs = context.prefs()
val settingsSubtypes = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!
.split(Separators.SETS).filter { it.isNotEmpty() }.map { it.toSettingsSubtype() }
val settingsSubtypes = createSettingsSubtypes(prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!)
for (settingsSubtype in settingsSubtypes) {
if (settingsSubtype.isAdditionalSubtype(prefs)) {
enabledSubtypes.add(settingsSubtype.toAdditionalSubtype())
@ -253,12 +261,11 @@ object SubtypeSettings {
/** @return whether pref was changed */
private fun removeEnabledSubtype(prefs: SharedPreferences, subtype: SettingsSubtype): Boolean {
val oldSubtypes = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!
.split(Separators.SETS).filter { it.isNotEmpty() }.map { it.toSettingsSubtype() }
val oldSubtypes = createSettingsSubtypes(prefs.getString(Settings.PREF_ENABLED_SUBTYPES, Defaults.PREF_ENABLED_SUBTYPES)!!)
val newSubtypes = oldSubtypes - subtype
if (oldSubtypes == newSubtypes)
return false // already removed
prefs.edit { putString(Settings.PREF_ENABLED_SUBTYPES, newSubtypes.joinToString(Separators.SETS) { it.toPref() }) }
prefs.edit { putString(Settings.PREF_ENABLED_SUBTYPES, createPrefSubtypes(newSubtypes)) }
if (subtype == prefs.getString(Settings.PREF_SELECTED_SUBTYPE, Defaults.PREF_SELECTED_SUBTYPE)!!.toSettingsSubtype()) {
// switch subtype if the currently used one has been disabled
try {

View file

@ -53,10 +53,11 @@ object SubtypeUtilsAdditional {
val prefs = context.prefs()
SubtypeSettings.removeEnabledSubtype(context, subtype)
val oldAdditionalSubtypesString = prefs.getString(Settings.PREF_ADDITIONAL_SUBTYPES, Defaults.PREF_ADDITIONAL_SUBTYPES)!!
val oldAdditionalSubtypes = createAdditionalSubtypes(oldAdditionalSubtypesString)
val newAdditionalSubtypes = oldAdditionalSubtypes.filter { it != subtype }
val newAdditionalSubtypesString = createPrefSubtypes(newAdditionalSubtypes)
Settings.writePrefAdditionalSubtypes(prefs, newAdditionalSubtypesString)
val oldAdditionalSubtypes = SubtypeSettings.createSettingsSubtypes(oldAdditionalSubtypesString)
val settingsSubtype = subtype.toSettingsSubtype()
val newAdditionalSubtypes = oldAdditionalSubtypes.filter { it != settingsSubtype }
val newAdditionalSubtypesString = SubtypeSettings.createPrefSubtypes(newAdditionalSubtypes)
prefs.edit().putString(Settings.PREF_ADDITIONAL_SUBTYPES, newAdditionalSubtypesString).apply()
}
// updates additional subtypes, enabled subtypes, and selected subtype
@ -97,9 +98,6 @@ object SubtypeUtilsAdditional {
else it.toSettingsSubtype().toAdditionalSubtype()
}
fun createPrefSubtypes(subtypes: Collection<InputMethodSubtype>): String =
subtypes.joinToString(Separators.SETS) { it.toSettingsSubtype().toPref() }
private fun getNameResId(locale: Locale, mainLayoutName: String): Int {
val nameId = SubtypeLocaleUtils.getSubtypeNameResId(locale, mainLayoutName)
if (nameId != SubtypeLocaleUtils.UNKNOWN_KEYBOARD_LAYOUT) return nameId