From f5614eb93a4d85dc75fc4b9c16b6c497419100d6 Mon Sep 17 00:00:00 2001 From: Colin Lee Date: Wed, 1 May 2019 12:07:03 -0500 Subject: [PATCH] [fenix] No issue: refresh after undoing bookmark deletion (https://github.com/mozilla-mobile/fenix/pull/2198) --- .../java/org/mozilla/fenix/ext/CoroutineScope.kt | 16 +++++++++++++--- .../fenix/library/bookmarks/BookmarkFragment.kt | 4 ++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/ext/CoroutineScope.kt b/app/src/main/java/org/mozilla/fenix/ext/CoroutineScope.kt index c22f01d243..2859a501b6 100644 --- a/app/src/main/java/org/mozilla/fenix/ext/CoroutineScope.kt +++ b/app/src/main/java/org/mozilla/fenix/ext/CoroutineScope.kt @@ -6,13 +6,20 @@ package org.mozilla.fenix.ext import android.view.View import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.mozilla.fenix.components.FenixSnackbar -fun CoroutineScope.allowUndo(view: View, message: String, undoActionTitle: String, operation: suspend () -> Unit) { - val undoJob = launch(Dispatchers.IO) { +fun CoroutineScope.allowUndo( + view: View, + message: String, + undoActionTitle: String, + onCancel: suspend () -> Unit = {}, + operation: suspend () -> Unit +) { + val undoJob = launch(IO) { delay(UNDO_DELAY) operation.invoke() } @@ -20,6 +27,9 @@ fun CoroutineScope.allowUndo(view: View, message: String, undoActionTitle: Strin .setText(message) .setAction(undoActionTitle) { undoJob.cancel() + launch(Main) { + onCancel.invoke() + } }.show() } diff --git a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragment.kt b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragment.kt index c9da1f37e9..32df67270d 100644 --- a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragment.kt @@ -219,7 +219,7 @@ class BookmarkFragment : Fragment(), CoroutineScope, BackHandler, AccountObserve CoroutineScope(Main).allowUndo( view!!, getString(R.string.bookmark_deletion_snackbar_message, it.item.url.urlToHost()), - getString(R.string.bookmark_undo_deletion) + getString(R.string.bookmark_undo_deletion), { refreshBookmarks(components) } ) { components.core.bookmarksStorage.deleteNode(it.item.guid) components.analytics.metrics.track(Event.RemoveBookmark) @@ -292,7 +292,7 @@ class BookmarkFragment : Fragment(), CoroutineScope, BackHandler, AccountObserve CoroutineScope(Main).allowUndo( view!!, getString(R.string.bookmark_deletion_multiple_snackbar_message), - getString(R.string.bookmark_undo_deletion) + getString(R.string.bookmark_undo_deletion), { refreshBookmarks(components) } ) { deleteSelectedBookmarks(selectedBookmarks, components) components.analytics.metrics.track(Event.RemoveBookmarks)