diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index 2e4bd1f5da..674b1d2acc 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -44,6 +44,7 @@ import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter; import net.osmand.plus.measurementtool.adapter.MeasurementToolItemTouchHelperCallback; import net.osmand.plus.measurementtool.command.AddPointCommand; +import net.osmand.plus.measurementtool.command.ClearPointsCommand; import net.osmand.plus.measurementtool.command.CommandManager; import net.osmand.plus.measurementtool.command.RemovePointCommand; import net.osmand.plus.measurementtool.command.ReorderPointCommand; @@ -145,9 +146,9 @@ public class MeasurementToolFragment extends Fragment { if (commandManager.canUndo()) { enable(undoBtn); } else { - disable(undoBtn, upDownBtn); - hidePointsList(); + disable(undoBtn); } + hidePointsListIfNeeded(); adapter.notifyDataSetChanged(); enable(redoBtn); updateText(); @@ -164,8 +165,9 @@ public class MeasurementToolFragment extends Fragment { } else { disable(redoBtn); } + hidePointsListIfNeeded(); adapter.notifyDataSetChanged(); - enable(undoBtn, upDownBtn); + enable(undoBtn); updateText(); } }); @@ -232,11 +234,10 @@ public class MeasurementToolFragment extends Fragment { } return true; case R.id.action_clear_all: - measurementLayer.clearPoints(); - hidePointsList(); - disable(undoBtn, redoBtn, upDownBtn); + commandManager.execute(new ClearPointsCommand(measurementLayer)); + hidePointsListIfNeeded(); + disable(redoBtn); updateText(); - commandManager.clear(); saved = false; return true; } @@ -265,13 +266,7 @@ public class MeasurementToolFragment extends Fragment { disable(redoBtn); updateText(); saved = false; - if (measurementLayer.getPointsCount() < 1) { - hidePointsList(); - disable(upDownBtn); - if (!commandManager.canUndo()) { - disable(undoBtn); - } - } + hidePointsListIfNeeded(); } @Override @@ -303,6 +298,18 @@ public class MeasurementToolFragment extends Fragment { return view; } + private void hidePointsListIfNeeded() { + MeasurementToolLayer measurementLayer = getMeasurementLayer(); + if (measurementLayer != null) { + if (measurementLayer.getPointsCount() < 1) { + disable(upDownBtn); + if (pointsListOpened) { + hidePointsList(); + } + } + } + } + private void addPoint() { MeasurementToolLayer measurementLayer = getMeasurementLayer(); if (measurementLayer != null) { diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolLayer.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolLayer.java index 0f24f0d71e..5603545434 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolLayer.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolLayer.java @@ -89,7 +89,7 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL return OsmAndFormatter.getFormattedDistance(dist, view.getApplication()); } - void clearPoints() { + public void clearPoints() { measurementPoints.clear(); view.refreshMap(); } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java new file mode 100644 index 0000000000..1bacf2aa22 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java @@ -0,0 +1,34 @@ +package net.osmand.plus.measurementtool.command; + +import net.osmand.plus.GPXUtilities; +import net.osmand.plus.measurementtool.MeasurementToolLayer; + +import java.util.LinkedList; +import java.util.List; + +public class ClearPointsCommand implements Command { + + private final MeasurementToolLayer measurementLayer; + private List points; + + public ClearPointsCommand(MeasurementToolLayer measurementLayer) { + this.measurementLayer = measurementLayer; + } + + @Override + public boolean execute() { + points = new LinkedList<>(measurementLayer.getMeasurementPoints()); + measurementLayer.clearPoints(); + return true; + } + + @Override + public void undo() { + measurementLayer.getMeasurementPoints().addAll(points); + } + + @Override + public void redo() { + measurementLayer.clearPoints(); + } +} diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/CommandManager.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/CommandManager.java index aada2ee5c3..3188506ded 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/CommandManager.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/CommandManager.java @@ -38,9 +38,4 @@ public class CommandManager { command.redo(); } } - - public void clear() { - undoCommands.clear(); - redoCommands.clear(); - } }