Fix collapsed categories ignoring filters

This commit is contained in:
Jays2Kings 2022-07-09 15:00:04 -04:00
parent 6ae512a9b5
commit 4b7755d63a

View file

@ -90,6 +90,7 @@ class LibraryPresenter(
private set private set
var allLibraryItems: List<LibraryItem> = emptyList() var allLibraryItems: List<LibraryItem> = emptyList()
private set private set
private var hiddenLibraryItems: List<LibraryItem> = emptyList()
var forceShowAllCategories = false var forceShowAllCategories = false
val showAllCategories val showAllCategories
get() = forceShowAllCategories || preferences.showAllCategories().get() get() = forceShowAllCategories || preferences.showAllCategories().get()
@ -157,13 +158,15 @@ class LibraryPresenter(
categories = lastCategories ?: db.getCategories().executeAsBlocking().toMutableList() categories = lastCategories ?: db.getCategories().executeAsBlocking().toMutableList()
} }
presenterScope.launch { presenterScope.launch {
val library = withContext(Dispatchers.IO) { getLibraryFromDB() } val (library, hiddenItems) = withContext(Dispatchers.IO) { getLibraryFromDB() }
library.apply { setDownloadCount(library)
setDownloadCount(library) setUnreadBadge(library)
setUnreadBadge(library) setSourceLanguage(library)
setSourceLanguage(library) setDownloadCount(hiddenItems)
} setUnreadBadge(hiddenItems)
setSourceLanguage(hiddenItems)
allLibraryItems = library allLibraryItems = library
hiddenLibraryItems = hiddenItems
var mangaMap = library var mangaMap = library
mangaMap = applyFilters(mangaMap) mangaMap = applyFilters(mangaMap)
mangaMap = applySort(mangaMap) mangaMap = applySort(mangaMap)
@ -262,7 +265,8 @@ class LibraryPresenter(
val missingCategorySet = categories.mapNotNull { it.id }.toMutableSet() val missingCategorySet = categories.mapNotNull { it.id }.toMutableSet()
val filteredItems = items.filter f@{ item -> val filteredItems = items.filter f@{ item ->
if (!showEmptyCategoriesWhileFiltering && item.manga.isHidden()) { if (!showEmptyCategoriesWhileFiltering && item.manga.isHidden()) {
val subItems = sectionedLibraryItems[item.manga.category] val subItems = sectionedLibraryItems[item.manga.category]?.takeUnless { it.size <= 1 }
?: hiddenLibraryItems.filter { it.manga.category == item.manga.category }
if (subItems.isNullOrEmpty()) return@f filtersOff if (subItems.isNullOrEmpty()) return@f filtersOff
else { else {
return@f subItems.any { return@f subItems.any {
@ -544,7 +548,7 @@ class LibraryPresenter(
* *
* @return an list of all the manga in a itemized form. * @return an list of all the manga in a itemized form.
*/ */
private fun getLibraryFromDB(): List<LibraryItem> { private fun getLibraryFromDB(): Pair<List<LibraryItem>, List<LibraryItem>> {
removeArticles = preferences.removeArticles().get() removeArticles = preferences.removeArticles().get()
val categories = db.getCategories().executeAsBlocking().toMutableList() val categories = db.getCategories().executeAsBlocking().toMutableList()
var libraryManga = db.getLibraryMangas().executeAsBlocking() var libraryManga = db.getLibraryMangas().executeAsBlocking()
@ -552,6 +556,7 @@ class LibraryPresenter(
if (groupType > BY_DEFAULT) { if (groupType > BY_DEFAULT) {
libraryManga = libraryManga.distinctBy { it.id } libraryManga = libraryManga.distinctBy { it.id }
} }
val hiddenItems = mutableListOf<LibraryItem>()
val items = if (groupType <= BY_DEFAULT || !libraryIsGrouped) { val items = if (groupType <= BY_DEFAULT || !libraryIsGrouped) {
val categoryAll = Category.createAll( val categoryAll = Category.createAll(
@ -601,6 +606,7 @@ class LibraryPresenter(
it.manga.title + "-" + it.manga.author it.manga.title + "-" + it.manga.author
} }
sectionedLibraryItems[catId] = mangaToRemove sectionedLibraryItems[catId] = mangaToRemove
hiddenItems.addAll(mangaToRemove)
items.removeAll(mangaToRemove) items.removeAll(mangaToRemove)
val headerItem = headerItems[catId] val headerItem = headerItems[catId]
if (headerItem != null) items.add( if (headerItem != null) items.add(
@ -628,7 +634,7 @@ class LibraryPresenter(
this.allCategories = categories this.allCategories = categories
return items return items to hiddenItems
} }
private fun getCustomMangaItems(libraryManga: List<LibraryManga>): Pair<List<LibraryItem>, private fun getCustomMangaItems(libraryManga: List<LibraryManga>): Pair<List<LibraryItem>,