Browse Source

Fixed drag and dropping using indicators

pull/71/head
babaliaris 4 years ago
parent
commit
eb9b6483e9
  1. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java
  2. 40
      app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksAdapter.java
  3. 96
      app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksFragment.java
  4. 93
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  5. 12
      app/src/main/res/drawable/bookmark_row_dashed_bg.xml

4
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<Bookmark> update_bookmarks)
{ {
this.swapBookmarks(first, second); this.updateBookmarksOnReorder(update_bookmarks);
} }
private boolean onTopicInteractionListener(String interactionType, Bookmark bookmarkedTopic) { private boolean onTopicInteractionListener(String interactionType, Bookmark bookmarkedTopic) {

40
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<ArrayList<Bookmark>, BookmarksAdapter.BookmarksViewHolder> public class BookmarksAdapter extends DragItemAdapter<ArrayList<Bookmark>, BookmarksAdapter.BookmarksViewHolder>
{ {
private final BookmarksFragment m_fragment; private final BookmarksFragment m_fragment;
private final ArrayList<Bookmark> m_bookMarks;
private final Drawable m_notificationsEnabled; private final Drawable m_notificationsEnabled;
private final Drawable m_notificationsDisabled; private final Drawable m_notificationsDisabled;
public BookmarksAdapter(BookmarksFragment fragment, ArrayList<Bookmark> bookmarks, Drawable noteEnabled, Drawable noteDisabled) public BookmarksAdapter(BookmarksFragment fragment, Drawable noteEnabled, Drawable noteDisabled)
{ {
this.m_fragment = fragment; this.m_fragment = fragment;
this.m_bookMarks = bookmarks;
this.m_notificationsEnabled = noteEnabled; this.m_notificationsEnabled = noteEnabled;
this.m_notificationsDisabled = noteDisabled; this.m_notificationsDisabled = noteDisabled;
} }
@ -39,7 +37,7 @@ public class BookmarksAdapter extends DragItemAdapter<ArrayList<Bookmark>, Bookm
@Override @Override
public long getUniqueItemId(int position) public long getUniqueItemId(int position)
{ {
return m_bookMarks.get(position).getId().hashCode(); return m_fragment.bookmarks.get(position).getId().hashCode();
} }
@NonNull @NonNull
@ -55,18 +53,26 @@ public class BookmarksAdapter extends DragItemAdapter<ArrayList<Bookmark>, Bookm
{ {
super.onBindViewHolder(holder, position); 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. //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. //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. //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. //Set Notifications Enabled Image Indicator.
if (m_bookMarks.get(position).isNotificationsEnabled()) if (m_fragment.bookmarks.get(position).isNotificationsEnabled())
holder.m_noteView.setImageDrawable(m_notificationsEnabled); holder.m_noteView.setImageDrawable(m_notificationsEnabled);
//Set Notifications Disabled Image Indicator. //Set Notifications Disabled Image Indicator.
@ -85,7 +91,7 @@ public class BookmarksAdapter extends DragItemAdapter<ArrayList<Bookmark>, Bookm
((BookmarksActivity) activity).onFragmentRowInteractionListener( ((BookmarksActivity) activity).onFragmentRowInteractionListener(
m_fragment.type, m_fragment.type,
m_fragment.interactionClick, m_fragment.interactionClick,
m_bookMarks.get(position)); m_fragment.bookmarks.get(position));
}); });
@ -93,7 +99,7 @@ public class BookmarksAdapter extends DragItemAdapter<ArrayList<Bookmark>, Bookm
holder.m_noteView.setOnClickListener(v -> { holder.m_noteView.setOnClickListener(v -> {
//Toggle the current local instance. //Toggle the current local instance.
m_bookMarks.get(position).toggleNotificationsEnabled(); m_fragment.bookmarks.get(position).toggleNotificationsEnabled();
//Get the fragment activity. //Get the fragment activity.
Activity activity = m_fragment.getActivity(); Activity activity = m_fragment.getActivity();
@ -106,7 +112,7 @@ public class BookmarksAdapter extends DragItemAdapter<ArrayList<Bookmark>, Bookm
if (((BookmarksActivity) activity).onFragmentRowInteractionListener( if (((BookmarksActivity) activity).onFragmentRowInteractionListener(
m_fragment.type, m_fragment.type,
m_fragment.interactionToggle, m_fragment.interactionToggle,
m_bookMarks.get(position))) m_fragment.bookmarks.get(position)))
{ {
holder.m_noteView.setImageDrawable(m_notificationsEnabled); holder.m_noteView.setImageDrawable(m_notificationsEnabled);
} }
@ -133,16 +139,16 @@ public class BookmarksAdapter extends DragItemAdapter<ArrayList<Bookmark>, Bookm
((BookmarksActivity) activity).onFragmentRowInteractionListener( ((BookmarksActivity) activity).onFragmentRowInteractionListener(
m_fragment.type, m_fragment.type,
m_fragment.interactionRemove, m_fragment.interactionRemove,
m_bookMarks.get(position)); m_fragment.bookmarks.get(position));
{ {
notifyItemRemoved(position); notifyItemRemoved(position);
notifyItemRangeChanged(position, m_bookMarks.size()); notifyItemRangeChanged(position, m_fragment.bookmarks.size());
m_bookMarks.remove(m_bookMarks.get(position)); m_fragment.bookmarks.remove(m_fragment.bookmarks.get(position));
} }
} }
//If the bookmarks are empty then show nothing marked. //If the bookmarks are empty then show nothing marked.
if (m_bookMarks.isEmpty()) if (m_fragment.bookmarks.isEmpty())
{ {
m_fragment.showNothingBookmarked(); m_fragment.showNothingBookmarked();
} }
@ -155,8 +161,8 @@ public class BookmarksAdapter extends DragItemAdapter<ArrayList<Bookmark>, Bookm
@Override @Override
public int getItemCount() public int getItemCount()
{ {
if (m_bookMarks != null) if (m_fragment.bookmarks != null)
return m_bookMarks.size(); return m_fragment.bookmarks.size();
return 0; return 0;
} }

96
app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksFragment.java

@ -39,7 +39,7 @@ public class BookmarksFragment extends Fragment {
private TextView nothingBookmarkedTextView; private TextView nothingBookmarkedTextView;
private ArrayList<Bookmark> bookmarks = null; public ArrayList<Bookmark> bookmarks = null;
public Type type; public Type type;
public String interactionClick, interactionToggle, interactionRemove; public String interactionClick, interactionToggle, interactionRemove;
@ -107,13 +107,40 @@ public class BookmarksFragment extends Fragment {
//Get the nothing bookmarked text view. //Get the nothing bookmarked text view.
nothingBookmarkedTextView = rootView.findViewById(R.id.nothing_bookmarked); 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); DragListView mDragListView = (DragListView) rootView.findViewById(R.id.fragment_bookmarks_dragList);
//Set the Drag List Listener.
mDragListView.setDragListListener(new DragListView.DragListListener() mDragListView.setDragListListener(new DragListView.DragListListener()
{ {
@Override @Override
public void onItemDragStarted(int position) 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 @Override
@ -126,36 +153,71 @@ public class BookmarksFragment extends Fragment {
public void onItemDragEnded(int fromPosition, int toPosition) 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 the drag and drop is not the same item.
if (fromPosition != toPosition) if (actualPos != toPosition)
{ {
//Get the from bookmark. //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. //You can only drop items in the indicator boxes!!!
bookmarks.set(fromPosition, bookmarks.get(toPosition)); //Indicator boxes are Bookmark objects with id "-1".
bookmarks.set(toPosition, from); 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. //------------------------Clean up the indicator boxes------------------------//
Activity unknownActivity = getActivity(); }
//Update the order of the bookmarks in the preferences. //Find all the indicator boxes in the bookmarks array.
if (unknownActivity instanceof BookmarksActivity) ArrayList<Bookmark> books_to_delete = new ArrayList<Bookmark>();
{ for (int i = 0; i < bookmarks.size(); i++)
//Cast to BookmarksActivity. {
BookmarksActivity activity = (BookmarksActivity)unknownActivity; Bookmark book = bookmarks.get(i);
//Call the swapBookmarksAfterReorder to apply the swapping changes to the preferences. if (book.getId().equals("-1"))
activity.swapBookmarksAfterReorder( bookmarks.get(fromPosition), bookmarks.get(toPosition) ); 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====================================// //====================================This is the code for the Drag and Drop Functionality====================================//
mDragListView.setLayoutManager(new LinearLayoutManager(getActivity())); mDragListView.setLayoutManager(new LinearLayoutManager(getActivity()));
BookmarksAdapter adapter = new BookmarksAdapter(this, bookmarks, notificationsEnabledButtonImage, notificationsDisabledButtonImage);
mDragListView.setAdapter(adapter, false); mDragListView.setAdapter(adapter, false);
mDragListView.setCanDragHorizontally(false); mDragListView.setCanDragHorizontally(false);
//====================================This is the code for the Drag and Drop Functionality====================================// //====================================This is the code for the Drag and Drop Functionality====================================//

93
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<Bookmark> updated_bookmarks)
{ {
//Define to empty arrays.
ArrayList<Bookmark> newTopicBookmarks = new ArrayList<Bookmark>();
ArrayList<Bookmark> newBoardBookmarks = new ArrayList<Bookmark>();
//No need to check is second is a topics bookmark. //For each bookmark in updated_bookmarks.
//because there is no way to drag and drop between topic and board. for (int i = 0; i < updated_bookmarks.size(); i++)
if ( first.matchExists(topicsBookmarked) )
{ {
//Find the index of the first and second bookmark. //Get the current updated bookamrk.
int firstIndex = -1, secondIndex = -1; Bookmark book = updated_bookmarks.get(i);
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. //Is it a topics bookmark?
Bookmark firstBookmark = topicsBookmarked.get(firstIndex); if ( book.matchExists(topicsBookmarked) )
newTopicBookmarks.add(book);
//Swap the bookmarks.
topicsBookmarked.set(firstIndex, topicsBookmarked.get(secondIndex));
topicsBookmarked.set(secondIndex, firstBookmark);
//Update the bookmarks. //Is it a board bookmark?
updateTopicBookmarks(); 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 (newTopicBookmarks.size() > 0)
if (firstIndex != -1 && secondIndex != -1) topicsBookmarked = newTopicBookmarks;
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. if (newBoardBookmarks.size() > 0)
boardsBookmarked.set(firstIndex, boardsBookmarked.get(secondIndex)); boardsBookmarked = newBoardBookmarks;
boardsBookmarked.set(secondIndex, firstBookmark);
//Update the bookmarks. //Update the bookmarks.
updateBoardBookmarks(); updateTopicBookmarks();
} updateBoardBookmarks();
} }
//-------------------------------------------BOOKMARKS END------------------------------------------ //-------------------------------------------BOOKMARKS END------------------------------------------

12
app/src/main/res/drawable/bookmark_row_dashed_bg.xml

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/background"/>
<stroke
android:width="2dp"
android:color="#B1BCBE"
android:dashWidth="10px"
android:dashGap="10px"
/>
<corners android:radius="10dp"/>
<padding android:left="8dp" android:top="8dp" android:right="8dp" android:bottom="8dp" />
</shape>
Loading…
Cancel
Save