From fc5021c79d50222a2093058bcc70f471ae6449eb Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 3 Dec 2023 14:26:44 -0500 Subject: [PATCH] Clean up startDownloadNow function a bit Fixes #9330, I think. If it was even still an issue. (cherry picked from commit 3aead3a2a924ae080b8fa5dbfdef15b91010d2c9) --- .../data/download/DownloadManager.kt | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 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 d472f7be1..d27f661f5 100755 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -71,7 +71,13 @@ class DownloadManager( * Tells the downloader to begin downloads. */ fun startDownloads() { - DownloadJob.start(context) + if (downloader.isRunning) return + + if (DownloadJob.isRunning(context)) { + downloader.start() + } else { + DownloadJob.start(context) + } } /** @@ -100,22 +106,16 @@ class DownloadManager( return queueState.value.find { it.chapter.id == chapterId } } - fun startDownloadNow(chapterId: Long?) { - if (chapterId == null) return - val download = getQueuedDownloadOrNull(chapterId) + fun startDownloadNow(chapterId: Long) { + val existingDownload = getQueuedDownloadOrNull(chapterId) // If not in queue try to start a new download - val toAdd = download ?: runBlocking { Download.fromChapterId(chapterId) } ?: return - val queue = queueState.value.toMutableList() - download?.let { queue.remove(it) } - queue.add(0, toAdd) - reorderQueue(queue) - if (!downloader.isRunning) { - if (DownloadJob.isRunning(context)) { - downloader.start() - } else { - DownloadJob.start(context) - } + val toAdd = existingDownload ?: runBlocking { Download.fromChapterId(chapterId) } ?: return + queueState.value.toMutableList().apply { + existingDownload?.let { remove(it) } + add(0, toAdd) + reorderQueue(this) } + startDownloads() } /** @@ -149,7 +149,7 @@ class DownloadManager( addAll(0, downloads) reorderQueue(this) } - if (!DownloadJob.isRunning(context)) DownloadJob.start(context) + if (!DownloadJob.isRunning(context)) startDownloads() } /**