Add some refactoring

This commit is contained in:
Alex 2017-08-27 00:16:22 +03:00
parent 60bfd28830
commit f09fe36214
9 changed files with 56 additions and 100 deletions

View file

@ -27,12 +27,9 @@ public class MeasurementEditingContext {
private OsmandApplication application;
private final MeasurementCommandManager commandManager = new MeasurementCommandManager();
private TrkSegment before = new TrkSegment();
// cache should be deleted if before changed or snappedToRoadPoints
private final TrkSegment before = new TrkSegment();
private TrkSegment beforeCacheForSnap;
private TrkSegment after = new TrkSegment();
// cache should be deleted if after changed or snappedToRoadPoints
private final TrkSegment after = new TrkSegment();
private TrkSegment afterCacheForSnap;
private NewGpxData newGpxData;
@ -44,42 +41,42 @@ public class MeasurementEditingContext {
private SnapToRoadProgressListener progressListener;
private ApplicationMode snapToRoadAppMode;
private RouteCalculationProgress calculationProgress;
private Queue<Pair<WptPt, WptPt>> snapToRoadPairsToCalculate = new ConcurrentLinkedQueue<>();
private Map<Pair<WptPt, WptPt>, List<WptPt>> snappedToRoadPoints = new ConcurrentHashMap<>();
private final Queue<Pair<WptPt, WptPt>> snapToRoadPairsToCalculate = new ConcurrentLinkedQueue<>();
private final Map<Pair<WptPt, WptPt>, List<WptPt>> snappedToRoadPoints = new ConcurrentHashMap<>();
public void setApplication(OsmandApplication application) {
this.application = application;
}
public MeasurementCommandManager getCommandManager() {
MeasurementCommandManager getCommandManager() {
return commandManager;
}
public boolean isInSnapToRoadMode() {
boolean isInSnapToRoadMode() {
return inSnapToRoadMode;
}
public int getSelectedPointPosition() {
int getSelectedPointPosition() {
return selectedPointPosition;
}
public void setSelectedPointPosition(int selectedPointPosition) {
void setSelectedPointPosition(int selectedPointPosition) {
this.selectedPointPosition = selectedPointPosition;
}
public WptPt getOriginalPointToMove() {
WptPt getOriginalPointToMove() {
return originalPointToMove;
}
public void setOriginalPointToMove(WptPt originalPointToMove) {
void setOriginalPointToMove(WptPt originalPointToMove) {
this.originalPointToMove = originalPointToMove;
}
public void setInSnapToRoadMode(boolean inSnapToRoadMode) {
void setInSnapToRoadMode(boolean inSnapToRoadMode) {
this.inSnapToRoadMode = inSnapToRoadMode;
}
public NewGpxData getNewGpxData() {
NewGpxData getNewGpxData() {
return newGpxData;
}
@ -87,30 +84,26 @@ public class MeasurementEditingContext {
this.newGpxData = newGpxData;
}
public void setProgressListener(SnapToRoadProgressListener progressListener) {
void setProgressListener(SnapToRoadProgressListener progressListener) {
this.progressListener = progressListener;
}
public ApplicationMode getSnapToRoadAppMode() {
ApplicationMode getSnapToRoadAppMode() {
return snapToRoadAppMode;
}
public void setSnapToRoadAppMode(ApplicationMode snapToRoadAppMode) {
void setSnapToRoadAppMode(ApplicationMode snapToRoadAppMode) {
this.snapToRoadAppMode = snapToRoadAppMode;
}
public Map<Pair<WptPt, WptPt>, List<WptPt>> getSnappedPoints() {
return snappedToRoadPoints;
}
public TrkSegment getBeforeTrkSegmentLine() {
TrkSegment getBeforeTrkSegmentLine() {
if (beforeCacheForSnap != null) {
return beforeCacheForSnap;
}
return before;
}
public TrkSegment getAfterTrkSegmentLine() {
TrkSegment getAfterTrkSegmentLine() {
if (afterCacheForSnap != null) {
return afterCacheForSnap;
}
@ -121,11 +114,11 @@ public class MeasurementEditingContext {
return getBeforePoints();
}
public List<WptPt> getBeforePoints() {
List<WptPt> getBeforePoints() {
return before.points;
}
public List<WptPt> getAfterPoints() {
List<WptPt> getAfterPoints() {
return after.points;
}
@ -133,7 +126,7 @@ public class MeasurementEditingContext {
return before.points.size();
}
public void splitSegments(int position) {
void splitSegments(int position) {
List<WptPt> points = new ArrayList<>();
points.addAll(before.points);
points.addAll(after.points);

View file

@ -48,6 +48,7 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.GpxUiHelper;
import net.osmand.plus.measurementtool.NewGpxData.ActionType;
import net.osmand.plus.measurementtool.OptionsBottomSheetDialogFragment.OptionsFragmentListener;
import net.osmand.plus.measurementtool.SaveAsNewTrackBottomSheetDialogFragment.SaveAsNewTrackFragmentListener;
import net.osmand.plus.measurementtool.SelectedPointBottomSheetDialogFragment.SelectedPointFragmentListener;
@ -60,7 +61,6 @@ import net.osmand.plus.measurementtool.command.ClearPointsCommand;
import net.osmand.plus.measurementtool.command.MovePointCommand;
import net.osmand.plus.measurementtool.command.RemovePointCommand;
import net.osmand.plus.measurementtool.command.ReorderPointCommand;
import net.osmand.plus.measurementtool.NewGpxData.ActionType;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController;
@ -115,7 +115,7 @@ public class MeasurementToolFragment extends Fragment {
LINE
}
public void setEditingCtx(MeasurementEditingContext editingCtx) {
private void setEditingCtx(MeasurementEditingContext editingCtx) {
this.editingCtx = editingCtx;
}
@ -606,7 +606,7 @@ public class MeasurementToolFragment extends Fragment {
@Override
public void addPointAfterOnClick() {
if (measurementLayer != null) {
measurementLayer.moveMapToPoint(editingCtx.getSelectedPointPosition());
measurementLayer.moveMapToPoint(editingCtx.getSelectedPointPosition());
editingCtx.splitSegments(editingCtx.getSelectedPointPosition() + 1);
}
((TextView) mainView.findViewById(R.id.add_point_before_after_text)).setText(mainView.getResources().getString(R.string.add_point_after));
@ -617,7 +617,7 @@ public class MeasurementToolFragment extends Fragment {
@Override
public void addPointBeforeOnClick() {
if (measurementLayer != null) {
measurementLayer.moveMapToPoint(editingCtx.getSelectedPointPosition());
measurementLayer.moveMapToPoint(editingCtx.getSelectedPointPosition());
editingCtx.splitSegments(editingCtx.getSelectedPointPosition());
}
((TextView) mainView.findViewById(R.id.add_point_before_after_text)).setText(mainView.getResources().getString(R.string.add_point_before));
@ -861,9 +861,9 @@ public class MeasurementToolFragment extends Fragment {
private void addPointBeforeAfter() {
MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null) {
int selectedPoint = editingCtx.getSelectedPointPosition(); //after = 1; before = 1;
int pointsCount = editingCtx.getPointsCount(); //after = 2; before = 1;
if (addCenterPoint()) { //выбрать вторую точку
int selectedPoint = editingCtx.getSelectedPointPosition(); // after = 1; before = 1;
int pointsCount = editingCtx.getPointsCount(); // after = 2; before = 1;
if (addCenterPoint()) { // select second point
if (selectedPoint == pointsCount) {
editingCtx.splitSegments(editingCtx.getPointsCount() - 1);
} else {

View file

@ -41,9 +41,9 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
private int marginPointIconY;
private int marginApplyingPointIconX;
private int marginApplyingPointIconY;
private Path path = new Path();
private TIntArrayList tx = new TIntArrayList();
private TIntArrayList ty = new TIntArrayList();
private final Path path = new Path();
private final TIntArrayList tx = new TIntArrayList();
private final TIntArrayList ty = new TIntArrayList();
private OnMeasureDistanceToCenter measureDistanceToCenterListener;
private OnSingleTapListener singleTapListener;
private OnEnterMovePointModeListener enterMovePointModeListener;
@ -131,7 +131,7 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
@Override
public boolean onLongPressEvent(PointF point, RotatedTileBox tileBox) {
if (inMeasurementMode) {
if (!overlapped && getEditingCtx().getSelectedPointPosition() == -1 && editingCtx.getPointsCount() > 0) {
if (!overlapped && getEditingCtx().getSelectedPointPosition() == -1 && editingCtx.getPointsCount() > 0) {
selectPoint(point.x, point.y, false);
if (editingCtx.getSelectedPointPosition() != -1) {
enterMovingPointMode();
@ -367,17 +367,17 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
view.getAnimatedDraggingThread().startMoving(lat, lon, view.getZoom(), true);
}
public void moveMapToPoint(int pos) {
if (editingCtx.getPointsCount() > 0) {
if (pos >= editingCtx.getPointsCount()) {
pos = editingCtx.getPointsCount() - 1;
} else if (pos < 0) {
pos = 0;
}
WptPt pt = editingCtx.getPoints().get(pos);
moveMapToLatLon(pt.getLatitude(), pt.getLongitude());
}
}
public void moveMapToPoint(int pos) {
if (editingCtx.getPointsCount() > 0) {
if (pos >= editingCtx.getPointsCount()) {
pos = editingCtx.getPointsCount() - 1;
} else if (pos < 0) {
pos = 0;
}
WptPt pt = editingCtx.getPoints().get(pos);
moveMapToLatLon(pt.getLatitude(), pt.getLongitude());
}
}
public void refreshMap() {
view.refreshMap();

View file

@ -4,7 +4,12 @@ import net.osmand.data.QuadRect;
import net.osmand.plus.GPXUtilities;
public class NewGpxData {
public enum ActionType { ADD_SEGMENT, ADD_ROUTE_POINTS, EDIT_SEGMENT }
public enum ActionType {
ADD_SEGMENT,
ADD_ROUTE_POINTS,
EDIT_SEGMENT
}
private GPXUtilities.GPXFile gpxFile;
private GPXUtilities.TrkSegment trkSegment;

View file

@ -18,7 +18,6 @@ import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.base.BottomSheetDialogFragment;
import net.osmand.plus.helpers.AndroidUiHelper;

View file

@ -17,7 +17,7 @@ import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.measurementtool.NewGpxData;
import net.osmand.plus.measurementtool.NewGpxData.ActionType;
import net.osmand.util.MapUtils;
import java.util.Collections;
@ -30,9 +30,9 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
private final List<WptPt> points;
private MeasurementAdapterListener listener;
private boolean nightMode;
private NewGpxData.ActionType actionType;
private final ActionType actionType;
public MeasurementToolAdapter(MapActivity mapActivity, List<WptPt> points, NewGpxData.ActionType actionType) {
public MeasurementToolAdapter(MapActivity mapActivity, List<WptPt> points, ActionType actionType) {
this.mapActivity = mapActivity;
this.points = points;
this.actionType = actionType;
@ -77,14 +77,14 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
holder.icon.setImageDrawable(iconsCache.getIcon(R.drawable.ic_action_measure_point,
nightMode ? R.color.ctx_menu_info_text_dark : R.color.icon_color));
if (nightMode) {
holder.title.setTextColor(mapActivity.getMyApplication().getResources().getColor(R.color.primary_text_dark));
holder.title.setTextColor(ContextCompat.getColor(mapActivity, R.color.primary_text_dark));
}
WptPt pt = points.get(pos);
String pointTitle = pt.name;
if (!TextUtils.isEmpty(pointTitle)) {
holder.title.setText(pointTitle);
} else {
if (actionType == NewGpxData.ActionType.ADD_ROUTE_POINTS) {
if (actionType == ActionType.ADD_ROUTE_POINTS) {
holder.title.setText(mapActivity.getString(R.string.route_point) + " - " + (pos + 1));
} else {
holder.title.setText(mapActivity.getString(R.string.plugin_distance_point) + " - " + (pos + 1));
@ -105,7 +105,7 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
holder.descr.setText(OsmAndFormatter.getFormattedDistance(dist, mapActivity.getMyApplication()));
}
}
if (actionType == NewGpxData.ActionType.EDIT_SEGMENT) {
if (actionType == ActionType.EDIT_SEGMENT) {
double elevation = pt.ele;
if (!Double.isNaN(elevation)) {
String eleStr = (mapActivity.getString(R.string.altitude)).substring(0, 1);

View file

@ -7,7 +7,7 @@ public class AddPointCommand extends MeasurementModeCommand {
private final int position;
private WptPt point;
private boolean center;
private final boolean center;
public AddPointCommand(MeasurementToolLayer measurementLayer, boolean center) {
this.measurementLayer = measurementLayer;

View file

@ -1,6 +1,6 @@
package net.osmand.plus.measurementtool.command;
public interface Command {
interface Command {
boolean execute();

View file

@ -1,41 +0,0 @@
package net.osmand.plus.measurementtool.command;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.measurementtool.MeasurementToolLayer;
import java.util.List;
public class SnapToRoadCommand extends MeasurementModeCommand {
private List<WptPt> snappedPoints;
public SnapToRoadCommand(MeasurementToolLayer measurementLayer, List<WptPt> points) {
this.measurementLayer = measurementLayer;
this.snappedPoints = points;
}
@Override
public boolean execute() {
// measurementLayer.getSnappedToRoadPoints().clear();
// measurementLayer.getSnappedToRoadPoints().addAll(snappedPoints);
// measurementLayer.refreshMap();
return true;
}
@Override
public void undo() {
// measurementLayer.getSnappedToRoadPoints().clear();
measurementLayer.refreshMap();
}
@Override
public void redo() {
// measurementLayer.getSnappedToRoadPoints().addAll(snappedPoints);
measurementLayer.refreshMap();
}
@Override
MeasurementCommandType getType() {
return MeasurementCommandType.SNAP_TO_ROAD;
}
}