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

(cherry picked from commit 8a3a7418d055b500517ef6c9c5623328ae01bc10)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt
This commit is contained in:
arkon 2022-08-29 14:34:11 -04:00 committed by Jobobby04
parent dbcc744e1f
commit 829bd7b247

View File

@ -143,19 +143,16 @@ class LibraryPresenter(
// SY <-- // SY <--
) : BasePresenter<LibraryController>(), LibraryState by state { ) : BasePresenter<LibraryController>(), LibraryState by state {
var loadedManga by mutableStateOf(emptyMap<Long, List<LibraryItem>>()) private var loadedManga by mutableStateOf(emptyMap<Long, List<LibraryItem>>())
private set
val isLibraryEmpty by derivedStateOf { loadedManga.isEmpty() } val isLibraryEmpty by derivedStateOf { loadedManga.isEmpty() }
val tabVisibility by preferences.categoryTabs().asState() val tabVisibility by preferences.categoryTabs().asState()
val mangaCountVisibility by preferences.categoryNumberOfItems().asState() val mangaCountVisibility by preferences.categoryNumberOfItems().asState()
var activeCategory: Int by preferences.lastUsedCategory().asState() var activeCategory: Int by preferences.lastUsedCategory().asState()
val isDownloadOnly: Boolean by preferences.downloadedOnly().asState() val isDownloadOnly: Boolean by preferences.downloadedOnly().asState()
val isIncognitoMode: Boolean by preferences.incognitoMode().asState() val isIncognitoMode: Boolean by preferences.incognitoMode().asState()
/** /**
@ -225,7 +222,7 @@ class LibraryPresenter(
*/ */
if (librarySubscription == null || librarySubscription!!.isCancelled) { if (librarySubscription == null || librarySubscription!!.isCancelled) {
librarySubscription = presenterScope.launchIO { librarySubscription = presenterScope.launchIO {
getLibraryObservable() getLibraryFlow().asObservable()
.combineLatest(badgeTriggerRelay.observeOn(Schedulers.io())) { lib, _ -> .combineLatest(badgeTriggerRelay.observeOn(Schedulers.io())) { lib, _ ->
lib.apply { setBadges(mangaMap) } lib.apply { setBadges(mangaMap) }
} }
@ -572,8 +569,16 @@ class LibraryPresenter(
* *
* @return an observable of the categories and its manga. * @return an observable of the categories and its manga.
*/ */
private fun getLibraryObservable(): Observable<Library> { private fun getLibraryFlow(): Flow<Library> {
return combine(getCategoriesFlow(), getLibraryMangasFlow()) { dbCategories, libraryManga -> 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()) { val categories = if (libraryManga.isNotEmpty() && libraryManga.containsKey(0).not()) {
dbCategories.filterNot { it.isSystemCategory } dbCategories.filterNot { it.isSystemCategory }
} else { } else {
@ -584,7 +589,7 @@ class LibraryPresenter(
state.ogCategories = categories state.ogCategories = categories
// SY <-- // SY <--
Library(categories, libraryManga) Library(categories, libraryManga)
}.asObservable() }
} }
// SY --> // SY -->
@ -613,31 +618,6 @@ class LibraryPresenter(
} }
// SY <-- // SY <--
/**
* Get the categories from the database.
*
* @return an observable of the categories.
*/
private fun getCategoriesFlow(): Flow<List<Category>> {
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<LibraryMap> {
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() }
}
}
/** /**
* Get the tracked manga from the database and checks if the filter gets changed * Get the tracked manga from the database and checks if the filter gets changed
* *
@ -746,8 +726,8 @@ class LibraryPresenter(
* @param mangas the list of manga. * @param mangas the list of manga.
*/ */
fun downloadUnreadChapters(mangas: List<Manga>) { fun downloadUnreadChapters(mangas: List<Manga>) {
mangas.forEach { manga -> launchIO {
launchIO { mangas.forEach { manga ->
if (manga.source == MERGED_SOURCE_ID) { if (manga.source == MERGED_SOURCE_ID) {
val mergedSource = sourceManager.get(MERGED_SOURCE_ID) as MergedSource val mergedSource = sourceManager.get(MERGED_SOURCE_ID) as MergedSource
val mergedMangas = getMergedMangaById.await(manga.id) val mergedMangas = getMergedMangaById.await(manga.id)
@ -816,8 +796,8 @@ class LibraryPresenter(
* @param mangas the list of manga. * @param mangas the list of manga.
*/ */
fun markReadStatus(mangas: List<Manga>, read: Boolean) { fun markReadStatus(mangas: List<Manga>, read: Boolean) {
mangas.forEach { manga -> launchIO {
launchIO { mangas.forEach { manga ->
setReadStatus.await( setReadStatus.await(
manga = manga, manga = manga,
read = read, read = read,
@ -913,14 +893,15 @@ class LibraryPresenter(
val title = if (tabVisibility.not()) { val title = if (tabVisibility.not()) {
getCategoryName(context, category, groupType, categoryName) getCategoryName(context, category, groupType, categoryName)
} else defaultTitle } else defaultTitle
val count = when {
category == null || mangaCountVisibility.not() -> null
tabVisibility.not() -> loadedManga[category.id]?.size
else -> loadedManga.values.flatten().distinct().size
}
value = when { value = when (category) {
category == null -> default null -> default
(tabVisibility.not() && mangaCountVisibility.not()) -> LibraryToolbarTitle(title) else -> LibraryToolbarTitle(title, count)
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
} }
} }
} }
@ -1118,10 +1099,6 @@ class LibraryPresenter(
} }
} }
fun hasSelection(): Boolean {
return selection.isNotEmpty()
}
fun clearSelection() { fun clearSelection() {
state.selection = emptyList() state.selection = emptyList()
} }