Simplify group by code further

This commit is contained in:
Jobobby04 2022-08-07 13:32:41 -04:00
parent ffa8cee0ea
commit ce16496275

View File

@ -1170,45 +1170,38 @@ class LibraryPresenter(
} }
private fun getGroupedMangaItems(groupType: Int, libraryManga: List<LibraryItem>): Pair<LibraryMap, List<Category>> { private fun getGroupedMangaItems(groupType: Int, libraryManga: List<LibraryItem>): Pair<LibraryMap, List<Category>> {
val manga = mutableMapOf<Long, MutableList<LibraryItem>>() val manga = when (groupType) {
when (groupType) {
LibraryGroup.BY_TRACK_STATUS -> { LibraryGroup.BY_TRACK_STATUS -> {
val tracks = runBlocking { getTracks.await() }.groupBy { it.mangaId } val tracks = runBlocking { getTracks.await() }.groupBy { it.mangaId }
libraryManga.forEach { libraryItem -> libraryManga.groupBy { libraryItem ->
val status = tracks[libraryItem.manga.id]?.firstNotNullOfOrNull { track -> val status = tracks[libraryItem.manga.id]?.firstNotNullOfOrNull { track ->
TrackStatus.parseTrackerStatus(track.syncId, track.status) TrackStatus.parseTrackerStatus(track.syncId, track.status)
} ?: TrackStatus.OTHER } ?: TrackStatus.OTHER
manga.getOrPut(status.int.toLong()) { mutableListOf() } += libraryItem status.int
} }.mapKeys { it.key.toLong() }
} }
LibraryGroup.BY_SOURCE -> { LibraryGroup.BY_SOURCE -> {
libraryManga.forEach { libraryItem -> libraryManga.groupBy { libraryItem ->
manga.getOrPut(libraryItem.manga.source) { mutableListOf() } += libraryItem libraryItem.manga.source
} }
} }
else -> { else -> {
libraryManga.forEach { libraryItem -> libraryManga.groupBy { libraryItem ->
manga.getOrPut(libraryItem.manga.status.toLong()) { mutableListOf() } += libraryItem libraryItem.manga.status
} }.mapKeys { it.key.toLong() }
} }
} }
val categories = when (groupType) { val categories = when (groupType) {
LibraryGroup.BY_SOURCE -> manga.keys.map { Category(it, sourceManager.getOrStub(it).name, 0, 0) } LibraryGroup.BY_SOURCE ->
.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name })
LibraryGroup.BY_TRACK_STATUS ->
manga.keys manga.keys
.map { id -> .map { Category(it, sourceManager.getOrStub(it).name, 0, 0) }
TrackStatus.values().find { id == it.int.toLong() } .sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name })
?: TrackStatus.OTHER LibraryGroup.BY_TRACK_STATUS, LibraryGroup.BY_STATUS ->
} manga.keys
.sortedBy { it.int } .sorted()
.map { .map { Category(it, "", 0, 0) }
Category(it.int.toLong(), "", 0, 0)
}
LibraryGroup.BY_STATUS -> manga.keys.sorted().map { Category(it, "", 0, 0) }
else -> throw IllegalStateException("Invalid group type $groupType") else -> throw IllegalStateException("Invalid group type $groupType")
} }