use of net.osmand.plus.measurementtool.adapter.MeasurementToolItemTouchHelperCallback in project Osmand by osmandapp.
the class MeasurementToolFragment method onCreateView.
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final MapActivity mapActivity = (MapActivity) getActivity();
final MeasurementToolLayer measurementLayer = mapActivity.getMapLayers().getMeasurementToolLayer();
editingCtx.setApplication(mapActivity.getMyApplication());
editingCtx.setProgressListener(new MeasurementEditingContext.SnapToRoadProgressListener() {
@Override
public void showProgressBar() {
MeasurementToolFragment.this.showProgressBar();
}
@Override
public void updateProgress(int progress) {
((ProgressBar) mainView.findViewById(R.id.snap_to_road_progress_bar)).setProgress(progress);
}
@Override
public void hideProgressBar() {
((ProgressBar) mainView.findViewById(R.id.snap_to_road_progress_bar)).setVisibility(View.GONE);
progressBarVisible = false;
}
@Override
public void refresh() {
measurementLayer.refreshMap();
updateText();
}
});
measurementLayer.setEditingCtx(editingCtx);
// Handling screen rotation
FragmentManager fragmentManager = mapActivity.getSupportFragmentManager();
Fragment selectedPointFragment = fragmentManager.findFragmentByTag(SelectedPointBottomSheetDialogFragment.TAG);
if (selectedPointFragment != null) {
SelectedPointBottomSheetDialogFragment fragment = (SelectedPointBottomSheetDialogFragment) selectedPointFragment;
fragment.setListener(createSelectedPointFragmentListener());
}
Fragment optionsFragment = fragmentManager.findFragmentByTag(OptionsBottomSheetDialogFragment.TAG);
if (optionsFragment != null) {
((OptionsBottomSheetDialogFragment) optionsFragment).setListener(createOptionsFragmentListener());
}
Fragment snapToRoadFragment = fragmentManager.findFragmentByTag(SnapToRoadBottomSheetDialogFragment.TAG);
if (snapToRoadFragment != null) {
((SnapToRoadBottomSheetDialogFragment) snapToRoadFragment).setListener(createSnapToRoadFragmentListener());
}
Fragment saveAsNewTrackFragment = mapActivity.getSupportFragmentManager().findFragmentByTag(SaveAsNewTrackBottomSheetDialogFragment.TAG);
if (saveAsNewTrackFragment != null) {
((SaveAsNewTrackBottomSheetDialogFragment) saveAsNewTrackFragment).setListener(createSaveAsNewTrackFragmentListener());
}
// the RecyclerViewFragment will exist without view. This is necessary to remove it.
if (!portrait) {
hidePointsListFragment();
}
editingCtx.getCommandManager().resetMeasurementLayer(measurementLayer);
nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls();
final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
final int backgroundColor = ContextCompat.getColor(getActivity(), nightMode ? R.color.ctx_menu_info_view_bg_dark : R.color.ctx_menu_info_view_bg_light);
portrait = AndroidUiHelper.isOrientationPortrait(getActivity());
upIcon = getContentIcon(R.drawable.ic_action_arrow_up);
downIcon = getContentIcon(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);
mainView = view.findViewById(R.id.main_view);
AndroidUtils.setBackground(mapActivity, mainView, nightMode, R.drawable.bg_bottom_menu_light, R.drawable.bg_bottom_menu_dark);
pointsListContainer = view.findViewById(R.id.points_list_container);
if (portrait && pointsListContainer != null) {
pointsListContainer.setBackgroundColor(backgroundColor);
}
if (progressBarVisible) {
showProgressBar();
}
distanceTv = (TextView) mainView.findViewById(R.id.measurement_distance_text_view);
pointsTv = (TextView) mainView.findViewById(R.id.measurement_points_text_view);
distanceToCenterTv = (TextView) mainView.findViewById(R.id.distance_to_center_text_view);
mainIcon = (ImageView) mainView.findViewById(R.id.main_icon);
final NewGpxData newGpxData = editingCtx.getNewGpxData();
if (editingCtx.getNewGpxData() != null) {
ActionType actionType = newGpxData.getActionType();
if (actionType == ActionType.ADD_SEGMENT || actionType == ActionType.EDIT_SEGMENT) {
mainIcon.setImageDrawable(getActiveIcon(R.drawable.ic_action_polygom_dark));
} else {
mainIcon.setImageDrawable(getActiveIcon(R.drawable.ic_action_markers_dark));
}
} else {
mainIcon.setImageDrawable(getActiveIcon(R.drawable.ic_action_ruler));
}
upDownBtn = (ImageView) mainView.findViewById(R.id.up_down_button);
upDownBtn.setImageDrawable(upIcon);
mainView.findViewById(R.id.cancel_move_point_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cancelMovePointMode();
}
});
mainView.findViewById(R.id.cancel_point_before_after_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cancelAddPointBeforeOrAfterMode();
}
});
upDownRow = mainView.findViewById(R.id.up_down_row);
upDownRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!pointsListOpened && editingCtx.getPointsCount() > 0 && editingCtx.getSelectedPointPosition() == -1) {
showPointsList();
} else {
hidePointsList();
}
}
});
mainView.findViewById(R.id.apply_move_point_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
applyMovePointMode();
}
});
mainView.findViewById(R.id.apply_point_before_after_point_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
applyAddPointBeforeAfterMode();
}
});
mainView.findViewById(R.id.add_point_before_after_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addPointBeforeAfter();
}
});
mainView.findViewById(R.id.options_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle args = new Bundle();
args.putBoolean(OptionsBottomSheetDialogFragment.SNAP_TO_ROAD_ENABLED_KEY, editingCtx.isInSnapToRoadMode());
args.putBoolean(OptionsBottomSheetDialogFragment.ADD_LINE_MODE_KEY, newGpxData != null);
OptionsBottomSheetDialogFragment fragment = new OptionsBottomSheetDialogFragment();
fragment.setArguments(args);
fragment.setUsedOnMap(true);
fragment.setListener(createOptionsFragmentListener());
fragment.show(mapActivity.getSupportFragmentManager(), OptionsBottomSheetDialogFragment.TAG);
}
});
undoBtn = ((ImageButton) mainView.findViewById(R.id.undo_point_button));
redoBtn = ((ImageButton) mainView.findViewById(R.id.redo_point_button));
undoBtn.setImageDrawable(getContentIcon(R.drawable.ic_action_undo_dark));
undoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editingCtx.getCommandManager().undo();
if (editingCtx.getCommandManager().canUndo()) {
enable(undoBtn);
} else {
disable(undoBtn);
}
hidePointsListIfNoPoints();
if (editingCtx.getPointsCount() > 0) {
enable(upDownBtn);
}
adapter.notifyDataSetChanged();
enable(redoBtn);
updateText();
}
});
redoBtn.setImageDrawable(getContentIcon(R.drawable.ic_action_redo_dark));
redoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editingCtx.getCommandManager().redo();
if (editingCtx.getCommandManager().canRedo()) {
enable(redoBtn);
} else {
disable(redoBtn);
}
hidePointsListIfNoPoints();
if (editingCtx.getPointsCount() > 0) {
enable(upDownBtn);
}
adapter.notifyDataSetChanged();
enable(undoBtn);
updateText();
}
});
mainView.findViewById(R.id.add_point_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addCenterPoint();
}
});
measurementLayer.setOnSingleTapListener(new MeasurementToolLayer.OnSingleTapListener() {
@Override
public void onAddPoint() {
addPoint();
}
@Override
public void onSelectPoint(int selectedPointPos) {
if (pointsListOpened) {
hidePointsList();
}
if (selectedPointPos != -1) {
openSelectedPointMenu(mapActivity);
}
}
});
measurementLayer.setOnMeasureDistanceToCenterListener(new MeasurementToolLayer.OnMeasureDistanceToCenter() {
@Override
public void onMeasure(float distance) {
String distStr = OsmAndFormatter.getFormattedDistance(distance, mapActivity.getMyApplication());
distanceToCenterTv.setText(" – " + distStr);
}
});
measurementLayer.setOnEnterMovePointModeListener(new MeasurementToolLayer.OnEnterMovePointModeListener() {
@Override
public void onEnterMovePointMode() {
if (pointsListOpened) {
hidePointsList();
}
switchMovePointMode(true);
}
});
if (!editingCtx.getCommandManager().canUndo()) {
disable(undoBtn);
}
if (!editingCtx.getCommandManager().canRedo()) {
disable(redoBtn);
}
if (editingCtx.getPointsCount() < 1) {
disable(upDownBtn);
}
toolBarController = new MeasurementToolBarController(newGpxData);
if (editingCtx.getSelectedPointPosition() != -1) {
toolBarController.setBackBtnIconIds(R.drawable.ic_action_mode_back, R.drawable.ic_action_mode_back);
} else {
toolBarController.setBackBtnIconIds(R.drawable.ic_action_remove_dark, R.drawable.ic_action_remove_dark);
}
if (newGpxData != null) {
NewGpxData.ActionType actionType = newGpxData.getActionType();
if (actionType == NewGpxData.ActionType.ADD_ROUTE_POINTS) {
toolBarController.setTitle(getString(R.string.add_route_points));
} else if (actionType == NewGpxData.ActionType.ADD_SEGMENT) {
toolBarController.setTitle(getString(R.string.add_line));
} else if (actionType == NewGpxData.ActionType.EDIT_SEGMENT) {
toolBarController.setTitle(getString(R.string.edit_line));
}
} else {
toolBarController.setTitle(getString(R.string.measurement_tool_action_bar));
}
toolBarController.setOnBackButtonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quit(false);
}
});
toolBarController.setOnSaveViewClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editingCtx.getPointsCount() > 0) {
addToGpx(mapActivity);
} else {
Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show();
}
}
});
toolBarController.setOnSwitchCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (!checked) {
disableSnapToRoadMode();
}
}
});
mapActivity.showTopToolbar(toolBarController);
adapter = new MeasurementToolAdapter(getMapActivity(), editingCtx.getPoints(), newGpxData != null ? newGpxData.getActionType() : null);
if (portrait) {
pointsRv = mainView.findViewById(R.id.measure_points_recycler_view);
} else {
pointsRv = new RecyclerView(getActivity());
}
final ItemTouchHelper touchHelper = new ItemTouchHelper(new MeasurementToolItemTouchHelperCallback(adapter));
touchHelper.attachToRecyclerView(pointsRv);
adapter.setAdapterListener(createMeasurementAdapterListener(touchHelper));
pointsRv.setLayoutManager(new LinearLayoutManager(getContext()));
pointsRv.setAdapter(adapter);
enterMeasurementMode();
if (editingCtx.isInSnapToRoadMode()) {
showSnapToRoadControls();
}
if (newGpxData != null && !gpxPointsAdded) {
NewGpxData.ActionType actionType = newGpxData.getActionType();
if (actionType == NewGpxData.ActionType.ADD_ROUTE_POINTS) {
displayRoutePoints();
gpxPointsAdded = true;
} else if (actionType == NewGpxData.ActionType.EDIT_SEGMENT) {
displaySegmentPoints();
gpxPointsAdded = true;
}
}
if (saved == null) {
saved = newGpxData != null && (newGpxData.getActionType() == NewGpxData.ActionType.ADD_ROUTE_POINTS || newGpxData.getActionType() == NewGpxData.ActionType.EDIT_SEGMENT);
}
return view;
}
Aggregations