From 6ad67082307a833a6e05d2ea6712f3f0407c463f Mon Sep 17 00:00:00 2001 From: TraceL Date: Thu, 13 Apr 2023 02:44:44 +0700 Subject: [PATCH] feat: allow option to skip duplicates when downloading chapters --- .../settings/screen/SettingsDownloadScreen.kt | 4 +++ .../tachiyomi/ui/manga/MangaScreenModel.kt | 34 +++++++++++++++---- .../download/service/DownloadPreferences.kt | 2 ++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDownloadScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDownloadScreen.kt index fa6b0af2bc..2c9a9b9d0e 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDownloadScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDownloadScreen.kt @@ -60,6 +60,10 @@ object SettingsDownloadScreen : SearchableSettings { title = stringResource(R.string.split_tall_images), subtitle = stringResource(R.string.split_tall_images_summary), ), + Preference.PreferenceItem.SwitchPreference( + pref = downloadPreferences.skipDupe(), + title = stringResource(R.string.pref_skip_dupe_chapters), + ), getDeleteChaptersGroup( downloadPreferences = downloadPreferences, categories = allCategories, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt index aab7179f35..bdb3f83f9d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt @@ -600,13 +600,35 @@ class MangaInfoScreenModel( } fun runDownloadAction(action: DownloadAction) { - val chaptersToDownload = when (action) { - DownloadAction.NEXT_1_CHAPTER -> getUnreadChaptersSorted().take(1) - DownloadAction.NEXT_5_CHAPTERS -> getUnreadChaptersSorted().take(5) - DownloadAction.NEXT_10_CHAPTERS -> getUnreadChaptersSorted().take(10) - DownloadAction.NEXT_25_CHAPTERS -> getUnreadChaptersSorted().take(25) - DownloadAction.UNREAD_CHAPTERS -> getUnreadChapters() + val validChapters = getUnreadChaptersSorted() + val amount = when (action) { + DownloadAction.NEXT_1_CHAPTER -> 1 + DownloadAction.NEXT_5_CHAPTERS -> 5 + DownloadAction.NEXT_10_CHAPTERS -> 10 + DownloadAction.NEXT_25_CHAPTERS -> 25 + DownloadAction.UNREAD_CHAPTERS -> validChapters.size } + + val chaptersToDownload: List = kotlin.run { + if (downloadPreferences.skipDupe().get()) { + mutableListOf().apply { + val firstChapter = validChapters.firstOrNull() ?: return + for (chapterEntry in validChapters.groupBy { it.chapterNumber }) { + if (size == amount) break + if (any { it.chapterNumber == chapterEntry.key }) continue + + add( + chapterEntry.value.find { it.id == firstChapter.id } + ?: chapterEntry.value.find { it.scanlator == firstChapter.scanlator } + ?: chapterEntry.value.first(), + ) + } + } + } else { + validChapters.take(amount) + } + } + if (chaptersToDownload.isNotEmpty()) { startDownload(chaptersToDownload, false) } diff --git a/domain/src/main/java/tachiyomi/domain/download/service/DownloadPreferences.kt b/domain/src/main/java/tachiyomi/domain/download/service/DownloadPreferences.kt index cc930cf513..fb248acad3 100644 --- a/domain/src/main/java/tachiyomi/domain/download/service/DownloadPreferences.kt +++ b/domain/src/main/java/tachiyomi/domain/download/service/DownloadPreferences.kt @@ -16,6 +16,8 @@ class DownloadPreferences( fun splitTallImages() = preferenceStore.getBoolean("split_tall_images", false) + fun skipDupe() = preferenceStore.getBoolean("pref_download_skip_dupe", false) + fun autoDownloadWhileReading() = preferenceStore.getInt("auto_download_while_reading", 0) fun removeAfterReadSlots() = preferenceStore.getInt("remove_after_read_slots", -1)