Cleanup LibraryScreenModel LibraryMap.applySort and some more

(cherry picked from commit 2beb89d53163a6d288f8acdebe0f5d26fea8ab3e)
This commit is contained in:
AntsyLich 2024-10-12 04:56:41 +06:00 committed by Cuong-Tran
parent fa32aea6d0
commit f3c48d3ad8
No known key found for this signature in database
GPG key ID: 733AA7624B9315C2

View file

@ -119,7 +119,6 @@ import tachiyomi.source.local.LocalSource
import tachiyomi.source.local.isLocal import tachiyomi.source.local.isLocal
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.util.Collections
import tachiyomi.domain.source.model.Source as DomainSource import tachiyomi.domain.source.model.Source as DomainSource
/** /**
@ -184,15 +183,15 @@ class LibraryScreenModel(
::Pair, ::Pair,
), ),
// SY <-- // SY <--
) { searchQuery, library, tracks, (trackingFiler, _), (groupType, sort) -> ) { searchQuery, library, tracks, (trackingFilter, _), (groupType, sort) ->
library library
// SY --> // SY -->
.applyGrouping(groupType) .applyGrouping(groupType)
// SY <-- // SY <--
.applyFilters(tracks, trackingFiler) .applyFilters(tracks, trackingFilter)
.applySort( .applySort(
tracks, tracks,
trackingFiler.keys, trackingFilter.keys,
// SY --> // SY -->
sort.takeIf { groupType != LibraryGroup.BY_DEFAULT }, sort.takeIf { groupType != LibraryGroup.BY_DEFAULT },
// SY <-- // SY <--
@ -201,7 +200,7 @@ class LibraryScreenModel(
if (searchQuery != null) { if (searchQuery != null) {
// Filter query // Filter query
// SY --> // SY -->
filterLibrary(value, searchQuery, trackingFiler) filterLibrary(value, searchQuery, trackingFilter)
// SY <-- // SY <--
} else { } else {
// Don't do anything // Don't do anything
@ -292,7 +291,7 @@ class LibraryScreenModel(
*/ */
private suspend fun LibraryMap.applyFilters( private suspend fun LibraryMap.applyFilters(
trackMap: Map<Long, List<Track>>, trackMap: Map<Long, List<Track>>,
trackingFiler: Map<Long, TriState>, trackingFilter: Map<Long, TriState>,
): LibraryMap { ): LibraryMap {
val prefs = getLibraryItemPreferencesFlow().first() val prefs = getLibraryItemPreferencesFlow().first()
val downloadedOnly = prefs.globalFilterDownloaded val downloadedOnly = prefs.globalFilterDownloaded
@ -304,10 +303,10 @@ class LibraryScreenModel(
val filterCompleted = prefs.filterCompleted val filterCompleted = prefs.filterCompleted
val filterIntervalCustom = prefs.filterIntervalCustom 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 excludedTracks = trackingFilter.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 includedTracks = trackingFilter.mapNotNull { if (it.value == TriState.ENABLED_IS) it.key else null }
val trackFiltersIsIgnored = includedTracks.isEmpty() && excludedTracks.isEmpty() val trackFiltersIsIgnored = includedTracks.isEmpty() && excludedTracks.isEmpty()
// SY --> // SY -->
@ -378,14 +377,13 @@ class LibraryScreenModel(
// SY <-- // SY <--
} }
return this.mapValues { entry -> entry.value.fastFilter(filterFn) } return mapValues { (_, value) -> value.fastFilter(filterFn) }
} }
/** /**
* Applies library sorting to the given map of manga. * Applies library sorting to the given map of manga.
*/ */
private fun LibraryMap.applySort( private fun LibraryMap.applySort(
// Map<MangaId, List<Track>>
trackMap: Map<Long, List<Track>>, trackMap: Map<Long, List<Track>>,
loggedInTrackerIds: Set<Long>, loggedInTrackerIds: Set<Long>,
// SY --> // SY -->
@ -425,9 +423,9 @@ class LibraryScreenModel(
} }
} }
val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 -> fun LibrarySort.comparator(): Comparator<LibraryItem> = Comparator { i1, i2 ->
// SY --> // SY -->
val sort = groupSort ?: keys.find { it.id == i1.libraryManga.category }!!.sort val sort = groupSort ?: this
// SY <-- // SY <--
when (sort.type) { when (sort.type) {
LibrarySort.Type.Alphabetical -> { LibrarySort.Type.Alphabetical -> {
@ -477,17 +475,18 @@ class LibraryScreenModel(
} }
} }
return this.mapValues { entry -> return mapValues { (key, value) ->
// SY --> val comparator = key.sort.comparator()
val isAscending = groupSort?.isAscending ?: keys.find { it.id == entry.key.id }!!.sort.isAscending .let {
// SY <-- if (/* SY --> */ groupSort?.isAscending ?: /* SY <-- */ key.sort.isAscending) {
val comparator = if ( /* SY --> */ isAscending /* SY <-- */) { it
Comparator(sortFn) } else {
} else { it.reversed()
Collections.reverseOrder(sortFn) }
} }
.thenComparator(sortAlphabetically)
entry.value.sortedWith(comparator.thenComparator(sortAlphabetically)) value.sortedWith(comparator)
} }
} }