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;
}
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);
}
}

View file

@ -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();

View file

@ -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();

View file

@ -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;
}