From 4816b4b53afa6db5dfaa2b023a4896e31425f97c Mon Sep 17 00:00:00 2001 From: Trace <49712094+TraceLosu@users.noreply.github.com> Date: Sat, 15 Apr 2023 20:34:02 +0700 Subject: [PATCH] fix: skip duplicate chapters on download ahead if option to skip duplicates is enabled (#9334) * fix: skip duplicate chapters on download ahead if option is enabled * fix: Use a function to filter duplicates --- .../tachiyomi/ui/reader/ReaderViewModel.kt | 18 ++++++++++-------- .../util/chapter/ChapterRemoveDuplicates.kt | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterRemoveDuplicates.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt index 57c8480918..32e74f43df 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt @@ -35,6 +35,7 @@ import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters import eu.kanade.tachiyomi.ui.reader.setting.OrientationType import eu.kanade.tachiyomi.ui.reader.setting.ReaderPreferences import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType +import eu.kanade.tachiyomi.util.chapter.removeDuplicates import eu.kanade.tachiyomi.util.editCover import eu.kanade.tachiyomi.util.lang.byteSize import eu.kanade.tachiyomi.util.lang.takeBytes @@ -175,12 +176,7 @@ class ReaderViewModel( else -> chapters }.run { if (readerPreferences.skipDupe().get()) { - groupBy { it.chapterNumber } - .map { (_, chapters) -> - chapters.find { it.id == selectedChapter.id } - ?: chapters.find { it.scanlator == selectedChapter.scanlator } - ?: chapters.first() - } + removeDuplicates(selectedChapter) } else { this } @@ -456,8 +452,14 @@ class ReaderViewModel( ) if (!isNextChapterDownloaded) return@launchIO - val chaptersToDownload = getNextChapters.await(manga.id, nextChapter.id!!) - .take(amount) + val chaptersToDownload = getNextChapters.await(manga.id, nextChapter.id!!).run { + if (readerPreferences.skipDupe().get()) { + removeDuplicates(nextChapter.toDomainChapter()!!) + } else { + this + } + }.take(amount) + downloadManager.downloadChapters( manga, chaptersToDownload, diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterRemoveDuplicates.kt b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterRemoveDuplicates.kt new file mode 100644 index 0000000000..d6e026f8de --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterRemoveDuplicates.kt @@ -0,0 +1,15 @@ +package eu.kanade.tachiyomi.util.chapter + +import tachiyomi.domain.chapter.model.Chapter + +/** + * Returns a copy of the list with duplicate chapters removed + */ +fun List.removeDuplicates(currentChapter: Chapter): List { + return groupBy { it.chapterNumber } + .map { (_, chapters) -> + chapters.find { it.id == currentChapter.id } + ?: chapters.find { it.scanlator == currentChapter.scanlator } + ?: chapters.first() + } +}