From 0a1c036cc974aca0c2cc143aadd817403491954f Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 4 May 2020 16:17:40 -0400 Subject: [PATCH] Filters now work on collasped categories closes #317 --- .../ui/library/LibraryCategoryAdapter.kt | 3 + .../tachiyomi/ui/library/LibraryController.kt | 3 +- .../tachiyomi/ui/library/LibraryHeaderItem.kt | 6 +- .../tachiyomi/ui/library/LibraryPresenter.kt | 148 ++++++++++-------- 4 files changed, 91 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt index 0e406c0a0b..70e5f664be 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt @@ -34,6 +34,9 @@ class LibraryCategoryAdapter(val controller: LibraryController) : val libraryListener: LibraryListener = controller + val isSingleCategory + get() = controller.singleCategory || !controller.presenter.showAllCategories + /** * Sets a list of manga in the adapter. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 9071f1b237..c2ebf5639c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -116,7 +116,8 @@ class LibraryController( private var libraryLayout: Int = preferences.libraryLayout().getOrDefault() - private var singleCategory: Boolean = false + var singleCategory: Boolean = false + private set /** * Library search query. diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderItem.kt index ce2522be44..a120f7bfb9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderItem.kt @@ -184,16 +184,16 @@ class LibraryHeaderItem( updateButton.gone() } LibraryUpdateService.categoryInQueue(category.id) -> { - expandImage.visibleIf(adapter.headerItems.size > 1) + expandImage.visibleIf(!adapter.isSingleCategory) checkboxImage.gone() catProgress.visible() updateButton.invisible() } else -> { - expandImage.visibleIf(adapter.headerItems.size > 1) + expandImage.visibleIf(!adapter.isSingleCategory) catProgress.gone() checkboxImage.gone() - updateButton.visInvisIf(category.id ?: 0 > -1 && adapter.headerItems.size > 1) + updateButton.visInvisIf(category.id ?: 0 > -1 && !adapter.isSingleCategory) } } } 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 a65054249d..0409fc09fb 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 @@ -60,7 +60,7 @@ class LibraryPresenter( /** List of all manga to update the */ var libraryItems: List = emptyList() - private var sectionedLibraryItems: Map> = emptyMap() + private var sectionedLibraryItems: MutableMap> = mutableMapOf() var currentCategory = -1 private set private var allLibraryItems: List = emptyList() @@ -117,7 +117,7 @@ class LibraryPresenter( val items = libraryItems val show = showAllCategories || preferences.hideCategories().getOrDefault() if (!show) { - sectionedLibraryItems = items.groupBy { it.manga.category } + sectionedLibraryItems = items.groupBy { it.manga.category }.toMutableMap() if (currentCategory == -1) currentCategory = categories.find { it.order == preferences.lastUsedCategory().getOrDefault() }?.id ?: 0 @@ -133,7 +133,7 @@ class LibraryPresenter( libraryItems = items val show = showAllCategories || preferences.hideCategories().getOrDefault() if (!show) { - sectionedLibraryItems = items.groupBy { it.manga.category } + sectionedLibraryItems = items.groupBy { it.manga.category }.toMutableMap() if (currentCategory == -1) currentCategory = categories.find { it.order == preferences.lastUsedCategory().getOrDefault() }?.id ?: 0 @@ -166,74 +166,91 @@ class LibraryPresenter( val filterTrackers = FilterBottomSheet.FILTER_TRACKER return items.filter f@{ item -> - if (item.manga.isBlank()) { + if (item.manga.status == -1) { + return@f sectionedLibraryItems[item.manga.category]?.any { + matchesFilters(it, filterDownloaded, filterUnread, filterCompleted, + filterTracked, filterMangaType, filterTrackers) + } ?: false + } else if (item.manga.isBlank()) { return@f filterDownloaded == 0 && filterUnread == 0 && filterCompleted == 0 && filterTracked == 0 && filterMangaType == 0 } - - if (filterUnread == STATE_INCLUDE && item.manga.unread == 0) return@f false - if (filterUnread == STATE_EXCLUDE && item.manga.unread > 0) return@f false - - // Filter for unread chapters - if (filterUnread == 3 && (item.manga.unread == 0 || db.getChapters(item.manga) - .executeAsBlocking().size != item.manga.unread) - ) return@f false - if (filterUnread == 4 && (item.manga.unread == 0 || db.getChapters(item.manga) - .executeAsBlocking().size == item.manga.unread) - ) return@f false - - if (filterMangaType > 0) { - if (if (filterMangaType == Manga.TYPE_MANHWA) (filterMangaType != item.manga.mangaType() && filterMangaType != Manga.TYPE_WEBTOON) - else filterMangaType != item.manga.mangaType() - ) return@f false - } - - // Filter for completed status of manga - if (filterCompleted == STATE_INCLUDE && item.manga.status != SManga.COMPLETED) return@f false - if (filterCompleted == STATE_EXCLUDE && item.manga.status == SManga.COMPLETED) return@f false - - // Filter for tracked (or per tracked service) - if (filterTracked != STATE_IGNORE) { - val tracks = db.getTracks(item.manga).executeAsBlocking() - - val hasTrack = loggedServices.any { service -> - tracks.any { it.sync_id == service.id } - } - val service = if (filterTrackers.isNotEmpty()) loggedServices.find { - it.name == filterTrackers - } else null - if (filterTracked == STATE_INCLUDE) { - if (!hasTrack) return@f false - if (filterTrackers.isNotEmpty()) { - if (service != null) { - val hasServiceTrack = tracks.any { it.sync_id == service.id } - if (!hasServiceTrack) return@f false - if (filterTracked == STATE_EXCLUDE && hasServiceTrack) return@f false - } - } - } else if (filterTracked == STATE_EXCLUDE) { - if (hasTrack && filterTrackers.isEmpty()) return@f false - if (filterTrackers.isNotEmpty()) { - if (service != null) { - val hasServiceTrack = tracks.any { it.sync_id == service.id } - if (hasServiceTrack) return@f false - } - } - } - } - // Filter for downloaded manga - if (filterDownloaded != STATE_IGNORE) { - val isDownloaded = when { - item.manga.source == LocalSource.ID -> true - item.downloadCount != -1 -> item.downloadCount > 0 - else -> downloadManager.getDownloadCount(item.manga) > 0 - } - return@f if (filterDownloaded == STATE_INCLUDE) isDownloaded else !isDownloaded - } - true + matchesFilters(item, filterDownloaded, filterUnread, filterCompleted, filterTracked, + filterMangaType, filterTrackers) } } + private fun matchesFilters( + item: LibraryItem, + filterDownloaded: Int, + filterUnread: Int, + filterCompleted: Int, + filterTracked: Int, + filterMangaType: Int, + filterTrackers: String + ): Boolean { + if (filterUnread == STATE_INCLUDE && item.manga.unread == 0) return false + if (filterUnread == STATE_EXCLUDE && item.manga.unread > 0) return false + + // Filter for unread chapters + if (filterUnread == 3 && (item.manga.unread == 0 || db.getChapters(item.manga) + .executeAsBlocking().size != item.manga.unread) + ) return false + if (filterUnread == 4 && (item.manga.unread == 0 || db.getChapters(item.manga) + .executeAsBlocking().size == item.manga.unread) + ) return false + + if (filterMangaType > 0) { + if (if (filterMangaType == Manga.TYPE_MANHWA) (filterMangaType != item.manga.mangaType() && filterMangaType != Manga.TYPE_WEBTOON) + else filterMangaType != item.manga.mangaType() + ) return false + } + + // Filter for completed status of manga + if (filterCompleted == STATE_INCLUDE && item.manga.status != SManga.COMPLETED) return false + if (filterCompleted == STATE_EXCLUDE && item.manga.status == SManga.COMPLETED) return false + + // Filter for tracked (or per tracked service) + if (filterTracked != STATE_IGNORE) { + val tracks = db.getTracks(item.manga).executeAsBlocking() + + val hasTrack = loggedServices.any { service -> + tracks.any { it.sync_id == service.id } + } + val service = if (filterTrackers.isNotEmpty()) loggedServices.find { + it.name == filterTrackers + } else null + if (filterTracked == STATE_INCLUDE) { + if (!hasTrack) return false + if (filterTrackers.isNotEmpty()) { + if (service != null) { + val hasServiceTrack = tracks.any { it.sync_id == service.id } + if (!hasServiceTrack) return false + if (filterTracked == STATE_EXCLUDE && hasServiceTrack) return false + } + } + } else if (filterTracked == STATE_EXCLUDE) { + if (hasTrack && filterTrackers.isEmpty()) return false + if (filterTrackers.isNotEmpty()) { + if (service != null) { + val hasServiceTrack = tracks.any { it.sync_id == service.id } + if (hasServiceTrack) return false + } + } + } + } + // Filter for downloaded manga + if (filterDownloaded != STATE_IGNORE) { + val isDownloaded = when { + item.manga.source == LocalSource.ID -> true + item.downloadCount != -1 -> item.downloadCount > 0 + else -> downloadManager.getDownloadCount(item.manga) > 0 + } + return if (filterDownloaded == STATE_INCLUDE) isDownloaded else !isDownloaded + } + return true + } + /** * Sets downloaded chapter count to each manga. * @@ -487,6 +504,7 @@ class LibraryPresenter( val mergedTitle = mangaToRemove.joinToString("-") { it.manga.title + "-" + it.manga.author } + sectionedLibraryItems[catId] = mangaToRemove items.removeAll(mangaToRemove) val headerItem = headerItems[catId] if (headerItem != null) items.add(