DownloadWorker: Split out download database update logic
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -20,6 +20,21 @@ interface DownloadDao {
|
||||
@Query("UPDATE download SET downloadStatus=:downloadStatus WHERE packageName=:packageName")
|
||||
suspend fun updateStatus(packageName: String, downloadStatus: DownloadStatus)
|
||||
|
||||
@Query(
|
||||
"""
|
||||
UPDATE download
|
||||
SET downloadStatus=:downloadStatus, progress=:progress, speed=:speed, timeRemaining=:timeRemaining
|
||||
WHERE packageName=:packageName
|
||||
"""
|
||||
)
|
||||
suspend fun updateStatusProgress(
|
||||
packageName: String,
|
||||
downloadStatus: DownloadStatus,
|
||||
progress: Int,
|
||||
speed: Long,
|
||||
timeRemaining: Long
|
||||
)
|
||||
|
||||
@Query("SELECT * FROM download")
|
||||
fun downloads(): Flow<List<Download>>
|
||||
|
||||
|
||||
@@ -131,6 +131,9 @@ class DownloadWorker @AssistedInject constructor(
|
||||
download.totalFiles = requestList.size
|
||||
totalBytes = requestList.sumOf { it.size }
|
||||
|
||||
// Update database with all latest changes
|
||||
downloadDao.update(download)
|
||||
|
||||
// Download and verify all files exists
|
||||
requestList.forEach { request ->
|
||||
downloading = true
|
||||
@@ -283,7 +286,13 @@ class DownloadWorker @AssistedInject constructor(
|
||||
this.speed = downloadInfo.speed
|
||||
this.timeRemaining = bytesRemaining / speed * 1000
|
||||
}
|
||||
downloadDao.update(download)
|
||||
downloadDao.updateStatusProgress(
|
||||
download.packageName,
|
||||
download.downloadStatus,
|
||||
download.progress,
|
||||
download.speed,
|
||||
download.timeRemaining
|
||||
)
|
||||
|
||||
notifyStatus(DownloadStatus.DOWNLOADING, NOTIFICATION_ID)
|
||||
totalProgress = progress
|
||||
@@ -308,7 +317,12 @@ class DownloadWorker @AssistedInject constructor(
|
||||
// Update database for all status except downloading which is handled onProgress
|
||||
if (status != DownloadStatus.DOWNLOADING) {
|
||||
download.downloadStatus = status
|
||||
downloadDao.updateStatus(download.packageName, status)
|
||||
if (download.downloadStatus == DownloadStatus.COMPLETED) {
|
||||
download.progress = 100
|
||||
downloadDao.updateStatusProgress(download.packageName, status, 100, 0, 0)
|
||||
} else {
|
||||
downloadDao.updateStatus(download.packageName, status)
|
||||
}
|
||||
}
|
||||
|
||||
if (status == DownloadStatus.CANCELLED) return
|
||||
|
||||
Reference in New Issue
Block a user