From 079da7b219729721ceb421d5805b00f31d623982 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 28 Dec 2020 12:42:31 +0200 Subject: [PATCH] Online Routing UI - refactoring part 1 --- .../online_routing_preference_segment.xml | 1 + .../osmand/plus/helpers/AndroidUiHelper.java | 8 +++ .../profiles/SelectProfileBottomSheet.java | 4 +- .../OnlineRoutingSegmentCard.java | 17 ++----- .../BasePreferenceBottomSheet.java | 2 +- .../fragments/GlobalSettingsFragment.java | 5 +- .../fragments/MainSettingsFragment.java | 5 +- .../fragments/NavigationFragment.java | 2 +- .../OnlineRoutingEngineFragment.java | 49 +++++++++++++------ .../fragments/ProfileAppearanceFragment.java | 2 +- 10 files changed, 59 insertions(+), 36 deletions(-) diff --git a/OsmAnd/res/layout/online_routing_preference_segment.xml b/OsmAnd/res/layout/online_routing_preference_segment.xml index 0b1799e478..bb52d533af 100644 --- a/OsmAnd/res/layout/online_routing_preference_segment.xml +++ b/OsmAnd/res/layout/online_routing_preference_segment.xml @@ -110,6 +110,7 @@ android:layout_height="wrap_content" android:inputType="textMultiLine" android:maxLines="4" + android:saveEnabled="false" android:scrollHorizontally="false" tools:text="Text" /> diff --git a/OsmAnd/src/net/osmand/plus/helpers/AndroidUiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/AndroidUiHelper.java index 45f5fe3ff4..9a038b787f 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AndroidUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AndroidUiHelper.java @@ -102,6 +102,14 @@ public class AndroidUiHelper { } } } + + public static void setVisibility(int visibility, View ... views) { + for (View view : views) { + if (view != null && view.getVisibility() != visibility) { + view.setVisibility(visibility); + } + } + } public static boolean isXLargeDevice(@NonNull Activity ctx) { int lt = (ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK); diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java index da92070b65..d86e1945f7 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java @@ -183,7 +183,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { @Override public void onClick(View v) { if (getActivity() != null) { - OnlineRoutingEngineFragment.showInstance(getActivity(), false); + OnlineRoutingEngineFragment.showInstance(getActivity(), getAppMode(), false); } dismiss(); } @@ -367,6 +367,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { public static void showInstance(@NonNull FragmentActivity activity, @NonNull DialogMode dialogMode, @Nullable Fragment target, + ApplicationMode appMode, String selectedItemKey, boolean usedOnMap) { SelectProfileBottomSheet fragment = new SelectProfileBottomSheet(); @@ -375,6 +376,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { args.putString(SELECTED_KEY, selectedItemKey); fragment.setArguments(args); fragment.setUsedOnMap(usedOnMap); + fragment.setAppMode(appMode); fragment.setTargetFragment(target, 0); fragment.show(activity.getSupportFragmentManager(), TAG); } diff --git a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java index 3ea6c4386f..75d90202b7 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java +++ b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java @@ -16,6 +16,7 @@ import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities.DialogButtonType; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionAdapterListener; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionItem; @@ -41,9 +42,9 @@ public class OnlineRoutingSegmentCard extends BaseCard { private View resultContainer; private OnTextChangedListener onTextChangedListener; - public OnlineRoutingSegmentCard(@NonNull MapActivity mapActivity) { + public OnlineRoutingSegmentCard(@NonNull MapActivity mapActivity, boolean nightMode) { super(mapActivity); - build(mapActivity); + this.nightMode = nightMode; } @Override @@ -157,19 +158,11 @@ public class OnlineRoutingSegmentCard extends BaseCard { } private void showElements(View... views) { - changeVisibility(View.VISIBLE, views); + AndroidUiHelper.setVisibility(View.VISIBLE, views); } private void hideElements(View... views) { - changeVisibility(View.GONE, views); - } - - private void changeVisibility(int visibility, View... views) { - for (View v : views) { - if (visibility != v.getVisibility()) { - v.setVisibility(visibility); - } - } + AndroidUiHelper.setVisibility(View.GONE, views); } public void setOnTextChangedListener(OnTextChangedListener onTextChangedListener) { diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheet.java index 6a71538ff1..67e2bfcfa0 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheet.java @@ -38,12 +38,12 @@ public abstract class BasePreferenceBottomSheet extends MenuBottomSheetDialogFra @Override public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); if (savedInstanceState != null) { appMode = ApplicationMode.valueOfStringKey(savedInstanceState.getString(APP_MODE_KEY), null); applyQueryType = ApplyQueryType.valueOf(savedInstanceState.getString(APPLY_QUERY_TYPE)); profileDependent = savedInstanceState.getBoolean(PROFILE_DEPENDENT, false); } + super.onCreate(savedInstanceState); } @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java index e957eb7748..fa7383228c 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java @@ -125,8 +125,9 @@ public class GlobalSettingsFragment extends BaseSettingsFragment if (prefId.equals(settings.DEFAULT_APPLICATION_MODE.getId())) { if (getActivity() != null) { String defaultModeKey = settings.DEFAULT_APPLICATION_MODE.get().getStringKey(); - SelectProfileBottomSheet.showInstance(getActivity(), - DialogMode.DEFAULT_PROFILE, this, defaultModeKey, false); + SelectProfileBottomSheet.showInstance( + getActivity(), DialogMode.DEFAULT_PROFILE, this, + getSelectedAppMode(), defaultModeKey, false); } } else if (settings.SPEED_CAMERAS_UNINSTALLED.getId().equals(prefId) && !settings.SPEED_CAMERAS_UNINSTALLED.get()) { FragmentManager fm = getFragmentManager(); diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/MainSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/MainSettingsFragment.java index f7ae122851..fdd5508352 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/MainSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/MainSettingsFragment.java @@ -119,8 +119,9 @@ public class MainSettingsFragment extends BaseSettingsFragment implements OnSele return true; } else if (CREATE_PROFILE.equals(prefId)) { if (getActivity() != null) { - SelectProfileBottomSheet.showInstance(getActivity(), - DialogMode.BASE_PROFILE, this, null, false); + SelectProfileBottomSheet.showInstance( + getActivity(), DialogMode.BASE_PROFILE, this, + getSelectedAppMode(), null, false); } } else if (IMPORT_PROFILE.equals(prefId)) { final MapActivity mapActivity = getMapActivity(); diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java index 0720dc7714..de8481f222 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java @@ -126,7 +126,7 @@ public class NavigationFragment extends BaseSettingsFragment implements OnSelect if (getActivity() != null) { SelectProfileBottomSheet.showInstance( getActivity(), SelectProfileBottomSheet.DialogMode.NAVIGATION_PROFILE, - this, routingProfileKey, false); + this, getSelectedAppMode(), routingProfileKey, false); } } return false; diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java index 44924f7352..07460805a2 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java @@ -31,6 +31,7 @@ import net.osmand.plus.profiles.onlinerouting.ServerType; import net.osmand.plus.profiles.onlinerouting.ExampleLocation; import net.osmand.plus.profiles.onlinerouting.VehicleType; import net.osmand.plus.routepreparationmenu.cards.BaseCard; +import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.util.Algorithms; import java.util.ArrayList; @@ -48,6 +49,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private static final String ENGINE_API_KEY_KEY = "engine_api_key"; private static final String ENGINE_NAME_CHANGED_BY_USER_KEY = "engine_name_changed_by_user_key"; private static final String EXAMPLE_LOCATION_KEY = "example_location"; + private static final String APP_MODE_KEY = "app_mode"; private OnlineRoutingSegmentCard nameCard; private OnlineRoutingSegmentCard serverCard; @@ -56,7 +58,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private OnlineRoutingSegmentCard exampleCard; private boolean isEditingMode; - private boolean nightMode; + private ApplicationMode appMode; private OnlineRoutingEngineObject engine; private ExampleLocation selectedLocation; @@ -82,7 +84,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { if (mapActivity == null) { return null; } - nightMode = !app.getSettings().isLightContent(); + boolean nightMode = isNightMode(app); View view = UiUtilities.getInflater(getContext(), nightMode) .inflate(R.layout.online_routing_engine_preference, container, false); @@ -95,7 +97,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { ViewGroup segmentsContainer = (ViewGroup) view.findViewById(R.id.segments_container); // create name card - nameCard = new OnlineRoutingSegmentCard(mapActivity); + nameCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + nameCard.build(mapActivity); nameCard.setDescription(getString(R.string.select_nav_profile_dialog_message)); nameCard.setFieldBoxLabelText(getString(R.string.shared_string_name)); nameCard.setOnTextChangedListener(new OnTextChangedListener() { @@ -111,7 +114,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(nameCard.getView()); // create server card - serverCard = new OnlineRoutingSegmentCard(mapActivity); + serverCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + serverCard.build(mapActivity); serverCard.setHeaderTitle(getString(R.string.shared_string_type)); List serverItems = new ArrayList<>(); for (ServerType server : ServerType.values()) { @@ -141,7 +145,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(serverCard.getView()); // create vehicle card - vehicleCard = new OnlineRoutingSegmentCard(mapActivity); + vehicleCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + vehicleCard.build(mapActivity); vehicleCard.setHeaderTitle(getString(R.string.shared_string_vehicle)); List vehicleItems = new ArrayList<>(); for (VehicleType vehicle : VehicleType.values()) { @@ -173,7 +178,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(vehicleCard.getView()); // create api key card - apiKeyCard = new OnlineRoutingSegmentCard(mapActivity); + apiKeyCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + apiKeyCard.build(mapActivity); apiKeyCard.setHeaderTitle(getString(R.string.shared_string_api_key)); apiKeyCard.setFieldBoxLabelText(getString(R.string.keep_it_empty_if_not)); apiKeyCard.setEditedText(engine.apiKey); @@ -188,7 +194,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(apiKeyCard.getView()); // create example card - exampleCard = new OnlineRoutingSegmentCard(mapActivity); + exampleCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + exampleCard.build(mapActivity); exampleCard.setHeaderTitle(getString(R.string.shared_string_example)); List locationItems = new ArrayList<>(); for (ExampleLocation location : ExampleLocation.values()) { @@ -330,14 +337,6 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } - public static void showInstance(FragmentActivity activity, boolean isEditingMode) { - OnlineRoutingEngineFragment fragment = new OnlineRoutingEngineFragment(); - fragment.isEditingMode = isEditingMode; - activity.getSupportFragmentManager().beginTransaction() - .add(R.id.fragmentContainer, fragment, TAG) - .addToBackStack(TAG).commitAllowingStateLoss(); - } - @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -353,6 +352,9 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { outState.putString(ENGINE_API_KEY_KEY, engine.apiKey); outState.putBoolean(ENGINE_NAME_CHANGED_BY_USER_KEY, engine.wasNameChangedByUser); outState.putString(EXAMPLE_LOCATION_KEY, selectedLocation.name()); + if (appMode != null) { + outState.putString(APP_MODE_KEY, appMode.getStringKey()); + } } private void restoreState(Bundle savedState) { @@ -364,9 +366,9 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { engine.apiKey = savedState.getString(ENGINE_API_KEY_KEY); engine.wasNameChangedByUser = savedState.getBoolean(ENGINE_NAME_CHANGED_BY_USER_KEY); selectedLocation = ExampleLocation.valueOf(savedState.getString(EXAMPLE_LOCATION_KEY)); + appMode = ApplicationMode.valueOfStringKey(savedState.getString(APP_MODE_KEY), null); } - private void initEngineState() { engine.serverType = ServerType.values()[0]; engine.vehicleType = VehicleType.values()[0]; @@ -380,6 +382,10 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } + protected boolean isNightMode(@NonNull OsmandApplication app) { + return !app.getSettings().isLightContentForMode(appMode); + } + @Nullable private MapActivity getMapActivity() { FragmentActivity activity = getActivity(); @@ -390,6 +396,17 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } + public static void showInstance(@NonNull FragmentActivity activity, + @NonNull ApplicationMode appMode, + boolean isEditingMode) { + OnlineRoutingEngineFragment fragment = new OnlineRoutingEngineFragment(); + fragment.isEditingMode = isEditingMode; + fragment.appMode = appMode; + activity.getSupportFragmentManager().beginTransaction() + .add(R.id.fragmentContainer, fragment, TAG) + .addToBackStack(TAG).commitAllowingStateLoss(); + } + private static class OnlineRoutingEngineObject { private String name; private ServerType serverType; diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java index 6913770773..7a42818745 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java @@ -403,7 +403,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment implements O if (getActivity() != null) { SelectProfileBottomSheet.showInstance( getActivity(), DialogMode.BASE_PROFILE, ProfileAppearanceFragment.this, - selectedAppModeKey, false); + getSelectedAppMode(), selectedAppModeKey, false); } } }