DownloadWorker: Also consider speed when updating progress

This should fix the issue where progress gets stuck because download speed
gets really low but UI still mentions old speed and time remaining

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2024-03-22 13:51:44 +05:30
parent 1edc0b3f53
commit 42a38ef741

View File

@@ -285,13 +285,12 @@ class DownloadWorker @AssistedInject constructor(
private suspend fun onProgress(downloadInfo: DownloadInfo) {
if (!isStopped && !download.isFinished) {
val progress = ((downloadedBytes + downloadInfo.bytesCopied) * 100 / totalBytes).toInt()
val bytesRemaining = totalBytes - (downloadedBytes + downloadInfo.bytesCopied)
val speed = if (downloadInfo.speed == 0L) 1 else downloadInfo.speed
// Individual file progress can be negligible in contrast to total progress
// Only notify the UI if progress is greater to avoid being rate-limited by Android
if (progress > totalProgress) {
val bytesRemaining = totalBytes - (downloadedBytes + downloadInfo.bytesCopied)
val speed = if (downloadInfo.speed == 0L) 1 else downloadInfo.speed
// Only notify the UI if progress is greater or speed has changed to avoid being rate-limited by Android
if (progress > totalProgress || speed != download.speed) {
if (downloadInfo.progress == 100) {
downloadedBytes += downloadInfo.bytesCopied
}