Skip to content

Commit 3c778c0

Browse files
authored
fix: next download isn't always starting automatically
1 parent 5ba9eb5 commit 3c778c0

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

app/src/main/java/com/github/libretube/services/DownloadService.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import androidx.core.app.PendingIntentCompat
1818
import androidx.core.app.ServiceCompat
1919
import androidx.core.content.getSystemService
2020
import androidx.core.util.keyIterator
21-
import androidx.core.util.remove
2221
import androidx.core.util.set
2322
import androidx.core.util.valueIterator
2423
import androidx.lifecycle.LifecycleService
@@ -59,7 +58,6 @@ import kotlinx.coroutines.SupervisorJob
5958
import kotlinx.coroutines.asCoroutineDispatcher
6059
import kotlinx.coroutines.flow.MutableSharedFlow
6160
import kotlinx.coroutines.flow.SharedFlow
62-
import kotlinx.coroutines.flow.firstOrNull
6361
import kotlinx.coroutines.launch
6462
import kotlinx.coroutines.withContext
6563
import okhttp3.OkHttpClient
@@ -252,18 +250,20 @@ class DownloadService : LifecycleService() {
252250

253251
downloadQueue[item.id] = false
254252

255-
if (downloadFlow.firstOrNull { it.first == item.id }?.second == DownloadStatus.Stopped) {
256-
downloadQueue.remove(item.id, false)
257-
}
258-
259253
// start the next download if there are any remaining ones enqueued
260-
val nextDownload =
261-
downloadFlow.firstOrNull { (_, status) -> status == DownloadStatus.Paused }
262-
if (nextDownload != null) {
263-
resume(nextDownload.first)
264-
} else {
265-
stopServiceIfDone()
254+
for (id in downloadQueue.keyIterator()) {
255+
if (downloadQueue[id]) continue
256+
257+
val dbItem = Database.downloadDao().findDownloadItemById(id)
258+
if (dbItem != null && (dbItem.downloadSize <= 0L || dbItem.path.fileSize() < dbItem.downloadSize)) {
259+
resume(id)
260+
return
261+
}
266262
}
263+
264+
// if no new download was enqueued (i.e. there's no paused/stopped download left),
265+
// look if any downloads are still running, and if not, stop the service
266+
stopServiceIfDone()
267267
}
268268

269269
private suspend fun progressDownload(

0 commit comments

Comments
 (0)