Fix download notification on download finish

pull/168/head
Koitharu 4 years ago
parent d5c1d86313
commit fce31df121
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -99,39 +99,42 @@ class DownloadService : BaseService() {
private fun listenJob(job: ProgressJob<DownloadState>) { private fun listenJob(job: ProgressJob<DownloadState>) {
lifecycleScope.launch { lifecycleScope.launch {
val startId = job.progressValue.startId val startId = job.progressValue.startId
val timeLeftEstimator = TimeLeftEstimator()
val notification = DownloadNotification(this@DownloadService, startId) val notification = DownloadNotification(this@DownloadService, startId)
notificationSwitcher.notify(startId, notification.create(job.progressValue, -1L)) try {
job.progressAsFlow() val timeLeftEstimator = TimeLeftEstimator()
.onEach { state -> notificationSwitcher.notify(startId, notification.create(job.progressValue, -1L))
if (state is DownloadState.Progress) { job.progressAsFlow()
timeLeftEstimator.tick(value = state.progress, total = state.max) .onEach { state ->
} else { if (state is DownloadState.Progress) {
timeLeftEstimator.emptyTick() 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 } notificationSwitcher.detach(
.whileActive() startId,
.collect { state -> if (job.isCancelled) {
val timeLeft = timeLeftEstimator.getEstimatedTimeLeft() null
notificationSwitcher.notify(startId, notification.create(state, timeLeft)) } else {
} notification.create(job.progressValue, -1L)
job.join() }
(job.progressValue as? DownloadState.Done)?.let {
sendBroadcast(
Intent(ACTION_DOWNLOAD_COMPLETE)
.putExtra(EXTRA_MANGA, ParcelableManga(it.localManga, withChapters = false))
) )
stopSelf(startId)
} }
notificationSwitcher.detach(
startId,
if (job.isCancelled) {
null
} else {
notification.create(job.progressValue, -1L)
}
)
stopSelf(startId)
} }
} }

Loading…
Cancel
Save