Skip to content

Commit

Permalink
Remove preference setup for unit type in RouteViewModel (#593)
Browse files Browse the repository at this point in the history
* Remove preference setup for unit type in RouteViewModel
- Only look at the NavigationViewOptions passed

* Pass unit type update method

* Remove ternary in favor of if else
  • Loading branch information
danesfeder authored Dec 15, 2017
1 parent 7ea236b commit 8aa843a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.content.SharedPreferences;
import android.location.Location;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;

import com.mapbox.api.directions.v5.DirectionsCriteria;
Expand All @@ -17,7 +15,6 @@
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.services.android.navigation.ui.v5.NavigationViewOptions;
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation;
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants;
import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;
import com.mapbox.services.android.navigation.v5.navigation.NavigationUnitType;

Expand All @@ -38,7 +35,6 @@ public class RouteViewModel extends AndroidViewModel implements Callback<Directi

public RouteViewModel(@NonNull Application application) {
super(application);
initUnitType(PreferenceManager.getDefaultSharedPreferences(application));
}

/**
Expand Down Expand Up @@ -77,6 +73,7 @@ public void updateRawLocation(@NonNull Location rawLocation) {
* @param options holds either a set of {@link Point} coordinates or a {@link DirectionsRoute}
*/
public void extractRouteOptions(NavigationViewOptions options) {
updateUnitType(options.navigationOptions().unitType());
if (extractRouteOptions) {
if (launchWithRoute(options)) {
extractRouteFromOptions(options);
Expand All @@ -86,6 +83,21 @@ public void extractRouteOptions(NavigationViewOptions options) {
}
}

/**
* Updates the request unit type based on what was set in
* {@link NavigationViewOptions}.
*
* @param unitType to be used for route requests
*/
private void updateUnitType(int unitType) {
boolean isImperialUnitType = unitType == NavigationUnitType.TYPE_IMPERIAL;
if (isImperialUnitType) {
this.unitType = DirectionsCriteria.IMPERIAL;
} else {
this.unitType = DirectionsCriteria.METRIC;
}
}

/**
* Called when an off-route event is fired and a new {@link DirectionsRoute}
* is needed to continue navigating.
Expand All @@ -98,16 +110,6 @@ public void fetchRouteNewOrigin(Point newOrigin) {
}
}

/**
* Initializes distance unit (imperial or metric).
*/
private void initUnitType(SharedPreferences preferences) {
int unitType = preferences.getInt(NavigationConstants.NAVIGATION_VIEW_UNIT_TYPE,
NavigationUnitType.TYPE_IMPERIAL);
boolean isImperialUnitType = unitType == NavigationUnitType.TYPE_IMPERIAL;
this.unitType = isImperialUnitType ? DirectionsCriteria.IMPERIAL : DirectionsCriteria.METRIC;
}

/**
* Requests a new {@link DirectionsRoute}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,9 @@ private NavigationConstants() {
public static final String NAVIGATION_VIEW_DESTINATION_LNG_KEY = "destination_long";
public static final String NAVIGATION_VIEW_DESTINATION = "destination";
public static final String NAVIGATION_VIEW_ROUTE_KEY = "route_json";
public static final String NAVIGATION_VIEW_LAUNCH_ROUTE = "launch_with_route";
public static final String NAVIGATION_VIEW_AWS_POOL_ID = "navigation_view_aws_pool_id";
public static final String NAVIGATION_VIEW_UNIT_TYPE = "navigation_view_unit_type";
public static final String NAVIGATION_VIEW_REROUTING = "Rerouting";
public static final String ROUTE_BELOW_LAYER = "admin-3-4-boundaries-bg";
public static final String DECIMAL_FORMAT = "#.#";

// Step Maneuver Types
Expand Down

0 comments on commit 8aa843a

Please sign in to comment.