From a1a3125834d584b1e1523b0e27fe298a278e5af4 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 6 Jul 2020 20:05:41 +0300 Subject: [PATCH] Fix crash on manga downloading --- app/build.gradle | 2 +- .../kotatsu/ui/download/DownloadService.kt | 38 ++++++++----------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d39d2687d..35732eed8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,7 +16,7 @@ android { minSdkVersion 21 targetSdkVersion 29 versionCode gitCommits - versionName '0.5-rc1' + versionName '0.5-rc2' kapt { arguments { diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/download/DownloadService.kt b/app/src/main/java/org/koitharu/kotatsu/ui/download/DownloadService.kt index 033d603b4..8019f7b1c 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/download/DownloadService.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/download/DownloadService.kt @@ -77,9 +77,9 @@ class DownloadService : BaseService() { return launch(Dispatchers.IO) { mutex.lock() wakeLock.acquire(TimeUnit.HOURS.toMillis(1)) + notification.fillFrom(manga) + notification.setCancelId(startId) withContext(Dispatchers.Main) { - notification.fillFrom(manga) - notification.setCancelId(startId) startForeground(DownloadNotification.NOTIFICATION_ID, notification()) } val destination = settings.getStorageDir(this@DownloadService) @@ -94,10 +94,8 @@ class DownloadService : BaseService() { .build() ).drawable } - withContext(Dispatchers.Main) { - notification.setLargeIcon(cover) - notification.update() - } + notification.setLargeIcon(cover) + notification.update() val data = if (manga.chapters == null) repo.getDetails(manga) else manga output = MangaZip.findInDir(destination, data) output.prepare(data) @@ -141,31 +139,25 @@ class DownloadService : BaseService() { } } } - withContext(Dispatchers.Main) { - notification.setCancelId(0) - notification.setPostProcessing() - notification.update() - } + notification.setCancelId(0) + notification.setPostProcessing() + notification.update() output.compress() val result = MangaProviderFactory.createLocal().getFromFile(output.file) - withContext(Dispatchers.Main) { - notification.setDone(result) - notification.dismiss() - notification.update(manga.id.toInt().absoluteValue) - } + notification.setDone(result) + notification.dismiss() + notification.update(manga.id.toInt().absoluteValue) } catch (_: CancellationException) { - withContext(Dispatchers.Main + NonCancellable) { + withContext(NonCancellable) { notification.setCancelling() notification.setCancelId(0) notification.update() } } catch (e: Throwable) { - withContext(Dispatchers.Main) { - notification.setError(e) - notification.setCancelId(0) - notification.dismiss() - notification.update(manga.id.toInt().absoluteValue) - } + notification.setError(e) + notification.setCancelId(0) + notification.dismiss() + notification.update(manga.id.toInt().absoluteValue) } finally { withContext(NonCancellable) { jobs.remove(startId)