diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java index dcf2ea84..6a281d92 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java @@ -77,6 +77,12 @@ public class BookmarksActivity extends BaseActivity { return false; } + + public void swapBookmarksAfterReorder(Bookmark first, Bookmark second) + { + this.swapBookmarks(first, second); + } + private boolean onTopicInteractionListener(String interactionType, Bookmark bookmarkedTopic) { switch (interactionType) { case BookmarksFragment.INTERACTION_CLICK_TOPIC_BOOKMARK: diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksFragment.java index d319f3ad..d3c801d4 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksFragment.java @@ -1,6 +1,7 @@ package gr.thmmy.mthmmy.activities.bookmarks; +import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; @@ -125,26 +126,39 @@ public class BookmarksFragment extends Fragment { public void onItemDragEnded(int fromPosition, int toPosition) { - //TODO: This only works locally. If the user exit the bookmarks - // or closes the app, the order of the bookmarks will be lost. - // make sure after the following swapping, to apply the changes - // in the actual data model of the bookmarks. - // AFTER SWAPPING UPDATE THE ORDER OF THOSE TWO IN THE PREFERENCES. - + //If the drag and drop is not the same item. if (fromPosition != toPosition) { + + //Get the from bookmark. Bookmark from = bookmarks.get(fromPosition); + //Swap the from and to bookmarks. bookmarks.set(fromPosition, bookmarks.get(toPosition)); 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; + + //Call the swapBookmarksAfterReorder to apply the swapping changes to the preferences. + activity.swapBookmarksAfterReorder( bookmarks.get(fromPosition), bookmarks.get(toPosition) ); + } } } }); + //====================================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====================================// //Hide Nothing Bookmarked. if(this.bookmarks != null && !this.bookmarks.isEmpty()) @@ -157,6 +171,7 @@ public class BookmarksFragment extends Fragment { showNothingBookmarked(); } + return rootView; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index 9d71710b..1c7d38c9 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -48,6 +48,7 @@ import net.gotev.uploadservice.UploadService; import java.io.File; import java.util.ArrayList; +import gr.thmmy.mthmmy.BuildConfig; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.AboutActivity; import gr.thmmy.mthmmy.activities.LoginActivity; @@ -689,6 +690,87 @@ public abstract class BaseActivity extends AppCompatActivity { Timber.w("No bookmark match exists!"); return false; } + + + //TODO: Abstract reusable code: find the index of the first and second bookmark. + protected void swapBookmarks(Bookmark first, Bookmark second) + { + + //No need to check is second is a topics bookmark. + //because there is no way to drag and drop between topic and board. + if ( first.matchExists(topicsBookmarked) ) + { + //Find the index of the first and second bookmark. + int firstIndex = -1, secondIndex = -1; + for (int i = 0; i < topicsBookmarked.size(); i++) + { + + //Find the first index. + if (first.getId().equals(topicsBookmarked.get(i).getId())) + firstIndex = i; + + //Find the second index. + else if (second.getId().equals(topicsBookmarked.get(i).getId())) + secondIndex = i; + + //If both indexes found, stop. + if (firstIndex != -1 && secondIndex != -1) + break; + } + + //Assert, both indexes should have been found. + if (BuildConfig.DEBUG && !(firstIndex != -1 && secondIndex != -1)) { + throw new AssertionError("firstIndex and secondIndex must exist!"); + } + + //Temp store the first bookmark. + Bookmark firstBookmark = topicsBookmarked.get(firstIndex); + + //Swap the bookmarks. + topicsBookmarked.set(firstIndex, topicsBookmarked.get(secondIndex)); + topicsBookmarked.set(secondIndex, firstBookmark); + + //Update the bookmarks. + updateTopicBookmarks(); + } + + //Swap on Board Bookmarks. + else if ( first.matchExists(boardsBookmarked) ) + { + //Find the index of the first and second bookmark. + int firstIndex = -1, secondIndex = -1; + for (int i = 0; i < boardsBookmarked.size(); i++) + { + + //Find the first index. + if (first.getId().equals(boardsBookmarked.get(i).getId())) + firstIndex = i; + + + //Find the second index. + else if (second.getId().equals(boardsBookmarked.get(i).getId())) + secondIndex = i; + + //If both indexes found, stop. + if (firstIndex != -1 && secondIndex != -1) + break; + } + + if (BuildConfig.DEBUG && !(firstIndex != -1 && secondIndex != -1)) { + throw new AssertionError("firstIndex and secondIndex must exist!"); + } + + //Temp store the first bookmark. + Bookmark firstBookmark = boardsBookmarked.get(firstIndex); + + //Swap the bookmarks. + boardsBookmarked.set(firstIndex, boardsBookmarked.get(secondIndex)); + boardsBookmarked.set(secondIndex, firstBookmark); + + //Update the bookmarks. + updateBoardBookmarks(); + } + } //-------------------------------------------BOOKMARKS END------------------------------------------ //-------PERMS---------