Fix downloaded badges appearing when filtering by downloaded

Fixes #8850
This commit is contained in:
arkon 2023-01-07 10:32:14 -05:00
parent 920ca405a2
commit c4c6e41c46

View File

@ -96,7 +96,8 @@ class LibraryScreenModel(
getLibraryFlow(), getLibraryFlow(),
getTracksPerManga.subscribe(), getTracksPerManga.subscribe(),
getTrackingFilterFlow(), getTrackingFilterFlow(),
) { searchQuery, library, tracks, loggedInTrackServices -> downloadCache.changes,
) { searchQuery, library, tracks, loggedInTrackServices, _ ->
library library
.applyFilters(tracks, loggedInTrackServices) .applyFilters(tracks, loggedInTrackServices)
.applySort() .applySort()
@ -180,14 +181,12 @@ class LibraryScreenModel(
val includedTracks = loggedInTrackServices.mapNotNull { if (it.value == TriStateGroup.State.INCLUDE.value) it.key else null } val includedTracks = loggedInTrackServices.mapNotNull { if (it.value == TriStateGroup.State.INCLUDE.value) it.key else null }
val trackFiltersIsIgnored = includedTracks.isEmpty() && excludedTracks.isEmpty() val trackFiltersIsIgnored = includedTracks.isEmpty() && excludedTracks.isEmpty()
val filterFnDownloaded: (LibraryItem) -> Boolean = downloaded@{ item -> val filterFnDownloaded: (LibraryItem) -> Boolean = downloaded@{
if (!downloadedOnly && filterDownloaded == TriStateGroup.State.IGNORE.value) return@downloaded true if (!downloadedOnly && filterDownloaded == TriStateGroup.State.IGNORE.value) return@downloaded true
val isDownloaded = when {
item.libraryManga.manga.isLocal() -> true
item.downloadCount != -1L -> item.downloadCount > 0
else -> downloadManager.getDownloadCount(item.libraryManga.manga) > 0
}
val isDownloaded = it.libraryManga.manga.isLocal() ||
it.downloadCount > 0 ||
downloadManager.getDownloadCount(it.libraryManga.manga) > 0
return@downloaded if (downloadedOnly || filterDownloaded == TriStateGroup.State.INCLUDE.value) { return@downloaded if (downloadedOnly || filterDownloaded == TriStateGroup.State.INCLUDE.value) {
isDownloaded isDownloaded
} else { } else {
@ -195,10 +194,10 @@ class LibraryScreenModel(
} }
} }
val filterFnUnread: (LibraryItem) -> Boolean = unread@{ item -> val filterFnUnread: (LibraryItem) -> Boolean = unread@{
if (filterUnread == TriStateGroup.State.IGNORE.value) return@unread true if (filterUnread == TriStateGroup.State.IGNORE.value) return@unread true
val isUnread = item.libraryManga.unreadCount > 0
val isUnread = it.libraryManga.unreadCount > 0
return@unread if (filterUnread == TriStateGroup.State.INCLUDE.value) { return@unread if (filterUnread == TriStateGroup.State.INCLUDE.value) {
isUnread isUnread
} else { } else {
@ -206,10 +205,10 @@ class LibraryScreenModel(
} }
} }
val filterFnStarted: (LibraryItem) -> Boolean = started@{ item -> val filterFnStarted: (LibraryItem) -> Boolean = started@{
if (filterStarted == TriStateGroup.State.IGNORE.value) return@started true if (filterStarted == TriStateGroup.State.IGNORE.value) return@started true
val hasStarted = item.libraryManga.hasStarted
val hasStarted = it.libraryManga.hasStarted
return@started if (filterStarted == TriStateGroup.State.INCLUDE.value) { return@started if (filterStarted == TriStateGroup.State.INCLUDE.value) {
hasStarted hasStarted
} else { } else {
@ -217,11 +216,10 @@ class LibraryScreenModel(
} }
} }
val filterFnBookmarked: (LibraryItem) -> Boolean = bookmarked@{ item -> val filterFnBookmarked: (LibraryItem) -> Boolean = bookmarked@{
if (filterBookmarked == TriStateGroup.State.IGNORE.value) return@bookmarked true if (filterBookmarked == TriStateGroup.State.IGNORE.value) return@bookmarked true
val hasBookmarks = item.libraryManga.hasBookmarks val hasBookmarks = it.libraryManga.hasBookmarks
return@bookmarked if (filterBookmarked == TriStateGroup.State.INCLUDE.value) { return@bookmarked if (filterBookmarked == TriStateGroup.State.INCLUDE.value) {
hasBookmarks hasBookmarks
} else { } else {
@ -229,10 +227,10 @@ class LibraryScreenModel(
} }
} }
val filterFnCompleted: (LibraryItem) -> Boolean = completed@{ item -> val filterFnCompleted: (LibraryItem) -> Boolean = completed@{
if (filterCompleted == TriStateGroup.State.IGNORE.value) return@completed true if (filterCompleted == TriStateGroup.State.IGNORE.value) return@completed true
val isCompleted = item.libraryManga.manga.status.toInt() == SManga.COMPLETED
val isCompleted = it.libraryManga.manga.status.toInt() == SManga.COMPLETED
return@completed if (filterCompleted == TriStateGroup.State.INCLUDE.value) { return@completed if (filterCompleted == TriStateGroup.State.INCLUDE.value) {
isCompleted isCompleted
} else { } else {
@ -260,14 +258,14 @@ class LibraryScreenModel(
return@tracking false return@tracking false
} }
val filterFn: (LibraryItem) -> Boolean = filter@{ item -> val filterFn: (LibraryItem) -> Boolean = filter@{
return@filter !( return@filter !(
!filterFnDownloaded(item) || !filterFnDownloaded(it) ||
!filterFnUnread(item) || !filterFnUnread(it) ||
!filterFnStarted(item) || !filterFnStarted(it) ||
!filterFnBookmarked(item) || !filterFnBookmarked(it) ||
!filterFnCompleted(item) || !filterFnCompleted(it) ||
!filterFnTracking(item) !filterFnTracking(it)
) )
} }
@ -363,8 +361,6 @@ class LibraryScreenModel(
/** /**
* Get the categories and all its manga from the database. * Get the categories and all its manga from the database.
*
* @return an observable of the categories and its manga.
*/ */
private fun getLibraryFlow(): Flow<LibraryMap> { private fun getLibraryFlow(): Flow<LibraryMap> {
val libraryMangasFlow = combine( val libraryMangasFlow = combine(
@ -374,13 +370,9 @@ class LibraryScreenModel(
) { libraryMangaList, prefs, _ -> ) { libraryMangaList, prefs, _ ->
libraryMangaList libraryMangaList
.map { libraryManga -> .map { libraryManga ->
val needsDownloadCounts = prefs.downloadBadge ||
prefs.filterDownloaded != TriStateGroup.State.IGNORE.value ||
prefs.globalFilterDownloaded
// Display mode based on user preference: take it from global library setting or category // Display mode based on user preference: take it from global library setting or category
LibraryItem(libraryManga).apply { LibraryItem(libraryManga).apply {
downloadCount = if (needsDownloadCounts) { downloadCount = if (prefs.downloadBadge) {
downloadManager.getDownloadCount(libraryManga.manga).toLong() downloadManager.getDownloadCount(libraryManga.manga).toLong()
} else { } else {
0 0