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 <arkon@users.noreply.github.com>
This commit is contained in:
Unlocked 2021-01-31 07:15:37 -08:00 committed by GitHub
parent 9bf452856c
commit c34b548a3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 5 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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
}

View File

@ -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()
}

View File

@ -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)
}

View File

@ -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>