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