fix: crash in DownloadService if trying to read dead download item

This commit is contained in:
Bnyro 2024-04-14 13:08:31 +02:00
parent a47eac5352
commit dd59b89ddf
2 changed files with 8 additions and 10 deletions

View File

@ -23,7 +23,7 @@ interface DownloadDao {
suspend fun findById(videoId: String): DownloadWithItems
@Query("SELECT * FROM downloaditem WHERE id = :id")
suspend fun findDownloadItemById(id: Int): DownloadItem
suspend fun findDownloadItemById(id: Int): DownloadItem?
@Query("DELETE FROM downloaditem WHERE id = :id")
suspend fun deleteDownloadItemById(id: Int)

View File

@ -55,7 +55,6 @@ import kotlin.io.path.div
import kotlin.io.path.fileSize
import kotlin.math.min
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
@ -341,7 +340,8 @@ class DownloadService : LifecycleService() {
}
lifecycleScope.launch(coroutineContext) {
downloadFile(Database.downloadDao().findDownloadItemById(id))
val file = Database.downloadDao().findDownloadItemById(id) ?: return@launch
downloadFile(file)
}
}
@ -361,16 +361,14 @@ class DownloadService : LifecycleService() {
/**
* Stop downloading job for given [id]. If no downloads are active, stop the service.
*/
private fun stop(id: Int) = CoroutineScope(Dispatchers.IO).launch {
private fun stop(id: Int) = lifecycleScope.launch(coroutineContext) {
downloadQueue[id] = false
_downloadFlow.emit(id to DownloadStatus.Stopped)
lifecycleScope.launch {
val item = Database.downloadDao().findDownloadItemById(id)
notificationManager.cancel(item.getNotificationId())
Database.downloadDao().deleteDownloadItemById(id)
stopServiceIfDone()
}
val item = Database.downloadDao().findDownloadItemById(id) ?: return@launch
notificationManager.cancel(item.getNotificationId())
Database.downloadDao().deleteDownloadItemById(id)
stopServiceIfDone()
}
/**