merge ui and backend fixes

This commit is contained in:
nazar-kutz 2021-01-18 11:32:15 +02:00
parent a2dd0425de
commit 29daef5594
2 changed files with 45 additions and 89 deletions

View file

@ -192,4 +192,14 @@ public abstract class OnlineRoutingEngine implements Cloneable {
public static String generateKey() { public static String generateKey() {
return ONLINE_ROUTING_ENGINE_PREFIX + System.currentTimeMillis(); return ONLINE_ROUTING_ENGINE_PREFIX + System.currentTimeMillis();
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof OnlineRoutingEngine)) return false;
OnlineRoutingEngine engine = (OnlineRoutingEngine) o;
if (getType() != engine.getType()) return false;
return Algorithms.objectEquals(getParams(), engine.getParams());
}
} }

View file

@ -62,13 +62,6 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
private static final String ENGINE_TYPE_KEY = "engine_type"; private static final String ENGINE_TYPE_KEY = "engine_type";
private static final String ENGINE_CUSTOM_VEHICLE_KEY = "engine_custom_vehicle"; private static final String ENGINE_CUSTOM_VEHICLE_KEY = "engine_custom_vehicle";
private static final String ENGINE_API_KEY_KEY = "engine_api_key";
private static final String INIT_ENGINE_NAME_KEY = "init_engine_name";
private static final String INIT_ENGINE_SERVER_KEY = "init_engine_server";
private static final String INIT_ENGINE_SERVER_URL_KEY = "init_engine_server_url";
private static final String INIT_ENGINE_VEHICLE_TYPE_KEY = "init_engine_vehicle_type";
private static final String INIT_ENGINE_CUSTOM_VEHICLE_KEY = "init_engine_custom_vehicle";
private static final String INIT_ENGINE_API_KEY_KEY = "init_engine_api_key";
private static final String EXAMPLE_LOCATION_KEY = "example_location"; private static final String EXAMPLE_LOCATION_KEY = "example_location";
private static final String APP_MODE_KEY = "app_mode"; private static final String APP_MODE_KEY = "app_mode";
private static final String EDITED_ENGINE_KEY = "edited_engine_key"; private static final String EDITED_ENGINE_KEY = "edited_engine_key";
@ -92,6 +85,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
private boolean isKeyboardShown = false; private boolean isKeyboardShown = false;
private OnlineRoutingEngine engine; private OnlineRoutingEngine engine;
private OnlineRoutingEngine initEngine;
private String customVehicleKey; private String customVehicleKey;
private ExampleLocation selectedLocation; private ExampleLocation selectedLocation;
private String editedEngineKey; private String editedEngineKey;
@ -208,23 +202,13 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
return view; return view;
} }
@Override
public void onDestroyView() {
super.onDestroyView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayout);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(onGlobalLayout);
}
}
private void setupToolbar(Toolbar toolbar) { private void setupToolbar(Toolbar toolbar) {
ImageView navigationIcon = toolbar.findViewById(R.id.close_button); ImageView navigationIcon = toolbar.findViewById(R.id.close_button);
navigationIcon.setImageResource(R.drawable.ic_action_close); navigationIcon.setImageResource(R.drawable.ic_action_close);
navigationIcon.setOnClickListener(new OnClickListener() { navigationIcon.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
dismiss(); showExitDialog();
} }
}); });
TextView title = toolbar.findViewById(R.id.toolbar_title); TextView title = toolbar.findViewById(R.id.toolbar_title);
@ -581,25 +565,11 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
exampleCard.setEditedText(getTestUrl()); exampleCard.setEditedText(getTestUrl());
} }
} }
engine.cloneIn(initEngine);
}
private void dismiss() {
FragmentActivity activity = getActivity();
if (activity != null) {
FragmentManager fragmentManager = activity.getSupportFragmentManager();
if (!fragmentManager.isStateSaved()) {
fragmentManager.popBackStack();
}
}
} }
public void showExitDialog() { public void showExitDialog() {
View focus = view.findFocus(); View focus = view.findFocus();
AndroidUtils.hideSoftKeyboard(mapActivity, focus); AndroidUtils.hideSoftKeyboard(mapActivity, focus);
if (engine.customName != null && initEngine.customName == null) {
initEngine.customName = initEngine.getName(app);
}
if (!engine.equals(initEngine)) { if (!engine.equals(initEngine)) {
AlertDialog.Builder dismissDialog = createWarningDialog(mapActivity, AlertDialog.Builder dismissDialog = createWarningDialog(mapActivity,
R.string.shared_string_dismiss, R.string.exit_without_saving, R.string.shared_string_cancel); R.string.shared_string_dismiss, R.string.exit_without_saving, R.string.shared_string_cancel);
@ -624,6 +594,16 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
return warningDialog; return warningDialog;
} }
private void dismiss() {
FragmentActivity activity = getActivity();
if (activity != null) {
FragmentManager fragmentManager = activity.getSupportFragmentManager();
if (!fragmentManager.isStateSaved()) {
fragmentManager.popBackStack();
}
}
}
private boolean isNightMode() { private boolean isNightMode() {
return !app.getSettings().isLightContentForMode(getAppMode()); return !app.getSettings().isLightContentForMode(getAppMode());
} }
@ -647,6 +627,16 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
return UiUtilities.getInflater(mapActivity, isNightMode()); return UiUtilities.getInflater(mapActivity, isNightMode());
} }
@Override
public void onDestroyView() {
super.onDestroyView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayout);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(onGlobalLayout);
}
}
@Override @Override
public void onSaveInstanceState(@NonNull Bundle outState) { public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
@ -668,6 +658,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
} }
private void restoreState(@NonNull Bundle savedState) { private void restoreState(@NonNull Bundle savedState) {
initEngine = createInitStateEngine();
String typeKey = savedState.getString(ENGINE_TYPE_KEY); String typeKey = savedState.getString(ENGINE_TYPE_KEY);
EngineType type = EngineType.getTypeByName(typeKey); EngineType type = EngineType.getTypeByName(typeKey);
engine = OnlineRoutingFactory.createEngine(type); engine = OnlineRoutingFactory.createEngine(type);
@ -684,22 +675,30 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
} }
private void initState() { private void initState() {
initEngine = createInitStateEngine();
selectedLocation = ExampleLocation.values()[0]; selectedLocation = ExampleLocation.values()[0];
engine = (OnlineRoutingEngine) initEngine.clone();
if (Algorithms.objectEquals(engine.getSelectedVehicleType(), CUSTOM_VEHICLE)) {
customVehicleKey = engine.get(EngineParameter.VEHICLE_KEY);
} else {
customVehicleKey = "";
}
}
private OnlineRoutingEngine createInitStateEngine() {
OnlineRoutingEngine engine;
OnlineRoutingEngine editedEngine = helper.getEngineByKey(editedEngineKey); OnlineRoutingEngine editedEngine = helper.getEngineByKey(editedEngineKey);
if (editedEngine != null) { if (editedEngine != null) {
engine = (OnlineRoutingEngine) editedEngine.clone(); engine = (OnlineRoutingEngine) editedEngine.clone();
if (Algorithms.objectEquals(engine.getSelectedVehicleType(), CUSTOM_VEHICLE)) {
customVehicleKey = engine.get(EngineParameter.VEHICLE_KEY);
}
} else { } else {
engine = OnlineRoutingFactory.createEngine(EngineType.values()[0]); engine = OnlineRoutingFactory.createEngine(EngineType.values()[0]);
String vehicle = engine.getAllowedVehicles().get(0).getKey(); String vehicle = engine.getAllowedVehicles().get(0).getKey();
engine.put(EngineParameter.VEHICLE_KEY, vehicle); engine.put(EngineParameter.VEHICLE_KEY, vehicle);
customVehicleKey = "";
if (editedEngineKey != null) { if (editedEngineKey != null) {
engine.put(EngineParameter.KEY, editedEngineKey); engine.put(EngineParameter.KEY, editedEngineKey);
} }
} }
return engine;
} }
public static void showInstance(@NonNull FragmentActivity activity, public static void showInstance(@NonNull FragmentActivity activity,
@ -715,57 +714,4 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
.addToBackStack(TAG).commitAllowingStateLoss(); .addToBackStack(TAG).commitAllowingStateLoss();
} }
} }
private static class OnlineRoutingEngineObject {
private String customName;
private EngineType type;
private String customServerUrl;
private VehicleType vehicleType;
private String customVehicleKey;
private String apiKey;
public String getVehicleKey() {
if (vehicleType == VehicleType.CUSTOM) {
return customVehicleKey;
}
return vehicleType.getKey();
}
public String getName(Context ctx) {
if (customName != null) {
return customName;
}
return OnlineRoutingEngine.getStandardName(ctx, type, getVehicleKey());
}
public String getBaseUrl() {
if (Algorithms.isEmpty(customServerUrl)) {
return type.getStandardUrl();
}
return customServerUrl;
}
public void cloneIn(OnlineRoutingEngineObject clone) {
clone.customName = customName;
clone.type = type;
clone.customServerUrl = customServerUrl;
clone.vehicleType = vehicleType;
clone.customVehicleKey = customVehicleKey;
clone.apiKey = apiKey;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
OnlineRoutingEngineObject engine = (OnlineRoutingEngineObject) obj;
if (!Algorithms.stringsEqual(customName, engine.customName)) return false;
if (type != engine.type) return false;
if (!Algorithms.stringsEqual(getBaseUrl(), engine.getBaseUrl())) return false;
if (vehicleType != engine.vehicleType) return false;
if (!Algorithms.stringsEqual(getVehicleKey(), engine.getVehicleKey())) return false;
return Algorithms.isEmpty(apiKey) ? Algorithms.isEmpty(engine.apiKey) : Algorithms.stringsEqual(apiKey, engine.apiKey);
}
}
} }