Added Undo button for swiping in manga details

This commit is contained in:
Jay 2020-04-06 03:34:34 -04:00
parent 651682df4a
commit ddb4092e16
4 changed files with 58 additions and 24 deletions

View File

@ -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<Snackbar>() {
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<ChapterItem>, bookmarked: Boolean) {

View File

@ -290,26 +290,16 @@ class MangaDetailsPresenter(
* @param chapters the list of chapters to delete.
*/
fun deleteChapters(chapters: List<ChapterItem>, 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<ChapterItem>) {
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<ChapterItem>, read: Boolean) {
fun markChaptersRead(
selectedChapters: List<ChapterItem>,
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()

View File

@ -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<Snackbar>() {

View File

@ -780,6 +780,9 @@
<string name="view_history">View history</string>
<string name="view_all_updates">View all updates</string>
<string name="marked_as_read">Marked as read</string>
<string name="marked_as_unread">Marked as unread</string>
<string name="bookmarked">Bookmarked</string>
<string name="removed_bookmark">Removed bookmark</string>
<string name="no_search_result">No search results</string>
<string name="search_recents">Search recents…</string>
</resources>