From 4ecb96eb93da652b3264b1931e344222c2d5d833 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Fri, 29 Sep 2017 14:21:36 -0700 Subject: [PATCH] Improve the scrolling of bookmarks. https://redmine.stoutner.com/issues/42. --- app/src/main/assets/en/css/dark_theme.css | 2 +- .../main/assets/it/about_licenses_dark.html | 6 + .../main/assets/it/about_licenses_light.html | 6 + .../activities/BookmarksActivity.java | 546 +++++++++--------- .../layout/bookmarks_coordinatorlayout.xml | 4 +- 5 files changed, 303 insertions(+), 261 deletions(-) diff --git a/app/src/main/assets/en/css/dark_theme.css b/app/src/main/assets/en/css/dark_theme.css index 8835ef0f..d236aa57 100644 --- a/app/src/main/assets/en/css/dark_theme.css +++ b/app/src/main/assets/en/css/dark_theme.css @@ -32,7 +32,7 @@ h3 { } strong.red { - color: #B71C1C; + color: #A71919; } strong.blue { diff --git a/app/src/main/assets/it/about_licenses_dark.html b/app/src/main/assets/it/about_licenses_dark.html index 4b213959..7727f949 100644 --- a/app/src/main/assets/it/about_licenses_dark.html +++ b/app/src/main/assets/it/about_licenses_dark.html @@ -44,6 +44,12 @@ sono state derivate da ic_security e ic_language, che fanno parte dell'Android Material icon set e sono state rilasciate sotto Licenza Apache 2.0. Il testo completo della Licenza è riportato di seguito. Copyright delle modifiche © 2016 Soren Stoutner. Le immagini risultanti sono rilasciate sotto Licenza GPLv3+.

+

è stata derivata da elementi di ic_folder e di ic_exit_to_app, che fanno parte dell'Android Material icon set + e sono state rilasciate sotto Licenza Apache 2.0. Copyright delle modifiche © 2017 Soren Stoutner. + L'immagine risultante è rilasciata sotto Licenza GPLv3+.

+

è stata derivata da elementi di ic_bookmark e di ic_create_new_folder, che fanno parte dell'Android Material icon set + e sono state rilasciate sotto Licenza Apache 2.0. Copyright delle modifiche © 2017 Soren Stoutner. + L'immagine risultante è rilasciata sotto Licenza GPLv3+.

è stata derivata da ic_create_new_folder, che fa parte dell'Android Material icon set ed è stata rilasciata sotto Licenza Apache 2.0. Copyright delle modifiche © 2017 Soren Stoutner. L'immagine risultante è rilasciata sotto Licenza GPLv3+.

diff --git a/app/src/main/assets/it/about_licenses_light.html b/app/src/main/assets/it/about_licenses_light.html index da4c7083..f93e858d 100644 --- a/app/src/main/assets/it/about_licenses_light.html +++ b/app/src/main/assets/it/about_licenses_light.html @@ -44,6 +44,12 @@ sono state derivate da ic_security e ic_language, che fanno parte dell'Android Material icon set e sono state rilasciate sotto Licenza Apache 2.0. Il testo completo della Licenza è riportato di seguito. Copyright delle modifiche © 2016 Soren Stoutner. Le immagini risultanti sono rilasciate sotto Licenza GPLv3+.

+

è stata derivata da elementi di ic_folder e di ic_exit_to_app, che fanno parte dell'Android Material icon set + e sono state rilasciate sotto Licenza Apache 2.0. Copyright delle modifiche © 2017 Soren Stoutner. + L'immagine risultante è rilasciata sotto Licenza GPLv3+.

+

è stata derivata da elementi di ic_bookmark e di ic_create_new_folder, che fanno parte dell'Android Material icon set + e sono state rilasciate sotto Licenza Apache 2.0. Copyright delle modifiche © 2017 Soren Stoutner. + L'immagine risultante è rilasciata sotto Licenza GPLv3+.

è stata derivata da ic_create_new_folder, che fa parte dell'Android Material icon set ed è stata rilasciata sotto Licenza Apache 2.0. Copyright delle modifiche © 2017 Soren Stoutner. L'immagine risultante è rilasciata sotto Licenza GPLv3+.

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 e4aa9258..8a69e2be 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java @@ -61,40 +61,47 @@ import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper; import java.io.ByteArrayOutputStream; -public class BookmarksActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, EditBookmarkDialog.EditBookmarkListener, EditBookmarkFolderDialog.EditBookmarkFolderListener, - MoveToFolderDialog.MoveToFolderListener { +public class BookmarksActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, EditBookmarkDialog.EditBookmarkListener, + EditBookmarkFolderDialog.EditBookmarkFolderListener, MoveToFolderDialog.MoveToFolderListener { - // `bookmarksDatabaseHelper` is public static so it can be accessed from `CreateBookmarkFolderDialog`, `EditBookmarkDialog`, `EditBookmarkFolderDialog` and `MoveToFolderDialog`. It is also used in `onCreate()`, - // `onCreateBookmarkCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`. + // `bookmarksDatabaseHelper` is public static so it can be accessed from `CreateBookmarkFolderDialog`, `EditBookmarkDialog`, `EditBookmarkFolderDialog` and `MoveToFolderDialog`. It is also used in `onCreate()`, `onOptionsItemSelected()`, + // `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, and `loadFolder(). public static BookmarksDatabaseHelper bookmarksDatabaseHelper; // `currentFolder` is public static so it can be accessed from `MoveToFolderDialog`. - // It is used in `onCreate`, `onOptionsItemSelected()`, `onCreateBookmarkCreate`, `onCreateBookmarkFolderCreate`, and `onEditBookmarkSave`. + // It is used in `onCreate`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, and `loadFolder()`. public static String currentFolder; // `checkedItemIds` is public static so it can be accessed from `EditBookmarkDialog`, `EditBookmarkFolderDialog`, and `MoveToFolderDialog`. - // It is also used in `onActionItemClicked`. + // It is also used in `onCreate()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, and `updateMoveIcons()`. public static long[] checkedItemIds; - // `bookmarksListView` is used in `onCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`. + // `bookmarksListView` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, `updateMoveIcons()`, `scrollBookmarks()`, + // and `loadFolder()`. private ListView bookmarksListView; - // `contextualActionMode` is used in `onCreate()` and `onEditBookmarkSave()`. - private ActionMode contextualActionMode; + // `bookmarksCursor` is used in `onCreate()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, and `loadFolder()`. + private Cursor bookmarksCursor; - // `bookmarkListViewPosition` is used in `onCreate()`, `onSaveEditBookmark()`, and `onSaveEditBookmarkFolder()`. - private int bookmarksListViewPosition; + // `bookmarksCursorAdapter` is used in `onCreate(), `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, and `onLoadFolder()`. + private CursorAdapter bookmarksCursorAdapter; - // `appBar` is used in `onCreate()` and `updateBookmarksListView()`. - private ActionBar appBar; + // `contextualActionMode` is used in `onCreate()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()` and `onMoveToFolder()`. + private ActionMode contextualActionMode; - // `bookmarksCursor` is used in `onCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`. - private Cursor bookmarksCursor; + // `appBar` is used in `onCreate()` and `loadFolder()`. + private ActionBar appBar; - // `oldFolderName` is used in `onCreate()` and `onEditBookmarkFolderSave()`. + // `oldFolderName` is used in `onCreate()` and `onSaveEditBookmarkFolder()`. private String oldFolderNameString; + // `moveBookmarkUpMenuItem` is used in `onCreate()` and `updateMoveIcons`. + private MenuItem moveBookmarkUpMenuItem; + + // `moveBookmarkDownMenuItem` is used in `onCreate()` and `updateMoveIcons`. + private MenuItem moveBookmarkDownMenuItem; + @Override protected void onCreate(Bundle savedInstanceState) { // Set the activity theme. @@ -114,25 +121,28 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar); setSupportActionBar(bookmarksAppBar); - // Display the home arrow on `SupportActionBar`. + // Get a handle for the activity, the app bar, and the `ListView`. + final Activity bookmarksActivity = this; appBar = getSupportActionBar(); - assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null. - appBar.setDisplayHomeAsUpEnabled(true); + bookmarksListView = (ListView) findViewById(R.id.bookmarks_listview); + + // This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null. + assert appBar != null; + // Display the home arrow on `SupportActionBar`. + appBar.setDisplayHomeAsUpEnabled(true); // Initialize the database helper and the `ListView`. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`. // The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`. bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0); - bookmarksListView = (ListView) findViewById(R.id.bookmarks_listview); // Set currentFolder to the home folder, which is `""` in the database. currentFolder = ""; - // Display the bookmarks in the ListView. - updateBookmarksListView(currentFolder); + // Load the home folder. + loadFolder(); - // Set a listener so that tapping a list item loads the URL. We need to store the activity in a variable so that we can return to the parent activity after loading the URL. - final Activity bookmarksActivity = this; + // Set a listener so that tapping a list item loads the URL or folder. bookmarksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { @@ -143,17 +153,18 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmarkCursor(databaseID); bookmarkCursor.moveToFirst(); - // If the bookmark is a folder load its contents into the ListView. - if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { + // Act upon the bookmark according to the type. + if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { // The selected bookmark is a folder. // Update `currentFolder`. currentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); - // Reload the ListView with `currentFolder`. - updateBookmarksListView(currentFolder); - } else { // Load the URL into `mainWebView`. - // Get the bookmark URL and assign it to formattedUrlString. `mainWebView` will automatically reload when `BookmarksActivity` closes. + // Load the new folder. + loadFolder(); + } else { // The selected bookmark is not a folder. + // Get the bookmark URL and assign it to `formattedUrlString`. `mainWebView` will automatically reload when `BookmarksActivity` closes. MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)); + // Return to `MainWebViewActivity`. NavUtils.navigateUpFromSameTask(bookmarksActivity); } @@ -164,16 +175,8 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // `MultiChoiceModeListener` handles long clicks. bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { - // `moveBookmarkUpMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`. - MenuItem moveBookmarkUpMenuItem; - - // `moveBookmarkDownMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`. - MenuItem moveBookmarkDownMenuItem; - - // `editBookmarkMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`. + // Instantiate the common variables. MenuItem editBookmarkMenuItem; - - // `selectAllBookmarks` is used in `onCreateActionMode()` and `onItemCheckedStateChanges`. MenuItem selectAllBookmarksMenuItem; @Override @@ -189,15 +192,16 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma mode.setTitle(currentFolder); } - // Get a handle for MenuItems we need to selectively disable. + // Get a handle for `MenuItems` that need to be selectively disabled. moveBookmarkUpMenuItem = menu.findItem(R.id.move_bookmark_up); moveBookmarkDownMenuItem = menu.findItem(R.id.move_bookmark_down); editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark); selectAllBookmarksMenuItem = menu.findItem(R.id.context_menu_select_all_bookmarks); - // Store `contextualActionMode` so we can close it programatically. + // Store `contextualActionMode` so it can be closed programatically. contextualActionMode = mode; + // Make it so. return true; } @@ -212,22 +216,23 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // 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. + // Make it so. return true; } @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { - // Get an array of the selected bookmarks. + // Get a long array of the selected bookmarks. long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds(); // Calculate the number of selected bookmarks. int numberOfSelectedBookmarks = selectedBookmarksLongArray.length; - // Adjust the `mode` and the menu for the number of selected bookmarks. - if (numberOfSelectedBookmarks == 0) { + // Adjust the `ActionMode` and the `Menu` according to the number of selected bookmarks. + if (numberOfSelectedBookmarks == 0) { // No bookmarks are selected. + // Close the `ActionMode`. mode.finish(); - } else if (numberOfSelectedBookmarks == 1) { + } else if (numberOfSelectedBookmarks == 1) { // One bookmark is selected. // List the number of selected bookmarks in the subtitle. mode.setSubtitle(getString(R.string.one_selected)); @@ -236,41 +241,8 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma moveBookmarkDownMenuItem.setVisible(true); editBookmarkMenuItem.setVisible(true); - // Get the database IDs for the bookmarks. - int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0]; - int firstBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(0); - // bookmarksListView is 0 indexed. - int lastBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(bookmarksListView.getCount() - 1); - - // Disable `moveBookmarkUpMenuItem` if the selected bookmark is at the top of the ListView. - if (selectedBookmarkDatabaseId == firstBookmarkDatabaseId) { - moveBookmarkUpMenuItem.setEnabled(false); - moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_disabled); - } else { // Otherwise enable `moveBookmarkUpMenuItem`. - moveBookmarkUpMenuItem.setEnabled(true); - - // Set the icon according to the theme. - if (MainWebViewActivity.darkTheme) { - moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_dark); - } else { - moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_light); - } - } - - // Disable `moveBookmarkDownMenuItem` if the selected bookmark is at the bottom of the ListView. - if (selectedBookmarkDatabaseId == lastBookmarkDatabaseId) { - moveBookmarkDownMenuItem.setEnabled(false); - moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_disabled); - } else { // Otherwise enable `moveBookmarkDownMenuItem`. - moveBookmarkDownMenuItem.setEnabled(true); - - // Set the icon according to the theme. - if (MainWebViewActivity.darkTheme) { - moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_dark); - } else { - moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_light); - } - } + // Update the enabled status of the move icons. + updateMoveIcons(); } else { // More than one bookmark is selected. // List the number of selected bookmarks in the subtitle. mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected)); @@ -295,21 +267,20 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma int menuItemId = item.getItemId(); // Instantiate the common variables. - int numberOfBookmarks; int selectedBookmarkPosition; int selectedBookmarkNewPosition; final SparseBooleanArray selectedBookmarksPositionsSparseBooleanArray; switch (menuItemId) { case R.id.move_bookmark_up: - // Get the array of checked bookmarks. + // Get the array of checked bookmark positions. selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); - // Store the position of the selected bookmark. + // Store the position of the selected bookmark. Only one bookmark is selected when `move_bookmark_up` is enabled. selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0); - // Initialize `selectedBookmarkNewPosition`. - selectedBookmarkNewPosition = 0; + // Calculate the new position of the selected bookmark. + selectedBookmarkNewPosition = selectedBookmarkPosition - 1; // Iterate through the bookmarks. for (int i = 0; i < bookmarksListView.getCount(); i++) { @@ -320,7 +291,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark. // Move the current bookmark up one. bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1); - selectedBookmarkNewPosition = i - 1; } else if ((i + 1) == selectedBookmarkPosition){ // The current bookmark is immediately above the selected bookmark. // Move the current bookmark down one. bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1); @@ -335,26 +305,28 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma } } - // Refresh the ListView. - updateBookmarksListView(currentFolder); + // Update `bookmarksCursor` with the current contents of the bookmarks database. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); - // Select the previously selected bookmark in the new location. - bookmarksListView.setItemChecked(selectedBookmarkNewPosition, true); + // Update the `ListView`. + bookmarksCursorAdapter.changeCursor(bookmarksCursor); - // Scroll `bookmarksListView` to five items above `selectedBookmarkNewPosition`. - bookmarksListView.setSelection(selectedBookmarkNewPosition - 5); + // Scroll with the bookmark. + scrollBookmarks(selectedBookmarkNewPosition); + // Update the enabled status of the move icons. + updateMoveIcons(); break; case R.id.move_bookmark_down: - // Get the array of checked bookmarks. + // Get the array of checked bookmark positions. selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); - // Store the position of the selected bookmark. + // Store the position of the selected bookmark. Only one bookmark is selected when `move_bookmark_down` is enabled. selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0); - // Initialize `selectedBookmarkNewPosition`. - selectedBookmarkNewPosition = 0; + // Calculate the new position of the selected bookmark. + selectedBookmarkNewPosition = selectedBookmarkPosition + 1; // Iterate through the bookmarks. for (int i = 0; i = (lastVisibleBookmarkPosition - 1)) { // The selected bookmark is at or below the bottom of the screen. The `-1` handles partial bookmarks displayed at the bottom of the `ListView`. + // Scroll to display the selected bookmark at the bottom of the screen. `+1` assured that the entire bookmark will be displayed in situations where only a partial bookmark fits at the bottom of the `ListView`. + bookmarksListView.setSelection(selectedBookmarkPosition - numberOfBookmarksPerScreen + 1); } } - private void updateBookmarksListViewExcept(long[] exceptIdLongArray, String folderName) { - // Get a `Cursor` with the current contents of the bookmarks database except for the specified database IDs. - bookmarksCursor = bookmarksDatabaseHelper.getBookmarksCursorExcept(exceptIdLongArray, folderName); + private void loadFolder() { + // Update `bookmarksCursor` with the contents of the bookmarks database for the current folder. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); - // Setup `bookmarksCursorAdapter` with `this` context. `false` disables autoRequery. - CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { + // Setup a `CursorAdapter`. `this` specifies the `Context`. `false` disables `autoRequery`. + bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { // Inflate the individual item layout. `false` does not attach it to the root. @@ -890,56 +936,40 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma @Override public void bindView(View view, Context context, Cursor cursor) { - // Get the favorite icon byte array from the cursor. + // Get handles for the views. + ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon); + TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name); + + // Get the favorite icon byte array from the `Cursor`. byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON)); - // Convert the byte array to a Bitmap beginning at the first byte and ending at the last. + // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last. Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); // Display the bitmap in `bookmarkFavoriteIcon`. - ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon); bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap); - // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`. String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); - TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name); bookmarkNameTextView.setText(bookmarkNameString); // Make the font bold for folders. if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { - // The first argument is `null` because we don't want to change the font. - bookmarkNameTextView.setTypeface(null, Typeface.BOLD); - } else { // Reset the font to default. + bookmarkNameTextView.setTypeface(Typeface.DEFAULT_BOLD); + } else { // Reset the font to default for normal bookmarks. bookmarkNameTextView.setTypeface(Typeface.DEFAULT); } } }; - // Update the `ListView`. + // Populate the `ListView` with the adapter. bookmarksListView.setAdapter(bookmarksCursorAdapter); - } - - private void deleteBookmarkFolderContents(int databaseId) { - // Get the name of the folder. - String folderName = bookmarksDatabaseHelper.getFolderName(databaseId); - // Get the contents of the folder. - Cursor folderCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(folderName); - - for (int i = 0; i < folderCursor.getCount(); i++) { - // Move `folderCursor` to the current row. - folderCursor.moveToPosition(i); - - // Get the database ID of the item. - int itemDatabaseId = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper._ID)); - - // If this is a folder, delete the contents first. - if (bookmarksDatabaseHelper.isFolder(itemDatabaseId)) { - deleteBookmarkFolderContents(itemDatabaseId); - } - - bookmarksDatabaseHelper.deleteBookmark(itemDatabaseId); + // Set the `AppBar` title. + if (currentFolder.isEmpty()) { + appBar.setTitle(R.string.bookmarks); + } else { + appBar.setTitle(currentFolder); } } } \ No newline at end of file diff --git a/app/src/main/res/layout/bookmarks_coordinatorlayout.xml b/app/src/main/res/layout/bookmarks_coordinatorlayout.xml index 6183daf5..a31ec0e1 100644 --- a/app/src/main/res/layout/bookmarks_coordinatorlayout.xml +++ b/app/src/main/res/layout/bookmarks_coordinatorlayout.xml @@ -49,14 +49,14 @@ + `android:dividerHeight` must be at least `1dp` or the `ListView` is inconsistent in calculating how many bookmarks are displayed. --> + android:dividerHeight="1dp" />