Remove Language class. App's language and hidden languages settings were reset

This commit is contained in:
len 2016-12-26 15:44:59 +01:00
parent 97fa659283
commit 1a3a1db4ff
23 changed files with 66 additions and 118 deletions

View File

@ -82,7 +82,7 @@ class PreferencesHelper(val context: Context) {
fun catalogueAsList() = rxPrefs.getBoolean(keys.catalogueAsList, false)
fun enabledLanguages() = rxPrefs.getStringSet(keys.enabledLanguages, setOf("EN"))
fun enabledLanguages() = rxPrefs.getStringSet(keys.enabledLanguages, setOf("en"))
fun sourceUsername(source: Source) = prefs.getString(keys.sourceUsername(source.id), "")
@ -142,6 +142,6 @@ class PreferencesHelper(val context: Context) {
fun downloadNew() = prefs.getBoolean(keys.downloadNew, false)
fun lang() = prefs.getInt(keys.lang, 0)
fun lang() = prefs.getString(keys.lang, "")
}

View File

@ -1,9 +0,0 @@
package eu.kanade.tachiyomi.data.source
class Language(val code: String, val lang: String)
val DE = Language("DE", "German")
val EN = Language("EN", "English")
val RU = Language("RU", "Russian")
fun getLanguages() = listOf(DE, EN, RU)

View File

@ -9,7 +9,6 @@ import eu.kanade.tachiyomi.data.network.NetworkHelper
import eu.kanade.tachiyomi.data.network.asObservableSuccess
import eu.kanade.tachiyomi.data.network.newCallWithProgress
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.Source
import eu.kanade.tachiyomi.data.source.model.MangasPage
import eu.kanade.tachiyomi.data.source.model.Page
@ -47,9 +46,9 @@ abstract class OnlineSource() : Source {
abstract val baseUrl: String
/**
* Language of the source.
* An ISO 639-1 compliant language code (two characters in lower case).
*/
abstract val lang: Language
abstract val lang: String
/**
* Whether the source has support for latest updates.
@ -82,7 +81,7 @@ abstract class OnlineSource() : Source {
/**
* Visible name of the source.
*/
override fun toString() = "$name (${lang.code})"
override fun toString() = "$name (${lang.toUpperCase()})"
/**
* Returns an observable containing a page with a list of manga. Normally it's not needed to

View File

@ -4,7 +4,6 @@ import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.network.GET
import eu.kanade.tachiyomi.data.network.POST
import eu.kanade.tachiyomi.data.source.getLanguages
import eu.kanade.tachiyomi.data.source.model.MangasPage
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.util.asJsoup
@ -27,9 +26,7 @@ class YamlOnlineSource(mappings: Map<*, *>) : OnlineSource() {
if (it.endsWith("/")) it.dropLast(1) else it
}
override val lang = map.lang.toUpperCase().let { code ->
getLanguages().find { code == it.code }!!
}
override val lang = map.lang.toLowerCase()
override val supportsLatest = map.latestupdates != null
@ -39,7 +36,7 @@ class YamlOnlineSource(mappings: Map<*, *>) : OnlineSource() {
}
override val id = map.id.let {
if (it is Int) it else (lang.code.hashCode() + 31 * it.hashCode()) and 0x7fffffff
if (it is Int) it else (lang.toUpperCase().hashCode() + 31 * it.hashCode()) and 0x7fffffff
}
override fun popularMangaRequest(page: MangasPage): Request {

View File

@ -7,8 +7,6 @@ import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.network.GET
import eu.kanade.tachiyomi.data.network.POST
import eu.kanade.tachiyomi.data.network.asObservable
import eu.kanade.tachiyomi.data.source.EN
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.model.MangasPage
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.data.source.online.LoginSource
@ -33,7 +31,7 @@ class Batoto(override val id: Int) : ParsedOnlineSource(), LoginSource {
override val baseUrl = "http://bato.to"
override val lang: Language get() = EN
override val lang = "en"
override val supportsLatest = true

View File

@ -1,12 +1,9 @@
package eu.kanade.tachiyomi.data.source.online.english
import android.content.Context
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.network.GET
import eu.kanade.tachiyomi.data.network.POST
import eu.kanade.tachiyomi.data.source.EN
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.model.MangasPage
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.data.source.online.ParsedOnlineSource
@ -25,7 +22,7 @@ class Kissmanga(override val id: Int) : ParsedOnlineSource() {
override val baseUrl = "http://kissmanga.com"
override val lang: Language get() = EN
override val lang = "en"
override val supportsLatest = true

View File

@ -2,8 +2,6 @@ package eu.kanade.tachiyomi.data.source.online.english
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.source.EN
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.data.source.online.ParsedOnlineSource
import eu.kanade.tachiyomi.util.asJsoup
@ -20,7 +18,7 @@ class Mangafox(override val id: Int) : ParsedOnlineSource() {
override val baseUrl = "http://mangafox.me"
override val lang: Language get() = EN
override val lang = "en"
override val supportsLatest = true

View File

@ -2,8 +2,6 @@ package eu.kanade.tachiyomi.data.source.online.english
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.source.EN
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.data.source.online.ParsedOnlineSource
import org.jsoup.nodes.Document
@ -18,7 +16,7 @@ class Mangahere(override val id: Int) : ParsedOnlineSource() {
override val baseUrl = "http://www.mangahere.co"
override val lang: Language get() = EN
override val lang = "en"
override val supportsLatest = true

View File

@ -3,8 +3,6 @@ package eu.kanade.tachiyomi.data.source.online.english
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.network.POST
import eu.kanade.tachiyomi.data.source.EN
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.model.MangasPage
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.data.source.online.ParsedOnlineSource
@ -24,7 +22,7 @@ class Mangasee(override val id: Int) : ParsedOnlineSource() {
override val baseUrl = "http://mangaseeonline.net"
override val lang: Language get() = EN
override val lang = "en"
override val supportsLatest = true

View File

@ -1,11 +1,8 @@
package eu.kanade.tachiyomi.data.source.online.english
import android.content.Context
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.network.POST
import eu.kanade.tachiyomi.data.source.EN
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.model.MangasPage
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.data.source.online.OnlineSource
@ -23,7 +20,7 @@ class Readmangatoday(override val id: Int) : ParsedOnlineSource() {
override val baseUrl = "http://www.readmanga.today"
override val lang: Language get() = EN
override val lang = "en"
override val supportsLatest = true

View File

@ -1,10 +1,7 @@
package eu.kanade.tachiyomi.data.source.online.german
import android.content.Context
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.source.DE
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.data.source.online.ParsedOnlineSource
import eu.kanade.tachiyomi.util.asJsoup
@ -19,7 +16,7 @@ class WieManga(override val id: Int) : ParsedOnlineSource() {
override val baseUrl = "http://www.wiemanga.com"
override val lang: Language get() = DE
override val lang = "de"
override val supportsLatest = true

View File

@ -2,8 +2,6 @@ package eu.kanade.tachiyomi.data.source.online.russian
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.RU
import eu.kanade.tachiyomi.data.source.model.MangasPage
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.data.source.online.ParsedOnlineSource
@ -20,7 +18,7 @@ class Mangachan(override val id: Int) : ParsedOnlineSource() {
override val baseUrl = "http://mangachan.me"
override val lang: Language get() = RU
override val lang = "ru"
override val supportsLatest = true

View File

@ -2,8 +2,6 @@ package eu.kanade.tachiyomi.data.source.online.russian
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.RU
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.data.source.online.ParsedOnlineSource
import okhttp3.Response
@ -19,7 +17,7 @@ class Mintmanga(override val id: Int) : ParsedOnlineSource() {
override val baseUrl = "http://mintmanga.com"
override val lang: Language get() = RU
override val lang = "ru"
override val supportsLatest = true

View File

@ -2,8 +2,6 @@ package eu.kanade.tachiyomi.data.source.online.russian
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.RU
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.data.source.online.ParsedOnlineSource
import okhttp3.Response
@ -19,7 +17,7 @@ class Readmanga(override val id: Int) : ParsedOnlineSource() {
override val baseUrl = "http://readmanga.me"
override val lang: Language get() = RU
override val lang = "ru"
override val supportsLatest = true

View File

@ -6,7 +6,6 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.source.EN
import eu.kanade.tachiyomi.data.source.Source
import eu.kanade.tachiyomi.data.source.SourceManager
import eu.kanade.tachiyomi.data.source.model.MangasPage
@ -333,13 +332,13 @@ open class CataloguePresenter : BasePresenter<CatalogueFragment>() {
// Ensure at least one language
if (languages.isEmpty()) {
languages.add(EN.code)
languages.add("en")
}
return sourceManager.getOnlineSources()
.filter { it.lang.code in languages }
.filter { it.lang in languages }
.filterNot { it.id.toString() in hiddenCatalogues }
.sortedBy { "(${it.lang.code}) ${it.name}" }
.sortedBy { "(${it.lang}) ${it.name}" }
}
/**

View File

@ -14,6 +14,7 @@ import eu.kanade.tachiyomi.util.plusAssign
import eu.kanade.tachiyomi.widget.preference.IntListPreference
import eu.kanade.tachiyomi.widget.preference.LibraryColumnsDialog
import eu.kanade.tachiyomi.widget.preference.SimpleDialogPreference
import net.xpece.android.support.preference.ListPreference
import net.xpece.android.support.preference.MultiSelectListPreference
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
@ -45,7 +46,7 @@ class SettingsGeneralFragment : SettingsFragment(),
val categoryUpdate: MultiSelectListPreference by bindPref(R.string.pref_library_update_categories_key)
val langPreference: IntListPreference by bindPref(R.string.pref_language_key)
val langPreference: ListPreference by bindPref(R.string.pref_language_key)
override fun onViewCreated(view: View, savedState: Bundle?) {
super.onViewCreated(view, savedState)
@ -105,9 +106,15 @@ class SettingsGeneralFragment : SettingsFragment(),
true
}
val langValues = langPreference.entryValues.map { value ->
val locale = LocaleHelper.getLocaleFromString(value.toString())
locale?.getDisplayName(locale)?.capitalize() ?: context.getString(R.string.system_default)
}
langPreference.entries = langValues.toTypedArray()
langPreference.setOnPreferenceChangeListener { preference, newValue ->
(activity as SettingsActivity).parentFlags = SettingsActivity.FLAG_LANG_CHANGED
LocaleHelper.changeLocale(newValue.toString().toInt())
LocaleHelper.changeLocale(newValue.toString())
LocaleHelper.updateCfg(activity.application, activity.baseContext.resources.configuration)
activity.recreate()
true

View File

@ -8,13 +8,14 @@ import android.view.View
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.source.SourceManager
import eu.kanade.tachiyomi.data.source.getLanguages
import eu.kanade.tachiyomi.data.source.online.OnlineSource
import eu.kanade.tachiyomi.widget.preference.LoginCheckBoxPreference
import eu.kanade.tachiyomi.widget.preference.SourceLoginDialog
import eu.kanade.tachiyomi.widget.preference.SwitchPreferenceCategory
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.util.*
class SettingsSourcesFragment : SettingsFragment() {
@ -45,33 +46,35 @@ class SettingsSourcesFragment : SettingsFragment() {
// Get the list of active language codes.
val activeLangsCodes = preferences.enabledLanguages().getOrDefault()
// Get the list of languages ordered by name.
val langs = getLanguages().sortedBy { it.lang }
// Get a map of sources grouped by language.
val sourcesByLang = onlineSources.groupByTo(TreeMap(), { it.lang })
// Order first by active languages, then inactive ones
val orderedLangs = langs.filter { it.code in activeLangsCodes } +
langs.filterNot { it.code in activeLangsCodes }
val orderedLangs = sourcesByLang.keys.filter { it in activeLangsCodes } +
sourcesByLang.keys.filterNot { it in activeLangsCodes }
orderedLangs.forEach { lang ->
val sources = sourcesByLang[lang].orEmpty().sortedBy { it.name }
// Create a preference group and set initial state and change listener
SwitchPreferenceCategory(context).apply {
preferenceScreen.addPreference(this)
title = lang.lang
title = Locale(lang).let { it.getDisplayLanguage(it).capitalize() }
isPersistent = false
if (lang.code in activeLangsCodes) {
if (lang in activeLangsCodes) {
setChecked(true)
addLanguageSources(this)
addLanguageSources(this, sources)
}
setOnPreferenceChangeListener { preference, any ->
val checked = any as Boolean
val current = preferences.enabledLanguages().getOrDefault()
if (!checked) {
preferences.enabledLanguages().set(current - lang.code)
preferences.enabledLanguages().set(current - lang)
removeAll()
} else {
preferences.enabledLanguages().set(current + lang.code)
addLanguageSources(this)
preferences.enabledLanguages().set(current + lang)
addLanguageSources(this, sources)
}
true
}
@ -84,8 +87,7 @@ class SettingsSourcesFragment : SettingsFragment() {
*
* @param group the language category.
*/
private fun addLanguageSources(group: SwitchPreferenceCategory) {
val sources = onlineSources.filter { it.lang.lang == group.title }.sortedBy { it.name }
private fun addLanguageSources(group: SwitchPreferenceCategory, sources: List<OnlineSource>) {
val hiddenCatalogues = preferences.hiddenCatalogues().getOrDefault()
sources.forEach { source ->

View File

@ -30,24 +30,21 @@ object LocaleHelper {
/**
* The application's locale. When it's null, the system locale is used.
*/
private var appLocale = getLocaleFromCode(preferences.lang())
private var appLocale = getLocaleFromString(preferences.lang())
/**
* Returns the locale for the value stored in preferences, or null if system language or unknown
* value is selected.
* Returns the locale for the value stored in preferences, or null if it's system language.
*
* @param pref the int value stored in preferences.
* @param pref the string value stored in preferences.
*/
private fun getLocaleFromCode(pref: Int): Locale? {
val code = when(pref) {
1 -> "en"
2 -> "es"
3 -> "it"
4 -> "pt"
else -> return null
fun getLocaleFromString(pref: String): Locale? {
if (pref.isNullOrEmpty()) {
return null
}
return Locale(code)
val parts = pref.split("_", "-")
val lang = parts[0]
val country = parts.getOrNull(1) ?: ""
return Locale(lang, country)
}
/**
@ -55,8 +52,8 @@ object LocaleHelper {
*
* @param pref the new value stored in preferences.
*/
fun changeLocale(pref: Int) {
appLocale = getLocaleFromCode(pref)
fun changeLocale(pref: String) {
appLocale = getLocaleFromString(pref)
}
/**

View File

@ -112,13 +112,7 @@
<string name="dark_theme">Tema scuro</string>
<string name="pref_start_screen">Schermata iniziale</string>
<string name="pref_language">Lingua</string>
<!-- Languages -->
<string name="system_default">Predefinita di sistema</string>
<string name="english">Inglese</string>
<string name="spanish">Spagnolo</string>
<string name="italian">Italiano</string>
<string name="portuguese">Portoghese</string>
<!-- Reader section -->
<string name="pref_fullscreen">Schermo intero</string>

View File

@ -188,20 +188,12 @@
<item>2</item>
</string-array>
<string-array name="languages">
<item>@string/system_default</item>
<item>@string/english</item>
<item>@string/spanish</item>
<item>@string/italian</item>
<item>@string/portuguese</item>
</string-array>
<string-array name="languages_values">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item></item>
<item>en</item>
<item>es</item>
<item>it</item>
<item>pt</item>
</string-array>
</resources>

View File

@ -21,7 +21,7 @@
<string name="pref_theme_key">pref_theme_key</string>
<string name="pref_library_update_restriction_key">library_update_restriction</string>
<string name="pref_start_screen_key">start_screen</string>
<string name="pref_language_key">language</string>
<string name="pref_language_key">app_language</string>
<string name="pref_default_viewer_key">pref_default_viewer_key</string>
<string name="pref_image_scale_type_key">pref_image_scale_type_key</string>
@ -51,7 +51,7 @@
<string name="pref_remove_after_marked_as_read_key">pref_remove_after_marked_as_read_key</string>
<string name="pref_last_used_category_key">last_used_category</string>
<string name="pref_source_languages">pref_source_languages</string>
<string name="pref_source_languages">source_languages</string>
<string name="pref_category_tracking_accounts_key">category_tracking_accounts</string>
<string name="pref_clear_chapter_cache_key">pref_clear_chapter_cache_key</string>

View File

@ -112,13 +112,7 @@
<string name="dark_theme">Dark theme</string>
<string name="pref_start_screen">Start screen</string>
<string name="pref_language">Language</string>
<!-- Languages -->
<string name="system_default">System Default</string>
<string name="english">English</string>
<string name="spanish">Spanish</string>
<string name="italian">Italian</string>
<string name="portuguese">Portuguese</string>
<string name="system_default">System default</string>
<!-- Reader section -->
<string name="pref_fullscreen">Fullscreen</string>

View File

@ -10,9 +10,8 @@
android:title="@string/pref_category_general"
app:asp_tintEnabled="true">
<eu.kanade.tachiyomi.widget.preference.IntListPreference
android:defaultValue="0"
android:entries="@array/languages"
<ListPreference
android:defaultValue=""
android:entryValues="@array/languages_values"
android:key="@string/pref_language_key"
android:summary="%s"