Add new segment item at plan route options

This commit is contained in:
max-klaus 2020-12-06 15:38:42 +03:00
parent 86e32e5cc3
commit e0189c904f
4 changed files with 51 additions and 6 deletions

View file

@ -296,6 +296,10 @@ public class MeasurementEditingContext {
return !newData && hasDefaultPointsOnly && getPoints().size() > 2; return !newData && hasDefaultPointsOnly && getPoints().size() > 2;
} }
public boolean isAddNewSegmentAllowed() {
return beforeSegments.size() > 0 && beforeSegments.get(beforeSegments.size() - 1).points.size() >= 2;
}
public void clearSnappedToRoadPoints() { public void clearSnappedToRoadPoints() {
roadSegmentData.clear(); roadSegmentData.clear();
} }
@ -523,15 +527,17 @@ public class MeasurementEditingContext {
public void splitPoints(int selectedPointPosition, boolean after) { public void splitPoints(int selectedPointPosition, boolean after) {
int pointIndex = after ? selectedPointPosition : selectedPointPosition - 1; 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 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); WptPt newPoint = new WptPt(point);
newPoint.copyExtensions(point); newPoint.copyExtensions(point);
newPoint.setGap(); newPoint.setGap();
before.points.remove(pointIndex); before.points.remove(pointIndex);
before.points.add(pointIndex, newPoint); before.points.add(pointIndex, newPoint);
roadSegmentData.remove(new Pair<>(point, nextPoint)); if (newPoint != null) {
roadSegmentData.remove(new Pair<>(point, nextPoint));
}
updateSegmentsForSnap(false); updateSegmentsForSnap(false);
} }
} }

View file

@ -370,9 +370,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
@Override @Override
public void onClick(View view) { public void onClick(View view) {
boolean trackSnappedToRoad = !editingCtx.isApproximationNeeded(); boolean trackSnappedToRoad = !editingCtx.isApproximationNeeded();
boolean addNewSegmentAllowed = editingCtx.isAddNewSegmentAllowed();
OptionsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), OptionsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(),
MeasurementToolFragment.this, MeasurementToolFragment.this,
trackSnappedToRoad, trackSnappedToRoad, addNewSegmentAllowed,
editingCtx.getAppMode().getStringKey() editingCtx.getAppMode().getStringKey()
); );
} }
@ -831,6 +832,11 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
startSnapToRoad(true); startSnapToRoad(true);
} }
@Override
public void addNewSegmentOnClick() {
onSplitPointsAfter();
}
@Override @Override
public void directionsOnClick() { public void directionsOnClick() {
MapActivity mapActivity = getMapActivity(); MapActivity mapActivity = getMapActivity();

View file

@ -15,6 +15,7 @@ import net.osmand.PlatformUtil;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; 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.BottomSheetItemWithDescriptionDifHeight;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; 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 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 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; private ApplicationMode routeAppMode;
@ -36,8 +38,10 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
public void createMenuItems(Bundle savedInstanceState) { public void createMenuItems(Bundle savedInstanceState) {
Bundle args = getArguments(); Bundle args = getArguments();
boolean trackSnappedToRoad = false; boolean trackSnappedToRoad = false;
boolean addNewSegmentAllowed = false;
if (args != null) { if (args != null) {
trackSnappedToRoad = args.getBoolean(TRACK_SNAPPED_TO_ROAD_KEY); 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); routeAppMode = ApplicationMode.valueOfStringKey(args.getString(SNAP_TO_ROAD_APP_MODE_KEY), null);
} }
@ -78,6 +82,25 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
items.add(snapToRoadItem); 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())); items.add(new OptionsDividerItem(getContext()));
BaseBottomSheetItem saveAsNewSegmentItem = new SimpleBottomSheetItem.Builder() BaseBottomSheetItem saveAsNewSegmentItem = new SimpleBottomSheetItem.Builder()
@ -200,12 +223,13 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
} }
public static void showInstance(@NonNull FragmentManager fm, Fragment targetFragment, public static void showInstance(@NonNull FragmentManager fm, Fragment targetFragment,
boolean trackSnappedToRoad, String routeAppModeStringKey) { boolean trackSnappedToRoad, boolean addNewSegmentAllowed, String routeAppModeStringKey) {
try { try {
if (!fm.isStateSaved()) { if (!fm.isStateSaved()) {
OptionsBottomSheetDialogFragment fragment = new OptionsBottomSheetDialogFragment(); OptionsBottomSheetDialogFragment fragment = new OptionsBottomSheetDialogFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putBoolean(TRACK_SNAPPED_TO_ROAD_KEY, trackSnappedToRoad); 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); args.putString(SNAP_TO_ROAD_APP_MODE_KEY, routeAppModeStringKey);
fragment.setArguments(args); fragment.setArguments(args);
fragment.setTargetFragment(targetFragment,0); fragment.setTargetFragment(targetFragment,0);
@ -225,6 +249,8 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
void snapToRoadOnCLick(); void snapToRoadOnCLick();
void addNewSegmentOnClick();
void saveChangesOnClick(); void saveChangesOnClick();
void saveAsNewTrackOnClick(); void saveAsNewTrackOnClick();

View file

@ -2,6 +2,8 @@ package net.osmand.plus.measurementtool.command;
import android.util.Pair; import android.util.Pair;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.TrkSegment;
import net.osmand.GPXUtilities.WptPt; import net.osmand.GPXUtilities.WptPt;
import net.osmand.plus.measurementtool.MeasurementEditingContext; import net.osmand.plus.measurementtool.MeasurementEditingContext;
import net.osmand.plus.measurementtool.MeasurementEditingContext.RoadSegmentData; import net.osmand.plus.measurementtool.MeasurementEditingContext.RoadSegmentData;
@ -21,11 +23,16 @@ public class SplitPointsCommand extends MeasurementModeCommand {
public SplitPointsCommand(MeasurementToolLayer measurementLayer, boolean after) { public SplitPointsCommand(MeasurementToolLayer measurementLayer, boolean after) {
super(measurementLayer); super(measurementLayer);
this.after = after; this.after = after;
MeasurementEditingContext editingCtx = getEditingCtx();
this.pointPosition = editingCtx.getSelectedPointPosition();
if (this.pointPosition == -1) {
this.after = true;
this.pointPosition = editingCtx.getPoints().size() - 1;
}
} }
@Override @Override
public boolean execute() { public boolean execute() {
pointPosition = getEditingCtx().getSelectedPointPosition();
executeCommand(); executeCommand();
return true; return true;
} }