From ba2a528886c6ae29d0f46070df736497921346e3 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Sat, 19 Nov 2022 09:27:39 +0600 Subject: [PATCH] Fix related to cancelling queued chapters (#8528) Tachi removes the downloaded chapter (if it exists) when you just cancelled a download from queue. PR fixes that Also removes redundant return --- .../data/download/DownloadManager.kt | 30 ++++--------------- .../ui/download/DownloadController.kt | 2 +- .../ui/download/DownloadPresenter.kt | 6 +--- .../tachiyomi/ui/manga/MangaScreenModel.kt | 2 +- .../tachiyomi/ui/reader/ReaderPresenter.kt | 4 +-- .../tachiyomi/ui/updates/UpdatesPresenter.kt | 2 +- 6 files changed, 11 insertions(+), 35 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 02dab3fae..ff200819f 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,22 +214,9 @@ class DownloadManager( return cache.getDownloadCount(manga) } - /** - * Calls delete chapter, which deletes a temp download. - * - * @param download the download to cancel. - */ - fun deletePendingDownload(download: Download) { - deleteChapters(listOf(download.chapter.toDomainChapter()!!), download.manga, download.source, true) - } - - fun deletePendingDownloads(vararg downloads: Download) { - val downloadsByManga = downloads.groupBy { it.manga.id } - downloadsByManga.map { entry -> - val manga = entry.value.first().manga - val source = entry.value.first().source - deleteChapters(entry.value.map { it.chapter.toDomainChapter()!! }, manga, source, true) - } + fun deletePendingDownloads(downloads: List) { + val domainChapters = downloads.map { it.chapter.toDomainChapter()!! } + removeFromDownloadQueue(domainChapters) } /** @@ -238,14 +225,9 @@ class DownloadManager( * @param chapters the list of chapters to delete. * @param manga the manga of the chapters. * @param source the source of the chapters. - * @param isCancelling true if it's simply cancelling a download */ - fun deleteChapters(chapters: List, manga: Manga, source: Source, isCancelling: Boolean = false): List { - val filteredChapters = if (isCancelling) { - chapters - } else { - getChaptersToDelete(chapters, manga) - } + fun deleteChapters(chapters: List, manga: Manga, source: Source) { + val filteredChapters = getChaptersToDelete(chapters, manga) if (filteredChapters.isNotEmpty()) { launchIO { @@ -269,8 +251,6 @@ class DownloadManager( } } } - - return filteredChapters } private fun removeFromDownloadQueue(chapters: List) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt index 7964a1de5..d9ddce190 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt @@ -479,7 +479,7 @@ class DownloadController : presenter.reorder(selectedSeries + otherSeries) } R.id.cancel_download -> { - presenter.cancelDownload(item.download) + presenter.cancelDownloads(listOf(item.download)) } R.id.cancel_series -> { val allDownloadsForSeries = adapter?.currentItems diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadPresenter.kt index e4280f767..19f80b786 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadPresenter.kt @@ -72,11 +72,7 @@ class DownloadPresenter : BasePresenter() { downloadManager.reorderQueue(downloads) } - fun cancelDownload(download: Download) { - downloadManager.deletePendingDownload(download) - } - fun cancelDownloads(downloads: List) { - downloadManager.deletePendingDownloads(*downloads.toTypedArray()) + downloadManager.deletePendingDownloads(downloads) } } 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 27f485c52..4ffa23fb6 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 @@ -658,7 +658,7 @@ class MangaInfoScreenModel( fun cancelDownload(chapterId: Long) { val activeDownload = downloadManager.queue.find { chapterId == it.chapter.id } ?: return - downloadManager.deletePendingDownload(activeDownload) + downloadManager.deletePendingDownloads(listOf(activeDownload)) updateDownloadState(activeDownload.apply { status = Download.State.NOT_DOWNLOADED }) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index 5ee333d37..32c45b339 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -490,8 +490,8 @@ class ReaderPresenter( * if setting is enabled and [currentChapter] is queued for download */ private fun deleteChapterFromDownloadQueue(currentChapter: ReaderChapter): Download? { - return downloadManager.getChapterDownloadOrNull(currentChapter.chapter.toDomainChapter()!!)?.apply { - downloadManager.deletePendingDownload(this) + return downloadManager.getChapterDownloadOrNull(currentChapter.chapter.toDomainChapter()!!)?.also { + downloadManager.deletePendingDownloads(listOf(it)) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesPresenter.kt index c61a5e9ee..c0488bb45 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesPresenter.kt @@ -199,7 +199,7 @@ class UpdatesPresenter( private fun cancelDownload(chapterId: Long) { val activeDownload = downloadManager.queue.find { chapterId == it.chapter.id } ?: return - downloadManager.deletePendingDownload(activeDownload) + downloadManager.deletePendingDownloads(listOf(activeDownload)) updateDownloadState(activeDownload.apply { status = Download.State.NOT_DOWNLOADED }) }