From 2abd184f786c55cbf332ea3a214b86dc542adb3d Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 13 Aug 2023 12:27:15 +0200 Subject: [PATCH] fix: wrong indicated download progress --- .../libretube/services/DownloadService.kt | 32 ++++++++++--------- .../com/github/libretube/util/TextUtils.kt | 1 - 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index e5e116824..225c773ae 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -157,7 +157,6 @@ class DownloadService : LifecycleService() { * Download file and emit [DownloadStatus] to the collectors of [downloadFlow] * and notification. */ - @Suppress("KotlinConstantConditions") private suspend fun downloadFile(item: DownloadItem) { downloadQueue[item.id] = true val notificationBuilder = getNotificationBuilder(item) @@ -221,9 +220,11 @@ class DownloadService : LifecycleService() { } } } catch (_: CancellationException) { + break } catch (e: Exception) { toastFromMainThread("${getString(R.string.download)}: ${e.message}") _downloadFlow.emit(item.id to DownloadStatus.Error(e.message.toString(), e)) + break } withContext(Dispatchers.IO) { @@ -233,14 +234,15 @@ class DownloadService : LifecycleService() { con.disconnect() } } catch (_: Exception) { + break } } val completed = totalRead < item.downloadSize if (completed) { - _downloadFlow.emit(item.id to DownloadStatus.Paused) - } else { _downloadFlow.emit(item.id to DownloadStatus.Completed) + } else { + _downloadFlow.emit(item.id to DownloadStatus.Paused) } setPauseNotification(notificationBuilder, item, completed) @@ -466,6 +468,18 @@ class DownloadService : LifecycleService() { ).build() } + private fun getPauseAction(id: Int): NotificationCompat.Action { + val intent = Intent(this, NotificationReceiver::class.java) + .setAction(ACTION_DOWNLOAD_PAUSE) + .putExtra("id", id) + + return NotificationCompat.Action.Builder( + R.drawable.ic_pause, + getString(R.string.pause), + PendingIntentCompat.getBroadcast(this, id, intent, FLAG_UPDATE_CURRENT, false) + ).build() + } + private fun getStopAction(id: Int): NotificationCompat.Action { val intent = Intent(this, NotificationReceiver::class.java).apply { action = ACTION_DOWNLOAD_STOP @@ -481,18 +495,6 @@ class DownloadService : LifecycleService() { ).build() } - private fun getPauseAction(id: Int): NotificationCompat.Action { - val intent = Intent(this, NotificationReceiver::class.java) - .setAction(ACTION_DOWNLOAD_PAUSE) - .putExtra("id", id) - - return NotificationCompat.Action.Builder( - R.drawable.ic_pause, - getString(R.string.pause), - PendingIntentCompat.getBroadcast(this, id, intent, FLAG_UPDATE_CURRENT, false) - ).build() - } - /** * Get a [File] from the corresponding download directory and the file name */ diff --git a/app/src/main/java/com/github/libretube/util/TextUtils.kt b/app/src/main/java/com/github/libretube/util/TextUtils.kt index 9b23ce1e3..59af28614 100644 --- a/app/src/main/java/com/github/libretube/util/TextUtils.kt +++ b/app/src/main/java/com/github/libretube/util/TextUtils.kt @@ -7,7 +7,6 @@ import android.text.format.DateUtils import com.github.libretube.R import java.time.Instant import java.time.LocalDate -import java.time.LocalDateTime import java.time.ZoneId import java.time.format.DateTimeFormatter import java.time.format.FormatStyle