mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-28 22:02:55 -05:00
Cleanup LibraryScreenModel
LibraryMap.applySort
and some more
This commit is contained in:
parent
016f627fb0
commit
2beb89d531
1 changed files with 18 additions and 34 deletions
|
@ -72,7 +72,6 @@ import tachiyomi.domain.track.model.Track
|
||||||
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
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Typealias for the library manga, using the category as keys, and list of manga as values.
|
* Typealias for the library manga, using the category as keys, and list of manga as values.
|
||||||
|
@ -107,16 +106,14 @@ class LibraryScreenModel(
|
||||||
getTracksPerManga.subscribe(),
|
getTracksPerManga.subscribe(),
|
||||||
getTrackingFilterFlow(),
|
getTrackingFilterFlow(),
|
||||||
downloadCache.changes,
|
downloadCache.changes,
|
||||||
) { searchQuery, library, tracks, trackingFiler, _ ->
|
) { searchQuery, library, tracks, trackingFilter, _ ->
|
||||||
library
|
library
|
||||||
.applyFilters(tracks, trackingFiler)
|
.applyFilters(tracks, trackingFilter)
|
||||||
.applySort(tracks, trackingFiler.keys)
|
.applySort(tracks, trackingFilter.keys)
|
||||||
.mapValues { (_, value) ->
|
.mapValues { (_, value) ->
|
||||||
if (searchQuery != null) {
|
if (searchQuery != null) {
|
||||||
// Filter query
|
|
||||||
value.filter { it.matches(searchQuery) }
|
value.filter { it.matches(searchQuery) }
|
||||||
} else {
|
} else {
|
||||||
// Don't do anything
|
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,12 +168,9 @@ class LibraryScreenModel(
|
||||||
.launchIn(screenModelScope)
|
.launchIn(screenModelScope)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Applies library filters to the given map of manga.
|
|
||||||
*/
|
|
||||||
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
|
||||||
|
@ -188,10 +182,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()
|
||||||
|
|
||||||
val filterFnDownloaded: (LibraryItem) -> Boolean = {
|
val filterFnDownloaded: (LibraryItem) -> Boolean = {
|
||||||
|
@ -249,17 +243,10 @@ class LibraryScreenModel(
|
||||||
filterFnTracking(it)
|
filterFnTracking(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.mapValues { entry -> entry.value.fastFilter(filterFn) }
|
return mapValues { (_, value) -> value.fastFilter(filterFn) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private fun LibraryMap.applySort(trackMap: Map<Long, List<Track>>, loggedInTrackerIds: Set<Long>): LibraryMap {
|
||||||
* 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 {
|
|
||||||
val sortAlphabetically: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
|
val sortAlphabetically: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
|
||||||
i1.libraryManga.manga.title.lowercase().compareToWithCollator(i2.libraryManga.manga.title.lowercase())
|
i1.libraryManga.manga.title.lowercase().compareToWithCollator(i2.libraryManga.manga.title.lowercase())
|
||||||
}
|
}
|
||||||
|
@ -278,9 +265,8 @@ class LibraryScreenModel(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val sortFn: (LibraryItem, LibraryItem) -> Int = { i1, i2 ->
|
fun LibrarySort.comparator(): Comparator<LibraryItem> = Comparator { i1, i2 ->
|
||||||
val sort = keys.find { it.id == i1.libraryManga.category }!!.sort
|
when (this.type) {
|
||||||
when (sort.type) {
|
|
||||||
LibrarySort.Type.Alphabetical -> {
|
LibrarySort.Type.Alphabetical -> {
|
||||||
sortAlphabetically(i1, i2)
|
sortAlphabetically(i1, i2)
|
||||||
}
|
}
|
||||||
|
@ -293,8 +279,8 @@ class LibraryScreenModel(
|
||||||
LibrarySort.Type.UnreadCount -> when {
|
LibrarySort.Type.UnreadCount -> when {
|
||||||
// Ensure unread content comes first
|
// Ensure unread content comes first
|
||||||
i1.libraryManga.unreadCount == i2.libraryManga.unreadCount -> 0
|
i1.libraryManga.unreadCount == i2.libraryManga.unreadCount -> 0
|
||||||
i1.libraryManga.unreadCount == 0L -> if (sort.isAscending) 1 else -1
|
i1.libraryManga.unreadCount == 0L -> if (this.isAscending) 1 else -1
|
||||||
i2.libraryManga.unreadCount == 0L -> if (sort.isAscending) -1 else 1
|
i2.libraryManga.unreadCount == 0L -> if (this.isAscending) -1 else 1
|
||||||
else -> i1.libraryManga.unreadCount.compareTo(i2.libraryManga.unreadCount)
|
else -> i1.libraryManga.unreadCount.compareTo(i2.libraryManga.unreadCount)
|
||||||
}
|
}
|
||||||
LibrarySort.Type.TotalChapters -> {
|
LibrarySort.Type.TotalChapters -> {
|
||||||
|
@ -317,14 +303,12 @@ class LibraryScreenModel(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.mapValues { entry ->
|
return mapValues { (key, value) ->
|
||||||
val comparator = if (keys.find { it.id == entry.key.id }!!.sort.isAscending) {
|
val comparator = key.sort.comparator()
|
||||||
Comparator(sortFn)
|
.let { if (key.sort.isAscending) it else it.reversed() }
|
||||||
} else {
|
.thenComparator(sortAlphabetically)
|
||||||
Collections.reverseOrder(sortFn)
|
|
||||||
}
|
|
||||||
|
|
||||||
entry.value.sortedWith(comparator.thenComparator(sortAlphabetically))
|
value.sortedWith(comparator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue