From f176a5179ad31c5752bed8c949e5a82cd9b8e1e6 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 17 Jun 2020 22:09:48 -0400 Subject: [PATCH] Restore individual sources in source filter view --- .../java/eu/kanade/tachiyomi/source/Source.kt | 2 + .../tachiyomi/source/online/HttpSource.kt | 2 +- .../details/ExtensionDetailsController.kt | 7 +- .../details/SourcePreferencesController.kt | 3 +- .../browse/source/SourceFilterController.kt | 80 ++++++++++++++++--- 5 files changed, 76 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt b/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt index ca63ae0410..fe7318ec27 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt @@ -47,3 +47,5 @@ interface Source { } fun Source.icon(): Drawable? = Injekt.get().getAppIconForSource(this) + +fun Source.getPreferenceKey(): String = "source_$id" diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt b/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt index 02ff3b8771..438662c6db 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt @@ -34,7 +34,7 @@ abstract class HttpSource : CatalogueSource { // * Preferences that a source may need. // */ // val preferences: SharedPreferences by lazy { -// Injekt.get().getSharedPreferences("source_$id", Context.MODE_PRIVATE) +// Injekt.get().getSharedPreferences(source.getPreferenceKey(), Context.MODE_PRIVATE) // } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt index 51f53f7c05..f0d58c1d61 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt @@ -27,6 +27,7 @@ import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.ConfigurableSource import eu.kanade.tachiyomi.source.Source +import eu.kanade.tachiyomi.source.getPreferenceKey import eu.kanade.tachiyomi.ui.base.controller.NoToolbarElevationController import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction @@ -128,7 +129,7 @@ class ExtensionDetailsController(bundle: Bundle? = null) : val sourcePrefs = mutableListOf() val block: (@DSL SwitchPreferenceCompat).() -> Unit = { - key = getSourceKey(source.id) + key = source.getPreferenceKey() title = when { isMultiSource && !isMultiLangSingleSource -> source.toString() else -> LocaleHelper.getSourceDisplayName(it.key, context) @@ -226,10 +227,6 @@ class ExtensionDetailsController(bundle: Bundle? = null) : return id.toString() !in preferences.hiddenCatalogues().get() } - private fun getSourceKey(sourceId: Long): String { - return "source_$sourceId" - } - private fun getPreferenceThemeContext(): Context { val tv = TypedValue() activity!!.theme.resolveAttribute(R.attr.preferenceTheme, tv, true) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/SourcePreferencesController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/SourcePreferencesController.kt index 41abd3ec82..7867f6c1dc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/SourcePreferencesController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/SourcePreferencesController.kt @@ -28,6 +28,7 @@ import eu.kanade.tachiyomi.data.preference.SharedPreferencesDataStore import eu.kanade.tachiyomi.databinding.SourcePreferencesControllerBinding import eu.kanade.tachiyomi.source.ConfigurableSource import eu.kanade.tachiyomi.source.Source +import eu.kanade.tachiyomi.source.getPreferenceKey import eu.kanade.tachiyomi.ui.base.controller.NucleusController import timber.log.Timber @@ -107,7 +108,7 @@ class SourcePreferencesController(bundle: Bundle? = null) : val context = screen.context val dataStore = SharedPreferencesDataStore( - context.getSharedPreferences("source_${source.id}", Context.MODE_PRIVATE) + context.getSharedPreferences(source.getPreferenceKey(), Context.MODE_PRIVATE) ) if (source is ConfigurableSource) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterController.kt index f7cffc56aa..4e4e378bb6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterController.kt @@ -1,12 +1,17 @@ package eu.kanade.tachiyomi.ui.browse.source import android.graphics.drawable.Drawable +import androidx.preference.CheckBoxPreference +import androidx.preference.PreferenceGroup import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.SourceManager +import eu.kanade.tachiyomi.source.getPreferenceKey +import eu.kanade.tachiyomi.source.icon +import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.setting.SettingsController import eu.kanade.tachiyomi.util.preference.onChange -import eu.kanade.tachiyomi.util.preference.switchPreference +import eu.kanade.tachiyomi.util.preference.switchPreferenceCategory import eu.kanade.tachiyomi.util.preference.titleRes import eu.kanade.tachiyomi.util.system.LocaleHelper import java.util.TreeMap @@ -27,25 +32,36 @@ class SourceFilterController : SettingsController() { val sourcesByLang = onlineSources.groupByTo(TreeMap(), { it.lang }) // Order first by active languages, then inactive ones - val orderedLangs = sourcesByLang.keys.sortedWith(compareBy({ it !in activeLangsCodes }, { LocaleHelper.getSourceDisplayName(it, context) })) + val orderedLangs = sourcesByLang.keys.sortedWith( + compareBy( + { it !in activeLangsCodes }, + { LocaleHelper.getSourceDisplayName(it, context) } + ) + ) orderedLangs.forEach { lang -> - switchPreference { + val sources = sourcesByLang[lang].orEmpty().sortedBy { it.name } + + // Create a preference group and set initial state and change listener + switchPreferenceCategory { preferenceScreen.addPreference(this) title = LocaleHelper.getSourceDisplayName(lang, context) isPersistent = false - isChecked = lang in activeLangsCodes + if (lang in activeLangsCodes) { + setChecked(true) + addLanguageSources(this, sources) + } onChange { newValue -> val checked = newValue as Boolean val current = preferences.enabledLanguages().get() - preferences.enabledLanguages().set( - if (!checked) { - current - lang - } else { - current + lang - } - ) + if (!checked) { + preferences.enabledLanguages().set(current - lang) + removeAll() + } else { + preferences.enabledLanguages().set(current + lang) + addLanguageSources(this, sources) + } true } } @@ -55,4 +71,46 @@ class SourceFilterController : SettingsController() { override fun setDivider(divider: Drawable?) { super.setDivider(null) } + + /** + * Adds the source list for the given group (language). + * + * @param group the language category. + */ + private fun addLanguageSources(group: PreferenceGroup, sources: List) { + val hiddenCatalogues = preferences.hiddenCatalogues().get() + + sources + .sortedBy { it.id.toString() in hiddenCatalogues } + .map { source -> + CheckBoxPreference(group.context).apply { + val id = source.id.toString() + title = source.name + key = source.getPreferenceKey() + isPersistent = false + isChecked = id !in hiddenCatalogues + + val sourceIcon = source.icon() + if (sourceIcon != null) { + icon = sourceIcon + } + + onChange { newValue -> + val checked = newValue as Boolean + val current = preferences.hiddenCatalogues().get() + + preferences.hiddenCatalogues().set( + if (checked) { + current - id + } else { + current + id + } + ) + + true + } + } + } + .forEach { group.addPreference(it) } + } }