diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt index 59121bcaf..10875ff13 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt @@ -72,7 +72,6 @@ import tachiyomi.domain.track.model.Track import tachiyomi.source.local.isLocal import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -import java.util.Collections /** * Typealias for the library manga, using the category as keys, and list of manga as values. @@ -107,16 +106,14 @@ class LibraryScreenModel( getTracksPerManga.subscribe(), getTrackingFilterFlow(), downloadCache.changes, - ) { searchQuery, library, tracks, trackingFiler, _ -> + ) { searchQuery, library, tracks, trackingFilter, _ -> library - .applyFilters(tracks, trackingFiler) - .applySort(tracks, trackingFiler.keys) + .applyFilters(tracks, trackingFilter) + .applySort(tracks, trackingFilter.keys) .mapValues { (_, value) -> if (searchQuery != null) { - // Filter query value.filter { it.matches(searchQuery) } } else { - // Don't do anything value } } @@ -171,12 +168,9 @@ class LibraryScreenModel( .launchIn(screenModelScope) } - /** - * Applies library filters to the given map of manga. - */ private suspend fun LibraryMap.applyFilters( trackMap: Map>, - trackingFiler: Map, + trackingFilter: Map, ): LibraryMap { val prefs = getLibraryItemPreferencesFlow().first() val downloadedOnly = prefs.globalFilterDownloaded @@ -188,10 +182,10 @@ class LibraryScreenModel( val filterCompleted = prefs.filterCompleted val filterIntervalCustom = prefs.filterIntervalCustom - val isNotLoggedInAnyTrack = trackingFiler.isEmpty() + val isNotLoggedInAnyTrack = trackingFilter.isEmpty() - val excludedTracks = trackingFiler.mapNotNull { if (it.value == TriState.ENABLED_NOT) it.key else null } - val includedTracks = trackingFiler.mapNotNull { if (it.value == TriState.ENABLED_IS) it.key else null } + val excludedTracks = trackingFilter.mapNotNull { if (it.value == TriState.ENABLED_NOT) it.key else null } + val includedTracks = trackingFilter.mapNotNull { if (it.value == TriState.ENABLED_IS) it.key else null } val trackFiltersIsIgnored = includedTracks.isEmpty() && excludedTracks.isEmpty() val filterFnDownloaded: (LibraryItem) -> Boolean = { @@ -249,17 +243,10 @@ class LibraryScreenModel( filterFnTracking(it) } - return this.mapValues { entry -> entry.value.fastFilter(filterFn) } + return mapValues { (_, value) -> value.fastFilter(filterFn) } } - /** - * Applies library sorting to the given map of manga. - */ - private fun LibraryMap.applySort( - // Map> - trackMap: Map>, - loggedInTrackerIds: Set, - ): LibraryMap { + private fun LibraryMap.applySort(trackMap: Map>, loggedInTrackerIds: Set): LibraryMap { val sortAlphabetically: (LibraryItem, LibraryItem) -> Int = { i1, i2 -> i1.libraryManga.manga.title.lowercase().compareToWithCollator(i2.libraryManga.manga.title.lowercase()) } @@ -278,9 +265,8 @@ class LibraryScreenModel( } } - val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 -> - val sort = keys.find { it.id == i1.libraryManga.category }!!.sort - when (sort.type) { + fun LibrarySort.comparator(): Comparator = Comparator { i1, i2 -> + when (this.type) { LibrarySort.Type.Alphabetical -> { sortAlphabetically(i1, i2) } @@ -293,8 +279,8 @@ class LibraryScreenModel( LibrarySort.Type.UnreadCount -> when { // Ensure unread content comes first i1.libraryManga.unreadCount == i2.libraryManga.unreadCount -> 0 - i1.libraryManga.unreadCount == 0L -> if (sort.isAscending) 1 else -1 - i2.libraryManga.unreadCount == 0L -> if (sort.isAscending) -1 else 1 + i1.libraryManga.unreadCount == 0L -> if (this.isAscending) 1 else -1 + i2.libraryManga.unreadCount == 0L -> if (this.isAscending) -1 else 1 else -> i1.libraryManga.unreadCount.compareTo(i2.libraryManga.unreadCount) } LibrarySort.Type.TotalChapters -> { @@ -317,14 +303,12 @@ class LibraryScreenModel( } } - return this.mapValues { entry -> - val comparator = if (keys.find { it.id == entry.key.id }!!.sort.isAscending) { - Comparator(sortFn) - } else { - Collections.reverseOrder(sortFn) - } + return mapValues { (key, value) -> + val comparator = key.sort.comparator() + .let { if (key.sort.isAscending) it else it.reversed() } + .thenComparator(sortAlphabetically) - entry.value.sortedWith(comparator.thenComparator(sortAlphabetically)) + value.sortedWith(comparator) } }