From 05efb4eb728ec39fb04d5fc93eaf181b177c6eb1 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Wed, 16 Sep 2015 21:51:15 -0400 Subject: [PATCH] Fixed bugs in the BookmarksFragment and BookmarkManager --- .../lightning/database/BookmarkManager.java | 36 ++++++++----- .../lightning/fragment/BookmarksFragment.java | 54 ++++++++++--------- 2 files changed, 51 insertions(+), 39 deletions(-) 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 5e07d76..ad309e5 100644 --- a/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java +++ b/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java @@ -66,7 +66,6 @@ public class BookmarkManager { } /** - * * @return true if the BookmarkManager was initialized, false otherwise */ public boolean isReady() { @@ -75,8 +74,9 @@ public class BookmarkManager { /** * Look for bookmark using the url - * @param url the lookup url - * @return the bookmark as an {@link HistoryItem} or null + * + * @param url the lookup url + * @return the bookmark as an {@link HistoryItem} or null */ @Nullable public HistoryItem findBookmarkForUrl(final String url) { @@ -87,7 +87,7 @@ public class BookmarkManager { * Initialize the BookmarkManager, it's a one-time operation and will be executed asynchronously. * When done, mReady flag will been set to true. */ - private class BookmarkInitializer implements Runnable{ + private class BookmarkInitializer implements Runnable { private final Context mContext; public BookmarkInitializer(Context context) { @@ -195,8 +195,8 @@ public class BookmarkManager { * This method adds the the HistoryItem item to permanent bookmark storage.
* This operation is blocking if the manager is still not ready. * - * @param item the item to add - * @return It returns true if the operation was successful. + * @param item the item to add + * @return It returns true if the operation was successful. */ public synchronized boolean addBookmark(@NonNull HistoryItem item) { final String url = item.getUrl(); @@ -251,7 +251,7 @@ public class BookmarkManager { if (newName.isEmpty()) { return; } - for (HistoryItem item: mBookmarksMap.values()) { + for (HistoryItem item : mBookmarksMap.values()) { if (item.getFolder().equals(oldName)) { item.setFolder(newName); } else if (item.isFolder() && item.getTitle().equals(oldName)) { @@ -269,7 +269,7 @@ public class BookmarkManager { */ public synchronized void deleteFolder(@NonNull String name) { final Map bookmarks = new HashMap<>(); - for (HistoryItem item: mBookmarksMap.values()) { + for (HistoryItem item : mBookmarksMap.values()) { final String url = item.getUrl(); if (item.isFolder()) { if (!item.getTitle().equals(name)) { @@ -357,8 +357,7 @@ public class BookmarkManager { * This is a disk-bound operation and should not be * done very frequently. * - * @param sort force to sort the returned bookmarkList - * + * @param sort force to sort the returned bookmarkList * @return returns a list of bookmarks that can be sorted */ public synchronized List getAllBookmarks(boolean sort) { @@ -385,7 +384,7 @@ public class BookmarkManager { folder = ""; } mCurrentFolder = folder; - for (HistoryItem item: mBookmarksMap.values()) { + for (HistoryItem item : mBookmarksMap.values()) { if (item.getFolder().equals(folder)) bookmarks.add(item); } @@ -404,6 +403,15 @@ public class BookmarkManager { return mCurrentFolder.isEmpty(); } + /** + * Returns the current folder + * + * @return the current folder + */ + public String getCurrentFolder() { + return mCurrentFolder; + } + /** * Method is used internally for searching the bookmarks * @@ -411,7 +419,7 @@ public class BookmarkManager { */ private Set getBookmarkUrls(List list) { Set set = new HashSet<>(); - for (HistoryItem item: mBookmarksMap.values()) { + for (HistoryItem item : mBookmarksMap.values()) { if (!item.isFolder()) set.add(item.getUrl()); } @@ -427,7 +435,7 @@ public class BookmarkManager { */ public synchronized List getFolders(boolean sort) { final HashMap folders = new HashMap<>(); - for (HistoryItem item: mBookmarksMap.values()) { + for (HistoryItem item : mBookmarksMap.values()) { final String folderName = item.getFolder(); if (folderName != null && !folderName.isEmpty() && !folders.containsKey(folderName)) { final HistoryItem folder = new HistoryItem(); @@ -453,7 +461,7 @@ public class BookmarkManager { */ public synchronized List getFolderTitles() { final Set folders = new HashSet<>(); - for (HistoryItem item: mBookmarksMap.values()) { + for (HistoryItem item : mBookmarksMap.values()) { final String folderName = item.getFolder(); if (folderName != null && !folderName.isEmpty()) { folders.add(folderName); diff --git a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java index fdf15f2..0986edb 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java @@ -58,11 +58,11 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, // Event bus @Inject - Bus eventBus; + Bus mEventBus; // Dialog builder @Inject - BookmarksDialogBuilder bookmarksDialogBuilder; + BookmarksDialogBuilder mBookmarksDialogBuilder; // Adapter private BookmarkViewAdapter mBookmarkAdapter; @@ -81,7 +81,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, private int mIconColor; // Init asynchronously the bookmark manager - private final Runnable initBookmarkManager = new Runnable() { + private final Runnable mInitBookmarkManager = new Runnable() { @Override public void run() { final Context context = getContext(); @@ -106,7 +106,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(item.getTitle(), true), true); } else { - eventBus.post(new BookmarkEvents.Clicked(item)); + mEventBus.post(new BookmarkEvents.Clicked(item)); } } }; @@ -120,6 +120,12 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, } }; + @Override + public void onResume() { + super.onResume(); + setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false); + } + @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -142,7 +148,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, }); // Must be called here, only here we have a reference to the ListView - new Thread(initBookmarkManager).run(); + new Thread(mInitBookmarkManager).run(); return view; } @@ -151,7 +157,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, // TODO this code depend way too much on BrowserActivity super.onActivityCreated(savedInstanceState); final BrowserActivity activity = (BrowserActivity) getActivity(); - final PreferenceManager preferenceManager =PreferenceManager.getInstance(); + final PreferenceManager preferenceManager = PreferenceManager.getInstance(); boolean darkTheme = preferenceManager.getUseTheme() != 0 || activity.isIncognito(); mWebpageBitmap = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_webpage, darkTheme); mFolderBitmap = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_folder, darkTheme); @@ -164,13 +170,13 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @Override public void onStart() { super.onStart(); - eventBus.register(this); + mEventBus.register(this); } @Override public void onStop() { super.onStop(); - eventBus.unregister(this); + mEventBus.unregister(this); } @Subscribe @@ -180,7 +186,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, mBookmarks.add(item); Collections.sort(mBookmarks, new BookmarkManager.SortIgnoreCase()); mBookmarkAdapter.notifyDataSetChanged(); - eventBus + mEventBus .post(new BookmarkEvents.Added(item)); updateBookmarkIndicator(event.url); } @@ -193,12 +199,8 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @Subscribe public void bookmarkChanged(BookmarkEvents.BookmarkChanged event) { - // final int size = mBookmarks.size(); - mBookmarks.remove(event.oldBookmark); - // assert mBookmarks.size() < size; - mBookmarks.add(event.newBookmark); - mBookmarkAdapter.notifyDataSetChanged(); - Collections.sort(mBookmarks, new BookmarkManager.SortIgnoreCase()); + String folder = mBookmarkManager.getCurrentFolder(); + setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(folder, true), false); } private void updateBookmarkIndicator(final String url) { @@ -214,8 +216,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @Subscribe public void userPressedBack(final BrowserEvents.UserPressedBack event) { if (mBookmarkManager.isRootFolder()) { - eventBus - .post(new BookmarkEvents.CloseBookmarks()); + mEventBus.post(new BookmarkEvents.CloseBookmarks()); } else { setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true); } @@ -223,10 +224,12 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @Subscribe public void bookmarkDeleted(final BookmarkEvents.Deleted event) { - // final int size = mBookmarks.size(); mBookmarks.remove(event.item); - // assert mBookmarks.size() < size; - mBookmarkAdapter.notifyDataSetChanged(); + if (event.item.isFolder()) { + setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false); + } else { + mBookmarkAdapter.notifyDataSetChanged(); + } } private void setBookmarkDataSet(List items, boolean animate) { @@ -234,10 +237,11 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, mBookmarks.addAll(items); mBookmarkAdapter.notifyDataSetChanged(); final int resource; - if (mBookmarkManager.isRootFolder()) + if (mBookmarkManager.isRootFolder()) { resource = R.drawable.ic_action_star; - else + } else { resource = R.drawable.ic_action_back; + } final Animation startRotation = new Animation() { @Override @@ -289,9 +293,9 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, private void handleLongPress(final HistoryItem item, final int position) { if (item.isFolder()) { - bookmarksDialogBuilder.showBookmarkFolderLongPressedDialog(getContext(), item); + mBookmarksDialogBuilder.showBookmarkFolderLongPressedDialog(getContext(), item); } else { - bookmarksDialogBuilder.showLongPressedDialogForUrl(getContext(), item); + mBookmarksDialogBuilder.showLongPressedDialogForUrl(getContext(), item); } } @@ -299,7 +303,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, public void onClick(View v) { switch (v.getId()) { case R.id.action_add_bookmark: - eventBus.post(new BookmarkEvents.WantToBookmarkCurrentPage()); + mEventBus.post(new BookmarkEvents.WantToBookmarkCurrentPage()); break; default: break;