From: Soren Stoutner Date: Fri, 22 Sep 2017 18:43:02 +0000 (-0700) Subject: Hide the move to folder menu item if no folders exist. https://redmine.stoutner... X-Git-Tag: v2.7~20 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=b9b9315f5b6b0bc171082f11660ca390748c0c2b;p=PrivacyBrowserAndroid.git Hide the move to folder menu item if no folders exist. https://redmine.stoutner.com/issues/195. --- diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java index a178d49a..4536001b 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java @@ -203,7 +203,17 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { - return false; + // Get a handle for the move to folder menu item. + MenuItem moveToFolderMenuItem = menu.findItem(R.id.move_to_folder); + + // Get a `Cursor` with all of the folders. + Cursor folderCursor = bookmarksDatabaseHelper.getAllFoldersCursor(); + + // Enable the move to folder menu item if at least one folder exists. + moveToFolderMenuItem.setVisible(folderCursor.getCount() > 0); + + // `return true` indicates that the menu has been updated. + return true; } @Override @@ -759,39 +769,35 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma ListView folderListView = (ListView) dialogFragment.getDialog().findViewById(R.id.move_to_folder_listview); long[] newFolderLongArray = folderListView.getCheckedItemIds(); - if (newFolderLongArray.length == 0) { // No new folder was selected. - Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), getString(R.string.cannot_move_bookmarks), Snackbar.LENGTH_INDEFINITE).show(); - } else { // Move the selected bookmarks. - // Get the new folder database ID. - int newFolderDatabaseId = (int) newFolderLongArray[0]; - - // Instantiate `newFolderName`. - String newFolderName; - - if (newFolderDatabaseId == 0) { - // The new folder is the home folder, represented as `""` in the database. - newFolderName = ""; - } else { - // Get the new folder name from the database. - newFolderName = bookmarksDatabaseHelper.getFolderName(newFolderDatabaseId); - } + // Get the new folder database ID. + int newFolderDatabaseId = (int) newFolderLongArray[0]; - // Get a long array with the the database ID of the selected bookmarks. - long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds(); - for (long databaseIdLong : selectedBookmarksLongArray) { - // Get `databaseIdInt` for each selected bookmark. - int databaseIdInt = (int) databaseIdLong; + // Instantiate `newFolderName`. + String newFolderName; - // Move the selected bookmark to the new folder. - bookmarksDatabaseHelper.moveToFolder(databaseIdInt, newFolderName); - } + if (newFolderDatabaseId == 0) { + // The new folder is the home folder, represented as `""` in the database. + newFolderName = ""; + } else { + // Get the new folder name from the database. + newFolderName = bookmarksDatabaseHelper.getFolderName(newFolderDatabaseId); + } - // Refresh the `ListView`. - updateBookmarksListView(currentFolder); + // Get a long array with the the database ID of the selected bookmarks. + long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds(); + for (long databaseIdLong : selectedBookmarksLongArray) { + // Get `databaseIdInt` for each selected bookmark. + int databaseIdInt = (int) databaseIdLong; - // Close the contextual app bar. - contextualActionMode.finish(); + // Move the selected bookmark to the new folder. + bookmarksDatabaseHelper.moveToFolder(databaseIdInt, newFolderName); } + + // Refresh the `ListView`. + updateBookmarksListView(currentFolder); + + // Close the contextual app bar. + contextualActionMode.finish(); } private void updateBookmarksListView(String folderName) { diff --git a/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java b/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java index 0cd2b683..55cc346b 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java +++ b/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java @@ -216,6 +216,19 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { return parentFolder; } + public Cursor getAllFoldersCursor() { + // Get a readable database handle. + SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); + + // Prepare the SQL statement to get the `Cursor` for all the folders. + final String GET_ALL_FOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE + + " WHERE " + IS_FOLDER + " = " + 1; + + // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`. + // We can't close the `Cursor` because we need to use it in the parent activity. + return bookmarksDatabase.rawQuery(GET_ALL_FOLDERS, null); + } + public Cursor getAllBookmarksCursor() { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index b81e5d92..2fcf842e 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -154,7 +154,6 @@ URL für das Lesezeichen Ordnernamen müssen einmalig sein Kann diesen Ordner nicht umbenennen, da der neue Name bereits existiert: - Kann die markierten Lesezeichen nicht verschieben, da kein neuer Ordner ausgewählt wurde. Lesezeichen Bearbeiten Ordner Bearbeiten In Ordner verschieben diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index c832ded3..894bd333 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -171,7 +171,6 @@ Marcador URL Los nombres de carpetas deben ser únicos No se guardó la carpeta porque el nuevo nombre no fue único: - No se movió los marcadores selectionados porque ninguna carpeta nueva fue seleccionada. Editar marcador Editar carpeta Mover a carpeta diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index e0a6c198..7fdaa3e2 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -173,7 +173,6 @@ URL Segnalibro I nomi delle cartelle devono essere unici Non è possibile salvare la cartella perché già esistente: - Non è possibile spostare il segnalibro, nessuna cartella selezionata. Modifica Segnalibro Modifica Cartella Sposta nella Cartella diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 75511e64..863b9670 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -176,7 +176,6 @@ Bookmark URL Folder names must be unique Cannot save the folder because the new name is not unique: - Cannot move the selected bookmarks because no new folder was selected. Edit Bookmark Edit Folder Move to Folder