diff --git a/java/src/org/futo/inputmethod/latin/LatinIME.kt b/java/src/org/futo/inputmethod/latin/LatinIME.kt index 183b136ce..4b8fd2d58 100644 --- a/java/src/org/futo/inputmethod/latin/LatinIME.kt +++ b/java/src/org/futo/inputmethod/latin/LatinIME.kt @@ -221,6 +221,8 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save override fun onCreate() { super.onCreate() + Subtypes.addDefaultSubtypesIfNecessary(this) + languageModelFacilitator = LanguageModelFacilitator( this, latinIMELegacy.mInputLogic, diff --git a/java/src/org/futo/inputmethod/latin/Subtypes.kt b/java/src/org/futo/inputmethod/latin/Subtypes.kt index 9ea2120f7..3690ac479 100644 --- a/java/src/org/futo/inputmethod/latin/Subtypes.kt +++ b/java/src/org/futo/inputmethod/latin/Subtypes.kt @@ -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 { + 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] diff --git a/java/src/org/futo/inputmethod/latin/uix/settings/pages/AddLanguage.kt b/java/src/org/futo/inputmethod/latin/uix/settings/pages/AddLanguage.kt index c27a2bf83..917ce30e1 100644 --- a/java/src/org/futo/inputmethod/latin/uix/settings/pages/AddLanguage.kt +++ b/java/src/org/futo/inputmethod/latin/uix/settings/pages/AddLanguage.kt @@ -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 = remember { mutableStateOf(context.resources.configuration.locale.toString()) } val selectedLayout: MutableState = 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) },