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 a9ab89109c..091f8fe562 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 a31be91aa8..428a0a4562 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,7 @@ Thx - Hardy --> + Are you sure you want to close Plan route without saving? You will lost all changes. Overwrite track Done Select a track file for open. 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..d32bc0ca0b --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/measurementtool/ExitBottomSheetDialogFragment.java @@ -0,0 +1,90 @@ +package net.osmand.plus.measurementtool; + +import android.os.Bundle; +import android.view.View; + +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 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 target = getTargetFragment(); + if (target instanceof ExitFragmentListener) { + ((ExitFragmentListener) target).exitOnClick(); + } + 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 target = getTargetFragment(); + if (target instanceof ExitFragmentListener) { + ((ExitFragmentListener) target).saveOnClick(); + } + dismiss(); + } + }) + .create()); + } + + @Override + protected int getDismissButtonTextId() { + return R.string.shared_string_close; + } + + public static void showInstance(FragmentManager fragmentManager, Fragment target) { + if (!fragmentManager.isStateSaved()) { + ExitBottomSheetDialogFragment fragment = new ExitBottomSheetDialogFragment(); + fragment.setUsedOnMap(true); + fragment.setTargetFragment(target, 0); + fragment.show(fragmentManager, TAG); + } + } + + interface ExitFragmentListener { + + void exitOnClick(); + + void saveOnClick(); + } +} diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index bc28b3995e..71fcfb71a6 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -78,7 +78,6 @@ import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.views.MapControlsLayer; import net.osmand.plus.views.controls.ReorderItemTouchHelperCallback; -import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControllerType; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarView; @@ -96,9 +95,10 @@ import static net.osmand.plus.measurementtool.SelectFileBottomSheet.SelectFileLi import static net.osmand.plus.measurementtool.StartPlanRouteBottomSheet.StartPlanRouteListener; -public class MeasurementToolFragment extends BaseOsmAndFragment { +public class MeasurementToolFragment extends BaseOsmAndFragment + implements ExitBottomSheetDialogFragment.ExitFragmentListener { - public static final String TAG = "MeasurementToolFragment"; + public static final String TAG = MeasurementToolFragment.class.getSimpleName(); private RecyclerView pointsRv; private String previousToolBarTitle = ""; @@ -123,6 +123,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { private boolean progressBarVisible; private boolean pointsListOpened; private boolean planRouteMode = false; + private boolean firstShow = true; private Boolean saved; private boolean portrait; private boolean nightMode; @@ -154,6 +155,9 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { @Override 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()); @@ -434,13 +438,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { 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); @@ -480,9 +478,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { initMeasurementMode(newGpxData); - if (planRouteMode) { + if (planRouteMode && firstShow) { StartPlanRouteBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), createStartPlanRouteListener()); + firstShow = false; } return view; } @@ -582,6 +581,16 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { return getIcon(id, nightMode ? R.color.osmand_orange : R.color.color_myloc_distance); } + @Override + public void exitOnClick() { + dismiss(getMapActivity()); + } + + @Override + public void saveOnClick() { + openSaveAsNewTrackMenu(getMapActivity()); + } + private void showProgressBar() { ProgressBar progressBar = (ProgressBar) mainView.findViewById(R.id.snap_to_road_progress_bar); progressBar.setVisibility(View.VISIBLE); @@ -647,13 +656,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { @Override public void saveAsNewTrackOnClick() { - if (mapActivity != null && measurementLayer != null) { - if (editingCtx.getPointsCount() > 0) { openSaveAsNewTrackMenu(mapActivity); - } else { - Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); - } - } } @Override @@ -1006,10 +1009,16 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { } 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 AlertDialog showAddToTrackDialog(final MapActivity mapActivity) { @@ -1718,51 +1727,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { dismiss(mapActivity); return; } - AlertDialog.Builder builder = new AlertDialog.Builder(UiUtilities.getThemedContext(mapActivity, nightMode)); - if (editingCtx.getNewGpxData() == null) { - 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); } }