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 6a281d92..e5654e4b 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 @@ -78,9 +78,9 @@ public class BookmarksActivity extends BaseActivity { } - public void swapBookmarksAfterReorder(Bookmark first, Bookmark second) + public void updateBookmarks(ArrayList update_bookmarks) { - this.swapBookmarks(first, second); + this.updateBookmarksOnReorder(update_bookmarks); } private boolean onTopicInteractionListener(String interactionType, Bookmark bookmarkedTopic) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksAdapter.java index 30d7aaf9..5ab65ee7 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksAdapter.java @@ -24,14 +24,12 @@ import gr.thmmy.mthmmy.model.Bookmark; public class BookmarksAdapter extends DragItemAdapter, BookmarksAdapter.BookmarksViewHolder> { private final BookmarksFragment m_fragment; - private final ArrayList m_bookMarks; private final Drawable m_notificationsEnabled; private final Drawable m_notificationsDisabled; - public BookmarksAdapter(BookmarksFragment fragment, ArrayList bookmarks, Drawable noteEnabled, Drawable noteDisabled) + public BookmarksAdapter(BookmarksFragment fragment, Drawable noteEnabled, Drawable noteDisabled) { this.m_fragment = fragment; - this.m_bookMarks = bookmarks; this.m_notificationsEnabled = noteEnabled; this.m_notificationsDisabled = noteDisabled; } @@ -39,7 +37,7 @@ public class BookmarksAdapter extends DragItemAdapter, Bookm @Override public long getUniqueItemId(int position) { - return m_bookMarks.get(position).getId().hashCode(); + return m_fragment.bookmarks.get(position).getId().hashCode(); } @NonNull @@ -55,18 +53,26 @@ public class BookmarksAdapter extends DragItemAdapter, Bookm { super.onBindViewHolder(holder, position); + //If this is a drop indicator, use the dashed corner background. + if (m_fragment.bookmarks.get(position).getId().equals("-1")) + { + holder.itemView.findViewById(R.id.bookmark_dragable).setBackgroundResource(R.drawable.bookmark_row_dashed_bg); + holder.itemView.findViewById(R.id.toggle_notification).setVisibility(View.GONE); + holder.itemView.findViewById(R.id.remove_bookmark).setVisibility(View.GONE); + } + //Check if bookMarks ArrayList Exists and is not empty. - if(m_bookMarks != null && !m_bookMarks.isEmpty()) + if(m_fragment.bookmarks != null && !m_fragment.bookmarks.isEmpty()) { //Check if the current bookmark exists and has a title. - if (m_bookMarks.get(position) != null && m_bookMarks.get(position).getTitle() != null) + if (m_fragment.bookmarks.get(position) != null && m_fragment.bookmarks.get(position).getTitle() != null) { //Set the title. - holder.m_textView.setText(m_bookMarks.get(position).getTitle()); + holder.m_textView.setText(m_fragment.bookmarks.get(position).getTitle()); //Set Notifications Enabled Image Indicator. - if (m_bookMarks.get(position).isNotificationsEnabled()) + if (m_fragment.bookmarks.get(position).isNotificationsEnabled()) holder.m_noteView.setImageDrawable(m_notificationsEnabled); //Set Notifications Disabled Image Indicator. @@ -85,7 +91,7 @@ public class BookmarksAdapter extends DragItemAdapter, Bookm ((BookmarksActivity) activity).onFragmentRowInteractionListener( m_fragment.type, m_fragment.interactionClick, - m_bookMarks.get(position)); + m_fragment.bookmarks.get(position)); }); @@ -93,7 +99,7 @@ public class BookmarksAdapter extends DragItemAdapter, Bookm holder.m_noteView.setOnClickListener(v -> { //Toggle the current local instance. - m_bookMarks.get(position).toggleNotificationsEnabled(); + m_fragment.bookmarks.get(position).toggleNotificationsEnabled(); //Get the fragment activity. Activity activity = m_fragment.getActivity(); @@ -106,7 +112,7 @@ public class BookmarksAdapter extends DragItemAdapter, Bookm if (((BookmarksActivity) activity).onFragmentRowInteractionListener( m_fragment.type, m_fragment.interactionToggle, - m_bookMarks.get(position))) + m_fragment.bookmarks.get(position))) { holder.m_noteView.setImageDrawable(m_notificationsEnabled); } @@ -133,16 +139,16 @@ public class BookmarksAdapter extends DragItemAdapter, Bookm ((BookmarksActivity) activity).onFragmentRowInteractionListener( m_fragment.type, m_fragment.interactionRemove, - m_bookMarks.get(position)); + m_fragment.bookmarks.get(position)); { notifyItemRemoved(position); - notifyItemRangeChanged(position, m_bookMarks.size()); - m_bookMarks.remove(m_bookMarks.get(position)); + notifyItemRangeChanged(position, m_fragment.bookmarks.size()); + m_fragment.bookmarks.remove(m_fragment.bookmarks.get(position)); } } //If the bookmarks are empty then show nothing marked. - if (m_bookMarks.isEmpty()) + if (m_fragment.bookmarks.isEmpty()) { m_fragment.showNothingBookmarked(); } @@ -155,8 +161,8 @@ public class BookmarksAdapter extends DragItemAdapter, Bookm @Override public int getItemCount() { - if (m_bookMarks != null) - return m_bookMarks.size(); + if (m_fragment.bookmarks != null) + return m_fragment.bookmarks.size(); return 0; } 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 d3c801d4..673c1393 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 @@ -39,7 +39,7 @@ public class BookmarksFragment extends Fragment { private TextView nothingBookmarkedTextView; - private ArrayList bookmarks = null; + public ArrayList 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 new_bookmarks = new ArrayList(); + + //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 books_to_delete = new ArrayList(); + 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====================================// 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 1c7d38c9..dbfdde58 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -692,84 +692,43 @@ public abstract class BaseActivity extends AppCompatActivity { } - //TODO: Abstract reusable code: find the index of the first and second bookmark. - protected void swapBookmarks(Bookmark first, Bookmark second) + /* + * This method should only be called after reordering the bookmarks. + * It Re-creates the topicsBookmarked and boardsBookmarked arrays + * with the reordered bookmarks. + * */ + protected void updateBookmarksOnReorder(ArrayList updated_bookmarks) { + //Define to empty arrays. + ArrayList newTopicBookmarks = new ArrayList(); + ArrayList newBoardBookmarks = new ArrayList(); - //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) ) + //For each bookmark in updated_bookmarks. + for (int i = 0; i < updated_bookmarks.size(); i++) { - //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!"); - } + //Get the current updated bookamrk. + Bookmark book = updated_bookmarks.get(i); - //Temp store the first bookmark. - Bookmark firstBookmark = topicsBookmarked.get(firstIndex); + //Is it a topics bookmark? + if ( book.matchExists(topicsBookmarked) ) + newTopicBookmarks.add(book); - //Swap the bookmarks. - topicsBookmarked.set(firstIndex, topicsBookmarked.get(secondIndex)); - topicsBookmarked.set(secondIndex, firstBookmark); - //Update the bookmarks. - updateTopicBookmarks(); + //Is it a board bookmark? + else if ( book.matchExists(boardsBookmarked) ) + newBoardBookmarks.add(book); } - //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); + if (newTopicBookmarks.size() > 0) + topicsBookmarked = newTopicBookmarks; - //Swap the bookmarks. - boardsBookmarked.set(firstIndex, boardsBookmarked.get(secondIndex)); - boardsBookmarked.set(secondIndex, firstBookmark); + if (newBoardBookmarks.size() > 0) + boardsBookmarked = newBoardBookmarks; - //Update the bookmarks. - updateBoardBookmarks(); - } + //Update the bookmarks. + updateTopicBookmarks(); + updateBoardBookmarks(); } //-------------------------------------------BOOKMARKS END------------------------------------------ diff --git a/app/src/main/res/drawable/bookmark_row_dashed_bg.xml b/app/src/main/res/drawable/bookmark_row_dashed_bg.xml new file mode 100644 index 00000000..7eb0a45c --- /dev/null +++ b/app/src/main/res/drawable/bookmark_row_dashed_bg.xml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file