toAdditional subtype not nullable any more

was causing too much trouble
and only was in there because "it always has been there"
This commit is contained in:
Helium314 2025-03-08 07:48:15 +01:00
parent fa9ac20d39
commit 15c1526895
4 changed files with 13 additions and 34 deletions

View file

@ -11,11 +11,9 @@ import helium314.keyboard.latin.common.LocaleUtils.constructLocale
import helium314.keyboard.latin.define.DebugFlags import helium314.keyboard.latin.define.DebugFlags
import helium314.keyboard.latin.utils.LayoutType import helium314.keyboard.latin.utils.LayoutType
import helium314.keyboard.latin.utils.LayoutType.Companion.toExtraValue import helium314.keyboard.latin.utils.LayoutType.Companion.toExtraValue
import helium314.keyboard.latin.utils.LayoutUtilsCustom
import helium314.keyboard.latin.utils.Log import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.ScriptUtils import helium314.keyboard.latin.utils.ScriptUtils
import helium314.keyboard.latin.utils.ScriptUtils.script import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.SubtypeLocaleUtils
import helium314.keyboard.latin.utils.SubtypeUtilsAdditional import helium314.keyboard.latin.utils.SubtypeUtilsAdditional
import helium314.keyboard.latin.utils.locale import helium314.keyboard.latin.utils.locale
import java.util.Locale import java.util.Locale
@ -27,23 +25,9 @@ data class SettingsSubtype(val locale: Locale, val extraValues: String) {
/** Creates an additional subtype from the SettingsSubtype. /** Creates an additional subtype from the SettingsSubtype.
* Resulting InputMethodSubtypes are equal if SettingsSubtypes are equal */ * Resulting InputMethodSubtypes are equal if SettingsSubtypes are equal */
fun toAdditionalSubtype(): InputMethodSubtype? { fun toAdditionalSubtype(): InputMethodSubtype {
val asciiCapable = locale.script() == ScriptUtils.SCRIPT_LATIN val asciiCapable = locale.script() == ScriptUtils.SCRIPT_LATIN
val subtype = SubtypeUtilsAdditional.createAdditionalSubtype(locale, extraValues, asciiCapable, true) return SubtypeUtilsAdditional.createAdditionalSubtype(locale, extraValues, asciiCapable, true)
// todo: this is returns null for all non-latin layouts
// either fix it, or remove the check
// if removed, removing a layout will result in fallback qwerty even for non-ascii, but this is better than the current alternative
/* if (subtype.nameResId == SubtypeLocaleUtils.UNKNOWN_KEYBOARD_LAYOUT
&& mainLayoutName()?.endsWith("+") != true // "+" layouts and custom layouts are always "unknown"
&& !LayoutUtilsCustom.isCustomLayout(mainLayoutName() ?: SubtypeLocaleUtils.QWERTY)
) {
// Skip unknown keyboard layout subtype. This may happen when predefined keyboard
// layout has been removed.
Log.w(SettingsSubtype::class.simpleName, "unknown additional subtype $this")
return null
}*/
return subtype
} }
fun mainLayoutName() = LayoutType.getMainLayoutFromExtraValue(extraValues) fun mainLayoutName() = LayoutType.getMainLayoutFromExtraValue(extraValues)

View file

@ -74,10 +74,8 @@ object SubtypeSettings {
fun getSelectedSubtype(prefs: SharedPreferences): InputMethodSubtype { fun getSelectedSubtype(prefs: SharedPreferences): InputMethodSubtype {
val selectedSubtype = prefs.getString(Settings.PREF_SELECTED_SUBTYPE, Defaults.PREF_SELECTED_SUBTYPE)!!.toSettingsSubtype() val selectedSubtype = prefs.getString(Settings.PREF_SELECTED_SUBTYPE, Defaults.PREF_SELECTED_SUBTYPE)!!.toSettingsSubtype()
if (selectedSubtype.isAdditionalSubtype(prefs)) { if (selectedSubtype.isAdditionalSubtype(prefs))
val selectedAdditionalSubtype = selectedSubtype.toAdditionalSubtype() return selectedSubtype.toAdditionalSubtype()
if (selectedAdditionalSubtype != null) return selectedAdditionalSubtype
}
// no additional subtype, must be a resource subtype // no additional subtype, must be a resource subtype
val subtype = enabledSubtypes.firstOrNull { it.toSettingsSubtype() == selectedSubtype } val subtype = enabledSubtypes.firstOrNull { it.toSettingsSubtype() == selectedSubtype }
@ -224,12 +222,9 @@ object SubtypeSettings {
.split(Separators.SETS).filter { it.isNotEmpty() }.map { it.toSettingsSubtype() } .split(Separators.SETS).filter { it.isNotEmpty() }.map { it.toSettingsSubtype() }
for (settingsSubtype in settingsSubtypes) { for (settingsSubtype in settingsSubtypes) {
if (settingsSubtype.isAdditionalSubtype(prefs)) { if (settingsSubtype.isAdditionalSubtype(prefs)) {
val additionalSubtype = settingsSubtype.toAdditionalSubtype() enabledSubtypes.add(settingsSubtype.toAdditionalSubtype())
if (additionalSubtype != null) {
enabledSubtypes.add(additionalSubtype)
continue continue
} }
}
val subtypesForLocale = resourceSubtypesByLocale[settingsSubtype.locale] val subtypesForLocale = resourceSubtypesByLocale[settingsSubtype.locale]
if (subtypesForLocale == null) { if (subtypesForLocale == null) {
val message = "no resource subtype for $settingsSubtype" val message = "no resource subtype for $settingsSubtype"

View file

@ -72,12 +72,12 @@ object SubtypeUtilsAdditional {
} + to.toPref() } + to.toPref()
prefs.edit().putString(Settings.PREF_ADDITIONAL_SUBTYPES, new.joinToString(Separators.SETS)).apply() prefs.edit().putString(Settings.PREF_ADDITIONAL_SUBTYPES, new.joinToString(Separators.SETS)).apply()
val fromSubtype = from.toAdditionalSubtype() // will be null if we edit a resource subtype val fromSubtype = from.toAdditionalSubtype()
val toSubtype = to.toAdditionalSubtype() // should never be null val toSubtype = to.toAdditionalSubtype()
if (isSelected && toSubtype != null) { if (isSelected) {
SubtypeSettings.setSelectedSubtype(prefs, toSubtype) SubtypeSettings.setSelectedSubtype(prefs, toSubtype)
} }
if (fromSubtype != null && isEnabled && toSubtype != null) { if (isEnabled) {
SubtypeSettings.removeEnabledSubtype(context, fromSubtype) SubtypeSettings.removeEnabledSubtype(context, fromSubtype)
SubtypeSettings.addEnabledSubtype(prefs, toSubtype) SubtypeSettings.addEnabledSubtype(prefs, toSubtype)
} }
@ -88,7 +88,7 @@ object SubtypeUtilsAdditional {
fun createAdditionalSubtypes(prefSubtypes: String): List<InputMethodSubtype> { fun createAdditionalSubtypes(prefSubtypes: String): List<InputMethodSubtype> {
if (prefSubtypes.isEmpty()) if (prefSubtypes.isEmpty())
return emptyList() return emptyList()
return prefSubtypes.split(Separators.SETS).mapNotNull { it.toSettingsSubtype().toAdditionalSubtype() } return prefSubtypes.split(Separators.SETS).map { it.toSettingsSubtype().toAdditionalSubtype() }
} }
fun createPrefSubtypes(subtypes: Collection<InputMethodSubtype>): String { fun createPrefSubtypes(subtypes: Collection<InputMethodSubtype>): String {

View file

@ -125,8 +125,8 @@ fun SubtypeDialog(
onConfirmed = { onConfirmed(currentSubtype) }, onConfirmed = { onConfirmed(currentSubtype) },
neutralButtonText = if (initialSubtype.isAdditionalSubtype(prefs)) stringResource(R.string.delete) else null, neutralButtonText = if (initialSubtype.isAdditionalSubtype(prefs)) stringResource(R.string.delete) else null,
onNeutral = { onNeutral = {
SubtypeUtilsAdditional.removeAdditionalSubtype(ctx, initialSubtype.toAdditionalSubtype()!!) SubtypeUtilsAdditional.removeAdditionalSubtype(ctx, initialSubtype.toAdditionalSubtype())
SubtypeSettings.removeEnabledSubtype(ctx, initialSubtype.toAdditionalSubtype()!!) SubtypeSettings.removeEnabledSubtype(ctx, initialSubtype.toAdditionalSubtype())
onDismissRequest() onDismissRequest()
}, },
title = { title = {