Fix listeners

This commit is contained in:
Dima-1 2020-08-06 12:40:09 +03:00
parent 8b58751667
commit fff9a415d5
8 changed files with 290 additions and 246 deletions

View file

@ -140,13 +140,13 @@ public abstract class ContextMenuScrollFragment extends ContextMenuFragment impl
return mapControlsContainer != null ? mapControlsContainer.getHeight() : 0; return mapControlsContainer != null ? mapControlsContainer.getHeight() : 0;
} }
public int getMapControlsVisibleMenuState() { public boolean shouldShowMapControls(int menuState) {
return MenuState.HEADER_ONLY; return menuState == MenuState.HEADER_ONLY;
} }
private void updateMapControlsVisibility(int menuState) { private void updateMapControlsVisibility(int menuState) {
if (mapBottomHudButtons != null) { if (mapBottomHudButtons != null) {
if ((menuState & getMapControlsVisibleMenuState()) != 0) { if (shouldShowMapControls(menuState)) {
if (mapBottomHudButtons.getVisibility() != View.VISIBLE) { if (mapBottomHudButtons.getVisibility() != View.VISIBLE) {
mapBottomHudButtons.setVisibility(View.VISIBLE); mapBottomHudButtons.setVisibility(View.VISIBLE);
} }

View file

@ -10,38 +10,33 @@ import android.widget.LinearLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.ContextMenuFragment;
import net.osmand.plus.base.ContextMenuScrollFragment; import net.osmand.plus.base.ContextMenuScrollFragment;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.ApplicationMode;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
public class GpxApproximationFragment extends ContextMenuScrollFragment { import static net.osmand.plus.measurementtool.ProfileCard.*;
import static net.osmand.plus.measurementtool.SliderCard.*;
public class GpxApproximationFragment extends ContextMenuScrollFragment implements SliderCardListener, ProfileCardListener {
private static final Log LOG = PlatformUtil.getLog(GpxApproximationFragment.class); private static final Log LOG = PlatformUtil.getLog(GpxApproximationFragment.class);
public static final String TAG = GpxApproximationFragment.class.getSimpleName(); public static final String TAG = GpxApproximationFragment.class.getSimpleName();
private GpxApproximationFragmentListener listener;
private int menuTitleHeight; private int menuTitleHeight;
private ApplicationMode snapToRoadAppMode; private ApplicationMode snapToRoadAppMode;
private int distanceThreshold; private int distanceThreshold;
public void setListener(GpxApproximationFragmentListener listener) {
this.listener = listener;
}
@Override @Override
public int getMainLayoutId() { public int getMainLayoutId() {
return R.layout.fragment_gpx_approximation_bottom_sheet_dialog; return R.layout.fragment_gpx_approximation_bottom_sheet_dialog;
@ -106,11 +101,12 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment {
@Override @Override
public void onDestroyView() { public void onDestroyView() {
if (listener != null) {
listener.onDestroyView();
}
super.onDestroyView(); super.onDestroyView();
exitGpxApproximationMode(); exitGpxApproximationMode();
Fragment fragment = getTargetFragment();
if (fragment instanceof GpxApproximationFragmentListener) {
((GpxApproximationFragmentListener) fragment).cancelButtonOnClick();
}
} }
private void updateCardsLayout() { private void updateCardsLayout() {
@ -137,11 +133,14 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment {
private void updateButtons(View view) { private void updateButtons(View view) {
View buttonsContainer = view.findViewById(R.id.buttons_container); View buttonsContainer = view.findViewById(R.id.buttons_container);
buttonsContainer.setBackgroundColor(AndroidUtils.getColorFromAttr(view.getContext(), R.attr.route_info_bg)); buttonsContainer.setBackgroundColor(AndroidUtils.getColorFromAttr(view.getContext(), R.attr.route_info_bg));
View saveButton = view.findViewById(R.id.right_bottom_button); View applyButton = view.findViewById(R.id.right_bottom_button);
saveButton.setOnClickListener(new View.OnClickListener() { applyButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// saveTrackInfo(); Fragment fragment = getTargetFragment();
if (fragment instanceof GpxApproximationFragmentListener) {
((GpxApproximationFragmentListener) fragment).applyButtonOnClick(snapToRoadAppMode, distanceThreshold);
}
dismiss(); dismiss();
} }
}); });
@ -150,7 +149,6 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment {
cancelButton.setOnClickListener(new View.OnClickListener() { cancelButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// discardSplitChanges();
FragmentActivity activity = getActivity(); FragmentActivity activity = getActivity();
if (activity != null) { if (activity != null) {
activity.onBackPressed(); activity.onBackPressed();
@ -159,9 +157,9 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment {
}); });
UiUtilities.setupDialogButton(isNightMode(), cancelButton, UiUtilities.DialogButtonType.SECONDARY, R.string.shared_string_cancel); UiUtilities.setupDialogButton(isNightMode(), cancelButton, UiUtilities.DialogButtonType.SECONDARY, R.string.shared_string_cancel);
UiUtilities.setupDialogButton(isNightMode(), saveButton, UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_apply); UiUtilities.setupDialogButton(isNightMode(), applyButton, UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_apply);
AndroidUiHelper.updateVisibility(saveButton, true); AndroidUiHelper.updateVisibility(applyButton, true);
AndroidUiHelper.updateVisibility(view.findViewById(R.id.buttons_divider), true); AndroidUiHelper.updateVisibility(view.findViewById(R.id.buttons_divider), true);
} }
@ -172,9 +170,12 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment {
cardsContainer.removeAllViews(); cardsContainer.removeAllViews();
SliderCard sliderCard = new SliderCard(mapActivity); SliderCard sliderCard = new SliderCard(mapActivity);
sliderCard.setListener(this);
cardsContainer.addView(sliderCard.build(mapActivity)); cardsContainer.addView(sliderCard.build(mapActivity));
ProfileCard profileCard = new ProfileCard(mapActivity); ProfileCard profileCard = new ProfileCard(mapActivity);
profileCard.setListener(this);
cardsContainer.addView(profileCard.build(mapActivity)); cardsContainer.addView(profileCard.build(mapActivity));
} }
} }
@ -202,21 +203,15 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment {
} }
@Override @Override
public int getMapControlsVisibleMenuState() { public boolean shouldShowMapControls(int menuState) {
return MenuState.HEADER_ONLY | MenuState.HALF_SCREEN; return (menuState & (MenuState.HEADER_ONLY | MenuState.HALF_SCREEN)) != 0;
} }
// public static void showInstance(FragmentManager fm, RouteBetweenPointsFragmentListener listener, public static void showInstance(FragmentManager fm, Fragment targetFragment) {
// CalculationType calculationType, ApplicationMode applicationMode) {
public static void showInstance(FragmentManager fm) {
try { try {
if (!fm.isStateSaved()) { if (!fm.isStateSaved()) {
GpxApproximationFragment fragment = new GpxApproximationFragment(); GpxApproximationFragment fragment = new GpxApproximationFragment();
// fragment.setListener(listener); fragment.setTargetFragment(targetFragment, 0);
fragment.setRetainInstance(true);
Bundle args = new Bundle();
args.putInt(ContextMenuFragment.MENU_STATE_KEY, MenuController.MenuState.HALF_SCREEN);
fragment.setArguments(args);
fm.beginTransaction() fm.beginTransaction()
.replace(R.id.fragmentContainer, fragment, TAG) .replace(R.id.fragmentContainer, fragment, TAG)
.addToBackStack(TAG) .addToBackStack(TAG)
@ -238,10 +233,30 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment {
} }
} }
@Override
public void onSliderChange(int sliderValue) {
Fragment fragment = getTargetFragment();
if (fragment instanceof GpxApproximationFragmentListener) {
((GpxApproximationFragmentListener) fragment).onParametersChanged(snapToRoadAppMode, sliderValue);
distanceThreshold = sliderValue;
}
}
@Override
public void onProfileSelect(ApplicationMode applicationMode) {
Fragment fragment = getTargetFragment();
if (fragment instanceof GpxApproximationFragmentListener) {
((GpxApproximationFragmentListener) fragment).onParametersChanged(applicationMode, distanceThreshold);
snapToRoadAppMode = applicationMode;
}
}
public interface GpxApproximationFragmentListener { public interface GpxApproximationFragmentListener {
void onDestroyView(); void onParametersChanged(ApplicationMode mode, int distanceThreshold);
void applyButtonOnClick(ApplicationMode mode, int distanceThreshold); void applyButtonOnClick(ApplicationMode mode, int distanceThreshold);
void cancelButtonOnClick();
} }
} }

View file

@ -90,6 +90,7 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import static net.osmand.IndexConstants.GPX_FILE_EXT; import static net.osmand.IndexConstants.GPX_FILE_EXT;
import static net.osmand.plus.measurementtool.GpxApproximationFragment.*;
import static net.osmand.plus.measurementtool.MeasurementEditingContext.CalculationType; import static net.osmand.plus.measurementtool.MeasurementEditingContext.CalculationType;
import static net.osmand.plus.measurementtool.MeasurementEditingContext.ExportAsGpxListener; import static net.osmand.plus.measurementtool.MeasurementEditingContext.ExportAsGpxListener;
import static net.osmand.plus.measurementtool.MeasurementEditingContext.SnapToRoadProgressListener; import static net.osmand.plus.measurementtool.MeasurementEditingContext.SnapToRoadProgressListener;
@ -99,7 +100,9 @@ import static net.osmand.plus.measurementtool.SelectFileBottomSheet.SelectFileLi
import static net.osmand.plus.measurementtool.SnapTrackWarningBottomSheet.SnapTrackWarningListener; import static net.osmand.plus.measurementtool.SnapTrackWarningBottomSheet.SnapTrackWarningListener;
import static net.osmand.plus.measurementtool.StartPlanRouteBottomSheet.StartPlanRouteListener; import static net.osmand.plus.measurementtool.StartPlanRouteBottomSheet.StartPlanRouteListener;
public class MeasurementToolFragment extends BaseOsmAndFragment { public class MeasurementToolFragment extends BaseOsmAndFragment
implements RouteBetweenPointsFragmentListener, GpxApproximationFragmentListener, OptionsFragmentListener,
SnapTrackWarningListener {
public static final String TAG = MeasurementToolFragment.class.getSimpleName(); public static final String TAG = MeasurementToolFragment.class.getSimpleName();
@ -126,7 +129,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
private boolean progressBarVisible; private boolean progressBarVisible;
private boolean pointsListOpened; private boolean pointsListOpened;
private boolean planRouteMode = false; private boolean planRouteMode = false;
private boolean firstShow = true;
private Boolean saved; private Boolean saved;
private boolean portrait; private boolean portrait;
private boolean nightMode; private boolean nightMode;
@ -156,7 +158,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
final MapActivity mapActivity = (MapActivity) getActivity(); final MapActivity mapActivity = (MapActivity) getActivity();
final MeasurementToolLayer measurementLayer = mapActivity.getMapLayers().getMeasurementToolLayer(); final MeasurementToolLayer measurementLayer = mapActivity.getMapLayers().getMeasurementToolLayer();
@ -211,7 +214,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
downIcon = getContentIcon(R.drawable.ic_action_arrow_down); downIcon = getContentIcon(R.drawable.ic_action_arrow_down);
pointsSt = getString(R.string.shared_string_gpx_points).toLowerCase(); pointsSt = getString(R.string.shared_string_gpx_points).toLowerCase();
View view = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.fragment_measurement_tool, null); View view = UiUtilities.getInflater(getContext(), nightMode)
.inflate(R.layout.fragment_measurement_tool, container, false);
mainView = view.findViewById(R.id.main_view); mainView = view.findViewById(R.id.main_view);
AndroidUtils.setBackground(mapActivity, mainView, nightMode, R.drawable.bg_bottom_menu_light, R.drawable.bg_bottom_menu_dark); AndroidUtils.setBackground(mapActivity, mainView, nightMode, R.drawable.bg_bottom_menu_light, R.drawable.bg_bottom_menu_dark);
@ -284,9 +288,12 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
OptionsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), OptionsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(),
MeasurementToolFragment.this,
editingCtx.isSnapToRoadTrack() || editingCtx.isNewData(), editingCtx.isSnapToRoadTrack() || editingCtx.isNewData(),
editingCtx.isInSnapToRoadMode(), editingCtx.isInSnapToRoadMode(),
editingCtx.getSnapToRoadAppMode(), createOptionsFragmentListener() editingCtx.getSnapToRoadAppMode() != null
? editingCtx.getSnapToRoadAppMode().getStringKey()
: null
); );
} }
}); });
@ -462,10 +469,9 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
initMeasurementMode(newGpxData); initMeasurementMode(newGpxData);
if (planRouteMode && firstShow) { if (planRouteMode && savedInstanceState == null) {
StartPlanRouteBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), StartPlanRouteBottomSheet.showInstance(mapActivity.getSupportFragmentManager(),
createStartPlanRouteListener()); createStartPlanRouteListener());
firstShow = false;
} }
return view; return view;
} }
@ -473,9 +479,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
private void initMeasurementMode(NewGpxData newGpxData) { private void initMeasurementMode(NewGpxData newGpxData) {
editingCtx.getCommandManager().resetMeasurementLayer(getMapActivity().getMapLayers().getMeasurementToolLayer()); editingCtx.getCommandManager().resetMeasurementLayer(getMapActivity().getMapLayers().getMeasurementToolLayer());
enterMeasurementMode(); enterMeasurementMode();
showSnapToRoadControls(); showSnapToRoadControls();
if (newGpxData != null && !gpxPointsAdded) { if (newGpxData != null && !gpxPointsAdded) {
List<WptPt> points = newGpxData.getGpxFile().getRoutePoints(); List<WptPt> points = newGpxData.getGpxFile().getRoutePoints();
if (!points.isEmpty()) { if (!points.isEmpty()) {
@ -494,9 +498,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
gpxPointsAdded = true; gpxPointsAdded = true;
} }
} }
if (saved == null) { if (saved == null) {
saved = newGpxData != null && (newGpxData.getActionType() == ActionType.ADD_ROUTE_POINTS || newGpxData.getActionType() == ActionType.EDIT_SEGMENT); saved = newGpxData != null
&& (newGpxData.getActionType() == ActionType.ADD_ROUTE_POINTS
|| newGpxData.getActionType() == ActionType.EDIT_SEGMENT);
} }
} }
@ -539,6 +544,21 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
return R.color.status_bar_transparent_gradient; return R.color.status_bar_transparent_gradient;
} }
@Override
public void onParametersChanged(ApplicationMode mode, int distanceThreshold) {
}
@Override
public void applyButtonOnClick(ApplicationMode mode, int distanceThreshold) {
}
@Override
public void cancelButtonOnClick() {
}
@Nullable @Nullable
private MapActivity getMapActivity() { private MapActivity getMapActivity() {
Activity activity = getActivity(); Activity activity = getActivity();
@ -596,19 +616,18 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
toolBarController.setTitle(getString(R.string.route_between_points)); toolBarController.setTitle(getString(R.string.route_between_points));
mapActivity.refreshMap(); mapActivity.refreshMap();
RouteBetweenPointsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), RouteBetweenPointsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(),
createRouteBetweenPointsFragmentListener(), editingCtx.getCalculationType(), this, editingCtx.getCalculationType(),
editingCtx.getSnapToRoadAppMode()); editingCtx.getSnapToRoadAppMode());
} }
} }
private SnapTrackWarningListener createSnapTrackWarningListener() {
return new SnapTrackWarningListener() {
@Override @Override
public void continueButtonOnClick() { public void continueButtonOnClick() {
MapActivity mapActivity = getMapActivity(); MapActivity mapActivity = getMapActivity();
if (mapActivity != null) { if (mapActivity != null) {
GpxApproximationFragment.showInstance(mapActivity.getSupportFragmentManager()); GpxApproximationFragment.showInstance(mapActivity.getSupportFragmentManager(),
MeasurementToolFragment.this);
} }
} }
@ -617,14 +636,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
toolBarController.setSaveViewVisible(true); toolBarController.setSaveViewVisible(true);
updateToolbar(); updateToolbar();
} }
};
}
private OptionsFragmentListener createOptionsFragmentListener() {
return new OptionsFragmentListener() {
final MapActivity mapActivity = getMapActivity();
final MeasurementToolLayer measurementLayer = getMeasurementLayer();
@Override @Override
public void snapToRoadOnCLick() { public void snapToRoadOnCLick() {
@ -633,6 +644,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
@Override @Override
public void directionsOnClick() { public void directionsOnClick() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) { if (mapActivity != null) {
MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer(); MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer();
if (mapControlsLayer != null) { if (mapControlsLayer != null) {
@ -647,7 +659,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
@Override @Override
public void addToGpxOnClick() { public void addToGpxOnClick() {
if (mapActivity != null && measurementLayer != null) { MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (editingCtx.getPointsCount() > 0) { if (editingCtx.getPointsCount() > 0) {
if (editingCtx.isInSnapToRoadMode()) { if (editingCtx.isInSnapToRoadMode()) {
editingCtx.getPoints().clear(); editingCtx.getPoints().clear();
@ -668,7 +681,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
@Override @Override
public void saveAsNewTrackOnClick() { public void saveAsNewTrackOnClick() {
if (mapActivity != null && measurementLayer != null) { MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (editingCtx.getPointsCount() > 0) { if (editingCtx.getPointsCount() > 0) {
openSaveAsNewTrackMenu(mapActivity); openSaveAsNewTrackMenu(mapActivity);
} else { } else {
@ -679,7 +693,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
@Override @Override
public void addToTheTrackOnClick() { public void addToTheTrackOnClick() {
if (mapActivity != null && measurementLayer != null) { MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (editingCtx.getPointsCount() > 0) { if (editingCtx.getPointsCount() > 0) {
showAddToTrackDialog(mapActivity); showAddToTrackDialog(mapActivity);
} else { } else {
@ -690,6 +705,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
@Override @Override
public void clearAllOnClick() { public void clearAllOnClick() {
MeasurementToolLayer measurementLayer = getMeasurementLayer();
editingCtx.getCommandManager().execute(new ClearPointsCommand(measurementLayer)); editingCtx.getCommandManager().execute(new ClearPointsCommand(measurementLayer));
editingCtx.cancelSnapToRoad(); editingCtx.cancelSnapToRoad();
if (pointsListOpened) { if (pointsListOpened) {
@ -705,8 +721,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
public void reverseRouteOnClick() { public void reverseRouteOnClick() {
} }
};
}
private SelectedPointFragmentListener createSelectedPointFragmentListener() { private SelectedPointFragmentListener createSelectedPointFragmentListener() {
return new SelectedPointFragmentListener() { return new SelectedPointFragmentListener() {
@ -765,8 +779,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
}; };
} }
private RouteBetweenPointsFragmentListener createRouteBetweenPointsFragmentListener() {
return new RouteBetweenPointsFragmentListener() {
@Override @Override
public void onDestroyView(boolean snapToRoadEnabled) { public void onDestroyView(boolean snapToRoadEnabled) {
if (!snapToRoadEnabled && !editingCtx.isInSnapToRoadMode()) { if (!snapToRoadEnabled && !editingCtx.isInSnapToRoadMode()) {
@ -793,8 +805,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
public void onCalculationTypeChanges(CalculationType calculationType) { public void onCalculationTypeChanges(CalculationType calculationType) {
editingCtx.setCalculationType(calculationType); editingCtx.setCalculationType(calculationType);
} }
};
}
private StartPlanRouteListener createStartPlanRouteListener() { private StartPlanRouteListener createStartPlanRouteListener() {
return new StartPlanRouteListener() { return new StartPlanRouteListener() {

View file

@ -8,6 +8,7 @@ import android.widget.ImageView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
@ -28,18 +29,10 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
public static final String SNAP_TO_ROAD_ENABLED_KEY = "snap_to_road_enabled"; public static final String SNAP_TO_ROAD_ENABLED_KEY = "snap_to_road_enabled";
public static final String TRACK_SNAPPED_TO_ROAD_KEY = "track_snapped_to_road"; public static final String TRACK_SNAPPED_TO_ROAD_KEY = "track_snapped_to_road";
public static final String SNAP_TO_ROAD_APP_MODE_KEY = "snap_to_road_app_mode";
private OptionsFragmentListener listener;
private ApplicationMode routeAppMode; private ApplicationMode routeAppMode;
public void setListener(OptionsFragmentListener listener) {
this.listener = listener;
}
private void setRouteAppMode(ApplicationMode routeAppMode) {
this.routeAppMode = routeAppMode;
}
@Override @Override
public void createMenuItems(Bundle savedInstanceState) { public void createMenuItems(Bundle savedInstanceState) {
Bundle args = getArguments(); Bundle args = getArguments();
@ -48,6 +41,7 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
if (args != null) { if (args != null) {
snapToRoadEnabled = args.getBoolean(SNAP_TO_ROAD_ENABLED_KEY); snapToRoadEnabled = args.getBoolean(SNAP_TO_ROAD_ENABLED_KEY);
trackSnappedToRoad = args.getBoolean(TRACK_SNAPPED_TO_ROAD_KEY); trackSnappedToRoad = args.getBoolean(TRACK_SNAPPED_TO_ROAD_KEY);
routeAppMode = ApplicationMode.valueOfStringKey(args.getString(SNAP_TO_ROAD_APP_MODE_KEY), null);
} }
items.add(new TitleItem(getString(R.string.shared_string_options))); items.add(new TitleItem(getString(R.string.shared_string_options)));
@ -75,8 +69,9 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
.setOnClickListener(new View.OnClickListener() { .setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (listener != null) { Fragment fragment = getTargetFragment();
listener.snapToRoadOnCLick(); if (fragment instanceof OptionsFragmentListener) {
((OptionsFragmentListener) fragment).snapToRoadOnCLick();
} }
dismiss(); dismiss();
} }
@ -94,8 +89,9 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
.setOnClickListener(new View.OnClickListener() { .setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (listener != null) { Fragment fragment = getTargetFragment();
listener.addToGpxOnClick(); if (fragment instanceof OptionsFragmentListener) {
((OptionsFragmentListener) fragment).addToGpxOnClick();
} }
dismiss(); dismiss();
} }
@ -112,8 +108,9 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
.setOnClickListener(new View.OnClickListener() { .setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (listener != null) { Fragment fragment = getTargetFragment();
listener.addToTheTrackOnClick(); if (fragment instanceof OptionsFragmentListener) {
((OptionsFragmentListener) fragment).addToTheTrackOnClick();
} }
dismiss(); dismiss();
} }
@ -131,8 +128,9 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
.setOnClickListener(new View.OnClickListener() { .setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (listener != null) { Fragment fragment = getTargetFragment();
listener.clearAllOnClick(); if (fragment instanceof OptionsFragmentListener) {
((OptionsFragmentListener) fragment).clearAllOnClick();
} }
dismiss(); dismiss();
} }
@ -149,8 +147,9 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
.setOnClickListener(new View.OnClickListener() { .setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (listener != null) { Fragment fragment = getTargetFragment();
listener.saveAsNewTrackOnClick(); if (fragment instanceof OptionsFragmentListener) {
((OptionsFragmentListener) fragment).saveAsNewTrackOnClick();
} }
dismiss(); dismiss();
} }
@ -166,18 +165,17 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
params.rightMargin = view.getContext().getResources().getDimensionPixelSize(R.dimen.bottom_sheet_icon_margin_large); params.rightMargin = view.getContext().getResources().getDimensionPixelSize(R.dimen.bottom_sheet_icon_margin_large);
} }
public static void showInstance(@NonNull FragmentManager fm, boolean trackSnappedToRoad, boolean snapToRoad, public static void showInstance(@NonNull FragmentManager fm, Fragment targetFragment, boolean trackSnappedToRoad,
ApplicationMode routeAppMode, OptionsFragmentListener optionsFragmentListener) { boolean snapToRoad, String routeAppModeStringKey) {
try { try {
if (!fm.isStateSaved()) { if (!fm.isStateSaved()) {
OptionsBottomSheetDialogFragment fragment = new OptionsBottomSheetDialogFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putBoolean(TRACK_SNAPPED_TO_ROAD_KEY, trackSnappedToRoad); args.putBoolean(TRACK_SNAPPED_TO_ROAD_KEY, trackSnappedToRoad);
args.putBoolean(SNAP_TO_ROAD_ENABLED_KEY, snapToRoad); args.putBoolean(SNAP_TO_ROAD_ENABLED_KEY, snapToRoad);
OptionsBottomSheetDialogFragment fragment = new OptionsBottomSheetDialogFragment(); args.putString(SNAP_TO_ROAD_APP_MODE_KEY, routeAppModeStringKey);
fragment.setArguments(args); fragment.setArguments(args);
fragment.setRouteAppMode(routeAppMode); fragment.setTargetFragment(targetFragment,0);
fragment.setUsedOnMap(true);
fragment.setListener(optionsFragmentListener);
fragment.show(fm, TAG); fragment.show(fm, TAG);
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {

View file

@ -19,17 +19,13 @@ import java.util.List;
public class ProfileCard extends BaseCard { public class ProfileCard extends BaseCard {
public static final int INIT_MODE = 0; private ApplicationMode selectedMode;
ApplicationMode selectedMode; private ProfileCardListener listener;
public ProfileCard(MapActivity mapActivity) { public ProfileCard(MapActivity mapActivity) {
super(mapActivity); super(mapActivity);
} }
public ApplicationMode getSelectedMode() {
return selectedMode;
}
@Override @Override
public int getCardLayoutId() { public int getCardLayoutId() {
return R.layout.navigation_profiles_card; return R.layout.navigation_profiles_card;
@ -52,6 +48,9 @@ public class ProfileCard extends BaseCard {
selectedMode = modes.get((Integer) v.getTag()); selectedMode = modes.get((Integer) v.getTag());
clearChecked(); clearChecked();
selectedProfile.setChecked(true); selectedProfile.setChecked(true);
if (listener != null) {
listener.onProfileSelect(selectedMode);
}
} }
private void clearChecked() { private void clearChecked() {
@ -63,12 +62,12 @@ public class ProfileCard extends BaseCard {
}; };
addProfileView(container, onClickListener, i, icon, title); addProfileView(container, onClickListener, i, icon, title);
} }
initSelected(modes); resetSelected(modes);
} }
private void initSelected(List<ApplicationMode> modes) { private void resetSelected(List<ApplicationMode> modes) {
selectedMode = modes.get(INIT_MODE); selectedMode = modes.get(0);
((RadioButton) view.findViewWithTag(INIT_MODE).findViewById(R.id.compound_button)).setChecked(true); ((RadioButton) view.findViewWithTag(0).findViewById(R.id.compound_button)).setChecked(true);
} }
private void addProfileView(LinearLayout container, View.OnClickListener onClickListener, Object tag, private void addProfileView(LinearLayout container, View.OnClickListener onClickListener, Object tag,
@ -84,4 +83,14 @@ public class ProfileCard extends BaseCard {
row.setTag(tag); row.setTag(tag);
container.addView(row); container.addView(row);
} }
public void setListener(ProfileCardListener listener) {
this.listener = listener;
}
interface ProfileCardListener {
void onProfileSelect(ApplicationMode applicationMode);
}
} }

View file

@ -19,6 +19,7 @@ import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -45,8 +46,9 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial
private static final Log LOG = PlatformUtil.getLog(RouteBetweenPointsBottomSheetDialogFragment.class); private static final Log LOG = PlatformUtil.getLog(RouteBetweenPointsBottomSheetDialogFragment.class);
public static final String TAG = RouteBetweenPointsBottomSheetDialogFragment.class.getSimpleName(); public static final String TAG = RouteBetweenPointsBottomSheetDialogFragment.class.getSimpleName();
public static final int STRAIGHT_LINE_TAG = -1; public static final int STRAIGHT_LINE_TAG = -1;
public static final String CALCULATION_TYPE_KEY = "calculation_type";
public static final String ROUTE_APP_MODE_KEY = "route_app_mode";
private RouteBetweenPointsFragmentListener listener;
private boolean nightMode; private boolean nightMode;
private boolean portrait; private boolean portrait;
private boolean snapToRoadEnabled; private boolean snapToRoadEnabled;
@ -58,13 +60,14 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial
private CalculationType calculationType = WHOLE_TRACK; private CalculationType calculationType = WHOLE_TRACK;
private ApplicationMode snapToRoadAppMode; private ApplicationMode snapToRoadAppMode;
public void setListener(RouteBetweenPointsFragmentListener listener) {
this.listener = listener;
}
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle args = getArguments();
if (args != null) {
snapToRoadAppMode = ApplicationMode.valueOfStringKey(args.getString(ROUTE_APP_MODE_KEY), null);
calculationType = (CalculationType) args.get(CALCULATION_TYPE_KEY);
}
OsmandApplication app = requiredMyApplication(); OsmandApplication app = requiredMyApplication();
nightMode = app.getDaynightHelper().isNightModeForMapControls(); nightMode = app.getDaynightHelper().isNightModeForMapControls();
FragmentActivity activity = requireActivity(); FragmentActivity activity = requireActivity();
@ -90,7 +93,7 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial
wholeTrackBtn = mainView.findViewById(R.id.whole_track_btn); wholeTrackBtn = mainView.findViewById(R.id.whole_track_btn);
btnDescription = mainView.findViewById(R.id.button_description); btnDescription = mainView.findViewById(R.id.button_description);
LinearLayout navigationType = (LinearLayout) mainView.findViewById(R.id.navigation_types_container); LinearLayout navigationType = mainView.findViewById(R.id.navigation_types_container);
final List<ApplicationMode> modes = new ArrayList<>(ApplicationMode.values(app)); final List<ApplicationMode> modes = new ArrayList<>(ApplicationMode.values(app));
modes.remove(ApplicationMode.DEFAULT); modes.remove(ApplicationMode.DEFAULT);
@ -98,13 +101,13 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial
@Override @Override
public void onClick(View view) { public void onClick(View view) {
snapToRoadEnabled = false; snapToRoadEnabled = false;
if (listener != null) {
ApplicationMode mode = null;
if ((int) view.getTag() != STRAIGHT_LINE_TAG) { if ((int) view.getTag() != STRAIGHT_LINE_TAG) {
mode = modes.get((int) view.getTag()); ApplicationMode mode = modes.get((int) view.getTag());
snapToRoadEnabled = true; snapToRoadEnabled = true;
Fragment fragment = getTargetFragment();
if (fragment instanceof RouteBetweenPointsFragmentListener) {
((RouteBetweenPointsFragmentListener) fragment).onApplicationModeItemClick(mode);
} }
listener.onApplicationModeItemClick(mode);
} }
dismiss(); dismiss();
} }
@ -176,8 +179,9 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial
btnDescription.setText(R.string.rourte_between_points_whole_track_button_desc); btnDescription.setText(R.string.rourte_between_points_whole_track_button_desc);
} }
setCalculationType(calculationType); setCalculationType(calculationType);
if (listener != null) { Fragment fragment = getTargetFragment();
listener.onCalculationTypeChanges(calculationType); if (fragment instanceof RouteBetweenPointsFragmentListener) {
((RouteBetweenPointsFragmentListener) fragment).onCalculationTypeChanges(calculationType);
} }
} }
@ -212,29 +216,27 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial
@Override @Override
public void onDestroyView() { public void onDestroyView() {
if (listener != null) { Fragment fragment = getTargetFragment();
listener.onDestroyView(snapToRoadEnabled); if (fragment instanceof RouteBetweenPointsFragmentListener) {
((RouteBetweenPointsFragmentListener) fragment).onDestroyView(snapToRoadEnabled);
} }
super.onDestroyView(); super.onDestroyView();
} }
public void setRouteMode(ApplicationMode snapToRoadAppMode) {
this.snapToRoadAppMode = snapToRoadAppMode;
}
public void setCalculationType(CalculationType calculationType) { public void setCalculationType(CalculationType calculationType) {
this.calculationType = calculationType; this.calculationType = calculationType;
} }
public static void showInstance(FragmentManager fm, RouteBetweenPointsFragmentListener listener, public static void showInstance(FragmentManager fm, Fragment targetFragment, CalculationType calculationType,
CalculationType calculationType, ApplicationMode applicationMode) { ApplicationMode applicationMode) {
try { try {
if (!fm.isStateSaved()) { if (!fm.isStateSaved()) {
RouteBetweenPointsBottomSheetDialogFragment fragment = new RouteBetweenPointsBottomSheetDialogFragment(); RouteBetweenPointsBottomSheetDialogFragment fragment = new RouteBetweenPointsBottomSheetDialogFragment();
fragment.setListener(listener); Bundle args = new Bundle();
fragment.setCalculationType(calculationType); args.putString(ROUTE_APP_MODE_KEY, applicationMode != null ? applicationMode.getStringKey() : null);
fragment.setRouteMode(applicationMode); args.putSerializable(CALCULATION_TYPE_KEY, calculationType);
fragment.setArguments(args);
fragment.setTargetFragment(targetFragment, 0);
fragment.show(fm, TAG); fragment.show(fm, TAG);
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {

View file

@ -16,6 +16,8 @@ public class SliderCard extends BaseCard {
public static final int DEFAULT_VALUE = 30; public static final int DEFAULT_VALUE = 30;
private SliderCardListener listener;
public SliderCard(MapActivity mapActivity) { public SliderCard(MapActivity mapActivity) {
super(mapActivity); super(mapActivity);
} }
@ -44,13 +46,26 @@ public class SliderCard extends BaseCard {
if (fromUser) { if (fromUser) {
String valueStr = getStringValueWithMetric((int) value); String valueStr = getStringValueWithMetric((int) value);
thresholdDistanceValue.setText(valueStr); thresholdDistanceValue.setText(valueStr);
if (listener != null) {
listener.onSliderChange((int) value);
}
} }
} }
}); });
} }
public void setListener(SliderCardListener listener) {
this.listener = listener;
}
private String getStringValueWithMetric(int value) { private String getStringValueWithMetric(int value) {
return String.format(view.getContext().getString(R.string.ltr_or_rtl_combine_via_space), return String.format(view.getContext().getString(R.string.ltr_or_rtl_combine_via_space),
String.valueOf(value), view.getContext().getString(R.string.m)); String.valueOf(value), view.getContext().getString(R.string.m));
} }
interface SliderCardListener {
void onSliderChange(int sliderValue);
}
} }

View file

@ -4,6 +4,7 @@ import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
@ -26,11 +27,6 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment {
protected View mainView; protected View mainView;
protected GpxTrackAdapter adapter; protected GpxTrackAdapter adapter;
private SnapTrackWarningListener listener;
public void setListener(SnapTrackWarningListener listener) {
this.listener = listener;
}
@Override @Override
public void createMenuItems(Bundle savedInstanceState) { public void createMenuItems(Bundle savedInstanceState) {
@ -49,22 +45,6 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment {
items.add(new DividerSpaceItem(app, app.getResources().getDimensionPixelSize(R.dimen.content_padding_half))); items.add(new DividerSpaceItem(app, app.getResources().getDimensionPixelSize(R.dimen.content_padding_half)));
} }
public static void showInstance(FragmentManager fm, SnapTrackWarningListener listener) {
try {
if (!fm.isStateSaved()) {
SnapTrackWarningBottomSheet fragment = new SnapTrackWarningBottomSheet();
fragment.setUsedOnMap(true);
fragment.setRetainInstance(true);
fragment.setListener(listener);
fm.beginTransaction()
.add(R.id.bottomFragmentContainer, fragment, TAG)
.commitAllowingStateLoss();
}
} catch (RuntimeException e) {
LOG.error("showInstance", e);
}
}
@Override @Override
protected int getRightBottomButtonTextId() { protected int getRightBottomButtonTextId() {
return R.string.shared_string_continue; return R.string.shared_string_continue;
@ -72,8 +52,9 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment {
@Override @Override
protected void onRightBottomButtonClick() { protected void onRightBottomButtonClick() {
if (listener != null) { Fragment fragment = getTargetFragment();
listener.continueButtonOnClick(); if (fragment instanceof SnapTrackWarningListener) {
((SnapTrackWarningListener) fragment).continueButtonOnClick();
} }
dismiss(); dismiss();
} }
@ -83,7 +64,6 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment {
return R.string.shared_string_cancel; return R.string.shared_string_cancel;
} }
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
@ -91,8 +71,23 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment {
if (activity instanceof MapActivity) { if (activity instanceof MapActivity) {
activity.findViewById(R.id.snap_to_road_image_button).setVisibility(View.VISIBLE); activity.findViewById(R.id.snap_to_road_image_button).setVisibility(View.VISIBLE);
} }
if (listener != null) { Fragment fragment = getTargetFragment();
listener.dismissButtonOnClick(); if (fragment instanceof SnapTrackWarningListener) {
((SnapTrackWarningListener) fragment).dismissButtonOnClick();
}
}
public static void showInstance(FragmentManager fm, Fragment targetFragment) {
try {
if (!fm.isStateSaved()) {
SnapTrackWarningBottomSheet fragment = new SnapTrackWarningBottomSheet();
fragment.setTargetFragment(targetFragment, 0);
fm.beginTransaction()
.add(R.id.bottomFragmentContainer, fragment, TAG)
.commitAllowingStateLoss();
}
} catch (RuntimeException e) {
LOG.error("showInstance", e);
} }
} }