From c7a97711c0082ab3b48162ff1d6a2076e1d14885 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 4 Aug 2022 11:55:59 +0300 Subject: [PATCH] Optimize chapters mapping --- .../details/ui/MangaDetailsDelegate.kt | 9 ++++++-- .../details/ui/model/ChapterListItem.kt | 21 +++++++++++++++---- .../ui/model/ListModelConversionExt.kt | 5 +++-- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/koitharu/kotatsu/details/ui/MangaDetailsDelegate.kt b/app/src/main/java/org/koitharu/kotatsu/details/ui/MangaDetailsDelegate.kt index 9ad1accfa..49bbde5ce 100644 --- a/app/src/main/java/org/koitharu/kotatsu/details/ui/MangaDetailsDelegate.kt +++ b/app/src/main/java/org/koitharu/kotatsu/details/ui/MangaDetailsDelegate.kt @@ -16,7 +16,6 @@ import org.koitharu.kotatsu.parsers.exception.NotFoundException import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaChapter import org.koitharu.kotatsu.parsers.model.MangaSource -import org.koitharu.kotatsu.parsers.util.mapToSet import org.koitharu.kotatsu.utils.ext.printStackTraceDebug class MangaDetailsDelegate( @@ -84,7 +83,7 @@ class MangaDetailsDelegate( val dateFormat = settings.getDateFormat() val currentIndex = chapters.indexOfFirst { it.id == currentId } val firstNewIndex = chapters.size - newCount - val downloadedIds = downloadedChapters?.mapToSet { it.id } + val downloadedIds = downloadedChapters?.mapTo(HashSet(downloadedChapters.size)) { it.id } for (i in chapters.indices) { val chapter = chapters[i] if (chapter.branch != branch) { @@ -99,6 +98,9 @@ class MangaDetailsDelegate( dateFormat = dateFormat, ) } + if (result.size < chapters.size / 2) { + result.trimToSize() + } return result } @@ -154,6 +156,9 @@ class MangaDetailsDelegate( } result.sortBy { it.chapter.number } } + if (result.size < sourceChapters.size / 2) { + result.trimToSize() + } return result } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/details/ui/model/ChapterListItem.kt b/app/src/main/java/org/koitharu/kotatsu/details/ui/model/ChapterListItem.kt index 2d5b90840..c3e39efec 100644 --- a/app/src/main/java/org/koitharu/kotatsu/details/ui/model/ChapterListItem.kt +++ b/app/src/main/java/org/koitharu/kotatsu/details/ui/model/ChapterListItem.kt @@ -1,13 +1,24 @@ package org.koitharu.kotatsu.details.ui.model +import java.text.DateFormat import org.koitharu.kotatsu.parsers.model.MangaChapter class ChapterListItem( val chapter: MangaChapter, val flags: Int, - val uploadDate: String?, + private val uploadDateMs: Long, + private val dateFormat: DateFormat, ) { + var uploadDate: String? = null + private set + get() { + if (field != null) return field + if (uploadDateMs == 0L) return null + field = dateFormat.format(uploadDateMs) + return field + } + val status: Int get() = flags and MASK_STATUS @@ -32,7 +43,8 @@ class ChapterListItem( if (chapter != other.chapter) return false if (flags != other.flags) return false - if (uploadDate != other.uploadDate) return false + if (uploadDateMs != other.uploadDateMs) return false + if (dateFormat != other.dateFormat) return false return true } @@ -40,7 +52,8 @@ class ChapterListItem( override fun hashCode(): Int { var result = chapter.hashCode() result = 31 * result + flags - result = 31 * result + (uploadDate?.hashCode() ?: 0) + result = 31 * result + uploadDateMs.hashCode() + result = 31 * result + dateFormat.hashCode() return result } @@ -53,4 +66,4 @@ class ChapterListItem( const val FLAG_DOWNLOADED = 32 const val MASK_STATUS = FLAG_UNREAD or FLAG_CURRENT } -} +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/details/ui/model/ListModelConversionExt.kt b/app/src/main/java/org/koitharu/kotatsu/details/ui/model/ListModelConversionExt.kt index 22d272346..e15669f94 100644 --- a/app/src/main/java/org/koitharu/kotatsu/details/ui/model/ListModelConversionExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/details/ui/model/ListModelConversionExt.kt @@ -1,12 +1,12 @@ package org.koitharu.kotatsu.details.ui.model +import java.text.DateFormat import org.koitharu.kotatsu.details.ui.model.ChapterListItem.Companion.FLAG_CURRENT import org.koitharu.kotatsu.details.ui.model.ChapterListItem.Companion.FLAG_DOWNLOADED import org.koitharu.kotatsu.details.ui.model.ChapterListItem.Companion.FLAG_MISSING import org.koitharu.kotatsu.details.ui.model.ChapterListItem.Companion.FLAG_NEW import org.koitharu.kotatsu.details.ui.model.ChapterListItem.Companion.FLAG_UNREAD import org.koitharu.kotatsu.parsers.model.MangaChapter -import java.text.DateFormat fun MangaChapter.toListItem( isCurrent: Boolean, @@ -25,6 +25,7 @@ fun MangaChapter.toListItem( return ChapterListItem( chapter = this, flags = flags, - uploadDate = if (uploadDate != 0L) dateFormat.format(uploadDate) else null + uploadDateMs = uploadDate, + dateFormat = dateFormat, ) } \ No newline at end of file