From 8fe79a1fb570aa5ab2263daf407c59bd8c05744e Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 10 Apr 2020 18:38:24 -0400 Subject: [PATCH] Add source pinning (closes #2322) --- .../data/preference/PreferencesHelper.kt | 2 + .../ui/catalogue/CatalogueController.kt | 39 +++++++++---------- .../ui/catalogue/CataloguePresenter.kt | 26 ++++++++++--- .../tachiyomi/util/system/LocaleHelper.kt | 2 + app/src/main/res/values/strings.xml | 3 ++ 5 files changed, 46 insertions(+), 26 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 d32c57fca..d15f654bf 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 @@ -198,6 +198,8 @@ class PreferencesHelper(val context: Context) { fun hiddenCatalogues() = rxPrefs.getStringSet("hidden_catalogues", emptySet()) + fun pinnedCatalogues() = rxPrefs.getStringSet("pinned_catalogues", emptySet()) + fun downloadNew() = rxPrefs.getBoolean(Keys.downloadNew, false) fun downloadNewCategories() = rxPrefs.getStringSet(Keys.downloadNewCategories, emptySet()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueController.kt index f12a42674..fceab5a3e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueController.kt @@ -57,24 +57,13 @@ class CatalogueController : NucleusController(), private lateinit var binding: CatalogueMainControllerBinding init { - // Enable the option menu setHasOptionsMenu(true) } - /** - * Set the title of controller. - * - * @return title. - */ override fun getTitle(): String? { return applicationContext?.getString(R.string.label_sources) } - /** - * Create the [CataloguePresenter] used in controller. - * - * @return instance of [CataloguePresenter] - */ override fun createPresenter(): CataloguePresenter { return CataloguePresenter() } @@ -91,11 +80,6 @@ class CatalogueController : NucleusController(), return binding.root } - /** - * Called when the view is created - * - * @param view view of controller - */ override fun onViewCreated(view: View) { super.onViewCreated(view) @@ -133,14 +117,18 @@ class CatalogueController : NucleusController(), val activity = activity ?: return val item = adapter?.getItem(position) as? SourceItem ?: return + val isPinned = item.header?.code?.equals(CataloguePresenter.PINNED_KEY) ?: false + MaterialDialog.Builder(activity) .title(item.source.name) - .items(activity.getString(R.string.action_hide)) + .items( + activity.getString(R.string.action_hide), + activity.getString(if (isPinned) R.string.action_unpin else R.string.action_pin) + ) .itemsCallback { _, _, which, _ -> when (which) { - 0 -> { - hideCatalogue(item.source) - } + 0 -> hideCatalogue(item.source) + 1 -> pinCatalogue(item.source, isPinned) } }.show() } @@ -152,6 +140,17 @@ class CatalogueController : NucleusController(), presenter.updateSources() } + private fun pinCatalogue(source: Source, isPinned: Boolean) { + val current = preferences.pinnedCatalogues().getOrDefault() + if (isPinned) { + preferences.pinnedCatalogues().set(current - source.id.toString()) + } else { + preferences.pinnedCatalogues().set(current + source.id.toString()) + } + + presenter.updateSources() + } + /** * Called when browse is clicked in [CatalogueAdapter] */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt index cd538a92a..769a93166 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt @@ -27,9 +27,6 @@ class CataloguePresenter( private val preferences: PreferencesHelper = Injekt.get() ) : BasePresenter() { - /** - * Enabled sources. - */ var sources = getEnabledSources() /** @@ -48,9 +45,12 @@ class CataloguePresenter( /** * Unsubscribe and create a new subscription to fetch enabled sources. */ - fun loadSources() { + private fun loadSources() { sourceSubscription?.unsubscribe() + val pinnedSources = mutableListOf() + val pinnedCatalogues = preferences.pinnedCatalogues().getOrDefault() + val map = TreeMap> { d1, d2 -> // Catalogues without a lang defined will be placed at the end when { @@ -60,9 +60,19 @@ class CataloguePresenter( } } val byLang = sources.groupByTo(map, { it.lang }) - val sourceItems = byLang.flatMap { + var sourceItems = byLang.flatMap { val langItem = LangItem(it.key) - it.value.map { source -> SourceItem(source, langItem) } + it.value.map { source -> + if (source.id.toString() in pinnedCatalogues) { + pinnedSources.add(SourceItem(source, LangItem(PINNED_KEY))) + } + + SourceItem(source, langItem) + } + } + + if (pinnedSources.isNotEmpty()) { + sourceItems = pinnedSources + sourceItems } sourceSubscription = Observable.just(sourceItems) @@ -101,4 +111,8 @@ class CataloguePresenter( .sortedBy { "(${it.lang}) ${it.name}" } + sourceManager.get(LocalSource.ID) as LocalSource } + + companion object { + const val PINNED_KEY = "pinned" + } } 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 730b09dcf..2d0703f4b 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 @@ -7,6 +7,7 @@ import android.os.Build import android.view.ContextThemeWrapper import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.ui.catalogue.CataloguePresenter import java.util.Locale import uy.kohesive.injekt.injectLazy @@ -50,6 +51,7 @@ object LocaleHelper { return when (lang) { null -> "" "" -> context.getString(R.string.other_source) + CataloguePresenter.PINNED_KEY -> context.getString(R.string.pinned_sources) "all" -> context.getString(R.string.all_lang) else -> { val locale = getLocale(lang) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 961dda117..afe574229 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -85,6 +85,8 @@ List Download badges Hide + Pin + Unpin Cancel Cancel all Sort @@ -388,6 +390,7 @@ Check website in WebView Local manga Other + Pinned Default can\'t be selected with other categories The manga has been added to your library Global search…