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(),
nightMode ? R.color.ctx_menu_info_view_bg_dark : R.color.ctx_menu_info_view_bg_light);
boolean portrait = AndroidUiHelper.isOrientationPortrait(mapActivity);
upIcon = iconsCache.getThemedIcon(R.drawable.ic_action_arrow_up);
downIcon = iconsCache.getThemedIcon(R.drawable.ic_action_arrow_down);
pointsSt = getString(R.string.points).toLowerCase();
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))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addPoint();
}
});
mainView.findViewById(R.id.add_point_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addPoint();
}
});
measurementLayer.setOnSingleTapListener(new MeasurementToolLayer.OnSingleTapListener() {
@Override
@ -224,7 +223,6 @@ public class MeasurementToolFragment extends Fragment {
if (pointsListOpened) {
hidePointsList();
}
inMovePointMode = true;
enterMovePointMode();
}
});
@ -335,8 +333,7 @@ public class MeasurementToolFragment extends Fragment {
@Override
public void onItemClick(View view) {
int pos = rv.indexOfChild(view);
measurementLayer.moveMapToPoint(pos);
measurementLayer.moveMapToPoint(rv.indexOfChild(view));
}
@Override
@ -381,7 +378,6 @@ public class MeasurementToolFragment extends Fragment {
private void cancelMovePointMode() {
if (inMovePointMode) {
exitMovePointMode();
inMovePointMode = false;
}
MeasurementToolLayer measurementToolLayer = getMeasurementLayer();
if (measurementToolLayer != null) {
@ -393,7 +389,6 @@ public class MeasurementToolFragment extends Fragment {
private void applyMovePointMode() {
if (inMovePointMode) {
exitMovePointMode();
inMovePointMode = false;
}
MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null) {
@ -409,10 +404,10 @@ public class MeasurementToolFragment extends Fragment {
measurementLayer.exitMovePointMode();
measurementLayer.refreshMap();
}
}
private void enterMovePointMode() {
inMovePointMode = true;
mark(View.GONE,
R.id.ruler_icon,
R.id.measurement_distance_text_view,
@ -429,6 +424,7 @@ public class MeasurementToolFragment extends Fragment {
}
private void exitMovePointMode() {
inMovePointMode = false;
mark(View.GONE,
R.id.move_point_icon,
R.id.move_point_text,
@ -747,9 +743,18 @@ public class MeasurementToolFragment extends Fragment {
measurementLayer.setInMeasurementMode(true);
mapActivity.refreshMap();
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,
R.id.map_search_button, R.id.map_quick_actions_button);
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,
R.id.map_search_button,
R.id.map_quick_actions_button);
View collapseButton = mapActivity.findViewById(R.id.map_collapse_button);
if (collapseButton != null && collapseButton.getVisibility() == View.VISIBLE) {
@ -771,18 +776,26 @@ public class MeasurementToolFragment extends Fragment {
mapActivity.hideTopToolbar(toolBarController);
}
measurementLayer.setInMeasurementMode(false);
mapActivity.refreshMap();
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,
R.id.map_search_button, R.id.map_quick_actions_button);
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,
R.id.map_search_button,
R.id.map_quick_actions_button);
View collapseButton = mapActivity.findViewById(R.id.map_collapse_button);
if (collapseButton != null && wasCollapseButtonVisible) {
collapseButton.setVisibility(View.VISIBLE);
}
measurementLayer.clearPoints();
measurementLayer.getMeasurementPoints().clear();
mapActivity.refreshMap();
}
}
@ -828,7 +841,6 @@ public class MeasurementToolFragment extends Fragment {
try {
if (inMovePointMode) {
exitMovePointMode();
inMovePointMode = false;
}
MeasurementToolLayer measurementToolLayer = getMeasurementLayer();
if (measurementToolLayer != null && measurementToolLayer.isInMovePointMode()) {

View file

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

View file

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

View file

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

View file

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

View file

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