From aced4a3cc73e92674b382edca6798a6e463cbdca Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Sun, 26 Jul 2015 20:19:47 -0400 Subject: [PATCH] Sort bookmarks so folders are at the end of the list, updated bookmarks page to better utilize space --- .../lightning/activity/BrowserActivity.java | 17 ++------- .../lightning/constant/BookmarkPage.java | 32 +++++++++++----- .../lightning/database/BookmarkManager.java | 37 ++++++------------- 3 files changed, 37 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index 30ee04a..95dd657 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -335,7 +335,7 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse mBookmarkList.add(bookmark); } } - Collections.sort(mBookmarkList, new SortIgnoreCase()); + Collections.sort(mBookmarkList, new BookmarkManager.SortIgnoreCase()); mPreferences.setDefaultBookmarks(false); } mBookmarkAdapter = new BookmarkViewAdapter(mActivity, R.layout.bookmark_list_item, @@ -822,7 +822,7 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse HistoryItem bookmark = new HistoryItem(url, title); if (mBookmarkManager.addBookmark(bookmark)) { mBookmarkList.add(bookmark); - Collections.sort(mBookmarkList, new SortIgnoreCase()); + Collections.sort(mBookmarkList, new BookmarkManager.SortIgnoreCase()); notifyBookmarkDataSetChanged(); mSearchAdapter.refreshBookmarks(); updateBookmarkIndicator(mCurrentView.getUrl()); @@ -1041,6 +1041,7 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse String currentFolder = mBookmarkList.get(id).getFolder(); item.setTitle(getTitle.getText().toString()); item.setUrl(getUrl.getText().toString()); + item.setUrl(getUrl.getText().toString()); item.setFolder(getFolder.getText().toString()); mBookmarkManager.editBookmark(mBookmarkList.get(id), item); @@ -1051,7 +1052,6 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse setBookmarkDataSet(list, false); } - Collections.sort(mBookmarkList, new SortIgnoreCase()); if (mCurrentView != null && mCurrentView.getUrl().startsWith(Constants.FILE) && mCurrentView.getUrl().endsWith(BookmarkPage.FILENAME)) { openBookmarkPage(mWebView); @@ -1094,7 +1094,6 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false); - Collections.sort(mBookmarkList, new SortIgnoreCase()); if (mCurrentView != null && mCurrentView.getUrl().startsWith(Constants.FILE) && mCurrentView.getUrl().endsWith(BookmarkPage.FILENAME)) { openBookmarkPage(mWebView); @@ -2544,7 +2543,6 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false); - Collections.sort(mBookmarkList, new SortIgnoreCase()); if (mCurrentView != null && mCurrentView.getUrl().startsWith(Constants.FILE) && mCurrentView.getUrl().endsWith(BookmarkPage.FILENAME)) { openBookmarkPage(mWebView); @@ -2760,15 +2758,6 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse } } - private class SortIgnoreCase implements Comparator { - - public int compare(HistoryItem o1, HistoryItem o2) { - return o1.getTitle().toLowerCase(Locale.getDefault()) - .compareTo(o2.getTitle().toLowerCase(Locale.getDefault())); - } - - } - @Override public void onClick(View v) { switch (v.getId()) { diff --git a/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java b/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java index ead1705..fdfdc25 100644 --- a/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java +++ b/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java @@ -9,21 +9,35 @@ import acr.browser.lightning.activity.BrowserApp; public class BookmarkPage { public static final String FILENAME = "bookmarks.html"; - - public static final String HEADING = "" - + BrowserApp.getAppContext().getString(R.string.action_bookmarks) - + " "; public static final String END = ""; - + } diff --git a/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java b/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java index 3f5d9fb..69e52cb 100644 --- a/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java +++ b/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java @@ -9,7 +9,6 @@ import android.support.annotation.NonNull; import org.json.JSONException; import org.json.JSONObject; -import org.w3c.dom.ls.LSInput; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -290,8 +289,6 @@ public class BookmarkManager { */ public synchronized List getBookmarksFromFolder(String folder, boolean sort) { List bookmarks = new ArrayList<>(); -// File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); -// BufferedReader bookmarksReader = null; if (folder == null || folder.length() == 0) { bookmarks.addAll(getFolders(sort)); folder = ""; @@ -301,26 +298,6 @@ public class BookmarkManager { if (mBookmarkList.get(n).getFolder().equals(folder)) bookmarks.add(mBookmarkList.get(n)); } -// try { -// bookmarksReader = new BufferedReader(new FileReader(bookmarksFile)); -// String line; -// while ((line = bookmarksReader.readLine()) != null) { -// JSONObject object = new JSONObject(line); -// if (object.getString(FOLDER).equals(folder)) { -// HistoryItem item = new HistoryItem(); -// item.setTitle(object.getString(TITLE)); -// item.setUrl(object.getString(URL)); -// item.setFolder(object.getString(FOLDER)); -// item.setOrder(object.getInt(ORDER)); -// item.setImageId(R.drawable.ic_bookmark); -// bookmarks.add(item); -// } -// } -// } catch (IOException | JSONException e) { -// e.printStackTrace(); -// } finally { -// Utils.close(bookmarksReader); -// } if (sort) { Collections.sort(bookmarks, new SortIgnoreCase()); } @@ -510,14 +487,22 @@ public class BookmarkManager { return -1; } - private class SortIgnoreCase implements Comparator { + /** + * This class sorts bookmarks alphabetically, with folders coming after bookmarks + */ + public static class SortIgnoreCase implements Comparator { public int compare(HistoryItem o1, HistoryItem o2) { if (o1 == null || o2 == null || o1.getTitle() == null || o2.getTitle() == null) { return 0; } - return o1.getTitle().toLowerCase(Locale.getDefault()) - .compareTo(o2.getTitle().toLowerCase(Locale.getDefault())); + if (o1.getIsFolder() == o2.getIsFolder()) { + return o1.getTitle().toLowerCase(Locale.getDefault()) + .compareTo(o2.getTitle().toLowerCase(Locale.getDefault())); + + } else { + return o1.getIsFolder() ? 1 : -1; + } } }