Add undo/redo for reordering
This commit is contained in:
parent
654f32652f
commit
284e96361e
5 changed files with 80 additions and 6 deletions
|
@ -46,6 +46,7 @@ import net.osmand.plus.measurementtool.adapter.MeasurementToolItemTouchHelperCal
|
||||||
import net.osmand.plus.measurementtool.command.AddPointCommand;
|
import net.osmand.plus.measurementtool.command.AddPointCommand;
|
||||||
import net.osmand.plus.measurementtool.command.CommandManager;
|
import net.osmand.plus.measurementtool.command.CommandManager;
|
||||||
import net.osmand.plus.measurementtool.command.RemovePointCommand;
|
import net.osmand.plus.measurementtool.command.RemovePointCommand;
|
||||||
|
import net.osmand.plus.measurementtool.command.ReorderPointCommand;
|
||||||
import net.osmand.plus.views.OsmandMapTileView;
|
import net.osmand.plus.views.OsmandMapTileView;
|
||||||
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory;
|
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory;
|
||||||
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController;
|
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController;
|
||||||
|
@ -242,6 +243,10 @@ public class MeasurementToolFragment extends Fragment {
|
||||||
final ItemTouchHelper touchHelper = new ItemTouchHelper(new MeasurementToolItemTouchHelperCallback(adapter));
|
final ItemTouchHelper touchHelper = new ItemTouchHelper(new MeasurementToolItemTouchHelperCallback(adapter));
|
||||||
touchHelper.attachToRecyclerView(rv);
|
touchHelper.attachToRecyclerView(rv);
|
||||||
adapter.setAdapterListener(new MeasurementToolAdapter.MeasurementAdapterListener() {
|
adapter.setAdapterListener(new MeasurementToolAdapter.MeasurementAdapterListener() {
|
||||||
|
|
||||||
|
private int fromPosition;
|
||||||
|
private int toPosition;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPointRemove(int position) {
|
public void onPointRemove(int position) {
|
||||||
commandManager.execute(new RemovePointCommand(measurementLayer, position));
|
commandManager.execute(new RemovePointCommand(measurementLayer, position));
|
||||||
|
@ -265,8 +270,19 @@ public class MeasurementToolFragment extends Fragment {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDragStarted(RecyclerView.ViewHolder holder) {
|
public void onDragStarted(RecyclerView.ViewHolder holder) {
|
||||||
|
fromPosition = holder.getAdapterPosition();
|
||||||
touchHelper.startDrag(holder);
|
touchHelper.startDrag(holder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDragEnded(RecyclerView.ViewHolder holder) {
|
||||||
|
toPosition = holder.getAdapterPosition();
|
||||||
|
if (toPosition != fromPosition) {
|
||||||
|
commandManager.execute(new ReorderPointCommand(measurementLayer, fromPosition, toPosition));
|
||||||
|
adapter.notifyDataSetChanged();
|
||||||
|
mapActivity.refreshMap();
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
rv.setLayoutManager(new LinearLayoutManager(getContext()));
|
rv.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||||
rv.setAdapter(adapter);
|
rv.setAdapter(adapter);
|
||||||
|
|
|
@ -74,7 +74,7 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
|
||||||
return measurementPoints.size();
|
return measurementPoints.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
LinkedList<WptPt> getMeasurementPoints() {
|
public LinkedList<WptPt> getMeasurementPoints() {
|
||||||
return measurementPoints;
|
return measurementPoints;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,6 +195,10 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void refreshMap() {
|
||||||
|
view.refreshMap();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroyLayer() {
|
public void destroyLayer() {
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
|
||||||
|
|
||||||
private final MapActivity mapActivity;
|
private final MapActivity mapActivity;
|
||||||
private final List<WptPt> points;
|
private final List<WptPt> points;
|
||||||
private MeasurementAdapterListener adapterListener;
|
private MeasurementAdapterListener listener;
|
||||||
|
|
||||||
public MeasurementToolAdapter(MapActivity mapActivity, List<WptPt> points) {
|
public MeasurementToolAdapter(MapActivity mapActivity, List<WptPt> points) {
|
||||||
this.mapActivity = mapActivity;
|
this.mapActivity = mapActivity;
|
||||||
|
@ -34,7 +34,7 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAdapterListener(MeasurementAdapterListener listener) {
|
public void setAdapterListener(MeasurementAdapterListener listener) {
|
||||||
this.adapterListener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -50,7 +50,7 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
|
||||||
view.setOnClickListener(new View.OnClickListener() {
|
view.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
adapterListener.onItemClick(view);
|
listener.onItemClick(view);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return new MeasureToolItemVH(view);
|
return new MeasureToolItemVH(view);
|
||||||
|
@ -64,7 +64,7 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouch(View view, MotionEvent event) {
|
public boolean onTouch(View view, MotionEvent event) {
|
||||||
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
|
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
|
||||||
adapterListener.onDragStarted(holder);
|
listener.onDragStarted(holder);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
|
||||||
holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
|
holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
adapterListener.onPointRemove(holder.getAdapterPosition());
|
listener.onPointRemove(holder.getAdapterPosition());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -102,6 +102,11 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onItemDismiss(RecyclerView.ViewHolder holder) {
|
||||||
|
listener.onDragEnded(holder);
|
||||||
|
}
|
||||||
|
|
||||||
static class MeasureToolItemVH extends RecyclerView.ViewHolder {
|
static class MeasureToolItemVH extends RecyclerView.ViewHolder {
|
||||||
|
|
||||||
final ImageView iconReorder;
|
final ImageView iconReorder;
|
||||||
|
@ -127,5 +132,7 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
|
||||||
void onItemClick(View view);
|
void onItemClick(View view);
|
||||||
|
|
||||||
void onDragStarted(RecyclerView.ViewHolder holder);
|
void onDragStarted(RecyclerView.ViewHolder holder);
|
||||||
|
|
||||||
|
void onDragEnded(RecyclerView.ViewHolder holder);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -38,7 +38,16 @@ public class MeasurementToolItemTouchHelperCallback extends ItemTouchHelper.Call
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
|
||||||
|
super.clearView(recyclerView, viewHolder);
|
||||||
|
adapter.onItemDismiss(viewHolder);
|
||||||
|
}
|
||||||
|
|
||||||
interface ItemTouchHelperAdapter {
|
interface ItemTouchHelperAdapter {
|
||||||
|
|
||||||
boolean onItemMove(int from, int to);
|
boolean onItemMove(int from, int to);
|
||||||
|
|
||||||
|
void onItemDismiss(RecyclerView.ViewHolder holder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package net.osmand.plus.measurementtool.command;
|
||||||
|
|
||||||
|
import net.osmand.plus.measurementtool.MeasurementToolLayer;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
public class ReorderPointCommand implements Command {
|
||||||
|
|
||||||
|
private final MeasurementToolLayer measurementLayer;
|
||||||
|
private final int from;
|
||||||
|
private final int to;
|
||||||
|
|
||||||
|
public ReorderPointCommand(MeasurementToolLayer measurementLayer, int from, int to) {
|
||||||
|
this.measurementLayer = measurementLayer;
|
||||||
|
this.from = from;
|
||||||
|
this.to = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void undo() {
|
||||||
|
swap();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void redo() {
|
||||||
|
swap();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void swap() {
|
||||||
|
Collections.swap(measurementLayer.getMeasurementPoints(), from, to);
|
||||||
|
measurementLayer.refreshMap();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue