Merge pull request #7222 from Bnyro/master

fix: max amount of concurrent download is ignored for playlists
This commit is contained in:
Bnyro 2025-03-19 19:57:45 +01:00 committed by GitHub
commit 84592fae53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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]
* 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)