Skip to content

Commit

Permalink
Cleanup LibraryScreenModel LibraryMap.applySort and some more
Browse files Browse the repository at this point in the history
  • Loading branch information
AntsyLich committed Oct 11, 2024
1 parent 016f627 commit 2beb89d
Showing 1 changed file with 18 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -171,12 +168,9 @@ class LibraryScreenModel(
.launchIn(screenModelScope)
}

/**
* Applies library filters to the given map of manga.
*/
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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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<MangaId, List<Track>>
trackMap: Map<Long, List<Track>>,
loggedInTrackerIds: Set<Long>,
): LibraryMap {
private fun LibraryMap.applySort(trackMap: Map<Long, List<Track>>, loggedInTrackerIds: Set<Long>): LibraryMap {
val sortAlphabetically: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
i1.libraryManga.manga.title.lowercase().compareToWithCollator(i2.libraryManga.manga.title.lowercase())
}
Expand All @@ -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<LibraryItem> = Comparator { i1, i2 ->
when (this.type) {
LibrarySort.Type.Alphabetical -> {
sortAlphabetically(i1, i2)
}
Expand All @@ -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 -> {
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 2beb89d

Please sign in to comment.