From cd8c559bdb8c22249264fad8cad6b66895bf3529 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 22 Aug 2017 13:20:32 +0300 Subject: [PATCH] Recalculate snap to road after adding changes --- .../MeasurementToolFragment.java | 86 +++++++++++-------- .../command/AddPointCommand.java | 5 ++ .../command/ClearPointsCommand.java | 5 ++ .../command/MeasurementCommandManager.java | 13 ++- .../command/MeasurementModeCommand.java | 13 ++- .../command/MovePointCommand.java | 5 ++ .../command/RemovePointCommand.java | 5 ++ .../command/ReorderPointCommand.java | 5 ++ .../command/SnapToRoadCommand.java | 5 ++ 9 files changed, 102 insertions(+), 40 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index f85c657198..0b8909183b 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -62,6 +62,7 @@ import net.osmand.plus.measurementtool.adapter.MeasurementToolItemTouchHelperCal import net.osmand.plus.measurementtool.command.AddPointCommand; import net.osmand.plus.measurementtool.command.ClearPointsCommand; import net.osmand.plus.measurementtool.command.MeasurementCommandManager; +import net.osmand.plus.measurementtool.command.MeasurementModeCommand.MeasurementCommandType; import net.osmand.plus.measurementtool.command.MovePointCommand; import net.osmand.plus.measurementtool.command.RemovePointCommand; import net.osmand.plus.measurementtool.command.ReorderPointCommand; @@ -307,7 +308,10 @@ public class MeasurementToolFragment extends Fragment { undoBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - commandManager.undo(); + MeasurementCommandType type = commandManager.undo(); + if (type != null && type != MeasurementCommandType.SNAP_TO_ROAD) { + recalculateSnapToRoadIfNedeed(); + } if (commandManager.canUndo()) { enable(undoBtn); } else { @@ -328,6 +332,7 @@ public class MeasurementToolFragment extends Fragment { @Override public void onClick(View view) { commandManager.redo(); + recalculateSnapToRoadIfNedeed(); if (commandManager.canRedo()) { enable(redoBtn); } else { @@ -478,6 +483,15 @@ public class MeasurementToolFragment extends Fragment { return view; } + private void recalculateSnapToRoadIfNedeed() { + if (calculationProgress != null) { + calculationProgress.isCancelled = true; + } + if (isInSnapToRoadMode) { + doSnapToRoad(); + } + } + @Override public void onDestroyView() { super.onDestroyView(); @@ -590,6 +604,9 @@ public class MeasurementToolFragment extends Fragment { @Override public void clearAllOnClick() { commandManager.execute(new ClearPointsCommand(measurementLayer)); + if (calculationProgress != null) { + calculationProgress.isCancelled = true; + } if (pointsListOpened) { hidePointsList(); } @@ -617,13 +634,7 @@ public class MeasurementToolFragment extends Fragment { public void deleteOnClick() { clearSelection(); if (measurementLayer != null) { - int position = measurementLayer.getSelectedPointPos(); - commandManager.execute(new RemovePointCommand(measurementLayer, position)); - adapter.notifyDataSetChanged(); - disable(redoBtn); - updateText(); - saved = false; - hidePointsListIfNoPoints(); + removePoint(measurementLayer, measurementLayer.getSelectedPointPos()); measurementLayer.clearSelection(); } } @@ -674,6 +685,16 @@ public class MeasurementToolFragment extends Fragment { }; } + private void removePoint(MeasurementToolLayer layer, int position) { + commandManager.execute(new RemovePointCommand(layer, position)); + recalculateSnapToRoadIfNedeed(); + adapter.notifyDataSetChanged(); + disable(redoBtn); + updateText(); + saved = false; + hidePointsListIfNoPoints(); + } + private SaveAsNewTrackFragmentListener createSaveAsNewTrackFragmentListener() { return new SaveAsNewTrackFragmentListener() { @Override @@ -699,12 +720,7 @@ public class MeasurementToolFragment extends Fragment { @Override public void onRemoveClick(int position) { if (measurementLayer != null) { - commandManager.execute(new RemovePointCommand(measurementLayer, position)); - adapter.notifyDataSetChanged(); - disable(redoBtn); - updateText(); - saved = false; - hidePointsListIfNoPoints(); + removePoint(measurementLayer, position); } } @@ -739,6 +755,7 @@ public class MeasurementToolFragment extends Fragment { toPosition = holder.getAdapterPosition(); if (toPosition >= 0 && fromPosition >= 0 && toPosition != fromPosition) { commandManager.execute(new ReorderPointCommand(measurementLayer, fromPosition, toPosition)); + recalculateSnapToRoadIfNedeed(); adapter.notifyDataSetChanged(); disable(redoBtn); updateText(); @@ -807,7 +824,6 @@ public class MeasurementToolFragment extends Fragment { params.calculationProgressCallback = new RoutingHelper.RouteCalculationProgressCallback() { @Override public void updateProgress(int progress) { - snapToRoadProgressBar.setVisibility(View.VISIBLE); snapToRoadProgressBar.setProgress(progress); } @@ -841,9 +857,10 @@ public class MeasurementToolFragment extends Fragment { } } }; + snapToRoadProgressBar.setVisibility(View.VISIBLE); mapActivity.getMyApplication().getRoutingHelper().recalculateSnapToRoad(start, end, intermediates, params); - } else { + } else if (calculationProgress != null) { calculationProgress.isCancelled = true; } } @@ -853,7 +870,9 @@ public class MeasurementToolFragment extends Fragment { toolBarController.setTitle(previousToolBarTitle); isInSnapToRoadMode = false; mainIcon.setImageDrawable(getActiveIcon(R.drawable.ic_action_ruler)); - calculationProgress.isCancelled = true; + if (calculationProgress != null) { + calculationProgress.isCancelled = true; + } MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { mapActivity.findViewById(R.id.snap_to_road_image_button).setVisibility(View.GONE); @@ -928,11 +947,7 @@ public class MeasurementToolFragment extends Fragment { WptPt oldPoint = measurementLayer.getSelectedCachedPoint(); int position = measurementLayer.getSelectedPointPos(); commandManager.execute(new MovePointCommand(measurementLayer, oldPoint, newPoint, position)); - enable(undoBtn, upDownBtn); - disable(redoBtn); - updateText(); - adapter.notifyDataSetChanged(); - saved = false; + doAddOrMovePointCommonStuff(); measurementLayer.exitMovePointMode(); measurementLayer.clearSelection(); measurementLayer.refreshMap(); @@ -1112,11 +1127,7 @@ public class MeasurementToolFragment extends Fragment { MeasurementToolLayer measurementLayer = getMeasurementLayer(); if (measurementLayer != null) { commandManager.execute(new AddPointCommand(measurementLayer, false)); - enable(undoBtn, upDownBtn); - disable(redoBtn); - updateText(); - adapter.notifyDataSetChanged(); - saved = false; + doAddOrMovePointCommonStuff(); } } @@ -1124,11 +1135,7 @@ public class MeasurementToolFragment extends Fragment { MeasurementToolLayer measurementLayer = getMeasurementLayer(); if (measurementLayer != null) { commandManager.execute(new AddPointCommand(measurementLayer, true)); - enable(undoBtn, upDownBtn); - disable(redoBtn); - updateText(); - adapter.notifyDataSetChanged(); - saved = false; + doAddOrMovePointCommonStuff(); } } @@ -1137,15 +1144,20 @@ public class MeasurementToolFragment extends Fragment { MeasurementToolLayer measurementLayer = getMeasurementLayer(); if (measurementLayer != null) { added = commandManager.execute(new AddPointCommand(measurementLayer, position)); - enable(undoBtn, upDownBtn); - disable(redoBtn); - updateText(); - adapter.notifyDataSetChanged(); - saved = false; + doAddOrMovePointCommonStuff(); } return added; } + private void doAddOrMovePointCommonStuff() { + recalculateSnapToRoadIfNedeed(); + enable(undoBtn, upDownBtn); + disable(redoBtn); + updateText(); + adapter.notifyDataSetChanged(); + saved = false; + } + private void showPointsList() { pointsListOpened = true; upDownBtn.setImageDrawable(downIcon); diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/AddPointCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/AddPointCommand.java index 6de9d2702c..1d4582dc17 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/AddPointCommand.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/AddPointCommand.java @@ -44,4 +44,9 @@ public class AddPointCommand extends MeasurementModeCommand { measurementLayer.refreshMap(); measurementLayer.moveMapToPoint(position); } + + @Override + MeasurementCommandType getType() { + return MeasurementCommandType.ADD_POINT; + } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java index 97e4afc4d2..f80340def6 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java @@ -40,4 +40,9 @@ public class ClearPointsCommand extends MeasurementModeCommand { measurementLayer.getSnappedToRoadPoints().clear(); measurementLayer.refreshMap(); } + + @Override + MeasurementCommandType getType() { + return MeasurementCommandType.CLEAR_POINTS; + } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/MeasurementCommandManager.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/MeasurementCommandManager.java index 503cae2704..4261271adb 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/MeasurementCommandManager.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/MeasurementCommandManager.java @@ -1,6 +1,9 @@ package net.osmand.plus.measurementtool.command; +import android.support.annotation.Nullable; + import net.osmand.plus.measurementtool.MeasurementToolLayer; +import net.osmand.plus.measurementtool.command.MeasurementModeCommand.MeasurementCommandType; import java.util.Deque; import java.util.LinkedList; @@ -27,20 +30,26 @@ public class MeasurementCommandManager { return false; } - public void undo() { + @Nullable + public MeasurementCommandType undo() { if (canUndo()) { MeasurementModeCommand command = undoCommands.pop(); redoCommands.push(command); command.undo(); + return command.getType(); } + return null; } - public void redo() { + @Nullable + public MeasurementCommandType redo() { if (canRedo()) { MeasurementModeCommand command = redoCommands.pop(); undoCommands.push(command); command.redo(); + return command.getType(); } + return null; } public void resetMeasurementLayer(MeasurementToolLayer layer) { diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/MeasurementModeCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/MeasurementModeCommand.java index 20d48372d3..f0fb2133ea 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/MeasurementModeCommand.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/MeasurementModeCommand.java @@ -2,11 +2,22 @@ package net.osmand.plus.measurementtool.command; import net.osmand.plus.measurementtool.MeasurementToolLayer; -abstract class MeasurementModeCommand implements Command { +public abstract class MeasurementModeCommand implements Command { MeasurementToolLayer measurementLayer; void setMeasurementLayer(MeasurementToolLayer layer) { this.measurementLayer = layer; } + + abstract MeasurementCommandType getType(); + + public enum MeasurementCommandType { + ADD_POINT, + CLEAR_POINTS, + MOVE_POINT, + REMOVE_POINT, + REORDER_POINT, + SNAP_TO_ROAD + } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/MovePointCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/MovePointCommand.java index b5cd4252f1..242f981ea1 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/MovePointCommand.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/MovePointCommand.java @@ -34,4 +34,9 @@ public class MovePointCommand extends MeasurementModeCommand { measurementLayer.getMeasurementPoints().add(position, newPoint); measurementLayer.refreshMap(); } + + @Override + MeasurementCommandType getType() { + return MeasurementCommandType.MOVE_POINT; + } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/RemovePointCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/RemovePointCommand.java index dea29a4e9d..380e55e331 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/RemovePointCommand.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/RemovePointCommand.java @@ -32,4 +32,9 @@ public class RemovePointCommand extends MeasurementModeCommand { measurementLayer.getMeasurementPoints().remove(position); measurementLayer.refreshMap(); } + + @Override + MeasurementCommandType getType() { + return MeasurementCommandType.REMOVE_POINT; + } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/ReorderPointCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/ReorderPointCommand.java index ecaf081d9c..69c9218a28 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/ReorderPointCommand.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/ReorderPointCommand.java @@ -34,4 +34,9 @@ public class ReorderPointCommand extends MeasurementModeCommand { Collections.swap(measurementLayer.getMeasurementPoints(), from, to); measurementLayer.refreshMap(); } + + @Override + MeasurementCommandType getType() { + return MeasurementCommandType.REORDER_POINT; + } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/SnapToRoadCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/SnapToRoadCommand.java index cf00791eaf..ceb4d1bdd2 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/SnapToRoadCommand.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/SnapToRoadCommand.java @@ -33,4 +33,9 @@ public class SnapToRoadCommand extends MeasurementModeCommand { measurementLayer.getSnappedToRoadPoints().addAll(snappedPoints); measurementLayer.refreshMap(); } + + @Override + MeasurementCommandType getType() { + return MeasurementCommandType.SNAP_TO_ROAD; + } }