mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 15:30:31 +05:30
Merge pull request #7222 from Bnyro/master
fix: max amount of concurrent download is ignored for playlists
This commit is contained in:
commit
84592fae53
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user