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 71e72d424b..b2e9729126 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 @@ -327,6 +327,7 @@ class DownloadManager(val context: Context) { queue.remove(manga) provider.findMangaDir(manga, source)?.delete() cache.removeManga(manga) + queue.updateListeners() } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt index cde04276b8..f7061a7cbc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt @@ -910,26 +910,31 @@ class MangaDetailsController : private fun massDeleteChapters(choice: Int) { val chaptersToDelete = when (choice) { - R.id.remove_all -> presenter.chapters - R.id.remove_non_bookmarked -> presenter.chapters.filter { !it.bookmark } - R.id.remove_read -> presenter.chapters.filter { it.read } + R.id.remove_all -> presenter.allChapters + R.id.remove_non_bookmarked -> presenter.allChapters.filter { !it.bookmark } + R.id.remove_read -> presenter.allChapters.filter { it.read } else -> emptyList() }.filter { it.isDownloaded } - if (chaptersToDelete.isNotEmpty()) { - massDeleteChapters(chaptersToDelete) + if (chaptersToDelete.isNotEmpty() || choice == R.id.remove_all) { + massDeleteChapters(chaptersToDelete, choice == R.id.remove_all) + } else { + snack?.dismiss() + snack = view?.snack(R.string.no_chapters_to_delete) } } - private fun massDeleteChapters(chapters: List) { + private fun massDeleteChapters(chapters: List, isEverything: Boolean) { val context = view?.context ?: return MaterialDialog(context).message( - text = context.resources.getQuantityString( + text = + if (isEverything) context.getString(R.string.remove_all_downloads) + else context.resources.getQuantityString( R.plurals.remove_n_chapters, chapters.size, chapters.size ) ).positiveButton(R.string.remove) { - presenter.deleteChapters(chapters) + presenter.deleteChapters(chapters, isEverything = isEverything) }.negativeButton(android.R.string.cancel).show() } @@ -941,8 +946,8 @@ class MangaDetailsController : createActionModeIfNeeded() return } - R.id.download_unread -> presenter.chapters.filter { !it.read } - R.id.download_all -> presenter.chapters + R.id.download_unread -> presenter.allChapters.filter { !it.read } + R.id.download_all -> presenter.allChapters else -> emptyList() } if (chaptersToDownload.isNotEmpty()) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt index a0ed1a87fe..0bac9a588e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt @@ -54,6 +54,7 @@ import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.cancel import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -92,6 +93,9 @@ class MangaDetailsPresenter( var chapters: List = emptyList() private set + var allChapters: List = emptyList() + private set + var headerItem = MangaHeaderItem(manga, controller.fromCatalogue) fun onCreate() { @@ -108,7 +112,7 @@ class MangaDetailsPresenter( controller.updateHeader() refreshAll() } else { - updateChapters() + runBlocking { getChapters() } controller.updateChapters(this.chapters) } setTrackItems() @@ -139,17 +143,7 @@ class MangaDetailsPresenter( setDownloadedChapters(chapters) // Store the last emission - this.chapters = applyChapterFilters(chapters) - } - - private fun updateChapters(fetchedChapters: List? = null) { - val chapters = - (fetchedChapters ?: db.getChapters(manga).executeAsBlocking()).map { it.toModel() } - - // Find downloaded chapters - setDownloadedChapters(chapters) - - // Store the last emission + allChapters = chapters this.chapters = applyChapterFilters(chapters) } @@ -178,7 +172,7 @@ class MangaDetailsPresenter( override fun updateDownloads() { scope.launch(Dispatchers.Default) { - updateChapters(chapters) + getChapters() withContext(Dispatchers.Main) { controller.updateChapters(chapters) } @@ -261,12 +255,12 @@ class MangaDetailsPresenter( return chapters.sortedByDescending { it.source_order }.find { !it.read } } - fun anyRead(): Boolean = chapters.any { it.read } - fun hasBookmark(): Boolean = chapters.any { it.bookmark } - fun hasDownloads(): Boolean = chapters.any { it.isDownloaded } + fun anyRead(): Boolean = allChapters.any { it.read } + fun hasBookmark(): Boolean = allChapters.any { it.bookmark } + fun hasDownloads(): Boolean = allChapters.any { it.isDownloaded } fun getUnreadChaptersSorted() = - chapters.filter { !it.read && it.status == Download.NOT_DOWNLOADED }.distinctBy { it.name } + allChapters.filter { !it.read && it.status == Download.NOT_DOWNLOADED }.distinctBy { it.name } .sortedByDescending { it.source_order } fun startDownloadingNow(chapter: Chapter) { @@ -299,8 +293,14 @@ class MangaDetailsPresenter( * Deletes the given list of chapter. * @param chapters the list of chapters to delete. */ - fun deleteChapters(chapters: List, update: Boolean = true) { - downloadManager.deleteChapters(chapters, manga, source) + fun deleteChapters(chapters: List, update: Boolean = true, isEverything: Boolean = false) { + scope.launchIO { + if (isEverything) { + downloadManager.deleteManga(manga, source) + } else { + downloadManager.deleteChapters(chapters, manga, source) + } + } chapters.forEach { chapter -> this.chapters.find { it.id == chapter.id }?.apply { status = Download.QUEUE @@ -394,7 +394,7 @@ class MangaDetailsPresenter( } } } - withContext(Dispatchers.IO) { updateChapters() } + getChapters() } isLoading = false if (chapterError == null) withContext(Dispatchers.Main) { controller.updateChapters(this@MangaDetailsPresenter.chapters) } @@ -431,7 +431,7 @@ class MangaDetailsPresenter( try { syncChaptersWithSource(db, chapters, manga, source) - updateChapters() + getChapters() withContext(Dispatchers.Main) { controller.updateChapters(this@MangaDetailsPresenter.chapters) } } catch (e: java.lang.Exception) { withContext(Dispatchers.Main) { @@ -534,7 +534,7 @@ class MangaDetailsPresenter( private fun asyncUpdateMangaAndChapters(justChapters: Boolean = false) { scope.launch { if (!justChapters) db.updateFlags(manga).executeOnIO() - updateChapters() + getChapters() withContext(Dispatchers.Main) { controller.updateChapters(chapters) } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 142cf5cae5..76fe6ae3f7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -52,6 +52,9 @@ Chapter not found No chapters found No pages found + Remove all downloads? + No chapters to delete + Remove %1$d downloaded chapter? Remove %1$d downloaded chapters?