diff --git a/OsmAnd/res/layout/bottom_sheet_button.xml b/OsmAnd/res/layout/bottom_sheet_button.xml new file mode 100644 index 0000000000..04affc5879 --- /dev/null +++ b/OsmAnd/res/layout/bottom_sheet_button.xml @@ -0,0 +1,38 @@ + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/values/sizes.xml b/OsmAnd/res/values/sizes.xml index 1b0adf805a..a4d3e4dae6 100644 --- a/OsmAnd/res/values/sizes.xml +++ b/OsmAnd/res/values/sizes.xml @@ -242,6 +242,7 @@ 20dp 16dp + 18dp 8dp 16dp 12dp diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 25ee7cc407..cf0510f67b 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,8 +11,9 @@ Thx - Hardy --> - In case of reverse direction + Are you sure you want to close Plan route without saving? You will lost all changes. + Street-level imagery Select a track file for which a new segment will be added. Navigation profile Threshold distance diff --git a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java index d57467a7e9..1914756d21 100644 --- a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java +++ b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java @@ -21,7 +21,7 @@ public class BaseBottomSheetItem { protected int layoutId = INVALID_ID; private Object tag; private boolean disabled; - private View.OnClickListener onClickListener; + protected View.OnClickListener onClickListener; protected int position = INVALID_POSITION; public View getView() { diff --git a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BottomSheetItemButton.java b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BottomSheetItemButton.java new file mode 100644 index 0000000000..286ca832dc --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BottomSheetItemButton.java @@ -0,0 +1,76 @@ +package net.osmand.plus.base.bottomsheetmenu; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; + +import androidx.annotation.ColorRes; +import androidx.annotation.LayoutRes; + +import net.osmand.plus.R; + +import static net.osmand.plus.UiUtilities.*; +import static net.osmand.plus.UiUtilities.DialogButtonType.PRIMARY; + +public class BottomSheetItemButton extends SimpleBottomSheetItem { + + protected CharSequence description; + + DialogButtonType buttonType; + + LinearLayout buttonView; + + public BottomSheetItemButton(View customView, + @LayoutRes int layoutId, + Object tag, + boolean disabled, + View.OnClickListener onClickListener, + int position, + Drawable icon, + Drawable background, + CharSequence title, + @ColorRes int titleColorId, + boolean iconHidden, + DialogButtonType buttonType) { + super(customView, layoutId, tag, disabled, onClickListener, position, icon, background, title, + titleColorId, iconHidden); + this.buttonType = buttonType; + } + + @Override + public void inflate(Context context, ViewGroup container, boolean nightMode) { + super.inflate(context, container, nightMode); + buttonView = view.findViewById(R.id.button); + if (buttonView != null) { + setupDialogButton(nightMode, buttonView, buttonType, title); + buttonView.setOnClickListener(onClickListener); + } + } + + public static class Builder extends SimpleBottomSheetItem.Builder { + + protected DialogButtonType buttonType = PRIMARY; + + public Builder setButtonType(DialogButtonType buttonType) { + this.buttonType = buttonType; + return this; + } + + public BottomSheetItemButton create() { + return new BottomSheetItemButton(customView, + layoutId, + tag, + disabled, + onClickListener, + position, + icon, + background, + title, + titleColorId, + iconHidden, + buttonType); + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/ExitBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/ExitBottomSheetDialogFragment.java new file mode 100644 index 0000000000..a7c946cf00 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/measurementtool/ExitBottomSheetDialogFragment.java @@ -0,0 +1,88 @@ +package net.osmand.plus.measurementtool; + +import android.os.Bundle; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; + +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.base.MenuBottomSheetDialogFragment; +import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemButton; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.ShortDescriptionItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; + +public class ExitBottomSheetDialogFragment extends MenuBottomSheetDialogFragment { + + public static final int REQUEST_CODE = 1001; + public static final int SAVE_RESULT_CODE = 2; + public static final int EXIT_RESULT_CODE = 3; + + public static final String TAG = ExitBottomSheetDialogFragment.class.getSimpleName(); + + @Override + public void createMenuItems(Bundle savedInstanceState) { + + items.add(new TitleItem(getString(R.string.exit_without_saving))); + + items.add(new ShortDescriptionItem.Builder() + .setDescription(getString(R.string.plan_route_exit_dialog_descr)) + .setDescriptionColorId(nightMode ? R.color.text_color_primary_dark : R.color.text_color_primary_light) + .setLayoutId(R.layout.bottom_sheet_item_description_long) + .create()); + + items.add(new DividerSpaceItem(getContext(), + getResources().getDimensionPixelSize(R.dimen.bottom_sheet_exit_button_margin))); + + items.add(new BottomSheetItemButton.Builder() + .setButtonType(UiUtilities.DialogButtonType.SECONDARY) + .setTitle(getString(R.string.shared_string_exit)) + .setLayoutId(R.layout.bottom_sheet_button) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Fragment targetFragment = getTargetFragment(); + if (targetFragment != null) { + targetFragment.onActivityResult(REQUEST_CODE, EXIT_RESULT_CODE, null); + } + dismiss(); + } + }) + .create()); + + items.add(new DividerSpaceItem(getContext(), + getResources().getDimensionPixelSize(R.dimen.bottom_sheet_icon_margin))); + + items.add(new BottomSheetItemButton.Builder() + .setTitle(getString(R.string.shared_string_save)) + .setLayoutId(R.layout.bottom_sheet_button) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Fragment targetFragment = getTargetFragment(); + if (targetFragment != null) { + targetFragment.onActivityResult(REQUEST_CODE, SAVE_RESULT_CODE, null); + } + dismiss(); + } + }) + .create()); + } + + @Override + protected int getDismissButtonTextId() { + return R.string.shared_string_close; + } + + public static void showInstance(@NonNull FragmentManager fragmentManager, @Nullable Fragment targetFragment) { + if (!fragmentManager.isStateSaved()) { + ExitBottomSheetDialogFragment fragment = new ExitBottomSheetDialogFragment(); + fragment.setTargetFragment(targetFragment, REQUEST_CODE); + fragment.show(fragmentManager, TAG); + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index 1b289e0149..0d010bb209 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -161,6 +161,9 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final MapActivity mapActivity = (MapActivity) getActivity(); + if (mapActivity == null) { + return null; + } final MeasurementToolLayer measurementLayer = mapActivity.getMapLayers().getMeasurementToolLayer(); editingCtx.setApplication(mapActivity.getMyApplication()); @@ -221,7 +224,9 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route 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) { - final int backgroundColor = ContextCompat.getColor(mapActivity, nightMode ? R.color.activity_background_color_dark : R.color.activity_background_color_light); + final int backgroundColor = ContextCompat.getColor(mapActivity, nightMode + ? R.color.activity_background_color_dark + : R.color.activity_background_color_light); pointsListContainer.setBackgroundColor(backgroundColor); } @@ -423,13 +428,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route if (editingCtx.getPointsCount() > 0) { if (newGpxData != null && newGpxData.getActionType() == ActionType.EDIT_SEGMENT && editingCtx.isInSnapToRoadMode()) { - if (mapActivity != null) { - if (editingCtx.getPointsCount() > 0) { openSaveAsNewTrackMenu(mapActivity); - } else { - Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); - } - } } else { if (newGpxData == null) { final File dir = mapActivity.getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR); @@ -614,20 +613,31 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route @Override public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); - if (requestCode == SnapTrackWarningBottomSheet.REQUEST_CODE) { - switch (resultCode) { - case SnapTrackWarningBottomSheet.CANCEL_REQUEST_CODE: - toolBarController.setSaveViewVisible(true); - updateToolbar(); - break; - case SnapTrackWarningBottomSheet.CONTINUE_REQUEST_CODE: - MapActivity mapActivity = getMapActivity(); - if (mapActivity != null) { - GpxApproximationFragment.showInstance(mapActivity.getSupportFragmentManager(), - this, new LocationsHolder(editingCtx.getPoints())); - } - break; - } + switch (requestCode) { + case SnapTrackWarningBottomSheet.REQUEST_CODE: + switch (resultCode) { + case SnapTrackWarningBottomSheet.CANCEL_RESULT_CODE: + toolBarController.setSaveViewVisible(true); + updateToolbar(); + break; + case SnapTrackWarningBottomSheet.CONTINUE_RESULT_CODE: + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + GpxApproximationFragment.showInstance(mapActivity.getSupportFragmentManager(), + this, new LocationsHolder(editingCtx.getPoints())); + } + break; + } + break; + case ExitBottomSheetDialogFragment.REQUEST_CODE: + switch (resultCode) { + case ExitBottomSheetDialogFragment.EXIT_RESULT_CODE: + dismiss(getMapActivity()); + break; + case ExitBottomSheetDialogFragment.SAVE_RESULT_CODE: + openSaveAsNewTrackMenu(getMapActivity()); + break; + } } } @@ -673,17 +683,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route } } - @Override - public void saveAsNewTrackOnClick() { - MapActivity mapActivity = getMapActivity(); - if (mapActivity != null) { - if (editingCtx.getPointsCount() > 0) { - openSaveAsNewTrackMenu(mapActivity); - } else { - Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); + @Override + public void saveAsNewTrackOnClick() { + openSaveAsNewTrackMenu(getMapActivity()); } - } - } @Override public void addToTheTrackOnClick() { @@ -814,6 +817,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route @Override public void openLastEditTrackOnClick(String gpxFileName) { addNewGpxData(getGpxFile(gpxFileName)); + saved = true; } @Override @@ -828,6 +832,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route @Override public void selectFileOnCLick(String gpxFileName) { addNewGpxData(getGpxFile(gpxFileName)); + saved = true; } @Override @@ -1074,10 +1079,16 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route } private void openSaveAsNewTrackMenu(MapActivity mapActivity) { - SaveAsNewTrackBottomSheetDialogFragment fragment = new SaveAsNewTrackBottomSheetDialogFragment(); - fragment.setUsedOnMap(true); - fragment.setListener(createSaveAsNewTrackFragmentListener()); - fragment.show(mapActivity.getSupportFragmentManager(), SaveAsNewTrackBottomSheetDialogFragment.TAG); + if (mapActivity != null) { + if (editingCtx.getPointsCount() > 0) { + SaveAsNewTrackBottomSheetDialogFragment fragment = new SaveAsNewTrackBottomSheetDialogFragment(); + fragment.setUsedOnMap(true); + fragment.setListener(createSaveAsNewTrackFragmentListener()); + fragment.show(mapActivity.getSupportFragmentManager(), SaveAsNewTrackBottomSheetDialogFragment.TAG); + } else { + Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); + } + } } private void showAddToTrackDialog(final MapActivity mapActivity) { @@ -1779,51 +1790,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route dismiss(mapActivity); return; } - AlertDialog.Builder builder = new AlertDialog.Builder(UiUtilities.getThemedContext(mapActivity, nightMode)); - if (editingCtx.isNewData()) { - final File dir = mapActivity.getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR); - final View view = UiUtilities.getInflater(mapActivity, nightMode).inflate(R.layout.close_measurement_tool_dialog, null); - final SwitchCompat showOnMapToggle = (SwitchCompat) view.findViewById(R.id.toggle_show_on_map); - - view.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - showOnMapToggle.setChecked(!showOnMapToggle.isChecked()); - } - }); - - builder.setView(view); - builder.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - if (showOnMapToggle.isChecked()) { - final String name = new SimpleDateFormat("yyyy-MM-dd_HH-mm_EEE", Locale.US).format(new Date()); - String fileName = name + GPX_FILE_EXT; - File fout = new File(dir, fileName); - int ind = 1; - while (fout.exists()) { - fileName = name + "_" + (++ind) + GPX_FILE_EXT; - fout = new File(dir, fileName); - } - saveNewGpx(dir, fileName, true, SaveType.LINE, true); - } else { - dismiss(mapActivity); - } - } - }); - UiUtilities.setupCompoundButton(showOnMapToggle, nightMode, UiUtilities.CompoundButtonType.GLOBAL); - } else { - builder.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - dismiss(mapActivity); - } - }); - } - builder.setTitle(getString(R.string.exit_without_saving)) - .setMessage(getString(R.string.unsaved_changes_will_be_lost)) - .setNegativeButton(R.string.shared_string_cancel, null); - builder.show(); + ExitBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), this); } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/SnapTrackWarningBottomSheet.java b/OsmAnd/src/net/osmand/plus/measurementtool/SnapTrackWarningBottomSheet.java index f91aba9464..666cb58b3f 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/SnapTrackWarningBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/SnapTrackWarningBottomSheet.java @@ -23,8 +23,8 @@ import org.apache.commons.logging.Log; public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment { public static final int REQUEST_CODE = 1000; - public static final int CANCEL_REQUEST_CODE = 2; - public static final int CONTINUE_REQUEST_CODE = 3; + public static final int CANCEL_RESULT_CODE = 2; + public static final int CONTINUE_RESULT_CODE = 3; public static final String TAG = SnapTrackWarningBottomSheet.class.getSimpleName(); private static final Log LOG = PlatformUtil.getLog(SnapTrackWarningBottomSheet.class); @@ -58,7 +58,7 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment { protected void onRightBottomButtonClick() { Fragment fragment = getTargetFragment(); if (fragment != null) { - fragment.onActivityResult(REQUEST_CODE, CONTINUE_REQUEST_CODE, null); + fragment.onActivityResult(REQUEST_CODE, CONTINUE_RESULT_CODE, null); } dismiss(); } @@ -77,7 +77,7 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment { } Fragment fragment = getTargetFragment(); if (fragment != null) { - fragment.onActivityResult(REQUEST_CODE, CANCEL_REQUEST_CODE, null); + fragment.onActivityResult(REQUEST_CODE, CANCEL_RESULT_CODE, null); } }