Refactor code

This commit is contained in:
Alex 2017-08-10 23:46:16 +03:00
parent 658d7bdd3e
commit 9b79fde226
7 changed files with 68 additions and 70 deletions

View file

@ -110,9 +110,9 @@ public class MeasurementToolFragment extends Fragment {
final int backgroundColor = ContextCompat.getColor(getActivity(), final int backgroundColor = ContextCompat.getColor(getActivity(),
nightMode ? R.color.ctx_menu_info_view_bg_dark : R.color.ctx_menu_info_view_bg_light); nightMode ? R.color.ctx_menu_info_view_bg_dark : R.color.ctx_menu_info_view_bg_light);
boolean portrait = AndroidUiHelper.isOrientationPortrait(mapActivity); boolean portrait = AndroidUiHelper.isOrientationPortrait(mapActivity);
upIcon = iconsCache.getThemedIcon(R.drawable.ic_action_arrow_up); upIcon = iconsCache.getThemedIcon(R.drawable.ic_action_arrow_up);
downIcon = iconsCache.getThemedIcon(R.drawable.ic_action_arrow_down); downIcon = iconsCache.getThemedIcon(R.drawable.ic_action_arrow_down);
pointsSt = getString(R.string.points).toLowerCase(); pointsSt = getString(R.string.points).toLowerCase();
View view = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_measurement_tool, null); View view = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_measurement_tool, null);
@ -203,13 +203,12 @@ public class MeasurementToolFragment extends Fragment {
} }
}); });
((Button) mainView.findViewById(R.id.add_point_button)) mainView.findViewById(R.id.add_point_button).setOnClickListener(new View.OnClickListener() {
.setOnClickListener(new View.OnClickListener() { @Override
@Override public void onClick(View view) {
public void onClick(View view) { addPoint();
addPoint(); }
} });
});
measurementLayer.setOnSingleTapListener(new MeasurementToolLayer.OnSingleTapListener() { measurementLayer.setOnSingleTapListener(new MeasurementToolLayer.OnSingleTapListener() {
@Override @Override
@ -224,7 +223,6 @@ public class MeasurementToolFragment extends Fragment {
if (pointsListOpened) { if (pointsListOpened) {
hidePointsList(); hidePointsList();
} }
inMovePointMode = true;
enterMovePointMode(); enterMovePointMode();
} }
}); });
@ -335,8 +333,7 @@ public class MeasurementToolFragment extends Fragment {
@Override @Override
public void onItemClick(View view) { public void onItemClick(View view) {
int pos = rv.indexOfChild(view); measurementLayer.moveMapToPoint(rv.indexOfChild(view));
measurementLayer.moveMapToPoint(pos);
} }
@Override @Override
@ -381,7 +378,6 @@ public class MeasurementToolFragment extends Fragment {
private void cancelMovePointMode() { private void cancelMovePointMode() {
if (inMovePointMode) { if (inMovePointMode) {
exitMovePointMode(); exitMovePointMode();
inMovePointMode = false;
} }
MeasurementToolLayer measurementToolLayer = getMeasurementLayer(); MeasurementToolLayer measurementToolLayer = getMeasurementLayer();
if (measurementToolLayer != null) { if (measurementToolLayer != null) {
@ -393,7 +389,6 @@ public class MeasurementToolFragment extends Fragment {
private void applyMovePointMode() { private void applyMovePointMode() {
if (inMovePointMode) { if (inMovePointMode) {
exitMovePointMode(); exitMovePointMode();
inMovePointMode = false;
} }
MeasurementToolLayer measurementLayer = getMeasurementLayer(); MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null) { if (measurementLayer != null) {
@ -409,10 +404,10 @@ public class MeasurementToolFragment extends Fragment {
measurementLayer.exitMovePointMode(); measurementLayer.exitMovePointMode();
measurementLayer.refreshMap(); measurementLayer.refreshMap();
} }
} }
private void enterMovePointMode() { private void enterMovePointMode() {
inMovePointMode = true;
mark(View.GONE, mark(View.GONE,
R.id.ruler_icon, R.id.ruler_icon,
R.id.measurement_distance_text_view, R.id.measurement_distance_text_view,
@ -429,6 +424,7 @@ public class MeasurementToolFragment extends Fragment {
} }
private void exitMovePointMode() { private void exitMovePointMode() {
inMovePointMode = false;
mark(View.GONE, mark(View.GONE,
R.id.move_point_icon, R.id.move_point_icon,
R.id.move_point_text, R.id.move_point_text,
@ -747,9 +743,18 @@ public class MeasurementToolFragment extends Fragment {
measurementLayer.setInMeasurementMode(true); measurementLayer.setInMeasurementMode(true);
mapActivity.refreshMap(); mapActivity.refreshMap();
mapActivity.disableDrawer(); mapActivity.disableDrawer();
mark(View.INVISIBLE, R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info);
mark(View.GONE, R.id.map_route_info_button, R.id.map_menu_button, R.id.map_compass_button, R.id.map_layers_button, mark(View.INVISIBLE,
R.id.map_search_button, R.id.map_quick_actions_button); R.id.map_left_widgets_panel,
R.id.map_right_widgets_panel,
R.id.map_center_info);
mark(View.GONE,
R.id.map_route_info_button,
R.id.map_menu_button,
R.id.map_compass_button,
R.id.map_layers_button,
R.id.map_search_button,
R.id.map_quick_actions_button);
View collapseButton = mapActivity.findViewById(R.id.map_collapse_button); View collapseButton = mapActivity.findViewById(R.id.map_collapse_button);
if (collapseButton != null && collapseButton.getVisibility() == View.VISIBLE) { if (collapseButton != null && collapseButton.getVisibility() == View.VISIBLE) {
@ -771,18 +776,26 @@ public class MeasurementToolFragment extends Fragment {
mapActivity.hideTopToolbar(toolBarController); mapActivity.hideTopToolbar(toolBarController);
} }
measurementLayer.setInMeasurementMode(false); measurementLayer.setInMeasurementMode(false);
mapActivity.refreshMap();
mapActivity.enableDrawer(); mapActivity.enableDrawer();
mark(View.VISIBLE, R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info,
R.id.map_route_info_button, R.id.map_menu_button, R.id.map_compass_button, R.id.map_layers_button, mark(View.VISIBLE,
R.id.map_search_button, R.id.map_quick_actions_button); R.id.map_left_widgets_panel,
R.id.map_right_widgets_panel,
R.id.map_center_info,
R.id.map_route_info_button,
R.id.map_menu_button,
R.id.map_compass_button,
R.id.map_layers_button,
R.id.map_search_button,
R.id.map_quick_actions_button);
View collapseButton = mapActivity.findViewById(R.id.map_collapse_button); View collapseButton = mapActivity.findViewById(R.id.map_collapse_button);
if (collapseButton != null && wasCollapseButtonVisible) { if (collapseButton != null && wasCollapseButtonVisible) {
collapseButton.setVisibility(View.VISIBLE); collapseButton.setVisibility(View.VISIBLE);
} }
measurementLayer.clearPoints(); measurementLayer.getMeasurementPoints().clear();
mapActivity.refreshMap();
} }
} }
@ -828,7 +841,6 @@ public class MeasurementToolFragment extends Fragment {
try { try {
if (inMovePointMode) { if (inMovePointMode) {
exitMovePointMode(); exitMovePointMode();
inMovePointMode = false;
} }
MeasurementToolLayer measurementToolLayer = getMeasurementLayer(); MeasurementToolLayer measurementToolLayer = getMeasurementLayer();
if (measurementToolLayer != null && measurementToolLayer.isInMovePointMode()) { if (measurementToolLayer != null && measurementToolLayer.isInMovePointMode()) {

View file

@ -6,7 +6,6 @@ import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Path; import android.graphics.Path;
import android.graphics.PointF; import android.graphics.PointF;
import android.util.Log;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
@ -85,7 +84,7 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
return inMeasurementMode; return inMeasurementMode;
} }
public boolean isInMovePointMode() { boolean isInMovePointMode() {
return inMovePointMode; return inMovePointMode;
} }
@ -112,11 +111,6 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
return OsmAndFormatter.getFormattedDistance(dist, view.getApplication()); return OsmAndFormatter.getFormattedDistance(dist, view.getApplication());
} }
public void clearPoints() {
measurementPoints.clear();
view.refreshMap();
}
@Override @Override
public boolean onSingleTap(PointF point, RotatedTileBox tileBox) { public boolean onSingleTap(PointF point, RotatedTileBox tileBox) {
if (inMeasurementMode && !inMovePointMode && singleTapListener != null) { if (inMeasurementMode && !inMovePointMode && singleTapListener != null) {
@ -203,6 +197,7 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
} }
calculatePath(tb, tx, ty, path); calculatePath(tb, tx, ty, path);
canvas.drawPath(path, lineAttrs.paint); canvas.drawPath(path, lineAttrs.paint);
WptPt pointToDrawOnTop = null; WptPt pointToDrawOnTop = null;
for (int i = 0; i < measurementPoints.size(); i++) { for (int i = 0; i < measurementPoints.size(); i++) {
WptPt pt = measurementPoints.get(i); WptPt pt = measurementPoints.get(i);
@ -243,26 +238,15 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
canvas.rotate(tb.getRotate(), center.x, center.y); canvas.rotate(tb.getRotate(), center.x, center.y);
} }
public void addPoint(int position, WptPt point) {
measurementPoints.add(position, point);
view.refreshMap();
}
public WptPt addPoint(int position) { public WptPt addPoint(int position) {
RotatedTileBox tb = view.getCurrentRotatedTileBox(); RotatedTileBox tb = view.getCurrentRotatedTileBox();
LatLon l = tb.getLatLonFromPixel(tb.getCenterPixelX(), tb.getCenterPixelY()); LatLon l = tb.getLatLonFromPixel(tb.getCenterPixelX(), tb.getCenterPixelY());
WptPt pt = new WptPt(); WptPt pt = new WptPt();
pt.lat = l.getLatitude(); pt.lat = l.getLatitude();
pt.lon = l.getLongitude(); pt.lon = l.getLongitude();
if (measurementPoints.size() > 0) { boolean allowed = measurementPoints.size() == 0 || !measurementPoints.get(measurementPoints.size() - 1).equals(pt);
if (!measurementPoints.get(measurementPoints.size() - 1).equals(pt)) { if (allowed) {
measurementPoints.add(position, pt);
view.refreshMap();
return pt;
}
} else {
measurementPoints.add(position, pt); measurementPoints.add(position, pt);
view.refreshMap();
return pt; return pt;
} }
return null; return null;
@ -277,12 +261,6 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
return pt; return pt;
} }
public WptPt removePoint(int position) {
WptPt res = measurementPoints.remove(position);
view.refreshMap();
return res;
}
public void moveMapToPoint(int pos) { public void moveMapToPoint(int pos) {
if (measurementPoints.size() > 0) { if (measurementPoints.size() > 0) {
if (pos >= measurementPoints.size()) { if (pos >= measurementPoints.size()) {

View file

@ -21,17 +21,21 @@ public class AddPointCommand implements Command {
@Override @Override
public boolean execute() { public boolean execute() {
return (point = measurementLayer.addPoint(position)) != null; point = measurementLayer.addPoint(position);
measurementLayer.refreshMap();
return point != null;
} }
@Override @Override
public void undo() { public void undo() {
measurementLayer.removePoint(position); measurementLayer.getMeasurementPoints().remove(position);
measurementLayer.refreshMap();
} }
@Override @Override
public void redo() { public void redo() {
measurementLayer.addPoint(position, point); measurementLayer.getMeasurementPoints().add(position, point);
measurementLayer.refreshMap();
measurementLayer.moveMapToPoint(position); measurementLayer.moveMapToPoint(position);
} }
} }

View file

@ -17,8 +17,10 @@ public class ClearPointsCommand implements Command {
@Override @Override
public boolean execute() { public boolean execute() {
points = new LinkedList<>(measurementLayer.getMeasurementPoints()); List<GPXUtilities.WptPt> pts = measurementLayer.getMeasurementPoints();
measurementLayer.clearPoints(); points = new LinkedList<>(pts);
pts.clear();
measurementLayer.refreshMap();
return true; return true;
} }
@ -30,7 +32,7 @@ public class ClearPointsCommand implements Command {
@Override @Override
public void redo() { public void redo() {
measurementLayer.clearPoints(); measurementLayer.getMeasurementPoints().clear();
measurementLayer.refreshMap(); measurementLayer.refreshMap();
} }
} }

View file

@ -10,7 +10,7 @@ public class MovePointCommand implements Command {
private final WptPt newPoint; private final WptPt newPoint;
private final int position; private final int position;
public MovePointCommand (MeasurementToolLayer measurementLayer, WptPt oldPoint, WptPt newPoint, int position) { public MovePointCommand(MeasurementToolLayer measurementLayer, WptPt oldPoint, WptPt newPoint, int position) {
this.measurementLayer = measurementLayer; this.measurementLayer = measurementLayer;
this.oldPoint = oldPoint; this.oldPoint = oldPoint;
this.newPoint = newPoint; this.newPoint = newPoint;
@ -24,21 +24,13 @@ public class MovePointCommand implements Command {
@Override @Override
public void undo() { public void undo() {
replacePointWithOldOne();
}
@Override
public void redo() {
replacePointWithNewOne();
}
private void replacePointWithOldOne() {
measurementLayer.getMeasurementPoints().remove(position); measurementLayer.getMeasurementPoints().remove(position);
measurementLayer.getMeasurementPoints().add(position, oldPoint); measurementLayer.getMeasurementPoints().add(position, oldPoint);
measurementLayer.refreshMap(); measurementLayer.refreshMap();
} }
private void replacePointWithNewOne() { @Override
public void redo() {
measurementLayer.getMeasurementPoints().remove(position); measurementLayer.getMeasurementPoints().remove(position);
measurementLayer.getMeasurementPoints().add(position, newPoint); measurementLayer.getMeasurementPoints().add(position, newPoint);
measurementLayer.refreshMap(); measurementLayer.refreshMap();

View file

@ -16,18 +16,21 @@ public class RemovePointCommand implements Command {
@Override @Override
public boolean execute() { public boolean execute() {
point = measurementLayer.removePoint(position); point = measurementLayer.getMeasurementPoints().remove(position);
measurementLayer.refreshMap();
return true; return true;
} }
@Override @Override
public void undo() { public void undo() {
measurementLayer.addPoint(position, point); measurementLayer.getMeasurementPoints().add(position, point);
measurementLayer.refreshMap();
measurementLayer.moveMapToPoint(position); measurementLayer.moveMapToPoint(position);
} }
@Override @Override
public void redo() { public void redo() {
measurementLayer.removePoint(position); measurementLayer.getMeasurementPoints().remove(position);
measurementLayer.refreshMap();
} }
} }

View file

@ -32,7 +32,14 @@ public class ReorderPointCommand implements Command {
} }
private void swap() { private void swap() {
Collections.swap(measurementLayer.getMeasurementPoints(), from, to); // todo: fix exception
try {
Collections.swap(measurementLayer.getMeasurementPoints(), from, to);
} catch (Exception e) {
// index out of bounds
// an exception occurs when to == -1 basically
// but maybe from == measurementPoints.size() sometimes so there is an exception
}
measurementLayer.refreshMap(); measurementLayer.refreshMap();
} }
} }