From c34b548a3e093a948d3de2dd784077395ea801ab Mon Sep 17 00:00:00 2001 From: Unlocked <10186337+TheUnlocked@users.noreply.github.com> Date: Sun, 31 Jan 2021 07:15:37 -0800 Subject: [PATCH] Add manga count to the library header (#3884) * Add manga count to the library header * Make showing the number of manga configurable Co-authored-by: arkon --- .../data/preference/PreferenceKeys.kt | 2 ++ .../data/preference/PreferencesHelper.kt | 2 ++ .../tachiyomi/ui/library/LibraryAdapter.kt | 22 ++++++++++++- .../tachiyomi/ui/library/LibraryController.kt | 33 +++++++++++++++++-- .../ui/library/LibrarySettingsSheet.kt | 7 ++-- app/src/main/res/values/strings.xml | 1 + 6 files changed, 62 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 3d02a8ac7b..e3cca4e806 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -171,6 +171,8 @@ object PreferenceKeys { const val categoryTabs = "display_category_tabs" + const val categoryNumberOfItems = "display_number_of_items" + const val alwaysShowChapterTransition = "always_show_chapter_transition" const val searchPinnedSourcesOnly = "search_pinned_sources_only" 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 27e65dc1a6..9f1ec165cb 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 @@ -215,6 +215,8 @@ class PreferencesHelper(val context: Context) { fun categoryTabs() = flowPrefs.getBoolean(Keys.categoryTabs, true) + fun categoryNumberOfItems() = flowPrefs.getBoolean(Keys.categoryNumberOfItems, false) + fun filterDownloaded() = flowPrefs.getInt(Keys.filterDownloaded, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value) fun filterUnread() = flowPrefs.getInt(Keys.filterUnread, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryAdapter.kt index 98c0c81622..7b594cf1b3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryAdapter.kt @@ -4,16 +4,22 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import eu.kanade.tachiyomi.data.database.models.Category +import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.databinding.LibraryCategoryBinding import eu.kanade.tachiyomi.util.view.inflate import eu.kanade.tachiyomi.widget.RecyclerViewPagerAdapter +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get /** * This adapter stores the categories from the library, used with a ViewPager. * * @constructor creates an instance of the adapter. */ -class LibraryAdapter(private val controller: LibraryController) : RecyclerViewPagerAdapter() { +class LibraryAdapter( + private val controller: LibraryController, + private val preferences: PreferencesHelper = Injekt.get() +) : RecyclerViewPagerAdapter() { /** * The categories to bind in the adapter. @@ -27,6 +33,17 @@ class LibraryAdapter(private val controller: LibraryController) : RecyclerViewPa } } + /** + * The number of manga in each category. + */ + var mangaCountPerCategory: Map = emptyMap() + set(value) { + if (field !== value) { + field = value + notifyDataSetChanged() + } + } + private var boundViews = arrayListOf() /** @@ -79,6 +96,9 @@ class LibraryAdapter(private val controller: LibraryController) : RecyclerViewPa * @return the title to display. */ override fun getPageTitle(position: Int): CharSequence { + if (preferences.categoryNumberOfItems().get()) { + return categories[position].let { "${it.name} (${mangaCountPerCategory[it.id]})" } + } return categories[position].name } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 35ae240e40..0d3d382c2b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -120,8 +120,12 @@ class LibraryController( private var tabsVisibilityRelay: BehaviorRelay = BehaviorRelay.create(false) + private var mangaCountVisibilityRelay: BehaviorRelay = BehaviorRelay.create(false) + private var tabsVisibilitySubscription: Subscription? = null + private var mangaCountVisibilitySubscription: Subscription? = null + init { setHasOptionsMenu(true) retainViewMode = RetainViewMode.RETAIN_DETACH @@ -140,13 +144,27 @@ class LibraryController( } private fun updateTitle() { - if (preferences.categoryTabs().get()) { + val categoryTabs = preferences.categoryTabs().get() + val currentCategory = adapter?.categories?.getOrNull(binding.libraryPager.currentItem) + + if (categoryTabs) { currentTitle = resources?.getString(R.string.label_library) } else { - adapter?.categories?.getOrNull(binding.libraryPager.currentItem)?.let { + currentCategory?.let { currentTitle = it.name } } + + if (preferences.categoryNumberOfItems().get() && libraryMangaRelay.hasValue()) { + libraryMangaRelay.value.mangas.let { mangaMap -> + if (!categoryTabs) { + currentTitle += " (${mangaMap[currentCategory?.id]?.size ?: 0})" + } else if (adapter?.categories?.size == 1) { + // special case for if there are no categories + currentTitle += " (${mangaMap[0]?.size ?: 0})" + } + } + } } override fun createPresenter(): LibraryPresenter { @@ -236,6 +254,10 @@ class LibraryController( tabAnimator?.collapse() } } + mangaCountVisibilitySubscription?.unsubscribe() + mangaCountVisibilitySubscription = mangaCountVisibilityRelay.subscribe { + adapter?.notifyDataSetChanged() + } } override fun cleanupTabs(tabs: TabLayout) { @@ -267,6 +289,9 @@ class LibraryController( // Set the categories adapter.categories = categories + adapter.mangaCountPerCategory = adapter.categories.map { + Pair(it.id ?: -1, mangaMap[it.id]?.size ?: 0) + }.toMap() // Restore active category. binding.libraryPager.setCurrentItem(activeCat, false) @@ -283,6 +308,9 @@ class LibraryController( // Send the manga map to child fragments after the adapter is updated. libraryMangaRelay.call(LibraryMangaEvent(mangaMap)) + + // Finally update the title + updateTitle() } /** @@ -309,6 +337,7 @@ class LibraryController( private fun onTabsSettingsChanged() { tabsVisibilityRelay.call(preferences.categoryTabs().get() && adapter?.categories?.size ?: 0 > 1) + mangaCountVisibilityRelay.call(preferences.categoryNumberOfItems().get()) updateTitle() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt index f64f00abe4..22c0f90550 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsSheet.kt @@ -295,20 +295,23 @@ class LibrarySettingsSheet( inner class TabsGroup : Group { private val showTabs = Item.CheckboxGroup(R.string.action_display_show_tabs, this) + private val showNumberOfItems = Item.CheckboxGroup(R.string.action_display_show_number_of_items, this) override val header = Item.Header(R.string.tabs_header) - override val items = listOf(showTabs) + override val items = listOf(showTabs, showNumberOfItems) override val footer = null override fun initModels() { showTabs.checked = preferences.categoryTabs().get() + showNumberOfItems.checked = preferences.categoryNumberOfItems().get() } override fun onItemClicked(item: Item) { item as Item.CheckboxGroup item.checked = !item.checked when (item) { - showTabs -> preferences.categoryTabs().set((item.checked)) + showTabs -> preferences.categoryTabs().set(item.checked) + showNumberOfItems -> preferences.categoryNumberOfItems().set(item.checked) } adapter.notifyItemChanged(item) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ee2a42f804..a21c7db101 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -96,6 +96,7 @@ Download badges Unread badges Show category tabs + Show number of items Disable Pin Unpin