From 084e6a964ee2f2a8499b861e5e3e58938ddab6e9 Mon Sep 17 00:00:00 2001 From: Alessandro Jean <14254807+alessandrojean@users.noreply.github.com> Date: Tue, 30 Aug 2022 16:37:15 -0300 Subject: [PATCH] Enable sub-languages on app first run (#7127) * Enable sub-languages on app first run. * Fix wrong language code string parsing. * Fix some build issues due to rebase. * Use PreferenceHelper and remove an extra preference. * Remove a constant. --- .../data/preference/PreferencesHelper.kt | 3 +- .../tachiyomi/extension/ExtensionManager.kt | 36 +++++++++++++++++++ .../tachiyomi/util/system/LocaleHelper.kt | 13 +++---- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 6981cf3d17..1e20a91894 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -17,6 +17,7 @@ import eu.kanade.tachiyomi.ui.library.setting.LibrarySort import eu.kanade.tachiyomi.ui.reader.setting.OrientationType import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType import eu.kanade.tachiyomi.util.system.DeviceUtil +import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.isDevFlavor import eu.kanade.tachiyomi.widget.ExtendedNavigationView import java.io.File @@ -170,7 +171,7 @@ class PreferencesHelper(val context: Context) { fun sourceDisplayMode() = flowPrefs.getObject("pref_display_mode_catalogue", LibraryDisplayMode.Serializer, LibraryDisplayMode.default) - fun enabledLanguages() = flowPrefs.getStringSet("source_languages", setOf("all", "en", Locale.getDefault().language)) + fun enabledLanguages() = flowPrefs.getStringSet("source_languages", LocaleHelper.getDefaultEnabledLanguages()) fun trackUsername(sync: TrackService) = prefs.getString(Keys.trackUsername(sync.id), "") diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt index f13a6298ad..b2a06ed154 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt @@ -7,6 +7,7 @@ import eu.kanade.domain.source.model.SourceData import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.extension.api.ExtensionGithubApi +import eu.kanade.tachiyomi.extension.model.AvailableSources import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.InstallStep import eu.kanade.tachiyomi.extension.model.LoadResult @@ -27,6 +28,7 @@ import logcat.LogPriority import rx.Observable import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import java.util.Locale /** * The manager of extensions installed as another apk which extend the available sources. It handles @@ -72,6 +74,8 @@ class ExtensionManager( private val installedExtensionsFlow = MutableStateFlow(installedExtensions) + private var subLanguagesEnabledOnFirstRun = preferences.enabledLanguages().isSet() + fun getInstalledExtensionsFlow(): StateFlow> { return installedExtensionsFlow.asStateFlow() } @@ -163,9 +167,41 @@ class ExtensionManager( emptyList() } + enableAdditionalSubLanguages(extensions) + availableExtensions = extensions } + /** + * Enables the additional sub-languages in the app first run. This addresses + * the issue where users still need to enable some specific languages even when + * the device language is inside that major group. As an example, if a user + * has a zh device language, the app will also enable zh-Hans and zh-Hant. + * + * If the user have already changed the enabledLanguages preference value once, + * the new languages will not be added to respect the user enabled choices. + */ + private fun enableAdditionalSubLanguages(extensions: List) { + if (subLanguagesEnabledOnFirstRun || extensions.isEmpty()) { + return + } + + // Use the source lang as some aren't present on the extension level. + val availableLanguages = extensions + .flatMap(Extension.Available::sources) + .distinctBy(AvailableSources::lang) + .map(AvailableSources::lang) + + val deviceLanguage = Locale.getDefault().language + val defaultLanguages = preferences.enabledLanguages().defaultValue + val languagesToEnable = availableLanguages.filter { + it != deviceLanguage && it.startsWith(deviceLanguage) + } + + preferences.enabledLanguages().set(defaultLanguages + languagesToEnable) + subLanguagesEnabledOnFirstRun = true + } + /** * Sets the update field of the installed extensions with the given [availableExtensions]. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/LocaleHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/LocaleHelper.kt index 40d9c95555..8925c8f1a2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/LocaleHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/LocaleHelper.kt @@ -37,20 +37,15 @@ object LocaleHelper { val locale = if (lang.isEmpty()) { LocaleListCompat.getAdjustedDefault()[0] } else { - getLocale(lang) + Locale.forLanguageTag(lang) } return locale!!.getDisplayName(locale).replaceFirstChar { it.uppercase(locale) } } /** - * Return Locale from string language code + * Return the default languages enabled for the sources. */ - private fun getLocale(lang: String): Locale { - val sp = lang.split("_", "-") - return when (sp.size) { - 2 -> Locale(sp[0], sp[1]) - 3 -> Locale(sp[0], sp[1], sp[2]) - else -> Locale(lang) - } + fun getDefaultEnabledLanguages(): Set { + return setOf("all", "en", Locale.getDefault().language) } }