Add system languages as subtypes when blank

This commit is contained in:
Aleksandras Kostarevas 2024-05-14 17:29:29 -05:00
parent 7570e4d258
commit 97859147d9
3 changed files with 37 additions and 4 deletions

View File

@ -221,6 +221,8 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save
override fun onCreate() {
super.onCreate()
Subtypes.addDefaultSubtypesIfNecessary(this)
languageModelFacilitator = LanguageModelFacilitator(
this,
latinIMELegacy.mInputLogic,

View File

@ -39,6 +39,7 @@ import org.futo.inputmethod.latin.uix.setSettingBlocking
import org.futo.inputmethod.latin.uix.settings.NavigationItem
import org.futo.inputmethod.latin.uix.settings.NavigationItemStyle
import org.futo.inputmethod.latin.uix.settings.SettingsActivity
import org.futo.inputmethod.latin.uix.settings.pages.LocaleLayoutMap
import org.futo.inputmethod.latin.uix.settings.useDataStoreValueBlocking
import org.futo.inputmethod.latin.uix.theme.Typography
import org.futo.inputmethod.latin.utils.SubtypeLocaleUtils
@ -55,6 +56,36 @@ val ActiveSubtype = SettingsKey(
)
object Subtypes {
fun addDefaultSubtypesIfNecessary(context: Context) {
val currentSubtypes = context.getSettingBlocking(SubtypesSetting)
if(currentSubtypes.isNotEmpty()) return
val locales = context.resources.configuration.locales
if(locales.size() == 0) return
for(i in 0 until locales.size()) {
val locale = locales.get(i)
val layout = findClosestLocaleLayouts(locale).firstOrNull() ?: continue
addLanguage(context, locale, layout)
}
context.setSettingBlocking(ActiveSubtype.key, context.getSettingBlocking(SubtypesSetting).first())
}
fun findClosestLocaleLayouts(locale: Locale): List<String> {
val supportedLocales = LocaleLayoutMap.toList().associate {
getLocale(it.first) to it.second
}
val perfectMatch = supportedLocales.keys.find { it.language == locale.language && it.country == locale.country }
val languageMatch = supportedLocales.keys.find { it.language == locale.language }
val match = perfectMatch ?: languageMatch
return match?.let { supportedLocales[it] } ?: listOf()
}
fun convertToSubtype(string: String): InputMethodSubtype {
val splits = string.split(":")
val locale = splits[0]

View File

@ -23,7 +23,7 @@ import org.futo.inputmethod.latin.uix.settings.SettingItem
val QwertyVariants = listOf("qwerty", "qwertz", "dvorak", "azerty", "colemak", "bepo", "pcqwerty")
val locales = mapOf(
val LocaleLayoutMap = mapOf(
"af" to listOf("qwerty"),
"ar" to listOf("arabic"),
"az_AZ" to listOf("qwerty"),
@ -114,7 +114,7 @@ fun AddLanguageScreen(navController: NavHostController = rememberNavController()
val selectedLocale: MutableState<String> = remember { mutableStateOf(context.resources.configuration.locale.toString()) }
val selectedLayout: MutableState<String> = remember { mutableStateOf("qwerty") }
val keys = remember { locales.keys.toList() }
val keys = remember { LocaleLayoutMap.keys.toList() }
ScrollableList {
ScreenTitle("Add Language", showBack = true, navController)
@ -125,7 +125,7 @@ fun AddLanguageScreen(navController: NavHostController = rememberNavController()
selectedLocale.value,
{
selectedLocale.value = it
selectedLayout.value = locales[it]!!.first()
selectedLayout.value = LocaleLayoutMap[it]!!.first()
},
{
Subtypes.getNameForLocale(it)
@ -137,7 +137,7 @@ fun AddLanguageScreen(navController: NavHostController = rememberNavController()
SettingItem(title = "Layout") {
DropDownPicker(
"",
locales[selectedLocale.value] ?: listOf(),
LocaleLayoutMap[selectedLocale.value] ?: listOf(),
selectedLayout.value,
{ selectedLayout.value = it },
{ Subtypes.getLayoutName(context, it) },