fix crash when changing setting in android api < 13

This commit is contained in:
greenart7c3
2023-07-14 05:51:09 -03:00
parent 2e7a955ada
commit e648970b17

View File

@@ -40,7 +40,9 @@ import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer
import com.vitorpamplona.amethyst.ui.theme.StdPadding import com.vitorpamplona.amethyst.ui.theme.StdPadding
import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.xmlpull.v1.XmlPullParser import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException import org.xmlpull.v1.XmlPullParserException
@@ -80,12 +82,19 @@ fun Context.getLangPreferenceDropdownEntries(): Map<String, String> {
} }
fun getLanguageIndex(languageEntries: Map<String, String>): Int { fun getLanguageIndex(languageEntries: Map<String, String>): Int {
var languageIndex = languageEntries.values.toTypedArray().indexOf(Locale.current.toLanguageTag()) val language = LocalPreferences.getPreferredLanguage()
var languageIndex = -1
if (language.isNotBlank()) {
languageIndex = languageEntries.values.toTypedArray().indexOf(language)
} else {
languageIndex = languageEntries.values.toTypedArray().indexOf(Locale.current.toLanguageTag())
}
if (languageIndex == -1) languageIndex = languageEntries.values.toTypedArray().indexOf(Locale.current.language) if (languageIndex == -1) languageIndex = languageEntries.values.toTypedArray().indexOf(Locale.current.language)
if (languageIndex == -1) languageIndex = languageEntries.values.toTypedArray().indexOf("en") if (languageIndex == -1) languageIndex = languageEntries.values.toTypedArray().indexOf("en")
return languageIndex return languageIndex
} }
@OptIn(DelicateCoroutinesApi::class)
@Composable @Composable
fun SettingsScreen( fun SettingsScreen(
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
@@ -139,10 +148,13 @@ fun SettingsScreen(
placeholder = languageList[languageIndex], placeholder = languageList[languageIndex],
options = languageList, options = languageList,
onSelect = { onSelect = {
scope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.Main) {
val job = scope.launch(Dispatchers.IO) {
val locale = languageEntries[languageList[it]] val locale = languageEntries[languageList[it]]
accountViewModel.account.settings.preferredLanguage = locale accountViewModel.account.settings.preferredLanguage = locale
LocalPreferences.saveToEncryptedStorage(accountViewModel.account) LocalPreferences.saveToEncryptedStorage(accountViewModel.account)
}
job.join()
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(languageEntries[languageList[it]]) val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(languageEntries[languageList[it]])
AppCompatDelegate.setApplicationLocales(appLocale) AppCompatDelegate.setApplicationLocales(appLocale)
} }