From fce31df1214acaca78dc936c45cb0fc14a3077a1 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 12 May 2022 12:50:06 +0300 Subject: [PATCH] Fix download notification on download finish --- .../download/ui/service/DownloadService.kt | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/org/koitharu/kotatsu/download/ui/service/DownloadService.kt b/app/src/main/java/org/koitharu/kotatsu/download/ui/service/DownloadService.kt index 05c6df6bd..91c742e73 100644 --- a/app/src/main/java/org/koitharu/kotatsu/download/ui/service/DownloadService.kt +++ b/app/src/main/java/org/koitharu/kotatsu/download/ui/service/DownloadService.kt @@ -99,39 +99,42 @@ class DownloadService : BaseService() { private fun listenJob(job: ProgressJob) { lifecycleScope.launch { val startId = job.progressValue.startId - val timeLeftEstimator = TimeLeftEstimator() val notification = DownloadNotification(this@DownloadService, startId) - notificationSwitcher.notify(startId, notification.create(job.progressValue, -1L)) - job.progressAsFlow() - .onEach { state -> - if (state is DownloadState.Progress) { - timeLeftEstimator.tick(value = state.progress, total = state.max) - } else { - timeLeftEstimator.emptyTick() + try { + val timeLeftEstimator = TimeLeftEstimator() + notificationSwitcher.notify(startId, notification.create(job.progressValue, -1L)) + job.progressAsFlow() + .onEach { state -> + if (state is DownloadState.Progress) { + timeLeftEstimator.tick(value = state.progress, total = state.max) + } else { + timeLeftEstimator.emptyTick() + } } + .throttle { state -> if (state is DownloadState.Progress) 400L else 0L } + .whileActive() + .collect { state -> + val timeLeft = timeLeftEstimator.getEstimatedTimeLeft() + notificationSwitcher.notify(startId, notification.create(state, timeLeft)) + } + job.join() + } finally { + (job.progressValue as? DownloadState.Done)?.let { + sendBroadcast( + Intent(ACTION_DOWNLOAD_COMPLETE) + .putExtra(EXTRA_MANGA, ParcelableManga(it.localManga, withChapters = false)) + ) } - .throttle { state -> if (state is DownloadState.Progress) 400L else 0L } - .whileActive() - .collect { state -> - val timeLeft = timeLeftEstimator.getEstimatedTimeLeft() - notificationSwitcher.notify(startId, notification.create(state, timeLeft)) - } - job.join() - (job.progressValue as? DownloadState.Done)?.let { - sendBroadcast( - Intent(ACTION_DOWNLOAD_COMPLETE) - .putExtra(EXTRA_MANGA, ParcelableManga(it.localManga, withChapters = false)) + notificationSwitcher.detach( + startId, + if (job.isCancelled) { + null + } else { + notification.create(job.progressValue, -1L) + } ) + stopSelf(startId) } - notificationSwitcher.detach( - startId, - if (job.isCancelled) { - null - } else { - notification.create(job.progressValue, -1L) - } - ) - stopSelf(startId) } }