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.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")
}
}
}

@ -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
}
}

Loading…
Cancel
Save