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.
This commit is contained in:
Alessandro Jean 2022-08-30 16:37:15 -03:00 committed by GitHub
parent 532f662b05
commit 084e6a964e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 10 deletions

View File

@ -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), "")

View File

@ -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<List<Extension.Installed>> {
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<Extension.Available>) {
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].
*

View File

@ -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<String> {
return setOf("all", "en", Locale.getDefault().language)
}
}