From ddb4092e1683652286b3e7c16757068886955eea Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 6 Apr 2020 03:34:34 -0400 Subject: [PATCH] Added Undo button for swiping in manga details --- .../ui/manga/MangaDetailsController.kt | 41 +++++++++++++++++-- .../ui/manga/MangaDetailsPresenter.kt | 36 ++++++++-------- .../RecentChaptersController.kt | 2 +- app/src/main/res/values/strings.xml | 3 ++ 4 files changed, 58 insertions(+), 24 deletions(-) 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 07c5b2bd96..3ffeca2592 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 @@ -547,13 +547,48 @@ class MangaDetailsController : BaseController, fun bookmarkChapter(position: Int) { val item = adapter?.getItem(position) as? ChapterItem ?: return - bookmarkChapters(listOf(item), !item.bookmark) + val chapter = item.chapter + val bookmarked = item.bookmark + bookmarkChapters(listOf(item), !bookmarked) + snack?.dismiss() + snack = view?.snack( + if (bookmarked) R.string.removed_bookmark + else R.string.bookmarked, Snackbar.LENGTH_INDEFINITE + ) { + setAction(R.string.action_undo) { + bookmarkChapters(listOf(item), bookmarked) + } + } + (activity as? MainActivity)?.setUndoSnackBar(snack) } fun toggleReadChapter(position: Int) { val item = adapter?.getItem(position) as? ChapterItem ?: return - if (!item.read) markAsRead(listOf(item)) - else markAsUnread(listOf(item)) + val chapter = item.chapter + val lastRead = chapter.last_page_read + val pagesLeft = chapter.pages_left + val read = item.chapter.read + presenter.markChaptersRead(listOf(item), !read, false) + snack?.dismiss() + snack = view?.snack( + if (read) R.string.marked_as_unread + else R.string.marked_as_read, Snackbar.LENGTH_INDEFINITE + ) { + var undoing = false + setAction(R.string.action_undo) { + presenter.markChaptersRead(listOf(item), read, true, lastRead, pagesLeft) + undoing = true + } + addCallback(object : BaseTransientBottomBar.BaseCallback() { + override fun onDismissed(transientBottomBar: Snackbar?, event: Int) { + super.onDismissed(transientBottomBar, event) + if (!undoing && !read && presenter.preferences.removeAfterMarkedAsRead()) { + presenter.deleteChapters(listOf(item)) + } + } + }) + } + (activity as? MainActivity)?.setUndoSnackBar(snack) } private fun bookmarkChapters(chapters: List, bookmarked: Boolean) { 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 9a55980e19..1df060a098 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 @@ -290,26 +290,16 @@ class MangaDetailsPresenter( * @param chapters the list of chapters to delete. */ fun deleteChapters(chapters: List, update: Boolean = true) { - deleteChaptersInternal(chapters) + downloadManager.deleteChapters(chapters, manga, source) chapters.forEach { chapter -> - this.chapters.find { it.id == chapter.id }?.download?.status = Download.NOT_DOWNLOADED + this.chapters.find { it.id == chapter.id }?.apply { + status = Download.NOT_DOWNLOADED + download = null + } } - if (update) - controller.updateChapters(this.chapters) - } - - /** - * Deletes a list of chapters from disk. This method is called in a background thread. - * @param chapters the chapters to delete. - */ - private fun deleteChaptersInternal(chapters: List) { - downloadManager.deleteChapters(chapters, manga, source) - chapters.forEach { - it.status = Download.NOT_DOWNLOADED - it.download = null - } + if (update) controller.updateChapters(this.chapters) } fun refreshAll() { @@ -412,17 +402,23 @@ class MangaDetailsPresenter( * @param selectedChapters the list of selected chapters. * @param read whether to mark chapters as read or unread. */ - fun markChaptersRead(selectedChapters: List, read: Boolean) { + fun markChaptersRead( + selectedChapters: List, + read: Boolean, + deleteNow: Boolean = true, + lastRead: Int? = null, + pagesLeft: Int? = null + ) { scope.launch(Dispatchers.IO) { selectedChapters.forEach { it.read = read if (!read) { - it.last_page_read = 0 - it.pages_left = 0 + it.last_page_read = lastRead ?: 0 + it.pages_left = pagesLeft ?: 0 } } db.updateChaptersProgress(selectedChapters).executeAsBlocking() - if (read && preferences.removeAfterMarkedAsRead()) { + if (read && deleteNow && preferences.removeAfterMarkedAsRead()) { deleteChapters(selectedChapters, false) } getChapters() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt index 898360e947..2ed2f4c4ad 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt @@ -203,7 +203,7 @@ class RecentChaptersController(bundle: Bundle? = null) : BaseController(bundle), snack = view?.snack(R.string.marked_as_read, Snackbar.LENGTH_INDEFINITE) { var undoing = false setAction(R.string.action_undo) { - presenter.markChapterRead(item, !item.chapter.read, lastRead, pagesLeft) + presenter.markChapterRead(item, read, lastRead, pagesLeft) undoing = true } addCallback(object : BaseTransientBottomBar.BaseCallback() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a565d3a7f6..f254b00c04 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -780,6 +780,9 @@ View history View all updates Marked as read + Marked as unread + Bookmarked + Removed bookmark No search results Search recents…