From 8735f3566ff1b7945ba062d098302875fdf8eeab Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 27 May 2023 18:30:59 -0400 Subject: [PATCH] Fix bookmarked chapters being deleted after manually marked as read Fixes #9520 --- .../data/download/DownloadManager.kt | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index 21b5814e35..184269ea0e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -214,19 +214,21 @@ class DownloadManager( * @param source the source of the chapters. */ fun deleteChapters(chapters: List, manga: Manga, source: Source) { - val filteredChapters = getChaptersToDelete(chapters, manga) - if (filteredChapters.isNotEmpty()) { - launchIO { - removeFromDownloadQueue(filteredChapters) + launchIO { + val filteredChapters = getChaptersToDelete(chapters, manga) + if (filteredChapters.isEmpty()) { + return@launchIO + } - val (mangaDir, chapterDirs) = provider.findChapterDirs(filteredChapters, manga, source) - chapterDirs.forEach { it.delete() } - cache.removeChapters(filteredChapters, manga) + removeFromDownloadQueue(filteredChapters) - // Delete manga directory if empty - if (mangaDir?.listFiles()?.isEmpty() == true) { - deleteManga(manga, source, removeQueued = false) - } + val (mangaDir, chapterDirs) = provider.findChapterDirs(filteredChapters, manga, source) + chapterDirs.forEach { it.delete() } + cache.removeChapters(filteredChapters, manga) + + // Delete manga directory if empty + if (mangaDir?.listFiles()?.isEmpty() == true) { + deleteManga(manga, source, removeQueued = false) } } } @@ -278,7 +280,7 @@ class DownloadManager( * @param chapters the list of chapters to delete. * @param manga the manga of the chapters. */ - fun enqueueChaptersToDelete(chapters: List, manga: Manga) { + suspend fun enqueueChaptersToDelete(chapters: List, manga: Manga) { pendingDeleter.addChapters(getChaptersToDelete(chapters, manga), manga) } @@ -351,22 +353,24 @@ class DownloadManager( } } - private fun getChaptersToDelete(chapters: List, manga: Manga): List { + private suspend fun getChaptersToDelete(chapters: List, manga: Manga): List { // Retrieve the categories that are set to exclude from being deleted on read val categoriesToExclude = downloadPreferences.removeExcludeCategories().get().map(String::toLong) - val categoriesForManga = runBlocking { getCategories.await(manga.id) } + val categoriesForManga = getCategories.await(manga.id) .map { it.id } - .takeUnless { it.isEmpty() } - ?: listOf(0) - - return if (categoriesForManga.intersect(categoriesToExclude).isNotEmpty()) { + .ifEmpty { listOf(0) } + val filteredCategoryManga = if (categoriesForManga.intersect(categoriesToExclude).isNotEmpty()) { chapters.filterNot { it.read } - } else if (!downloadPreferences.removeBookmarkedChapters().get()) { - chapters.filterNot { it.bookmark } } else { chapters } + + return if (!downloadPreferences.removeBookmarkedChapters().get()) { + filteredCategoryManga.filterNot { it.bookmark } + } else { + filteredCategoryManga + } } fun statusFlow(): Flow = queueState