Эх сурвалжийг харах

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 <[email protected]>
Unlocked 4 жил өмнө
parent
commit
c34b548a3e

+ 2 - 0
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"

+ 2 - 0
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)

+ 21 - 1
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<Int, Int> = emptyMap()
+        set(value) {
+            if (field !== value) {
+                field = value
+                notifyDataSetChanged()
+            }
+        }
+
     private var boundViews = arrayListOf<View>()
 
     /**
@@ -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
     }
 

+ 31 - 2
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt

@@ -120,8 +120,12 @@ class LibraryController(
 
     private var tabsVisibilityRelay: BehaviorRelay<Boolean> = BehaviorRelay.create(false)
 
+    private var mangaCountVisibilityRelay: BehaviorRelay<Boolean> = 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()
     }
 

+ 5 - 2
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)
             }

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -96,6 +96,7 @@
     <string name="action_display_download_badge">Download badges</string>
     <string name="action_display_unread_badge">Unread badges</string>
     <string name="action_display_show_tabs">Show category tabs</string>
+    <string name="action_display_show_number_of_items">Show number of items</string>
     <string name="action_disable">Disable</string>
     <string name="action_pin">Pin</string>
     <string name="action_unpin">Unpin</string>