fix fenix crashing when trying to add an invalid bookmark

upstream-sync
Sammy Khamis 2 years ago committed by mergify[bot]
parent 5c1953d792
commit 7d34fbb87d

@ -39,6 +39,7 @@ import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import mozilla.appservices.places.BookmarkRoot import mozilla.appservices.places.BookmarkRoot
import mozilla.appservices.places.uniffi.PlacesException
import mozilla.components.browser.state.action.ContentAction import mozilla.components.browser.state.action.ContentAction
import mozilla.components.browser.state.selector.findCustomTab import mozilla.components.browser.state.selector.findCustomTab
import mozilla.components.browser.state.selector.findCustomTabOrSelectedTab import mozilla.components.browser.state.selector.findCustomTabOrSelectedTab
@ -1230,34 +1231,38 @@ abstract class BaseBrowserFragment :
} }
} else { } else {
// Save bookmark, then go to edit fragment // Save bookmark, then go to edit fragment
val guid = bookmarksStorage.addItem( try {
BookmarkRoot.Mobile.id, val guid = bookmarksStorage.addItem(
url = sessionUrl, BookmarkRoot.Mobile.id,
title = sessionTitle, url = sessionUrl,
position = null title = sessionTitle,
) position = null
)
withContext(Main) { withContext(Main) {
requireComponents.analytics.metrics.track(Event.AddBookmark) requireComponents.analytics.metrics.track(Event.AddBookmark)
view?.let { view?.let {
FenixSnackbar.make( FenixSnackbar.make(
view = binding.browserLayout, view = binding.browserLayout,
duration = FenixSnackbar.LENGTH_LONG, duration = FenixSnackbar.LENGTH_LONG,
isDisplayedWithBrowserToolbar = true isDisplayedWithBrowserToolbar = true
) )
.setText(getString(R.string.bookmark_saved_snackbar)) .setText(getString(R.string.bookmark_saved_snackbar))
.setAction(getString(R.string.edit_bookmark_snackbar_action)) { .setAction(getString(R.string.edit_bookmark_snackbar_action)) {
nav( nav(
R.id.browserFragment, R.id.browserFragment,
BrowserFragmentDirections.actionGlobalBookmarkEditFragment( BrowserFragmentDirections.actionGlobalBookmarkEditFragment(
guid, guid,
true true
)
) )
) }
} .show()
.show() }
} }
} catch (e: PlacesException.UrlParseFailed) {
println("We should do something here")
} }
} }
} }

@ -6,6 +6,7 @@ package org.mozilla.fenix.components.bookmarks
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import mozilla.appservices.places.BookmarkRoot import mozilla.appservices.places.BookmarkRoot
import mozilla.appservices.places.uniffi.PlacesException
import mozilla.components.concept.storage.BookmarksStorage import mozilla.components.concept.storage.BookmarksStorage
import mozilla.components.concept.storage.HistoryStorage import mozilla.components.concept.storage.HistoryStorage
import org.mozilla.fenix.home.recentbookmarks.RecentBookmark import org.mozilla.fenix.home.recentbookmarks.RecentBookmark
@ -29,18 +30,21 @@ class BookmarksUseCase(
*/ */
@WorkerThread @WorkerThread
suspend operator fun invoke(url: String, title: String, position: UInt? = null): Boolean { 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) { if (canAdd) {
storage.addItem( storage.addItem(
BookmarkRoot.Mobile.id, BookmarkRoot.Mobile.id,
url = url, url = url,
title = title, title = title,
position = position position = position
) )
}
return canAdd
} catch (e: PlacesException.UrlParseFailed) {
return false
} }
return canAdd
} }
} }

Loading…
Cancel
Save