Add undo/redo for clear all; add some improvements

This commit is contained in:
Alexander Sytnyk 2017-08-09 18:12:24 +03:00
parent c286fa8506
commit 17ccf0c16b
4 changed files with 56 additions and 20 deletions

View file

@ -44,6 +44,7 @@ import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter; import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter;
import net.osmand.plus.measurementtool.adapter.MeasurementToolItemTouchHelperCallback; import net.osmand.plus.measurementtool.adapter.MeasurementToolItemTouchHelperCallback;
import net.osmand.plus.measurementtool.command.AddPointCommand; 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.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.measurementtool.command.ReorderPointCommand;
@ -145,9 +146,9 @@ public class MeasurementToolFragment extends Fragment {
if (commandManager.canUndo()) { if (commandManager.canUndo()) {
enable(undoBtn); enable(undoBtn);
} else { } else {
disable(undoBtn, upDownBtn); disable(undoBtn);
hidePointsList();
} }
hidePointsListIfNeeded();
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
enable(redoBtn); enable(redoBtn);
updateText(); updateText();
@ -164,8 +165,9 @@ public class MeasurementToolFragment extends Fragment {
} else { } else {
disable(redoBtn); disable(redoBtn);
} }
hidePointsListIfNeeded();
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
enable(undoBtn, upDownBtn); enable(undoBtn);
updateText(); updateText();
} }
}); });
@ -232,11 +234,10 @@ public class MeasurementToolFragment extends Fragment {
} }
return true; return true;
case R.id.action_clear_all: case R.id.action_clear_all:
measurementLayer.clearPoints(); commandManager.execute(new ClearPointsCommand(measurementLayer));
hidePointsList(); hidePointsListIfNeeded();
disable(undoBtn, redoBtn, upDownBtn); disable(redoBtn);
updateText(); updateText();
commandManager.clear();
saved = false; saved = false;
return true; return true;
} }
@ -265,13 +266,7 @@ public class MeasurementToolFragment extends Fragment {
disable(redoBtn); disable(redoBtn);
updateText(); updateText();
saved = false; saved = false;
if (measurementLayer.getPointsCount() < 1) { hidePointsListIfNeeded();
hidePointsList();
disable(upDownBtn);
if (!commandManager.canUndo()) {
disable(undoBtn);
}
}
} }
@Override @Override
@ -303,6 +298,18 @@ public class MeasurementToolFragment extends Fragment {
return view; return view;
} }
private void hidePointsListIfNeeded() {
MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null) {
if (measurementLayer.getPointsCount() < 1) {
disable(upDownBtn);
if (pointsListOpened) {
hidePointsList();
}
}
}
}
private void addPoint() { private void addPoint() {
MeasurementToolLayer measurementLayer = getMeasurementLayer(); MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null) { if (measurementLayer != null) {

View file

@ -89,7 +89,7 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
return OsmAndFormatter.getFormattedDistance(dist, view.getApplication()); return OsmAndFormatter.getFormattedDistance(dist, view.getApplication());
} }
void clearPoints() { public void clearPoints() {
measurementPoints.clear(); measurementPoints.clear();
view.refreshMap(); view.refreshMap();
} }

View file

@ -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<GPXUtilities.WptPt> 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();
}
}

View file

@ -38,9 +38,4 @@ public class CommandManager {
command.redo(); command.redo();
} }
} }
public void clear() {
undoCommands.clear();
redoCommands.clear();
}
} }