From b931957426142a9ace922f9b9b582b30793a4417 Mon Sep 17 00:00:00 2001 From: Colin Lee Date: Wed, 1 May 2019 12:21:28 -0500 Subject: [PATCH] For #1586: Behavior when adding a duplicate bookmark (#2195) --- .../mozilla/fenix/browser/BrowserFragment.kt | 89 +++++++++++-------- 1 file changed, 53 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt index 99d5224b31..62f4dabfaf 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt @@ -370,42 +370,7 @@ class BrowserFragment : Fragment(), BackHandler, CoroutineScope, } is QuickActionAction.BookmarkPressed -> { requireComponents.analytics.metrics.track(Event.QuickActionSheetBookmarkTapped) - getSessionById()?.let { session -> - CoroutineScope(IO).launch { - val guid = requireComponents.core.bookmarksStorage - .addItem( - BookmarkRoot.Mobile.id, - session.url, - session.title, - null - ) - launch(Main) { - getManagedEmitter() - .onNext(QuickActionChange.BookmarkedStateChange(true)) - requireComponents.analytics.metrics.track(Event.AddBookmark) - view?.let { - FenixSnackbar.make( - it, - Snackbar.LENGTH_LONG - ) - .setAction(getString(R.string.edit_bookmark_snackbar_action)) { - Navigation.findNavController( - requireActivity(), - R.id.container - ) - .navigate( - BrowserFragmentDirections - .actionBrowserFragmentToBookmarkEditFragment( - guid - ) - ) - } - .setText(getString(R.string.bookmark_saved_snackbar)) - .show() - } - } - } - } + bookmarkTapped() } is QuickActionAction.ReadPressed -> { requireComponents.analytics.metrics.track(Event.QuickActionSheetReadTapped) @@ -416,6 +381,58 @@ class BrowserFragment : Fragment(), BackHandler, CoroutineScope, assignSitePermissionsRules() } + private fun bookmarkTapped() { + getSessionById()?.let { session -> + CoroutineScope(IO).launch { + val components = requireComponents + val existing = components.core.bookmarksStorage.getBookmarksWithUrl(session.url) + val found = existing.isNotEmpty() && existing[0].url == session.url + if (found) { + launch(Main) { + Navigation.findNavController(requireActivity(), R.id.container) + .navigate( + BrowserFragmentDirections + .actionBrowserFragmentToBookmarkEditFragment(existing[0].guid) + ) + } + } else { + val guid = components.core.bookmarksStorage + .addItem( + BookmarkRoot.Mobile.id, + session.url, + session.title, + null + ) + launch(Main) { + getManagedEmitter() + .onNext(QuickActionChange.BookmarkedStateChange(true)) + requireComponents.analytics.metrics.track(Event.AddBookmark) + view?.let { + FenixSnackbar.make( + it, + Snackbar.LENGTH_LONG + ) + .setAction(getString(R.string.edit_bookmark_snackbar_action)) { + Navigation.findNavController( + requireActivity(), + R.id.container + ) + .navigate( + BrowserFragmentDirections + .actionBrowserFragmentToBookmarkEditFragment( + guid + ) + ) + } + .setText(getString(R.string.bookmark_saved_snackbar)) + .show() + } + } + } + } + } + } + override fun onStop() { super.onStop() sessionObserver?.let {