feat: allow option to skip duplicates when downloading chapters

This commit is contained in:
TraceL 2023-04-13 02:44:44 +07:00
parent e3fbd26880
commit 6ad6708230
3 changed files with 34 additions and 6 deletions

View File

@ -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,

View File

@ -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<Chapter> = kotlin.run {
if (downloadPreferences.skipDupe().get()) {
mutableListOf<Chapter>().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)
}

View File

@ -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)