From 8a3a7418d055b500517ef6c9c5623328ae01bc10 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 29 Aug 2022 14:34:11 -0400 Subject: [PATCH] Show number of unique library items (closes #6522) - Filters do affect this - Won't be shown if tabs aren't visible and there's more than 1 category (so it'd always show the per-category count), but a separate stats page should show that info instead --- .../tachiyomi/ui/library/LibraryPresenter.kt | 73 +++++++------------ 1 file changed, 25 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index 10647bc8c..a4806506f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -97,19 +97,16 @@ class LibraryPresenter( private val trackManager: TrackManager = Injekt.get(), ) : BasePresenter(), LibraryState by state { - var loadedManga by mutableStateOf(emptyMap>()) - private set + private var loadedManga by mutableStateOf(emptyMap>()) val isLibraryEmpty by derivedStateOf { loadedManga.isEmpty() } val tabVisibility by preferences.categoryTabs().asState() - val mangaCountVisibility by preferences.categoryNumberOfItems().asState() var activeCategory: Int by preferences.lastUsedCategory().asState() val isDownloadOnly: Boolean by preferences.downloadedOnly().asState() - val isIncognitoMode: Boolean by preferences.incognitoMode().asState() /** @@ -147,7 +144,7 @@ class LibraryPresenter( */ if (librarySubscription == null || librarySubscription!!.isCancelled) { librarySubscription = presenterScope.launchIO { - getLibraryObservable() + getLibraryFlow().asObservable() .combineLatest(badgeTriggerRelay.observeOn(Schedulers.io())) { lib, _ -> lib.apply { setBadges(mangaMap) } } @@ -402,8 +399,16 @@ class LibraryPresenter( * * @return an observable of the categories and its manga. */ - private fun getLibraryObservable(): Observable { - return combine(getCategoriesFlow(), getLibraryMangasFlow()) { dbCategories, libraryManga -> + private fun getLibraryFlow(): Flow { + val categoriesFlow = getCategories.subscribe() + val libraryMangasFlow = getLibraryManga.subscribe() + .map { list -> + list.map { libraryManga -> + // Display mode based on user preference: take it from global library setting or category + LibraryItem(libraryManga) + }.groupBy { it.manga.category.toLong() } + } + return combine(categoriesFlow, libraryMangasFlow) { dbCategories, libraryManga -> val categories = if (libraryManga.isNotEmpty() && libraryManga.containsKey(0).not()) { dbCategories.filterNot { it.isSystemCategory } } else { @@ -412,32 +417,7 @@ class LibraryPresenter( state.categories = categories Library(categories, libraryManga) - }.asObservable() - } - - /** - * Get the categories from the database. - * - * @return an observable of the categories. - */ - private fun getCategoriesFlow(): Flow> { - return getCategories.subscribe() - } - - /** - * Get the manga grouped by categories. - * - * @return an observable containing a map with the category id as key and a list of manga as the - * value. - */ - private fun getLibraryMangasFlow(): Flow { - return getLibraryManga.subscribe() - .map { list -> - list.map { libraryManga -> - // Display mode based on user preference: take it from global library setting or category - LibraryItem(libraryManga) - }.groupBy { it.manga.category.toLong() } - } + } } /** @@ -531,8 +511,8 @@ class LibraryPresenter( * @param mangas the list of manga. */ fun downloadUnreadChapters(mangas: List) { - mangas.forEach { manga -> - launchIO { + launchIO { + mangas.forEach { manga -> val chapters = getChapterByMangaId.await(manga.id) .filter { !it.read } .map { it.toDbChapter() } @@ -548,8 +528,8 @@ class LibraryPresenter( * @param mangas the list of manga. */ fun markReadStatus(mangas: List, read: Boolean) { - mangas.forEach { manga -> - launchIO { + launchIO { + mangas.forEach { manga -> setReadStatus.await( manga = manga, read = read, @@ -635,14 +615,15 @@ class LibraryPresenter( return produceState(initialValue = default, category, loadedManga, mangaCountVisibility, tabVisibility) { val title = if (tabVisibility.not()) categoryName else defaultTitle + val count = when { + category == null || mangaCountVisibility.not() -> null + tabVisibility.not() -> loadedManga[category.id]?.size + else -> loadedManga.values.flatten().distinct().size + } - value = when { - category == null -> default - (tabVisibility.not() && mangaCountVisibility.not()) -> LibraryToolbarTitle(title) - tabVisibility.not() && mangaCountVisibility -> LibraryToolbarTitle(title, loadedManga[category.id]?.size) - (tabVisibility && categories.size > 1) && mangaCountVisibility -> LibraryToolbarTitle(title) - tabVisibility && mangaCountVisibility -> LibraryToolbarTitle(title, loadedManga[category.id]?.size) - else -> default + value = when (category) { + null -> default + else -> LibraryToolbarTitle(title, count) } } } @@ -674,10 +655,6 @@ class LibraryPresenter( } } - fun hasSelection(): Boolean { - return selection.isNotEmpty() - } - fun clearSelection() { state.selection = emptyList() }