From f85194ec467ef661628861d33502db336b60da05 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 16 Jan 2021 15:48:02 -0500 Subject: [PATCH] Handle download cancelation from icon properly (fixes #4241) --- .../tachiyomi/data/download/DownloadManager.kt | 11 +++++++++++ .../tachiyomi/data/download/DownloadNotifier.kt | 2 ++ .../tachiyomi/data/download/Downloader.kt | 17 +++++++++-------- .../tachiyomi/ui/manga/chapter/ChapterHolder.kt | 4 +++- .../ui/manga/chapter/base/BaseChapterHolder.kt | 8 ++++---- .../ui/recent/updates/UpdatesHolder.kt | 4 +++- .../main/res/layout/chapter_download_view.xml | 6 +++--- 7 files changed, 35 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index ac2df1ee9..be0487003 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -212,8 +212,19 @@ class DownloadManager(private val context: Context) { fun deleteChapters(chapters: List, manga: Manga, source: Source): List { val filteredChapters = getChaptersToDelete(chapters) + val wasRunning = downloader.isRunning + downloader.pause() + + downloader.queue.remove(filteredChapters) queue.remove(filteredChapters) + if (downloader.queue.isEmpty()) { + DownloadService.stop(context) + downloader.stop() + } else if (wasRunning && downloader.queue.isNotEmpty()) { + downloader.start() + } + val chapterDirs = provider.findChapterDirs(filteredChapters, manga, source) chapterDirs.forEach { it.delete() } cache.removeChapters(filteredChapters, manga) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt index 16bcee8de..c4570846c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt @@ -165,6 +165,8 @@ internal class DownloadNotifier(private val context: Context) { * This function shows a notification to inform download tasks are done. */ fun onComplete() { + dismissProgress() + if (!errorThrown) { // Create notification with(completeNotificationBuilder) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index e28f1e922..cedb7451c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -134,15 +134,16 @@ class Downloader( if (reason != null) { notifier.onWarning(reason) - } else { - if (notifier.paused) { - notifier.paused = false - notifier.onPaused() - } else { - notifier.dismissProgress() - notifier.onComplete() - } + return } + + if (notifier.paused && !queue.isEmpty()) { + notifier.onPaused() + } else { + notifier.onComplete() + } + + notifier.paused = false } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt index c0abbfd1f..e37a44eb3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt @@ -20,7 +20,9 @@ class ChapterHolder( private val binding = ChaptersItemBinding.bind(view) init { - binding.download.setOnClickListener { onDownloadClick(it) } + binding.download.setOnClickListener { + onDownloadClick(it, bindingAdapterPosition) + } } fun bind(item: ChapterItem, manga: Manga) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt index b7bd417d1..60daf5405 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt @@ -11,11 +11,11 @@ open class BaseChapterHolder( private val adapter: BaseChaptersAdapter<*> ) : FlexibleViewHolder(view, adapter) { - fun onDownloadClick(view: View) { - val item = adapter.getItem(bindingAdapterPosition) as? BaseChapterItem<*, *> ?: return + fun onDownloadClick(view: View, position: Int) { + val item = adapter.getItem(position) as? BaseChapterItem<*, *> ?: return when (item.status) { Download.State.NOT_DOWNLOADED, Download.State.ERROR -> { - adapter.clickListener.downloadChapter(bindingAdapterPosition) + adapter.clickListener.downloadChapter(position) } else -> { view.popupMenu( @@ -28,7 +28,7 @@ open class BaseChapterHolder( findItem(R.id.cancel_download).isVisible = item.status != Download.State.DOWNLOADED }, onMenuItemClick = { - adapter.clickListener.deleteChapter(bindingAdapterPosition) + adapter.clickListener.deleteChapter(position) true } ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesHolder.kt index 878affbb6..e0afdcbdc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesHolder.kt @@ -32,7 +32,9 @@ class UpdatesHolder(private val view: View, private val adapter: UpdatesAdapter) adapter.coverClickListener.onCoverClick(bindingAdapterPosition) } - binding.download.setOnClickListener { onDownloadClick(it) } + binding.download.setOnClickListener { + onDownloadClick(it, bindingAdapterPosition) + } } fun bind(item: UpdatesItem) { diff --git a/app/src/main/res/layout/chapter_download_view.xml b/app/src/main/res/layout/chapter_download_view.xml index ce1b0d1d7..c8e61a955 100644 --- a/app/src/main/res/layout/chapter_download_view.xml +++ b/app/src/main/res/layout/chapter_download_view.xml @@ -2,8 +2,8 @@ @@ -36,7 +36,7 @@ android:visibility="gone" app:indicatorColor="@color/material_on_surface_emphasis_medium" app:indicatorInset="0dp" - app:indicatorSize="26dp" + app:indicatorSize="24dp" app:trackThickness="2dp" />