2019-03-21 20:41:41 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 20:38:15 +02:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2019-03-21 20:41:41 +01:00
|
|
|
|
|
|
|
package org.mozilla.fenix.library.bookmarks
|
|
|
|
|
2020-04-15 13:54:56 +02:00
|
|
|
import android.content.DialogInterface
|
2019-03-21 20:41:41 +01:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuInflater
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2020-04-15 13:54:56 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
2020-06-13 00:58:11 +02:00
|
|
|
import androidx.core.view.isVisible
|
2019-07-26 04:55:51 +02:00
|
|
|
import androidx.fragment.app.activityViewModels
|
2019-09-01 00:41:50 +02:00
|
|
|
import androidx.lifecycle.ViewModelProvider
|
2019-06-13 02:14:46 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2019-08-21 17:36:41 +02:00
|
|
|
import androidx.navigation.NavDirections
|
2019-07-01 17:57:16 +02:00
|
|
|
import androidx.navigation.fragment.findNavController
|
2020-06-13 00:58:11 +02:00
|
|
|
import androidx.navigation.fragment.navArgs
|
2020-06-11 19:44:44 +02:00
|
|
|
import kotlinx.android.synthetic.main.component_bookmark.view.*
|
2019-03-21 20:41:41 +01:00
|
|
|
import kotlinx.android.synthetic.main.fragment_bookmark.view.*
|
2019-09-27 01:52:59 +02:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
2019-03-21 20:41:41 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2019-08-07 23:00:53 +02:00
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
2019-10-05 07:20:58 +02:00
|
|
|
import kotlinx.coroutines.async
|
|
|
|
import kotlinx.coroutines.awaitAll
|
2019-07-01 17:57:31 +02:00
|
|
|
import kotlinx.coroutines.isActive
|
2019-03-21 20:41:41 +01:00
|
|
|
import kotlinx.coroutines.launch
|
2019-09-27 17:57:39 +02:00
|
|
|
import kotlinx.coroutines.withContext
|
2019-03-21 20:41:41 +01:00
|
|
|
import mozilla.appservices.places.BookmarkRoot
|
2019-11-25 20:07:21 +01:00
|
|
|
import mozilla.components.concept.engine.prompt.ShareData
|
2019-03-21 20:41:41 +01:00
|
|
|
import mozilla.components.concept.storage.BookmarkNode
|
2019-08-02 19:11:21 +02:00
|
|
|
import mozilla.components.concept.storage.BookmarkNodeType
|
2019-07-25 16:32:32 +02:00
|
|
|
import mozilla.components.lib.state.ext.consumeFrom
|
2019-11-28 00:02:47 +01:00
|
|
|
import mozilla.components.support.base.feature.UserInteractionHandler
|
2020-01-16 19:03:18 +01:00
|
|
|
import org.mozilla.fenix.HomeActivity
|
2019-03-21 20:41:41 +01:00
|
|
|
import org.mozilla.fenix.R
|
2020-01-02 23:31:52 +01:00
|
|
|
import org.mozilla.fenix.components.FenixSnackbar
|
2019-07-26 04:55:51 +02:00
|
|
|
import org.mozilla.fenix.components.StoreProvider
|
2019-04-17 19:37:36 +02:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2019-06-12 21:10:43 +02:00
|
|
|
import org.mozilla.fenix.ext.bookmarkStorage
|
2019-05-01 15:14:00 +02:00
|
|
|
import org.mozilla.fenix.ext.components
|
2019-06-12 21:10:43 +02:00
|
|
|
import org.mozilla.fenix.ext.minus
|
2019-06-06 21:40:10 +02:00
|
|
|
import org.mozilla.fenix.ext.nav
|
2020-04-17 08:43:08 +02:00
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2020-04-15 13:54:56 +02:00
|
|
|
import org.mozilla.fenix.ext.toShortUrl
|
2019-08-01 17:58:41 +02:00
|
|
|
import org.mozilla.fenix.library.LibraryPageFragment
|
2019-05-30 02:14:51 +02:00
|
|
|
import org.mozilla.fenix.utils.allowUndo
|
2019-03-21 20:41:41 +01:00
|
|
|
|
2020-05-06 21:37:04 +02:00
|
|
|
/**
|
|
|
|
* The screen that displays the user's bookmark list in their Library.
|
|
|
|
*/
|
2019-09-27 17:57:39 +02:00
|
|
|
@Suppress("TooManyFunctions", "LargeClass")
|
2019-11-28 00:02:47 +01:00
|
|
|
class BookmarkFragment : LibraryPageFragment<BookmarkNode>(), UserInteractionHandler {
|
2019-03-21 20:41:41 +01:00
|
|
|
|
2019-08-30 14:09:42 +02:00
|
|
|
private lateinit var bookmarkStore: BookmarkFragmentStore
|
2019-07-26 04:55:51 +02:00
|
|
|
private lateinit var bookmarkView: BookmarkView
|
2020-04-18 04:27:33 +02:00
|
|
|
private var _bookmarkInteractor: BookmarkFragmentInteractor? = null
|
2020-06-13 00:58:11 +02:00
|
|
|
private val bookmarkInteractor: BookmarkFragmentInteractor
|
2020-04-18 04:27:33 +02:00
|
|
|
get() = _bookmarkInteractor!!
|
2019-07-26 04:55:51 +02:00
|
|
|
|
2019-09-01 00:41:50 +02:00
|
|
|
private val sharedViewModel: BookmarksSharedViewModel by activityViewModels {
|
|
|
|
ViewModelProvider.NewInstanceFactory() // this is a workaround for #4652
|
|
|
|
}
|
2020-05-06 19:17:17 +02:00
|
|
|
private val desktopFolders by lazy { DesktopFolders(requireContext(), showMobileRoot = false) }
|
2019-07-26 04:55:51 +02:00
|
|
|
|
2019-06-26 00:56:00 +02:00
|
|
|
private var pendingBookmarkDeletionJob: (suspend () -> Unit)? = null
|
2019-07-26 04:55:51 +02:00
|
|
|
private var pendingBookmarksToDelete: MutableSet<BookmarkNode> = mutableSetOf()
|
|
|
|
|
|
|
|
private val metrics
|
|
|
|
get() = context?.components?.analytics?.metrics
|
2019-03-21 20:41:41 +01:00
|
|
|
|
2019-08-01 17:58:41 +02:00
|
|
|
override val selectedItems get() = bookmarkStore.state.mode.selectedItems
|
|
|
|
|
2019-03-21 20:41:41 +01:00
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
|
|
val view = inflater.inflate(R.layout.fragment_bookmark, container, false)
|
2019-07-26 04:55:51 +02:00
|
|
|
|
|
|
|
bookmarkStore = StoreProvider.get(this) {
|
2019-09-25 03:52:02 +02:00
|
|
|
BookmarkFragmentStore(BookmarkFragmentState(null))
|
2019-07-26 04:55:51 +02:00
|
|
|
}
|
2020-03-12 17:54:13 +01:00
|
|
|
|
2020-04-18 04:27:33 +02:00
|
|
|
_bookmarkInteractor = BookmarkFragmentInteractor(
|
2019-08-19 17:34:57 +02:00
|
|
|
bookmarksController = DefaultBookmarkController(
|
2020-05-06 19:17:17 +02:00
|
|
|
context = requireContext(),
|
2019-08-19 17:34:57 +02:00
|
|
|
navController = findNavController(),
|
2020-06-13 00:58:11 +02:00
|
|
|
scope = viewLifecycleOwner.lifecycleScope,
|
|
|
|
store = bookmarkStore,
|
|
|
|
sharedViewModel = sharedViewModel,
|
|
|
|
loadBookmarkNode = ::loadBookmarkNode,
|
2020-04-18 22:03:57 +02:00
|
|
|
showSnackbar = ::showSnackBarWithText,
|
2019-08-21 17:36:41 +02:00
|
|
|
deleteBookmarkNodes = ::deleteMulti,
|
2020-04-15 13:54:56 +02:00
|
|
|
deleteBookmarkFolder = ::showRemoveFolderDialog,
|
2019-08-21 17:36:41 +02:00
|
|
|
invokePendingDeletion = ::invokePendingDeletion
|
2019-08-19 17:34:57 +02:00
|
|
|
),
|
|
|
|
metrics = metrics!!
|
2019-05-15 08:16:48 +02:00
|
|
|
)
|
2019-07-26 04:55:51 +02:00
|
|
|
|
2020-06-11 19:44:44 +02:00
|
|
|
bookmarkView = BookmarkView(view.bookmarkLayout, bookmarkInteractor, findNavController())
|
|
|
|
bookmarkView.view.bookmark_folders_sign_in.visibility = View.GONE
|
2019-09-27 17:57:39 +02:00
|
|
|
|
2020-04-18 04:27:33 +02:00
|
|
|
viewLifecycleOwner.lifecycle.addObserver(
|
2019-08-15 20:33:57 +02:00
|
|
|
BookmarkDeselectNavigationListener(
|
|
|
|
findNavController(),
|
|
|
|
sharedViewModel,
|
|
|
|
bookmarkInteractor
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-03-21 20:41:41 +01:00
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:03:57 +02:00
|
|
|
private fun showSnackBarWithText(text: String) {
|
|
|
|
view?.let {
|
|
|
|
FenixSnackbar.make(
|
|
|
|
view = it,
|
|
|
|
duration = FenixSnackbar.LENGTH_LONG,
|
|
|
|
isDisplayedWithBrowserToolbar = false
|
|
|
|
).setText(text).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 23:00:53 +02:00
|
|
|
@ExperimentalCoroutinesApi
|
2019-07-26 04:55:51 +02:00
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2020-06-13 00:58:11 +02:00
|
|
|
val accountManager = requireComponents.backgroundServices.accountManager
|
2019-07-25 16:32:32 +02:00
|
|
|
consumeFrom(bookmarkStore) {
|
|
|
|
bookmarkView.update(it)
|
2020-06-13 00:58:11 +02:00
|
|
|
|
|
|
|
// Only display the sign-in prompt if we're inside of the virtual "Desktop Bookmarks" node.
|
|
|
|
// Don't want to pester user too much with it, and if there are lots of bookmarks present,
|
|
|
|
// it'll just get visually lost. Inside of the "Desktop Bookmarks" node, it'll nicely stand-out,
|
|
|
|
// since there are always only three other items in there. It's also the right place contextually.
|
|
|
|
bookmarkView.view.bookmark_folders_sign_in.isVisible =
|
|
|
|
it.tree?.guid == BookmarkRoot.Root.id && accountManager.authenticatedAccount() == null
|
2019-07-26 04:55:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 20:41:41 +01:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setHasOptionsMenu(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2019-07-03 20:13:53 +02:00
|
|
|
|
2020-01-16 19:03:18 +01:00
|
|
|
(activity as HomeActivity).getSupportActionBarAndInflateIfNecessary().show()
|
2019-04-17 19:37:36 +02:00
|
|
|
|
2020-06-13 00:58:11 +02:00
|
|
|
// Reload bookmarks when returning to this fragment in case they have been edited
|
|
|
|
val args by navArgs<BookmarkFragmentArgs>()
|
|
|
|
val currentGuid = bookmarkStore.state.tree?.guid
|
|
|
|
?: if (args.currentRoot.isNotEmpty()) {
|
|
|
|
args.currentRoot
|
|
|
|
} else {
|
|
|
|
BookmarkRoot.Mobile.id
|
|
|
|
}
|
|
|
|
loadInitialBookmarkFolder(currentGuid)
|
2019-05-07 23:36:37 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 00:58:11 +02:00
|
|
|
private fun loadInitialBookmarkFolder(currentGuid: String) {
|
|
|
|
viewLifecycleOwner.lifecycleScope.launch(Main) {
|
|
|
|
val currentRoot = loadBookmarkNode(currentGuid)
|
2019-04-17 19:37:36 +02:00
|
|
|
|
2020-06-13 00:58:11 +02:00
|
|
|
if (isActive && currentRoot != null) {
|
2019-09-27 17:57:39 +02:00
|
|
|
bookmarkInteractor.onBookmarksChanged(currentRoot)
|
2019-04-17 19:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 22:40:39 +02:00
|
|
|
}
|
|
|
|
|
2019-03-21 20:41:41 +01:00
|
|
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
2019-08-04 00:23:15 +02:00
|
|
|
when (val mode = bookmarkStore.state.mode) {
|
2019-12-12 06:49:33 +01:00
|
|
|
is BookmarkFragmentState.Mode.Normal -> {
|
|
|
|
if (mode.showMenu) {
|
|
|
|
inflater.inflate(R.menu.bookmarks_menu, menu)
|
|
|
|
}
|
2019-04-11 06:30:15 +02:00
|
|
|
}
|
2019-08-30 14:09:42 +02:00
|
|
|
is BookmarkFragmentState.Mode.Selecting -> {
|
2019-08-02 19:11:21 +02:00
|
|
|
if (mode.selectedItems.any { it.type != BookmarkNodeType.ITEM }) {
|
|
|
|
inflater.inflate(R.menu.bookmarks_select_multi_not_item, menu)
|
|
|
|
} else {
|
|
|
|
inflater.inflate(R.menu.bookmarks_select_multi, menu)
|
|
|
|
}
|
2019-04-11 06:30:15 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-21 20:41:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
return when (item.itemId) {
|
2020-06-22 19:15:33 +02:00
|
|
|
R.id.close_bookmarks -> {
|
|
|
|
invokePendingDeletion()
|
|
|
|
close()
|
|
|
|
true
|
|
|
|
}
|
2019-06-27 22:11:25 +02:00
|
|
|
R.id.add_bookmark_folder -> {
|
2019-08-21 17:36:41 +02:00
|
|
|
navigate(
|
2019-06-27 22:11:25 +02:00
|
|
|
BookmarkFragmentDirections
|
|
|
|
.actionBookmarkFragmentToBookmarkAddFolderFragment()
|
|
|
|
)
|
|
|
|
true
|
|
|
|
}
|
2019-04-11 06:30:15 +02:00
|
|
|
R.id.open_bookmarks_in_new_tabs_multi_select -> {
|
2019-08-01 17:58:41 +02:00
|
|
|
openItemsInNewTab { node -> node.url }
|
2019-04-11 06:30:15 +02:00
|
|
|
|
2020-04-14 06:43:45 +02:00
|
|
|
navigate(BookmarkFragmentDirections.actionGlobalHome())
|
2019-07-26 04:55:51 +02:00
|
|
|
metrics?.track(Event.OpenedBookmarksInNewTabs)
|
2019-04-11 06:30:15 +02:00
|
|
|
true
|
|
|
|
}
|
2019-08-01 17:58:41 +02:00
|
|
|
R.id.open_bookmarks_in_private_tabs_multi_select -> {
|
|
|
|
openItemsInNewTab(private = true) { node -> node.url }
|
|
|
|
|
2020-04-14 06:43:45 +02:00
|
|
|
navigate(BookmarkFragmentDirections.actionGlobalHome())
|
2019-08-01 17:58:41 +02:00
|
|
|
metrics?.track(Event.OpenedBookmarksInPrivateTabs)
|
|
|
|
true
|
|
|
|
}
|
2019-10-22 23:34:05 +02:00
|
|
|
R.id.share_bookmark_multi_select -> {
|
2020-03-03 18:05:41 +01:00
|
|
|
val shareTabs = bookmarkStore.state.mode.selectedItems.map {
|
|
|
|
ShareData(url = it.url, title = it.title)
|
|
|
|
}
|
2019-08-21 17:36:41 +02:00
|
|
|
navigate(
|
2020-04-14 06:43:45 +02:00
|
|
|
BookmarkFragmentDirections.actionGlobalShareFragment(
|
2020-03-03 18:05:41 +01:00
|
|
|
data = shareTabs.toTypedArray()
|
2019-08-21 17:36:41 +02:00
|
|
|
)
|
2019-06-06 21:40:10 +02:00
|
|
|
)
|
2019-04-11 06:30:15 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.delete_bookmarks_multi_select -> {
|
2019-08-01 17:58:41 +02:00
|
|
|
deleteMulti(bookmarkStore.state.mode.selectedItems)
|
2019-04-11 06:30:15 +02:00
|
|
|
true
|
|
|
|
}
|
2019-03-21 20:41:41 +01:00
|
|
|
else -> super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:36:41 +02:00
|
|
|
private fun navigate(directions: NavDirections) {
|
|
|
|
invokePendingDeletion()
|
2020-04-14 06:43:45 +02:00
|
|
|
findNavController().nav(
|
|
|
|
R.id.bookmarkFragment,
|
|
|
|
directions
|
|
|
|
)
|
2019-08-21 17:36:41 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 20:24:59 +02:00
|
|
|
override fun onBackPressed(): Boolean {
|
|
|
|
invokePendingDeletion()
|
|
|
|
return bookmarkView.onBackPressed()
|
|
|
|
}
|
2019-04-04 22:40:39 +02:00
|
|
|
|
2020-06-13 00:58:11 +02:00
|
|
|
private suspend fun loadBookmarkNode(guid: String): BookmarkNode? = withContext(IO) {
|
|
|
|
requireContext().bookmarkStorage
|
|
|
|
.getTree(guid, false)
|
|
|
|
?.let { desktopFolders.withOptionalDesktopFolders(it) }
|
|
|
|
}
|
|
|
|
|
2019-05-29 01:50:26 +02:00
|
|
|
private suspend fun refreshBookmarks() {
|
2019-08-10 02:43:48 +02:00
|
|
|
// The bookmark tree in our 'state' can be null - meaning, no bookmark tree has been selected.
|
|
|
|
// If that's the case, we don't know what node to refresh, and so we bail out.
|
|
|
|
// See https://github.com/mozilla-mobile/fenix/issues/4671
|
|
|
|
val currentGuid = bookmarkStore.state.tree?.guid ?: return
|
2020-06-13 00:58:11 +02:00
|
|
|
loadBookmarkNode(currentGuid)
|
2019-04-11 06:30:15 +02:00
|
|
|
?.let { node ->
|
2019-09-24 18:17:29 +02:00
|
|
|
val rootNode = node - pendingBookmarksToDelete
|
2019-08-19 17:34:57 +02:00
|
|
|
bookmarkInteractor.onBookmarksChanged(rootNode)
|
2019-04-11 06:30:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 00:56:00 +02:00
|
|
|
override fun onPause() {
|
|
|
|
invokePendingDeletion()
|
|
|
|
super.onPause()
|
|
|
|
}
|
|
|
|
|
2019-08-01 17:58:41 +02:00
|
|
|
private suspend fun deleteSelectedBookmarks(selected: Set<BookmarkNode>) {
|
2019-09-27 01:52:59 +02:00
|
|
|
CoroutineScope(IO).launch {
|
|
|
|
val tempStorage = context?.bookmarkStorage
|
2019-10-05 07:20:58 +02:00
|
|
|
selected.map {
|
|
|
|
async { tempStorage?.deleteNode(it.guid) }
|
|
|
|
}.awaitAll()
|
2019-07-26 04:55:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun deleteMulti(selected: Set<BookmarkNode>, eventType: Event = Event.RemoveBookmarks) {
|
2020-04-15 13:54:56 +02:00
|
|
|
updatePendingBookmarksToDelete(selected)
|
2019-07-26 04:55:51 +02:00
|
|
|
|
2020-04-15 13:54:56 +02:00
|
|
|
pendingBookmarkDeletionJob = getDeleteOperation(eventType)
|
2019-07-26 04:55:51 +02:00
|
|
|
|
|
|
|
val message = when (eventType) {
|
|
|
|
is Event.RemoveBookmarks -> {
|
2020-06-19 00:49:27 +02:00
|
|
|
getRemoveBookmarksSnackBarMessage(selected)
|
2019-07-26 04:55:51 +02:00
|
|
|
}
|
|
|
|
is Event.RemoveBookmarkFolder,
|
|
|
|
is Event.RemoveBookmark -> {
|
|
|
|
val bookmarkNode = selected.first()
|
|
|
|
getString(
|
|
|
|
R.string.bookmark_deletion_snackbar_message,
|
2020-05-06 19:17:17 +02:00
|
|
|
bookmarkNode.url?.toShortUrl(requireContext().components.publicSuffixList) ?: bookmarkNode.title
|
2019-07-26 04:55:51 +02:00
|
|
|
)
|
|
|
|
}
|
2019-08-19 17:34:57 +02:00
|
|
|
else -> throw IllegalStateException("Illegal event type in onDeleteSome")
|
2019-07-26 04:55:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 05:41:20 +02:00
|
|
|
viewLifecycleOwner.lifecycleScope.allowUndo(
|
2020-05-06 19:17:17 +02:00
|
|
|
requireView(), message,
|
2019-07-26 04:55:51 +02:00
|
|
|
getString(R.string.bookmark_undo_deletion), {
|
2020-04-15 13:54:56 +02:00
|
|
|
undoPendingDeletion(selected)
|
|
|
|
}, operation = getDeleteOperation(eventType)
|
2019-07-26 04:55:51 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-19 00:49:27 +02:00
|
|
|
private fun getRemoveBookmarksSnackBarMessage(selected: Set<BookmarkNode>): String {
|
2020-04-15 13:54:56 +02:00
|
|
|
return if (selected.size > 1) {
|
2020-06-19 00:49:27 +02:00
|
|
|
getString(R.string.bookmark_deletion_multiple_snackbar_message_2)
|
2020-04-15 13:54:56 +02:00
|
|
|
} else {
|
|
|
|
val bookmarkNode = selected.first()
|
|
|
|
getString(
|
|
|
|
R.string.bookmark_deletion_snackbar_message,
|
2020-05-06 19:17:17 +02:00
|
|
|
bookmarkNode.url?.toShortUrl(requireContext().components.publicSuffixList)
|
2020-04-15 13:54:56 +02:00
|
|
|
?: bookmarkNode.title
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 04:27:33 +02:00
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
_bookmarkInteractor = null
|
|
|
|
}
|
|
|
|
|
2020-06-19 00:49:27 +02:00
|
|
|
private fun showRemoveFolderDialog(selected: BookmarkNode) {
|
2020-04-15 13:54:56 +02:00
|
|
|
activity?.let { activity ->
|
|
|
|
AlertDialog.Builder(activity).apply {
|
2020-06-19 00:49:27 +02:00
|
|
|
setMessage(R.string.bookmark_delete_folder_confirmation_dialog)
|
2020-04-15 13:54:56 +02:00
|
|
|
setNegativeButton(R.string.delete_browsing_data_prompt_cancel) { dialog: DialogInterface, _ ->
|
|
|
|
dialog.cancel()
|
|
|
|
}
|
|
|
|
setPositiveButton(R.string.delete_browsing_data_prompt_allow) { dialog: DialogInterface, _ ->
|
2020-06-19 00:49:27 +02:00
|
|
|
updatePendingBookmarksToDelete(setOf(selected))
|
2020-04-15 13:54:56 +02:00
|
|
|
pendingBookmarkDeletionJob = getDeleteOperation(Event.RemoveBookmarkFolder)
|
|
|
|
dialog.dismiss()
|
2020-06-19 00:49:27 +02:00
|
|
|
val message = getDeleteDialogString(selected)
|
2020-05-07 17:55:07 +02:00
|
|
|
viewLifecycleOwner.lifecycleScope.allowUndo(
|
2020-05-06 19:17:17 +02:00
|
|
|
requireView(),
|
2020-06-19 00:49:27 +02:00
|
|
|
message,
|
2020-04-15 13:54:56 +02:00
|
|
|
getString(R.string.bookmark_undo_deletion),
|
|
|
|
{
|
2020-06-19 00:49:27 +02:00
|
|
|
undoPendingDeletion(setOf(selected))
|
2020-04-15 13:54:56 +02:00
|
|
|
},
|
|
|
|
operation = getDeleteOperation(Event.RemoveBookmarkFolder)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
create()
|
|
|
|
}
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updatePendingBookmarksToDelete(selected: Set<BookmarkNode>) {
|
|
|
|
pendingBookmarksToDelete.addAll(selected)
|
|
|
|
val bookmarkTree = sharedViewModel.selectedFolder!! - pendingBookmarksToDelete
|
|
|
|
bookmarkInteractor.onBookmarksChanged(bookmarkTree)
|
|
|
|
}
|
|
|
|
|
2020-06-19 00:49:27 +02:00
|
|
|
private fun getDeleteDialogString(selected: BookmarkNode): String {
|
|
|
|
return getString(
|
|
|
|
R.string.bookmark_deletion_snackbar_message,
|
|
|
|
context?.components?.publicSuffixList?.let { selected.url?.toShortUrl(it) }
|
|
|
|
?: selected.title
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:54:56 +02:00
|
|
|
private suspend fun undoPendingDeletion(selected: Set<BookmarkNode>) {
|
|
|
|
pendingBookmarksToDelete.removeAll(selected)
|
|
|
|
pendingBookmarkDeletionJob = null
|
|
|
|
refreshBookmarks()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getDeleteOperation(event: Event): (suspend () -> Unit) {
|
|
|
|
return {
|
|
|
|
deleteSelectedBookmarks(pendingBookmarksToDelete)
|
|
|
|
pendingBookmarkDeletionJob = null
|
|
|
|
// Since this runs in a coroutine, we can't depend upon the fragment still being attached
|
|
|
|
metrics?.track(event)
|
|
|
|
refreshBookmarks()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 00:56:00 +02:00
|
|
|
private fun invokePendingDeletion() {
|
|
|
|
pendingBookmarkDeletionJob?.let {
|
|
|
|
viewLifecycleOwner.lifecycleScope.launch {
|
|
|
|
it.invoke()
|
|
|
|
}.invokeOnCompletion {
|
|
|
|
pendingBookmarkDeletionJob = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 15:14:00 +02:00
|
|
|
}
|