fix: max amount of concurrent download is ignored for playlists

This commit is contained in:
Bnyro 2025-03-19 19:50:30 +01:00
parent 246e49e145
commit 0b4dbf6b3d
No known key found for this signature in database

View File

@ -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] * Download file and emit [DownloadStatus] to the collectors of [downloadFlow]
* and notification. * and notification.
@ -378,17 +361,45 @@ class DownloadService : LifecycleService() {
return response.body 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. * Resume download which may have been paused.
*/ */
fun resume(id: Int) { fun resume(id: Int) {
// If file is already downloading then avoid new download job. // If file is already downloading then avoid new download job.
if (downloadQueue[id]) { if (downloadQueue[id]) return
return
}
val downloadCount = downloadQueue.valueIterator().asSequence().count { it } if (!mayStartNewDownload()) {
if (downloadCount >= DownloadHelper.getMaxConcurrentDownloads()) {
toastFromMainThread(getString(R.string.concurrent_downloads_limit_reached)) toastFromMainThread(getString(R.string.concurrent_downloads_limit_reached))
lifecycleScope.launch(coroutineContext) { lifecycleScope.launch(coroutineContext) {
_downloadFlow.emit(id to DownloadStatus.Paused) _downloadFlow.emit(id to DownloadStatus.Paused)