From 89678ebb17ad62b3b0118e13d7ab431f5ead8c46 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 6 Jan 2024 17:53:31 -0500 Subject: [PATCH] Show non-localized language names too in app language selection --- .../screen/SettingsAppearanceScreen.kt | 61 +-------- .../screen/appearance/AppLanguageScreen.kt | 128 ++++++++++++++++++ 2 files changed, 134 insertions(+), 55 deletions(-) create mode 100644 app/src/main/java/eu/kanade/presentation/more/settings/screen/appearance/AppLanguageScreen.kt diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt index 365e86b6fb..d1474bf6a0 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt @@ -1,34 +1,26 @@ package eu.kanade.presentation.more.settings.screen import android.app.Activity -import android.content.Context -import androidx.appcompat.app.AppCompatDelegate import androidx.compose.foundation.layout.Column import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.ReadOnlyComposable import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import androidx.core.app.ActivityCompat -import androidx.core.os.LocaleListCompat +import cafe.adriel.voyager.navigator.LocalNavigator +import cafe.adriel.voyager.navigator.currentOrThrow import eu.kanade.domain.ui.UiPreferences import eu.kanade.domain.ui.model.TabletUiMode import eu.kanade.domain.ui.model.ThemeMode import eu.kanade.domain.ui.model.setAppCompatDelegateThemeMode import eu.kanade.presentation.more.settings.Preference +import eu.kanade.presentation.more.settings.screen.appearance.AppLanguageScreen import eu.kanade.presentation.more.settings.widget.AppThemeModePreferenceWidget import eu.kanade.presentation.more.settings.widget.AppThemePreferenceWidget -import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.toast -import kotlinx.collections.immutable.ImmutableMap import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toImmutableMap -import org.xmlpull.v1.XmlPullParser -import tachiyomi.core.i18n.stringResource import tachiyomi.i18n.MR import tachiyomi.presentation.core.i18n.stringResource import tachiyomi.presentation.core.util.collectAsState @@ -107,11 +99,8 @@ object SettingsAppearanceScreen : SearchableSettings { uiPreferences: UiPreferences, ): Preference.PreferenceGroup { val context = LocalContext.current + val navigator = LocalNavigator.currentOrThrow - val langs = remember { getLangs(context) } - var currentLanguage by remember { - mutableStateOf(AppCompatDelegate.getApplicationLocales().get(0)?.toLanguageTag() ?: "") - } val now = remember { Instant.now().toEpochMilli() } val dateFormat by uiPreferences.dateFormat().collectAsState() @@ -119,26 +108,12 @@ object SettingsAppearanceScreen : SearchableSettings { UiPreferences.dateFormat(dateFormat).format(now) } - LaunchedEffect(currentLanguage) { - val locale = if (currentLanguage.isEmpty()) { - LocaleListCompat.getEmptyLocaleList() - } else { - LocaleListCompat.forLanguageTags(currentLanguage) - } - AppCompatDelegate.setApplicationLocales(locale) - } - return Preference.PreferenceGroup( title = stringResource(MR.strings.pref_category_display), preferenceItems = persistentListOf( - Preference.PreferenceItem.BasicListPreference( - value = currentLanguage, + Preference.PreferenceItem.TextPreference( title = stringResource(MR.strings.pref_app_language), - entries = langs, - onValueChanged = { newValue -> - currentLanguage = newValue - true - }, + onClick = { navigator.push(AppLanguageScreen()) }, ), Preference.PreferenceItem.ListPreference( pref = uiPreferences.tabletUiMode(), @@ -173,30 +148,6 @@ object SettingsAppearanceScreen : SearchableSettings { ), ) } - private fun getLangs(context: Context): ImmutableMap { - val langs = mutableListOf>() - val parser = context.resources.getXml(R.xml.locales_config) - var eventType = parser.eventType - while (eventType != XmlPullParser.END_DOCUMENT) { - if (eventType == XmlPullParser.START_TAG && parser.name == "locale") { - for (i in 0.. + AppBar( + title = stringResource(MR.strings.pref_app_language), + navigateUp = navigator::pop, + scrollBehavior = scrollBehavior, + ) + }, + ) { contentPadding -> + LazyColumn( + modifier = Modifier.padding(contentPadding), + ) { + items(langs) { + ListItem( + modifier = Modifier.clickable { + currentLanguage = it.langTag + }, + headlineContent = { Text(it.displayName) }, + supportingContent = { + it.localizedDisplayName?.let { + Text(it) + } + }, + trailingContent = { + if (currentLanguage == it.langTag) { + Icon( + imageVector = Icons.Default.Check, + contentDescription = null, + tint = MaterialTheme.colorScheme.primary, + ) + } + }, + ) + } + } + } + } + + private fun getLangs(context: Context): ImmutableList { + val langs = mutableListOf() + val parser = context.resources.getXml(R.xml.locales_config) + var eventType = parser.eventType + while (eventType != XmlPullParser.END_DOCUMENT) { + if (eventType == XmlPullParser.START_TAG && parser.name == "locale") { + for (i in 0..