From 6ced5fcd2cf7fe3283216b759a7a969e744f0ce5 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Mon, 11 Jun 2018 17:05:10 -0400 Subject: [PATCH] Use theme attribute to update MapView map style URL --- .../android/navigation/ui/v5/NavigationView.java | 10 +++++++++- .../android/navigation/ui/v5/ThemeSwitcher.java | 10 +++++----- .../src/main/res/layout/navigation_view_layout.xml | 1 + 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java index 5ffa252e084..13b24e079e6 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java @@ -20,6 +20,7 @@ import com.mapbox.api.directions.v5.models.DirectionsRoute; import com.mapbox.geojson.Point; +import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; @@ -95,7 +96,7 @@ public NavigationView(Context context, @Nullable AttributeSet attrs, int defStyl * @param savedInstanceState to restore state if not null */ public void onCreate(@Nullable Bundle savedInstanceState) { - mapView.setStyleUrl(ThemeSwitcher.retrieveMapStyle(getContext())); + updateSavedInstanceStateMapStyle(savedInstanceState); mapView.onCreate(savedInstanceState); updatePresenterState(savedInstanceState); navigationViewModel.onCreate(); @@ -407,6 +408,13 @@ private void initializeInstructionListListener() { navigationViewEventDispatcher)); } + private void updateSavedInstanceStateMapStyle(@Nullable Bundle savedInstanceState) { + if (savedInstanceState != null) { + String mapStyleUrl = ThemeSwitcher.retrieveMapStyle(getContext()); + savedInstanceState.putString(MapboxConstants.STATE_STYLE_URL, mapStyleUrl); + } + } + /** * Sets the {@link BottomSheetBehavior} based on the last state stored * in {@link Bundle} savedInstanceState. diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java index 934f155d79f..3059978a5d3 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java @@ -105,6 +105,11 @@ static void setTheme(Context context, AttributeSet attrs) { context.setTheme(darkThemeEnabled ? darkTheme : lightTheme); } + static String retrieveMapStyle(Context context) { + TypedValue mapStyleAttr = resolveAttributeFromId(context, R.attr.navigationViewMapStyle); + return mapStyleAttr.string.toString(); + } + /** * Returns true if the current UI_MODE_NIGHT is enabled, false otherwise. * @@ -116,11 +121,6 @@ private static boolean isDarkThemeEnabled(Context context) { return uiMode == Configuration.UI_MODE_NIGHT_YES; } - static String retrieveMapStyle(Context context) { - TypedValue mapStyleAttr = resolveAttributeFromId(context, R.attr.navigationViewMapStyle); - return mapStyleAttr.string.toString(); - } - @NonNull private static TypedValue resolveAttributeFromId(Context context, int resId) { TypedValue outValue = new TypedValue(); diff --git a/libandroid-navigation-ui/src/main/res/layout/navigation_view_layout.xml b/libandroid-navigation-ui/src/main/res/layout/navigation_view_layout.xml index d1d45223052..08c08ce66ab 100644 --- a/libandroid-navigation-ui/src/main/res/layout/navigation_view_layout.xml +++ b/libandroid-navigation-ui/src/main/res/layout/navigation_view_layout.xml @@ -12,6 +12,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" app:mapbox_uiAttribution="false" + app:mapbox_styleUrl="?attr/navigationViewMapStyle" app:mapbox_uiCompass="false" app:mapbox_uiLogo="false"/>