Improve chapters mapping

This commit is contained in:
Koitharu
2023-10-12 13:24:51 +03:00
parent 3def71ccc1
commit cc2f9d4529

View File

@@ -21,12 +21,11 @@ fun MangaDetails.mapChapters(
val bookmarked = bookmarks.mapToSet { it.chapterId } val bookmarked = bookmarks.mapToSet { it.chapterId }
val currentId = history?.chapterId ?: 0L val currentId = history?.chapterId ?: 0L
val newFrom = if (newCount == 0 || remoteChapters.isEmpty()) Int.MAX_VALUE else remoteChapters.size - newCount val newFrom = if (newCount == 0 || remoteChapters.isEmpty()) Int.MAX_VALUE else remoteChapters.size - newCount
val chaptersSize = maxOf(remoteChapters.size, localChapters.size) val ids = buildSet(maxOf(remoteChapters.size, localChapters.size)) {
val ids = buildSet(chaptersSize) {
remoteChapters.mapTo(this) { it.id } remoteChapters.mapTo(this) { it.id }
localChapters.mapTo(this) { it.id } localChapters.mapTo(this) { it.id }
} }
val result = ArrayList<ChapterListItem>(chaptersSize) val result = ArrayList<ChapterListItem>(ids.size)
val localMap = if (localChapters.isNotEmpty()) { val localMap = if (localChapters.isNotEmpty()) {
localChapters.associateByTo(LinkedHashMap(localChapters.size)) { it.id } localChapters.associateByTo(LinkedHashMap(localChapters.size)) { it.id }
} else { } else {
@@ -38,7 +37,7 @@ fun MangaDetails.mapChapters(
if (chapter.id == currentId) { if (chapter.id == currentId) {
isUnread = true isUnread = true
} }
result += chapter.toListItem( result += (local ?: chapter).toListItem(
isCurrent = chapter.id == currentId, isCurrent = chapter.id == currentId,
isUnread = isUnread, isUnread = isUnread,
isNew = isUnread && result.size >= newFrom, isNew = isUnread && result.size >= newFrom,