Skip to content

Commit

Permalink
fix(android): fire move event after movement
Browse files Browse the repository at this point in the history
  • Loading branch information
garymathews authored and sgtcoolguy committed Jan 19, 2021
1 parent a743e6d commit 012d9b5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ public void moveItem(int fromAdapterIndex, int toAdapterIndex)
toSection.add(toIndex, fromItem);

update();
}
}

/**
* Fire `move` event upon finalized movement of an item.
*
* @param fromAdapterIndex Index of item in adapter.
*/
public void fireMoveEvent(int fromAdapterIndex)
{
final TiTableView tableView = getTableView();

if (tableView != null) {
final TableViewRowProxy fromItem = tableView.getAdapterItem(fromAdapterIndex);

fromItem.fireEvent(TiC.EVENT_MOVE, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
*/
package ti.modules.titanium.ui.widget.listview;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

Expand All @@ -25,17 +29,42 @@ public class ItemTouchHandler extends ItemTouchHelper.SimpleCallback
{
private TiRecyclerViewAdapter adapter;
private RecyclerViewProxy recyclerViewProxy;
private Pair<Integer, Integer> movePair = null;

private Drawable icon;
private final ColorDrawable background;

public ItemTouchHandler(TiRecyclerViewAdapter adapter, RecyclerViewProxy recyclerViewProxy)
@SuppressLint("ClickableViewAccessibility")
public ItemTouchHandler(@NonNull TiRecyclerViewAdapter adapter,
@NonNull RecyclerViewProxy recyclerViewProxy,
@NonNull TiNestedRecyclerView recyclerView)
{
super(0, 0);

this.adapter = adapter;
this.recyclerViewProxy = recyclerViewProxy;

// Listen for touch events to fire `move` event.
recyclerView.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_UP) {

// Only fire `move` event upon final movement location.

if (movePair != null) {
final int fromIndex = movePair.first;

recyclerViewProxy.fireMoveEvent(fromIndex);
movePair = null;
}
}
return false;
}
});

this.icon = this.adapter.getContext().getResources().getDrawable(R.drawable.titanium_icon_delete);
this.background = new ColorDrawable(Color.RED);
}
Expand Down Expand Up @@ -202,7 +231,11 @@ public boolean onMove(@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder fromHolder,
@NonNull RecyclerView.ViewHolder toHolder)
{
this.recyclerViewProxy.moveItem(fromHolder.getAdapterPosition(), toHolder.getAdapterPosition());
final int fromIndex = fromHolder.getAdapterPosition();
final int toIndex = toHolder.getAdapterPosition();

movePair = new Pair<>(fromIndex, toIndex);
this.recyclerViewProxy.moveItem(fromIndex, toIndex);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ public void moveItem(int fromAdapterIndex, int toAdapterIndex)

fromSection.deleteItemsAt(fromIndex, 1, null);
toSection.insertItemsAt(toIndex, fromItem, null);
}
}

/**
* Fire `move` event upon finalized movement of an item.
*
* @param fromAdapterIndex Index of item in adapter.
*/
public void fireMoveEvent(int fromAdapterIndex)
{
final TiListView listView = getListView();

if (listView != null) {
final ListItemProxy fromItem = listView.getAdapterItem(fromAdapterIndex);

fromItem.fireEvent(TiC.EVENT_MOVE, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public abstract class RecyclerViewProxy extends TiViewProxy
public abstract void swipeItem(int index);

public abstract void moveItem(int from, int to);

public abstract void fireMoveEvent(int from);
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public TiListView(ListViewProxy proxy)
this.recyclerView.setAdapter(this.adapter);

// Create ItemTouchHelper for swipe-to-delete and move gestures.
final ItemTouchHandler itemTouchHandler = new ItemTouchHandler(this.adapter, this.proxy);
final ItemTouchHandler itemTouchHandler = new ItemTouchHandler(this.adapter, this.proxy, this.recyclerView);
final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHandler);
itemTouchHelper.attachToRecyclerView(this.recyclerView);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public TiTableView(TableViewProxy proxy)
this.recyclerView.setAdapter(this.adapter);

// Create ItemTouchHelper for swipe-to-delete and move gestures.
final ItemTouchHandler itemTouchHandler = new ItemTouchHandler(this.adapter, this.proxy);
final ItemTouchHandler itemTouchHandler = new ItemTouchHandler(this.adapter, this.proxy, this.recyclerView);
final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHandler);
itemTouchHelper.attachToRecyclerView(this.recyclerView);

Expand Down

0 comments on commit 012d9b5

Please sign in to comment.