Skip to content

Commit

Permalink
add 12/24 hour format navigation view option
Browse files Browse the repository at this point in the history
  • Loading branch information
Guardiola31337 committed Mar 28, 2018
1 parent 2975aea commit 34f21e6
Show file tree
Hide file tree
Showing 13 changed files with 471 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.mapbox.services.android.navigation.v5.location.MockLocationEngine;
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation;
import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;
import com.mapbox.services.android.navigation.v5.navigation.NavigationTimeFormat;
import com.mapbox.services.android.navigation.v5.navigation.NavigationUnitType;
import com.mapbox.services.android.navigation.v5.utils.LocaleUtils;

Expand Down Expand Up @@ -357,7 +358,7 @@ public void updateLocationLayer(Location location) {
public void startNavigation(NavigationViewOptions options) {
clearMarkers();
if (!isInitialized) {
setLocale(options);
establish(options);
navigationViewModel.initializeNavigationOptions(getContext().getApplicationContext(),
options.navigationOptions().toBuilder().isFromNavigationUi(true).build());
initCamera();
Expand All @@ -369,29 +370,6 @@ public void startNavigation(NavigationViewOptions options) {
routeViewModel.extractRouteOptions(options);
}

private void setLocale(NavigationViewOptions options) {
Locale locale = LocaleUtils.getNonNullLocale(getContext(), options.navigationOptions().locale());
@NavigationUnitType.UnitType int unitType = options.navigationOptions().unitType();

instructionView.setLocale(locale);
instructionView.setUnitType(unitType);
summaryBottomSheet.setLocale(locale);
summaryBottomSheet.setUnitType(unitType);
}

/**
* Should be called after {@link NavigationView#onCreate(Bundle)}.
* <p>
* This method adds the {@link OnNavigationReadyCallback},
* which will fire ready / cancel events for this view.
*
* @param onNavigationReadyCallback to be set to this view
*/
public void getNavigationAsync(OnNavigationReadyCallback onNavigationReadyCallback) {
this.onNavigationReadyCallback = onNavigationReadyCallback;
mapView.getMapAsync(this);
}

/**
* Gives the ability to manipulate the map directly for anything that might not currently be
* supported. This returns null until the view is initialized
Expand All @@ -402,6 +380,26 @@ public MapboxMap getMapboxMap() {
return map;
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mapView.onStart();
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
mapView.onResume();
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
mapView.onPause();
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
mapView.onStop();
}

private void init() {
InstructionLoader.getInstance().initialize(getContext());
inflate(getContext(), R.layout.navigation_view_layout, this);
Expand Down Expand Up @@ -433,6 +431,48 @@ private void initViewModels() {
}
}

/**
* Adds this view as a lifecycle observer.
* This needs to be done earlier than the other observers (prior to the style loading).
*/
private void initNavigationViewObserver() {
try {
((LifecycleOwner) getContext()).getLifecycle().addObserver(this);
} catch (ClassCastException exception) {
throw new ClassCastException("Please ensure that the provided Context is a valid LifecycleOwner");
}
}

/**
* Initializes the {@link BottomSheetBehavior} for {@link SummaryBottomSheet}.
*/
private void initSummaryBottomSheet() {
summaryBehavior = BottomSheetBehavior.from(summaryBottomSheet);
summaryBehavior.setHideable(false);
summaryBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
navigationViewEventDispatcher.onBottomSheetStateChanged(bottomSheet, newState);

if (newState == BottomSheetBehavior.STATE_HIDDEN && navigationPresenter != null) {
navigationPresenter.onSummaryBottomSheetHidden();
}
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}

/**
* Initialize a new event dispatcher in charge of firing all navigation
* listener updates to the classes that have implemented these listeners.
*/
private void initNavigationEventDispatcher() {
navigationViewEventDispatcher = new NavigationViewEventDispatcher();
}

/**
* Sets the {@link BottomSheetBehavior} based on the last state stored
* in {@link Bundle} savedInstanceState.
Expand Down Expand Up @@ -465,31 +505,6 @@ private void initRoute() {
mapRoute = new NavigationMapRoute(null, mapView, map, routeStyleRes);
}

/**
* Initializes the {@link NavigationCamera} that will be used to follow
* the {@link Location} updates from {@link MapboxNavigation}.
*/
private void initCamera() {
camera = new NavigationCamera(map, navigationViewModel.getNavigation());
}

/**
* Subscribes the {@link InstructionView} and {@link SummaryBottomSheet} to the {@link NavigationViewModel}.
* <p>
* Then, creates an instance of {@link NavigationViewSubscriber}, which takes a presenter and listener.
* <p>
* The subscriber then subscribes to the view models, setting up the appropriate presenter / listener
* method calls based on the {@link android.arch.lifecycle.LiveData} updates.
*/
private void subscribeViewModels() {
instructionView.subscribe(navigationViewModel);
summaryBottomSheet.subscribe(navigationViewModel);

NavigationViewSubscriber subscriber = new NavigationViewSubscriber(navigationPresenter,
navigationViewEventDispatcher);
subscriber.subscribe(((LifecycleOwner) getContext()), locationViewModel, routeViewModel, navigationViewModel);
}

/**
* Initializes the {@link LocationLayerPlugin} to be used to draw the current
* location.
Expand All @@ -502,18 +517,6 @@ private void initLocationLayer() {
locationLayer.setLocationLayerEnabled(LocationLayerMode.NAVIGATION);
}

/**
* Adds this view as a lifecycle observer.
* This needs to be done earlier than the other observers (prior to the style loading).
*/
private void initNavigationViewObserver() {
try {
((LifecycleOwner) getContext()).getLifecycle().addObserver(this);
} catch (ClassCastException exception) {
throw new ClassCastException("Please ensure that the provided Context is a valid LifecycleOwner");
}
}

/**
* Add lifecycle observers to ensure these objects properly
* start / stop based on the Android lifecycle.
Expand All @@ -527,34 +530,6 @@ private void initLifecycleObservers() {
}
}

/**
* Initialize a new event dispatcher in charge of firing all navigation
* listener updates to the classes that have implemented these listeners.
*/
private void initNavigationEventDispatcher() {
navigationViewEventDispatcher = new NavigationViewEventDispatcher();
}

/**
* Sets up the listeners in the dispatcher, as well as the listeners in the {@link MapboxNavigation}
*
* @param navigationViewOptions that contains all listeners to attach
*/
private void setupListeners(NavigationViewOptions navigationViewOptions) {
navigationViewEventDispatcher.setFeedbackListener(navigationViewOptions.feedbackListener());
navigationViewEventDispatcher.setNavigationListener(navigationViewOptions.navigationListener());
navigationViewEventDispatcher.setRouteListener(navigationViewOptions.routeListener());
navigationViewEventDispatcher.setBottomSheetCallback(navigationViewOptions.bottomSheetCallback());

if (navigationViewOptions.progressChangeListener() != null) {
navigationViewModel.getNavigation().addProgressChangeListener(navigationViewOptions.progressChangeListener());
}

if (navigationViewOptions.milestoneEventListener() != null) {
navigationViewModel.getNavigation().addMilestoneEventListener(navigationViewOptions.milestoneEventListener());
}
}

/**
* Initialize a new presenter for this Activity.
*/
Expand All @@ -581,28 +556,6 @@ public void onClick(View view) {
});
}

/**
* Initializes the {@link BottomSheetBehavior} for {@link SummaryBottomSheet}.
*/
private void initSummaryBottomSheet() {
summaryBehavior = BottomSheetBehavior.from(summaryBottomSheet);
summaryBehavior.setHideable(false);
summaryBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
navigationViewEventDispatcher.onBottomSheetStateChanged(bottomSheet, newState);

if (newState == BottomSheetBehavior.STATE_HIDDEN && navigationPresenter != null) {
navigationPresenter.onSummaryBottomSheetHidden();
}
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}

/**
* Removes any markers on the map that were added using addMarker()
*/
Expand All @@ -612,23 +565,86 @@ private void clearMarkers() {
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mapView.onStart();
private void establish(NavigationViewOptions options) {
establishLocale(options);
establishUnitType(options);
establishTimeFormat(options);
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
mapView.onResume();
private void establishLocale(NavigationViewOptions options) {
Locale locale = LocaleUtils.getNonNullLocale(getContext(), options.navigationOptions().locale());
instructionView.setLocale(locale);
summaryBottomSheet.setLocale(locale);
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
mapView.onPause();
private void establishUnitType(NavigationViewOptions options) {
@NavigationUnitType.UnitType
int unitType = options.navigationOptions().unitType();
instructionView.setUnitType(unitType);
summaryBottomSheet.setUnitType(unitType);
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
mapView.onStop();
private void establishTimeFormat(NavigationViewOptions options) {
@NavigationTimeFormat.Type
int timeFormatType = options.navigationOptions().timeFormatType();
summaryBottomSheet.setTimeFormat(timeFormatType);
}

/**
* Initializes the {@link NavigationCamera} that will be used to follow
* the {@link Location} updates from {@link MapboxNavigation}.
*/
private void initCamera() {
camera = new NavigationCamera(map, navigationViewModel.getNavigation());
}

/**
* Sets up the listeners in the dispatcher, as well as the listeners in the {@link MapboxNavigation}
*
* @param navigationViewOptions that contains all listeners to attach
*/
private void setupListeners(NavigationViewOptions navigationViewOptions) {
navigationViewEventDispatcher.setFeedbackListener(navigationViewOptions.feedbackListener());
navigationViewEventDispatcher.setNavigationListener(navigationViewOptions.navigationListener());
navigationViewEventDispatcher.setRouteListener(navigationViewOptions.routeListener());
navigationViewEventDispatcher.setBottomSheetCallback(navigationViewOptions.bottomSheetCallback());

if (navigationViewOptions.progressChangeListener() != null) {
navigationViewModel.getNavigation().addProgressChangeListener(navigationViewOptions.progressChangeListener());
}

if (navigationViewOptions.milestoneEventListener() != null) {
navigationViewModel.getNavigation().addMilestoneEventListener(navigationViewOptions.milestoneEventListener());
}
}

/**
* Subscribes the {@link InstructionView} and {@link SummaryBottomSheet} to the {@link NavigationViewModel}.
* <p>
* Then, creates an instance of {@link NavigationViewSubscriber}, which takes a presenter and listener.
* <p>
* The subscriber then subscribes to the view models, setting up the appropriate presenter / listener
* method calls based on the {@link android.arch.lifecycle.LiveData} updates.
*/
private void subscribeViewModels() {
instructionView.subscribe(navigationViewModel);
summaryBottomSheet.subscribe(navigationViewModel);

NavigationViewSubscriber subscriber = new NavigationViewSubscriber(navigationPresenter,
navigationViewEventDispatcher);
subscriber.subscribe(((LifecycleOwner) getContext()), locationViewModel, routeViewModel, navigationViewModel);
}

/**
* Should be called after {@link NavigationView#onCreate(Bundle)}.
* <p>
* This method adds the {@link OnNavigationReadyCallback},
* which will fire ready / cancel events for this view.
*
* @param onNavigationReadyCallback to be set to this view
*/
public void getNavigationAsync(OnNavigationReadyCallback onNavigationReadyCallback) {
this.onNavigationReadyCallback = onNavigationReadyCallback;
mapView.getMapAsync(this);
}
}
Loading

0 comments on commit 34f21e6

Please sign in to comment.