|
|
@ -39,7 +39,7 @@ public class BookmarksFragment extends Fragment { |
|
|
|
|
|
|
|
private TextView nothingBookmarkedTextView; |
|
|
|
|
|
|
|
private ArrayList<Bookmark> bookmarks = null; |
|
|
|
public ArrayList<Bookmark> bookmarks = null; |
|
|
|
public Type type; |
|
|
|
public String interactionClick, interactionToggle, interactionRemove; |
|
|
|
|
|
|
@ -107,13 +107,40 @@ public class BookmarksFragment extends Fragment { |
|
|
|
//Get the nothing bookmarked text view.
|
|
|
|
nothingBookmarkedTextView = rootView.findViewById(R.id.nothing_bookmarked); |
|
|
|
|
|
|
|
//Create the adapter.
|
|
|
|
BookmarksAdapter adapter = new BookmarksAdapter(this, notificationsEnabledButtonImage, notificationsDisabledButtonImage); |
|
|
|
|
|
|
|
//Get the drag list view.
|
|
|
|
DragListView mDragListView = (DragListView) rootView.findViewById(R.id.fragment_bookmarks_dragList); |
|
|
|
|
|
|
|
//Set the Drag List Listener.
|
|
|
|
mDragListView.setDragListListener(new DragListView.DragListListener() |
|
|
|
{ |
|
|
|
@Override |
|
|
|
public void onItemDragStarted(int position) |
|
|
|
{ |
|
|
|
//Create a new array of bookmarks.
|
|
|
|
ArrayList<Bookmark> new_bookmarks = new ArrayList<Bookmark>(); |
|
|
|
|
|
|
|
//For each bookmark in the current bookmarks array.
|
|
|
|
for (int i = 0; i < bookmarks.size(); i++) |
|
|
|
{ |
|
|
|
//Create an indicator bookmark.
|
|
|
|
Bookmark indicator = new Bookmark("Drop Here", "-1", true); |
|
|
|
|
|
|
|
//Add the indicator followed by the current actual bookmark.
|
|
|
|
new_bookmarks.add(indicator); |
|
|
|
new_bookmarks.add(bookmarks.get(i)); |
|
|
|
} |
|
|
|
|
|
|
|
//Replace the bookmarks with the new bookmarks that contains the indicators.
|
|
|
|
bookmarks = new_bookmarks; |
|
|
|
|
|
|
|
//Add one last indicator.
|
|
|
|
bookmarks.add(new Bookmark("Drop Here", "-1", true)); |
|
|
|
|
|
|
|
//Notify the adapter that the bookmarks array has changed!
|
|
|
|
adapter.notifyDataSetChanged(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
@ -126,36 +153,71 @@ public class BookmarksFragment extends Fragment { |
|
|
|
public void onItemDragEnded(int fromPosition, int toPosition) |
|
|
|
{ |
|
|
|
|
|
|
|
//This is VERY IMPORTANT: Because I added indicator boxes
|
|
|
|
//in the onItemDragStarted, I need to recalculate the actual position
|
|
|
|
//of the started item (fromPosition) because it has changed!!!
|
|
|
|
int offset = fromPosition + 1; |
|
|
|
int actualPos = fromPosition + offset; |
|
|
|
|
|
|
|
//If the drag and drop is not the same item.
|
|
|
|
if (fromPosition != toPosition) |
|
|
|
if (actualPos != toPosition) |
|
|
|
{ |
|
|
|
|
|
|
|
//Get the from bookmark.
|
|
|
|
Bookmark from = bookmarks.get(fromPosition); |
|
|
|
Bookmark from = bookmarks.get(actualPos); |
|
|
|
Bookmark to = bookmarks.get(toPosition); |
|
|
|
|
|
|
|
//Swap the from and to bookmarks.
|
|
|
|
bookmarks.set(fromPosition, bookmarks.get(toPosition)); |
|
|
|
bookmarks.set(toPosition, from); |
|
|
|
//You can only drop items in the indicator boxes!!!
|
|
|
|
//Indicator boxes are Bookmark objects with id "-1".
|
|
|
|
if (to.getId().equals("-1")) |
|
|
|
{ |
|
|
|
//Swap the indicator with the actual.
|
|
|
|
bookmarks.set(actualPos, to); |
|
|
|
bookmarks.set(toPosition, from); |
|
|
|
|
|
|
|
//Get the fragments activity.
|
|
|
|
Activity unknownActivity = getActivity(); |
|
|
|
|
|
|
|
//Update the order of the bookmarks in the preferences.
|
|
|
|
if (unknownActivity instanceof BookmarksActivity) |
|
|
|
{ |
|
|
|
//Cast to BookmarksActivity.
|
|
|
|
BookmarksActivity activity = (BookmarksActivity)unknownActivity; |
|
|
|
|
|
|
|
//Update the preferences.
|
|
|
|
activity.updateBookmarks(bookmarks); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//Get the fragments activity.
|
|
|
|
Activity unknownActivity = getActivity(); |
|
|
|
//------------------------Clean up the indicator boxes------------------------//
|
|
|
|
} |
|
|
|
|
|
|
|
//Update the order of the bookmarks in the preferences.
|
|
|
|
if (unknownActivity instanceof BookmarksActivity) |
|
|
|
{ |
|
|
|
//Cast to BookmarksActivity.
|
|
|
|
BookmarksActivity activity = (BookmarksActivity)unknownActivity; |
|
|
|
//Find all the indicator boxes in the bookmarks array.
|
|
|
|
ArrayList<Bookmark> books_to_delete = new ArrayList<Bookmark>(); |
|
|
|
for (int i = 0; i < bookmarks.size(); i++) |
|
|
|
{ |
|
|
|
Bookmark book = bookmarks.get(i); |
|
|
|
|
|
|
|
//Call the swapBookmarksAfterReorder to apply the swapping changes to the preferences.
|
|
|
|
activity.swapBookmarksAfterReorder( bookmarks.get(fromPosition), bookmarks.get(toPosition) ); |
|
|
|
} |
|
|
|
if (book.getId().equals("-1")) |
|
|
|
books_to_delete.add(book); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Remove all the indicators.
|
|
|
|
for (int i = 0; i < books_to_delete.size(); i++) |
|
|
|
{ |
|
|
|
bookmarks.remove(books_to_delete.get(i)); |
|
|
|
} |
|
|
|
|
|
|
|
//------------------------Clean up the indicator boxes------------------------//
|
|
|
|
|
|
|
|
//Notify the adapter, because I made changes to the bookmarks array.
|
|
|
|
adapter.notifyDataSetChanged(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
//====================================This is the code for the Drag and Drop Functionality====================================//
|
|
|
|
mDragListView.setLayoutManager(new LinearLayoutManager(getActivity())); |
|
|
|
BookmarksAdapter adapter = new BookmarksAdapter(this, bookmarks, notificationsEnabledButtonImage, notificationsDisabledButtonImage); |
|
|
|
mDragListView.setAdapter(adapter, false); |
|
|
|
mDragListView.setCanDragHorizontally(false); |
|
|
|
//====================================This is the code for the Drag and Drop Functionality====================================//
|
|
|
|