diff --git a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt index 3e8355a632..18c003fa5c 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt @@ -39,6 +39,7 @@ import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import mozilla.appservices.places.BookmarkRoot +import mozilla.appservices.places.uniffi.PlacesException import mozilla.components.browser.state.action.ContentAction import mozilla.components.browser.state.selector.findCustomTab import mozilla.components.browser.state.selector.findCustomTabOrSelectedTab @@ -1230,34 +1231,38 @@ abstract class BaseBrowserFragment : } } else { // Save bookmark, then go to edit fragment - val guid = bookmarksStorage.addItem( - BookmarkRoot.Mobile.id, - url = sessionUrl, - title = sessionTitle, - position = null - ) + try { + val guid = bookmarksStorage.addItem( + BookmarkRoot.Mobile.id, + url = sessionUrl, + title = sessionTitle, + position = null + ) - withContext(Main) { - requireComponents.analytics.metrics.track(Event.AddBookmark) + withContext(Main) { + requireComponents.analytics.metrics.track(Event.AddBookmark) - view?.let { - FenixSnackbar.make( - view = binding.browserLayout, - duration = FenixSnackbar.LENGTH_LONG, - isDisplayedWithBrowserToolbar = true - ) - .setText(getString(R.string.bookmark_saved_snackbar)) - .setAction(getString(R.string.edit_bookmark_snackbar_action)) { - nav( - R.id.browserFragment, - BrowserFragmentDirections.actionGlobalBookmarkEditFragment( - guid, - true + view?.let { + FenixSnackbar.make( + view = binding.browserLayout, + duration = FenixSnackbar.LENGTH_LONG, + isDisplayedWithBrowserToolbar = true + ) + .setText(getString(R.string.bookmark_saved_snackbar)) + .setAction(getString(R.string.edit_bookmark_snackbar_action)) { + nav( + R.id.browserFragment, + BrowserFragmentDirections.actionGlobalBookmarkEditFragment( + guid, + true + ) ) - ) - } - .show() + } + .show() + } } + } catch (e: PlacesException.UrlParseFailed) { + println("We should do something here") } } } diff --git a/app/src/main/java/org/mozilla/fenix/components/bookmarks/BookmarksUseCase.kt b/app/src/main/java/org/mozilla/fenix/components/bookmarks/BookmarksUseCase.kt index 42373f1cbd..97bb2ae3db 100644 --- a/app/src/main/java/org/mozilla/fenix/components/bookmarks/BookmarksUseCase.kt +++ b/app/src/main/java/org/mozilla/fenix/components/bookmarks/BookmarksUseCase.kt @@ -6,6 +6,7 @@ package org.mozilla.fenix.components.bookmarks import androidx.annotation.WorkerThread import mozilla.appservices.places.BookmarkRoot +import mozilla.appservices.places.uniffi.PlacesException import mozilla.components.concept.storage.BookmarksStorage import mozilla.components.concept.storage.HistoryStorage import org.mozilla.fenix.home.recentbookmarks.RecentBookmark @@ -29,18 +30,21 @@ class BookmarksUseCase( */ @WorkerThread suspend operator fun invoke(url: String, title: String, position: UInt? = null): Boolean { - val canAdd = storage.getBookmarksWithUrl(url).firstOrNull { it.url == it.url } == null + try { + val canAdd = storage.getBookmarksWithUrl(url).firstOrNull { it.url == it.url } == null - if (canAdd) { - storage.addItem( - BookmarkRoot.Mobile.id, - url = url, - title = title, - position = position - ) + if (canAdd) { + storage.addItem( + BookmarkRoot.Mobile.id, + url = url, + title = title, + position = position + ) + } + return canAdd + } catch (e: PlacesException.UrlParseFailed) { + return false } - - return canAdd } }