- break;
-
- case R.id.delete_bookmark:
- // Set the deleting bookmarks flag, which prevents the delete menu item from being enabled until the current process finishes.
- deletingBookmarks = true;
-
- // Get an array of the selected row IDs.
- final long[] selectedBookmarksIdsLongArray = bookmarksListView.getCheckedItemIds();
-
- // Get an array of checked bookmarks. `.clone()` makes a copy that won't change if the list view is reloaded, which is needed for re-selecting the bookmarks on undelete.
- selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
-
- // Update the bookmarks cursor with the current contents of the bookmarks database except for the specified database IDs.
- bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, currentFolder);
-
- // Update the list view.
- bookmarksCursorAdapter.changeCursor(bookmarksCursor);
-
- // Show a Snackbar with the number of deleted bookmarks.
- bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), getString(R.string.bookmarks_deleted) + " " + selectedBookmarksIdsLongArray.length,
- Snackbar.LENGTH_LONG)
- .setAction(R.string.undo, view -> {
- // Do nothing because everything will be handled by `onDismissed()` below.
- })
- .addCallback(new Snackbar.Callback() {
- @SuppressLint("SwitchIntDef") // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
- @Override
- public void onDismissed(Snackbar snackbar, int event) {
- switch (event) {
- // The user pushed the `Undo` button.
- case Snackbar.Callback.DISMISS_EVENT_ACTION:
- // Update the bookmarks cursor with the current contents of the bookmarks database, including the "deleted" bookmarks.
- bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
-
- // Update the list view.
- bookmarksCursorAdapter.changeCursor(bookmarksCursor);
-
- // Re-select the previously selected bookmarks.
- for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
- bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
- }
- break;
-
- // The Snackbar was dismissed without the `Undo` button being pushed.
- default:
- // Delete each selected bookmark.
- for (long databaseIdLong : selectedBookmarksIdsLongArray) {
- // Convert `databaseIdLong` to an int.
- int databaseIdInt = (int) databaseIdLong;
-
- // Delete the contents of the folder if the selected bookmark is a folder.
- if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
- deleteBookmarkFolderContents(databaseIdInt);
- }
-
- // Delete the selected bookmark.
- bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);
- }
-
- // Update the display order.
- for (int i = 0; i < bookmarksListView.getCount(); i++) {
- // Get the database ID for the current bookmark.
- int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
-
- // Move `bookmarksCursor` to the current bookmark position.
- bookmarksCursor.moveToPosition(i);
-
- // Update the display order only if it is not correct in the database.
- if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
- bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
- }
- }
+
+ // Increment the count of the number of bookmarks to delete.
+ numberOfBookmarksToDelete++;
+ }
+
+ // Get an array of checked bookmarks. `.clone()` makes a copy that won't change if the list view is reloaded, which is needed for re-selecting the bookmarks on undelete.
+ selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
+
+ // Update the bookmarks cursor with the current contents of the bookmarks database except for the specified database IDs.
+ bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, currentFolder);
+
+ // Update the list view.
+ bookmarksCursorAdapter.changeCursor(bookmarksCursor);
+
+ // Create a Snackbar with the number of deleted bookmarks.
+ bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), getString(R.string.bookmarks_deleted) + " " + numberOfBookmarksToDelete,
+ Snackbar.LENGTH_LONG)
+ .setAction(R.string.undo, view -> {
+ // Do nothing because everything will be handled by `onDismissed()` below.
+ })
+ .addCallback(new Snackbar.Callback() {
+ @SuppressLint("SwitchIntDef") // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
+ @Override
+ public void onDismissed(Snackbar snackbar, int event) {
+ if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) { // The user pushed the undo button.
+ // Update the bookmarks cursor with the current contents of the bookmarks database, including the "deleted" bookmarks.
+ bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
+
+ // Update the list view.
+ bookmarksCursorAdapter.changeCursor(bookmarksCursor);
+
+ // Re-select the previously selected bookmarks.
+ for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
+ bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
+ }
+ } else { // The snackbar was dismissed without the undo button being pushed.
+ // Delete each selected bookmark.
+ for (long databaseIdLong : selectedBookmarksIdsLongArray) {
+ // Convert `databaseIdLong` to an int.
+ int databaseIdInt = (int) databaseIdLong;
+
+ // Delete the contents of the folder if the selected bookmark is a folder.
+ if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
+ deleteBookmarkFolderContents(databaseIdInt);
+ }
+
+ // Delete the selected bookmark.
+ bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);