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 f766aa3ec..e4d07dd42 100755 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -143,19 +143,16 @@ class LibraryPresenter( // SY <-- ) : 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() /** @@ -225,7 +222,7 @@ class LibraryPresenter( */ if (librarySubscription == null || librarySubscription!!.isCancelled) { librarySubscription = presenterScope.launchIO { - getLibraryObservable() + getLibraryFlow().asObservable() .combineLatest(badgeTriggerRelay.observeOn(Schedulers.io())) { lib, _ -> lib.apply { setBadges(mangaMap) } } @@ -572,8 +569,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 { @@ -584,7 +589,7 @@ class LibraryPresenter( state.ogCategories = categories // SY <-- Library(categories, libraryManga) - }.asObservable() + } } // SY --> @@ -613,31 +618,6 @@ class LibraryPresenter( } // SY <-- - /** - * 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() } - } - } - /** * 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. */ fun downloadUnreadChapters(mangas: List) { - mangas.forEach { manga -> - launchIO { + launchIO { + mangas.forEach { manga -> if (manga.source == MERGED_SOURCE_ID) { val mergedSource = sourceManager.get(MERGED_SOURCE_ID) as MergedSource val mergedMangas = getMergedMangaById.await(manga.id) @@ -816,8 +796,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, @@ -913,14 +893,15 @@ class LibraryPresenter( val title = if (tabVisibility.not()) { getCategoryName(context, category, groupType, 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) } } } @@ -1118,10 +1099,6 @@ class LibraryPresenter( } } - fun hasSelection(): Boolean { - return selection.isNotEmpty() - } - fun clearSelection() { state.selection = emptyList() }