From 82905381dc232a01495d5f421852aa3274192cdb Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Tue, 18 Aug 2020 14:41:36 +0300 Subject: [PATCH] Plan route point menu - Trim left/Trim right --- .../MeasurementEditingContext.java | 39 +++++++++++++++++-- .../MeasurementToolFragment.java | 31 +++++++++------ ...electedPointBottomSheetDialogFragment.java | 33 ++++++---------- .../command/ClearPointsCommand.java | 36 +++++++++++++---- 4 files changed, 96 insertions(+), 43 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java index d83ce40f1b..da881c9d30 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java @@ -262,22 +262,55 @@ public class MeasurementEditingContext { return pt; } + public void trimBefore(int selectedPointPosition) { + splitSegments(selectedPointPosition); + clearBeforeSegments(); + } + + public void trimAfter(int selectedPointPosition) { + splitSegments(selectedPointPosition + 1); + clearAfterSegments(); + } + public void clearSegments() { + clearBeforeSegments(); + clearAfterSegments(); + } + + public void clearBeforeSegments() { before.points.clear(); - after.points.clear(); if (isInSnapToRoadMode()) { - if (beforeCacheForSnap != null && afterCacheForSnap != null) { + if (beforeCacheForSnap != null) { beforeCacheForSnap.points.clear(); - afterCacheForSnap.points.clear(); } needUpdateCacheForSnap = true; } else { beforeCacheForSnap = null; + needUpdateCacheForSnap = false; + } + } + + public void clearAfterSegments() { + after.points.clear(); + if (isInSnapToRoadMode()) { + if (afterCacheForSnap != null) { + afterCacheForSnap.points.clear(); + } + needUpdateCacheForSnap = true; + } else { afterCacheForSnap = null; needUpdateCacheForSnap = false; } } + public boolean isFirstPointSelected() { + return selectedPointPosition == 0; + } + + public boolean isLastPointSelected() { + return selectedPointPosition == getPoints().size() - 1; + } + public void scheduleRouteCalculateIfNotEmpty() { needUpdateCacheForSnap = true; if (application == null || (before.points.size() == 0 && after.points.size() == 0)) { diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index b534184a34..f7fa333906 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -102,6 +102,8 @@ import static net.osmand.plus.measurementtool.SelectFileBottomSheet.Mode.ADD_TO_ import static net.osmand.plus.measurementtool.SelectFileBottomSheet.Mode.OPEN_TRACK; import static net.osmand.plus.measurementtool.SelectFileBottomSheet.SelectFileListener; import static net.osmand.plus.measurementtool.StartPlanRouteBottomSheet.StartPlanRouteListener; +import static net.osmand.plus.measurementtool.command.ClearPointsCommand.*; +import static net.osmand.plus.measurementtool.command.ClearPointsCommand.ClearCommandMode.*; public class MeasurementToolFragment extends BaseOsmAndFragment implements RouteBetweenPointsFragmentListener, OptionsFragmentListener, GpxApproximationFragmentListener, SelectedPointFragmentListener { @@ -701,7 +703,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route @Override public void clearAllOnClick() { MeasurementToolLayer measurementLayer = getMeasurementLayer(); - editingCtx.getCommandManager().execute(new ClearPointsCommand(measurementLayer)); + editingCtx.getCommandManager().execute(new ClearPointsCommand(measurementLayer, ALL)); editingCtx.cancelSnapToRoad(); if (pointsListOpened) { hidePointsList(); @@ -763,12 +765,25 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route @Override public void onTrimRouteBefore() { - + trimRoute(BEFORE); } @Override public void onTrimRouteAfter() { + trimRoute(AFTER); + } + private void trimRoute(ClearCommandMode before) { + MeasurementToolLayer measurementLayer = getMeasurementLayer(); + editingCtx.getCommandManager().execute(new ClearPointsCommand(measurementLayer, before)); + if (pointsListOpened) { + hidePointsList(); + } + editingCtx.setSelectedPointPosition(-1); + editingCtx.splitSegments(editingCtx.getBeforePoints().size() + editingCtx.getAfterPoints().size()); + updateUndoRedoButton(false, redoBtn); + updateDistancePointsText(); + saved = false; } @Override @@ -1009,7 +1024,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route mainIcon.setImageDrawable(getActiveIcon(R.drawable.ic_action_ruler)); editingCtx.resetAppMode(); editingCtx.cancelSnapToRoad(); - visibleSnapToRoadIcon(false); MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { mainView.findViewById(R.id.snap_to_road_progress_bar).setVisibility(View.GONE); @@ -1033,14 +1047,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route } } - private void visibleSnapToRoadIcon(boolean show) { + private void hideSnapToRoadIcon() { MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { - if (show) { - mapActivity.findViewById(R.id.snap_to_road_image_button).setVisibility(View.VISIBLE); - } else { - mapActivity.findViewById(R.id.snap_to_road_image_button).setVisibility(View.GONE); - } + mapActivity.findViewById(R.id.snap_to_road_image_button).setVisibility(View.GONE); } } @@ -1794,9 +1804,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route } if (editingCtx.isInSnapToRoadMode()) { resetSnapToRoadMode(); - } else { - visibleSnapToRoadIcon(false); } + hideSnapToRoadIcon(); if (!editingCtx.isNewData() && !planRouteMode) { GPXFile gpx = editingCtx.getNewGpxData().getGpxFile(); Intent newIntent = new Intent(mapActivity, mapActivity.getMyApplication().getAppCustomization().getTrackActivity()); diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/SelectedPointBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/SelectedPointBottomSheetDialogFragment.java index be6b587c82..bfa1e51df6 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/SelectedPointBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/SelectedPointBottomSheetDialogFragment.java @@ -37,9 +37,15 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo public static final String TAG = SelectedPointBottomSheetDialogFragment.class.getSimpleName(); private static final Log LOG = PlatformUtil.getLog(SelectedPointBottomSheetDialogFragment.class); + private MeasurementEditingContext editingCtx; @Override public void createMenuItems(Bundle savedInstanceState) { + MapActivity mapActivity = getMapActivity(); + if (mapActivity == null) { + return; + } + editingCtx = mapActivity.getMapLayers().getMeasurementToolLayer().getEditingCtx(); View titleView = UiUtilities.getInflater(getContext(), nightMode) .inflate(R.layout.bottom_sheet_item_with_descr_pad_32dp, null, false); @@ -126,6 +132,7 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo dismiss(); } }) + .setDisabled(editingCtx.isFirstPointSelected()) .create(); items.add(trimRouteBefore); @@ -144,6 +151,7 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo dismiss(); } }) + .setDisabled(editingCtx.isLastPointSelected()) .create(); items.add(trimRouteAfter); @@ -164,6 +172,7 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo dismiss(); } }) + .setDisabled(editingCtx.isFirstPointSelected()) .create(); items.add(changeRouteTypeBefore); @@ -182,6 +191,7 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo dismiss(); } }) + .setDisabled(editingCtx.isLastPointSelected()) .create(); items.add(changeRouteTypeAfter); @@ -249,24 +259,15 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo @NonNull private String getTitle() { - MapActivity mapActivity = getMapActivity(); - if (mapActivity == null) { - return ""; - } - - MeasurementEditingContext editingCtx = mapActivity.getMapLayers().getMeasurementToolLayer().getEditingCtx(); int pos = editingCtx.getSelectedPointPosition(); - String pointName = editingCtx.getPoints().get(pos).name; if (!TextUtils.isEmpty(pointName)) { return pointName; } - NewGpxData newGpxData = editingCtx.getNewGpxData(); if (newGpxData != null && newGpxData.getActionType() == ActionType.ADD_ROUTE_POINTS) { return getString(R.string.route_point) + " - " + (pos + 1); } - return getString(R.string.plugin_distance_point) + " - " + (pos + 1); } @@ -276,18 +277,15 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo if (mapActivity == null) { return ""; } - StringBuilder description = new StringBuilder(); - MeasurementEditingContext editingCtx = mapActivity.getMapLayers().getMeasurementToolLayer().getEditingCtx(); int pos = editingCtx.getSelectedPointPosition(); List points = editingCtx.getPoints(); WptPt pt = points.get(pos); - String pointDesc = pt.desc; if (!TextUtils.isEmpty(pointDesc)) { description.append(pointDesc); - } else if (pos < 1) { + } else if (pos < 1 && before) { description.append(getString(R.string.shared_string_control_start)); } else { float dist = 0; @@ -307,7 +305,6 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo } description.append(OsmAndFormatter.getFormattedDistance(dist, mapActivity.getMyApplication())); } - NewGpxData newGpxData = editingCtx.getNewGpxData(); if (newGpxData != null && newGpxData.getActionType() == ActionType.EDIT_SEGMENT) { double elevation = pt.ele; @@ -321,22 +318,14 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo description.append(OsmAndFormatter.getFormattedSpeed(speed, mapActivity.getMyApplication())); } } - return description.toString(); } @Nullable private Drawable getRouteTypeIcon(boolean before) { Drawable icon = getContentIcon(R.drawable.ic_action_split_interval); - MapActivity mapActivity = getMapActivity(); - if (mapActivity == null) { - return icon; - } - - MeasurementEditingContext editingCtx = mapActivity.getMapLayers().getMeasurementToolLayer().getEditingCtx(); int pos = editingCtx.getSelectedPointPosition(); pos = before ? pos : Math.max(pos - 1, 0); - String profileType = editingCtx.getPoints().get(pos).getProfileType(); ApplicationMode routeAppMode = ApplicationMode.valueOfStringKey(profileType, null); if (routeAppMode != null) { diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java index 9023675105..7748b435ea 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/ClearPointsCommand.java @@ -4,31 +4,54 @@ import net.osmand.GPXUtilities.WptPt; import net.osmand.plus.measurementtool.MeasurementToolLayer; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; public class ClearPointsCommand extends MeasurementModeCommand { private List points; private boolean needUpdateCache; + ClearCommandMode clearMode; + private int pointPosition; - public ClearPointsCommand(MeasurementToolLayer measurementLayer) { + public enum ClearCommandMode { + ALL, + BEFORE, + AFTER + } + + public ClearPointsCommand(MeasurementToolLayer measurementLayer, ClearCommandMode clearMode) { super(measurementLayer); + this.clearMode = clearMode; } @Override public boolean execute() { + pointPosition = getEditingCtx().getSelectedPointPosition(); + executeCommand(); + return true; + } + + private void executeCommand() { List pts = getEditingCtx().getPoints(); needUpdateCache = getEditingCtx().isNeedUpdateCacheForSnap(); points = new ArrayList<>(pts); - pts.clear(); - getEditingCtx().clearSegments(); + switch (clearMode) { + case ALL: + pts.clear(); + getEditingCtx().clearSegments(); + break; + case BEFORE: + getEditingCtx().trimBefore(pointPosition); + break; + case AFTER: + getEditingCtx().trimAfter(pointPosition); + } refreshMap(); - return true; } @Override public void undo() { + getEditingCtx().clearSegments(); getEditingCtx().addPoints(points); if (needUpdateCache) { getEditingCtx().setNeedUpdateCacheForSnap(true); @@ -38,8 +61,7 @@ public class ClearPointsCommand extends MeasurementModeCommand { @Override public void redo() { - getEditingCtx().clearSegments(); - refreshMap(); + executeCommand(); } @Override