From 0b4dbf6b3d0e55591d26a70b4417a4c2d7e3ae2e Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 19 Mar 2025 19:50:30 +0100 Subject: [PATCH] fix: max amount of concurrent download is ignored for playlists --- .../libretube/services/DownloadService.kt | 55 +++++++++++-------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index e7975c3cb..378f03470 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -183,23 +183,6 @@ class DownloadService : LifecycleService() { } } - /** - * Initiate download [Job] using [DownloadItem] by creating file according to [FileType] - * for the requested file. - */ - private fun start(item: DownloadItem) { - item.path = when (item.type) { - FileType.AUDIO -> getDownloadPath(DownloadHelper.AUDIO_DIR, item.fileName) - FileType.VIDEO -> getDownloadPath(DownloadHelper.VIDEO_DIR, item.fileName) - FileType.SUBTITLE -> getDownloadPath(DownloadHelper.SUBTITLE_DIR, item.fileName) - }.apply { deleteIfExists() }.createFile() - - lifecycleScope.launch(coroutineContext) { - item.id = Database.downloadDao().insertDownloadItem(item).toInt() - downloadFile(item) - } - } - /** * Download file and emit [DownloadStatus] to the collectors of [downloadFlow] * and notification. @@ -378,17 +361,45 @@ class DownloadService : LifecycleService() { return response.body } + /** + * Returns true if the current amount of downloads is still less than the maximum amount of + * concurrent downloads. + */ + private fun mayStartNewDownload(): Boolean { + val downloadCount = downloadQueue.valueIterator().asSequence().count { it } + return downloadCount < DownloadHelper.getMaxConcurrentDownloads() + } + + /** + * Initiate download [Job] using [DownloadItem] by creating file according to [FileType] + * for the requested file. + */ + private fun start(item: DownloadItem) { + item.path = when (item.type) { + FileType.AUDIO -> getDownloadPath(DownloadHelper.AUDIO_DIR, item.fileName) + FileType.VIDEO -> getDownloadPath(DownloadHelper.VIDEO_DIR, item.fileName) + FileType.SUBTITLE -> getDownloadPath(DownloadHelper.SUBTITLE_DIR, item.fileName) + }.apply { deleteIfExists() }.createFile() + + lifecycleScope.launch(coroutineContext) { + item.id = Database.downloadDao().insertDownloadItem(item).toInt() + + if (mayStartNewDownload()) { + downloadFile(item) + } else { + pause(item.id) + } + } + } + /** * Resume download which may have been paused. */ fun resume(id: Int) { // If file is already downloading then avoid new download job. - if (downloadQueue[id]) { - return - } + if (downloadQueue[id]) return - val downloadCount = downloadQueue.valueIterator().asSequence().count { it } - if (downloadCount >= DownloadHelper.getMaxConcurrentDownloads()) { + if (!mayStartNewDownload()) { toastFromMainThread(getString(R.string.concurrent_downloads_limit_reached)) lifecycleScope.launch(coroutineContext) { _downloadFlow.emit(id to DownloadStatus.Paused)