Clean up startDownloadNow function a bit

Fixes #9330, I think. If it was even still an issue.
This commit is contained in:
arkon 2023-12-03 14:26:44 -05:00
parent 6a48fed170
commit 3aead3a2a9

View File

@ -68,7 +68,13 @@ class DownloadManager(
* Tells the downloader to begin downloads. * Tells the downloader to begin downloads.
*/ */
fun startDownloads() { fun startDownloads() {
DownloadJob.start(context) if (downloader.isRunning) return
if (DownloadJob.isRunning(context)) {
downloader.start()
} else {
DownloadJob.start(context)
}
} }
/** /**
@ -97,22 +103,16 @@ class DownloadManager(
return queueState.value.find { it.chapter.id == chapterId } return queueState.value.find { it.chapter.id == chapterId }
} }
fun startDownloadNow(chapterId: Long?) { fun startDownloadNow(chapterId: Long) {
if (chapterId == null) return val existingDownload = getQueuedDownloadOrNull(chapterId)
val download = getQueuedDownloadOrNull(chapterId)
// If not in queue try to start a new download // If not in queue try to start a new download
val toAdd = download ?: runBlocking { Download.fromChapterId(chapterId) } ?: return val toAdd = existingDownload ?: runBlocking { Download.fromChapterId(chapterId) } ?: return
val queue = queueState.value.toMutableList() queueState.value.toMutableList().apply {
download?.let { queue.remove(it) } existingDownload?.let { remove(it) }
queue.add(0, toAdd) add(0, toAdd)
reorderQueue(queue) reorderQueue(this)
if (!downloader.isRunning) {
if (DownloadJob.isRunning(context)) {
downloader.start()
} else {
DownloadJob.start(context)
}
} }
startDownloads()
} }
/** /**
@ -146,7 +146,7 @@ class DownloadManager(
addAll(0, downloads) addAll(0, downloads)
reorderQueue(this) reorderQueue(this)
} }
if (!DownloadJob.isRunning(context)) DownloadJob.start(context) if (!DownloadJob.isRunning(context)) startDownloads()
} }
/** /**