Skip to content

Commit

Permalink
Use theme attribute to update MapView map style URL
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Jun 20, 2018
1 parent 4a99564 commit 6ced5fc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

Expand Down

0 comments on commit 6ced5fc

Please sign in to comment.