Browse Source

Fixed bugs in the BookmarksFragment and BookmarkManager

master
Anthony Restaino 9 years ago
parent
commit
05efb4eb72
  1. 36
      app/src/main/java/acr/browser/lightning/database/BookmarkManager.java
  2. 54
      app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java

36
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 * @return true if the BookmarkManager was initialized, false otherwise
*/ */
public boolean isReady() { public boolean isReady() {
@ -75,8 +74,9 @@ public class BookmarkManager {
/** /**
* Look for bookmark using the url * 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 @Nullable
public HistoryItem findBookmarkForUrl(final String url) { 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. * Initialize the BookmarkManager, it's a one-time operation and will be executed asynchronously.
* When done, mReady flag will been set to true. * When done, mReady flag will been set to true.
*/ */
private class BookmarkInitializer implements Runnable{ private class BookmarkInitializer implements Runnable {
private final Context mContext; private final Context mContext;
public BookmarkInitializer(Context context) { public BookmarkInitializer(Context context) {
@ -195,8 +195,8 @@ public class BookmarkManager {
* This method adds the the HistoryItem item to permanent bookmark storage.<br> * This method adds the the HistoryItem item to permanent bookmark storage.<br>
* This operation is blocking if the manager is still not ready. * This operation is blocking if the manager is still not ready.
* *
* @param item the item to add * @param item the item to add
* @return It returns true if the operation was successful. * @return It returns true if the operation was successful.
*/ */
public synchronized boolean addBookmark(@NonNull HistoryItem item) { public synchronized boolean addBookmark(@NonNull HistoryItem item) {
final String url = item.getUrl(); final String url = item.getUrl();
@ -251,7 +251,7 @@ public class BookmarkManager {
if (newName.isEmpty()) { if (newName.isEmpty()) {
return; return;
} }
for (HistoryItem item: mBookmarksMap.values()) { for (HistoryItem item : mBookmarksMap.values()) {
if (item.getFolder().equals(oldName)) { if (item.getFolder().equals(oldName)) {
item.setFolder(newName); item.setFolder(newName);
} else if (item.isFolder() && item.getTitle().equals(oldName)) { } else if (item.isFolder() && item.getTitle().equals(oldName)) {
@ -269,7 +269,7 @@ public class BookmarkManager {
*/ */
public synchronized void deleteFolder(@NonNull String name) { public synchronized void deleteFolder(@NonNull String name) {
final Map<String, HistoryItem> bookmarks = new HashMap<>(); final Map<String, HistoryItem> bookmarks = new HashMap<>();
for (HistoryItem item: mBookmarksMap.values()) { for (HistoryItem item : mBookmarksMap.values()) {
final String url = item.getUrl(); final String url = item.getUrl();
if (item.isFolder()) { if (item.isFolder()) {
if (!item.getTitle().equals(name)) { if (!item.getTitle().equals(name)) {
@ -357,8 +357,7 @@ public class BookmarkManager {
* This is a disk-bound operation and should not be * This is a disk-bound operation and should not be
* done very frequently. * 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 * @return returns a list of bookmarks that can be sorted
*/ */
public synchronized List<HistoryItem> getAllBookmarks(boolean sort) { public synchronized List<HistoryItem> getAllBookmarks(boolean sort) {
@ -385,7 +384,7 @@ public class BookmarkManager {
folder = ""; folder = "";
} }
mCurrentFolder = folder; mCurrentFolder = folder;
for (HistoryItem item: mBookmarksMap.values()) { for (HistoryItem item : mBookmarksMap.values()) {
if (item.getFolder().equals(folder)) if (item.getFolder().equals(folder))
bookmarks.add(item); bookmarks.add(item);
} }
@ -404,6 +403,15 @@ public class BookmarkManager {
return mCurrentFolder.isEmpty(); return mCurrentFolder.isEmpty();
} }
/**
* Returns the current folder
*
* @return the current folder
*/
public String getCurrentFolder() {
return mCurrentFolder;
}
/** /**
* Method is used internally for searching the bookmarks * Method is used internally for searching the bookmarks
* *
@ -411,7 +419,7 @@ public class BookmarkManager {
*/ */
private Set<String> getBookmarkUrls(List<HistoryItem> list) { private Set<String> getBookmarkUrls(List<HistoryItem> list) {
Set<String> set = new HashSet<>(); Set<String> set = new HashSet<>();
for (HistoryItem item: mBookmarksMap.values()) { for (HistoryItem item : mBookmarksMap.values()) {
if (!item.isFolder()) if (!item.isFolder())
set.add(item.getUrl()); set.add(item.getUrl());
} }
@ -427,7 +435,7 @@ public class BookmarkManager {
*/ */
public synchronized List<HistoryItem> getFolders(boolean sort) { public synchronized List<HistoryItem> getFolders(boolean sort) {
final HashMap<String, HistoryItem> folders = new HashMap<>(); final HashMap<String, HistoryItem> folders = new HashMap<>();
for (HistoryItem item: mBookmarksMap.values()) { for (HistoryItem item : mBookmarksMap.values()) {
final String folderName = item.getFolder(); final String folderName = item.getFolder();
if (folderName != null && !folderName.isEmpty() && !folders.containsKey(folderName)) { if (folderName != null && !folderName.isEmpty() && !folders.containsKey(folderName)) {
final HistoryItem folder = new HistoryItem(); final HistoryItem folder = new HistoryItem();
@ -453,7 +461,7 @@ public class BookmarkManager {
*/ */
public synchronized List<String> getFolderTitles() { public synchronized List<String> getFolderTitles() {
final Set<String> folders = new HashSet<>(); final Set<String> folders = new HashSet<>();
for (HistoryItem item: mBookmarksMap.values()) { for (HistoryItem item : mBookmarksMap.values()) {
final String folderName = item.getFolder(); final String folderName = item.getFolder();
if (folderName != null && !folderName.isEmpty()) { if (folderName != null && !folderName.isEmpty()) {
folders.add(folderName); folders.add(folderName);

54
app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java

@ -58,11 +58,11 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
// Event bus // Event bus
@Inject @Inject
Bus eventBus; Bus mEventBus;
// Dialog builder // Dialog builder
@Inject @Inject
BookmarksDialogBuilder bookmarksDialogBuilder; BookmarksDialogBuilder mBookmarksDialogBuilder;
// Adapter // Adapter
private BookmarkViewAdapter mBookmarkAdapter; private BookmarkViewAdapter mBookmarkAdapter;
@ -81,7 +81,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
private int mIconColor; private int mIconColor;
// Init asynchronously the bookmark manager // Init asynchronously the bookmark manager
private final Runnable initBookmarkManager = new Runnable() { private final Runnable mInitBookmarkManager = new Runnable() {
@Override @Override
public void run() { public void run() {
final Context context = getContext(); final Context context = getContext();
@ -106,7 +106,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(item.getTitle(), true), setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(item.getTitle(), true),
true); true);
} else { } 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 @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 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 // Must be called here, only here we have a reference to the ListView
new Thread(initBookmarkManager).run(); new Thread(mInitBookmarkManager).run();
return view; return view;
} }
@ -151,7 +157,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
// TODO this code depend way too much on BrowserActivity // TODO this code depend way too much on BrowserActivity
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
final BrowserActivity activity = (BrowserActivity) getActivity(); final BrowserActivity activity = (BrowserActivity) getActivity();
final PreferenceManager preferenceManager =PreferenceManager.getInstance(); final PreferenceManager preferenceManager = PreferenceManager.getInstance();
boolean darkTheme = preferenceManager.getUseTheme() != 0 || activity.isIncognito(); boolean darkTheme = preferenceManager.getUseTheme() != 0 || activity.isIncognito();
mWebpageBitmap = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_webpage, darkTheme); mWebpageBitmap = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_webpage, darkTheme);
mFolderBitmap = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_folder, darkTheme); mFolderBitmap = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_folder, darkTheme);
@ -164,13 +170,13 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
eventBus.register(this); mEventBus.register(this);
} }
@Override @Override
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
eventBus.unregister(this); mEventBus.unregister(this);
} }
@Subscribe @Subscribe
@ -180,7 +186,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
mBookmarks.add(item); mBookmarks.add(item);
Collections.sort(mBookmarks, new BookmarkManager.SortIgnoreCase()); Collections.sort(mBookmarks, new BookmarkManager.SortIgnoreCase());
mBookmarkAdapter.notifyDataSetChanged(); mBookmarkAdapter.notifyDataSetChanged();
eventBus mEventBus
.post(new BookmarkEvents.Added(item)); .post(new BookmarkEvents.Added(item));
updateBookmarkIndicator(event.url); updateBookmarkIndicator(event.url);
} }
@ -193,12 +199,8 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Subscribe @Subscribe
public void bookmarkChanged(BookmarkEvents.BookmarkChanged event) { public void bookmarkChanged(BookmarkEvents.BookmarkChanged event) {
// final int size = mBookmarks.size(); String folder = mBookmarkManager.getCurrentFolder();
mBookmarks.remove(event.oldBookmark); setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(folder, true), false);
// assert mBookmarks.size() < size;
mBookmarks.add(event.newBookmark);
mBookmarkAdapter.notifyDataSetChanged();
Collections.sort(mBookmarks, new BookmarkManager.SortIgnoreCase());
} }
private void updateBookmarkIndicator(final String url) { private void updateBookmarkIndicator(final String url) {
@ -214,8 +216,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Subscribe @Subscribe
public void userPressedBack(final BrowserEvents.UserPressedBack event) { public void userPressedBack(final BrowserEvents.UserPressedBack event) {
if (mBookmarkManager.isRootFolder()) { if (mBookmarkManager.isRootFolder()) {
eventBus mEventBus.post(new BookmarkEvents.CloseBookmarks());
.post(new BookmarkEvents.CloseBookmarks());
} else { } else {
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true); setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true);
} }
@ -223,10 +224,12 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Subscribe @Subscribe
public void bookmarkDeleted(final BookmarkEvents.Deleted event) { public void bookmarkDeleted(final BookmarkEvents.Deleted event) {
// final int size = mBookmarks.size();
mBookmarks.remove(event.item); mBookmarks.remove(event.item);
// assert mBookmarks.size() < size; if (event.item.isFolder()) {
mBookmarkAdapter.notifyDataSetChanged(); setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false);
} else {
mBookmarkAdapter.notifyDataSetChanged();
}
} }
private void setBookmarkDataSet(List<HistoryItem> items, boolean animate) { private void setBookmarkDataSet(List<HistoryItem> items, boolean animate) {
@ -234,10 +237,11 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
mBookmarks.addAll(items); mBookmarks.addAll(items);
mBookmarkAdapter.notifyDataSetChanged(); mBookmarkAdapter.notifyDataSetChanged();
final int resource; final int resource;
if (mBookmarkManager.isRootFolder()) if (mBookmarkManager.isRootFolder()) {
resource = R.drawable.ic_action_star; resource = R.drawable.ic_action_star;
else } else {
resource = R.drawable.ic_action_back; resource = R.drawable.ic_action_back;
}
final Animation startRotation = new Animation() { final Animation startRotation = new Animation() {
@Override @Override
@ -289,9 +293,9 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
private void handleLongPress(final HistoryItem item, final int position) { private void handleLongPress(final HistoryItem item, final int position) {
if (item.isFolder()) { if (item.isFolder()) {
bookmarksDialogBuilder.showBookmarkFolderLongPressedDialog(getContext(), item); mBookmarksDialogBuilder.showBookmarkFolderLongPressedDialog(getContext(), item);
} else { } 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) { public void onClick(View v) {
switch (v.getId()) { switch (v.getId()) {
case R.id.action_add_bookmark: case R.id.action_add_bookmark:
eventBus.post(new BookmarkEvents.WantToBookmarkCurrentPage()); mEventBus.post(new BookmarkEvents.WantToBookmarkCurrentPage());
break; break;
default: default:
break; break;

Loading…
Cancel
Save