From e0189c904fd6cafede06a20c49478ee1b9eab0c3 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Sun, 6 Dec 2020 15:38:42 +0300 Subject: [PATCH] Add new segment item at plan route options --- .../MeasurementEditingContext.java | 12 ++++++-- .../MeasurementToolFragment.java | 8 +++++- .../OptionsBottomSheetDialogFragment.java | 28 ++++++++++++++++++- .../command/SplitPointsCommand.java | 9 +++++- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java index 160a5687ff..9ce68686de 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java @@ -296,6 +296,10 @@ public class MeasurementEditingContext { return !newData && hasDefaultPointsOnly && getPoints().size() > 2; } + public boolean isAddNewSegmentAllowed() { + return beforeSegments.size() > 0 && beforeSegments.get(beforeSegments.size() - 1).points.size() >= 2; + } + public void clearSnappedToRoadPoints() { roadSegmentData.clear(); } @@ -523,15 +527,17 @@ public class MeasurementEditingContext { public void splitPoints(int selectedPointPosition, boolean after) { int pointIndex = after ? selectedPointPosition : selectedPointPosition - 1; - if (pointIndex >= 0 && pointIndex + 1 < before.points.size()) { + if (pointIndex >= 0 && pointIndex < before.points.size()) { WptPt point = before.points.get(pointIndex); - WptPt nextPoint = before.points.get(pointIndex + 1); + WptPt nextPoint = before.points.size() > pointIndex + 1 ? before.points.get(pointIndex + 1) : null; WptPt newPoint = new WptPt(point); newPoint.copyExtensions(point); newPoint.setGap(); before.points.remove(pointIndex); before.points.add(pointIndex, newPoint); - roadSegmentData.remove(new Pair<>(point, nextPoint)); + if (newPoint != null) { + roadSegmentData.remove(new Pair<>(point, nextPoint)); + } updateSegmentsForSnap(false); } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index 7ddee21f35..3102ba3f67 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -370,9 +370,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route @Override public void onClick(View view) { boolean trackSnappedToRoad = !editingCtx.isApproximationNeeded(); + boolean addNewSegmentAllowed = editingCtx.isAddNewSegmentAllowed(); OptionsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), MeasurementToolFragment.this, - trackSnappedToRoad, + trackSnappedToRoad, addNewSegmentAllowed, editingCtx.getAppMode().getStringKey() ); } @@ -831,6 +832,11 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route startSnapToRoad(true); } + @Override + public void addNewSegmentOnClick() { + onSplitPointsAfter(); + } + @Override public void directionsOnClick() { MapActivity mapActivity = getMapActivity(); diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/OptionsBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/OptionsBottomSheetDialogFragment.java index 0a9c690a1e..f286a814b3 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/OptionsBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/OptionsBottomSheetDialogFragment.java @@ -15,6 +15,7 @@ import net.osmand.PlatformUtil; import net.osmand.plus.R; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescriptionDifHeight; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; @@ -29,6 +30,7 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm public static final String TRACK_SNAPPED_TO_ROAD_KEY = "track_snapped_to_road"; public static final String SNAP_TO_ROAD_APP_MODE_KEY = "snap_to_road_app_mode"; + public static final String ADD_NEW_SEGMENT_ALLOWED_KEY = "add_new_segment_allowed"; private ApplicationMode routeAppMode; @@ -36,8 +38,10 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm public void createMenuItems(Bundle savedInstanceState) { Bundle args = getArguments(); boolean trackSnappedToRoad = false; + boolean addNewSegmentAllowed = false; if (args != null) { trackSnappedToRoad = args.getBoolean(TRACK_SNAPPED_TO_ROAD_KEY); + addNewSegmentAllowed = args.getBoolean(ADD_NEW_SEGMENT_ALLOWED_KEY); routeAppMode = ApplicationMode.valueOfStringKey(args.getString(SNAP_TO_ROAD_APP_MODE_KEY), null); } @@ -78,6 +82,25 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm items.add(snapToRoadItem); + if (addNewSegmentAllowed) { + BaseBottomSheetItem addNewSegment = new BottomSheetItemWithDescription.Builder() + //.setIcon(getContentIcon(R.drawable.ic_action_trim_right)) + .setTitle(getString(R.string.plan_route_add_new_segment)) + .setLayoutId(R.layout.bottom_sheet_item_with_descr_pad_32dp) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Fragment fragment = getTargetFragment(); + if (fragment instanceof OptionsFragmentListener) { + ((OptionsFragmentListener) fragment).addNewSegmentOnClick(); + } + dismiss(); + } + }) + .create(); + items.add(addNewSegment); + } + items.add(new OptionsDividerItem(getContext())); BaseBottomSheetItem saveAsNewSegmentItem = new SimpleBottomSheetItem.Builder() @@ -200,12 +223,13 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm } public static void showInstance(@NonNull FragmentManager fm, Fragment targetFragment, - boolean trackSnappedToRoad, String routeAppModeStringKey) { + boolean trackSnappedToRoad, boolean addNewSegmentAllowed, String routeAppModeStringKey) { try { if (!fm.isStateSaved()) { OptionsBottomSheetDialogFragment fragment = new OptionsBottomSheetDialogFragment(); Bundle args = new Bundle(); args.putBoolean(TRACK_SNAPPED_TO_ROAD_KEY, trackSnappedToRoad); + args.putBoolean(ADD_NEW_SEGMENT_ALLOWED_KEY, addNewSegmentAllowed); args.putString(SNAP_TO_ROAD_APP_MODE_KEY, routeAppModeStringKey); fragment.setArguments(args); fragment.setTargetFragment(targetFragment,0); @@ -225,6 +249,8 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm void snapToRoadOnCLick(); + void addNewSegmentOnClick(); + void saveChangesOnClick(); void saveAsNewTrackOnClick(); diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/command/SplitPointsCommand.java b/OsmAnd/src/net/osmand/plus/measurementtool/command/SplitPointsCommand.java index 83af88d7f0..6bfb75d682 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/command/SplitPointsCommand.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/command/SplitPointsCommand.java @@ -2,6 +2,8 @@ package net.osmand.plus.measurementtool.command; import android.util.Pair; +import net.osmand.GPXUtilities; +import net.osmand.GPXUtilities.TrkSegment; import net.osmand.GPXUtilities.WptPt; import net.osmand.plus.measurementtool.MeasurementEditingContext; import net.osmand.plus.measurementtool.MeasurementEditingContext.RoadSegmentData; @@ -21,11 +23,16 @@ public class SplitPointsCommand extends MeasurementModeCommand { public SplitPointsCommand(MeasurementToolLayer measurementLayer, boolean after) { super(measurementLayer); this.after = after; + MeasurementEditingContext editingCtx = getEditingCtx(); + this.pointPosition = editingCtx.getSelectedPointPosition(); + if (this.pointPosition == -1) { + this.after = true; + this.pointPosition = editingCtx.getPoints().size() - 1; + } } @Override public boolean execute() { - pointPosition = getEditingCtx().getSelectedPointPosition(); executeCommand(); return true; }