Skip to content

Commit

Permalink
Add OnCameraTrackingChangedListener to NavigationMapboxMap (#1386)
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder authored Oct 3, 2018
1 parent 267fba6 commit c7ae8e9
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mapbox.services.android.navigation.ui.v5;

import android.support.design.widget.BottomSheetBehavior;

import com.mapbox.mapboxsdk.plugins.locationlayer.OnCameraTrackingChangedListener;

/**
* Listener used to detect user interaction with the map while driving.
* <p>
* If the camera tracking is dismissed, we notify the presenter to adjust UI accordingly.
*/
public class NavigationOnCameraTrackingChangedListener implements OnCameraTrackingChangedListener {

private final NavigationPresenter navigationPresenter;
private final BottomSheetBehavior summaryBehavior;

NavigationOnCameraTrackingChangedListener(NavigationPresenter navigationPresenter,
BottomSheetBehavior summaryBehavior) {
this.navigationPresenter = navigationPresenter;
this.summaryBehavior = summaryBehavior;
}

@Override
public void onCameraTrackingDismissed() {
if (summaryBehavior.getState() != BottomSheetBehavior.STATE_HIDDEN) {
navigationPresenter.onCameraTrackingDismissed();
}
}

@Override
public void onCameraTrackingChanged(int currentMode) {
// Intentionally empty
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void onRecenterClick() {
view.hideRecenterBtn();
}

void onMapScroll() {
void onCameraTrackingDismissed() {
if (!view.isSummaryBottomSheetHidden()) {
view.setSummaryBehaviorHideable(true);
view.setSummaryBehaviorState(BottomSheetBehavior.STATE_HIDDEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public class NavigationView extends CoordinatorLayout implements LifecycleObserv
private NavigationViewModel navigationViewModel;
private NavigationMapboxMap navigationMap;
private OnNavigationReadyCallback onNavigationReadyCallback;
private MapboxMap.OnMoveListener onMoveListener;
private MapboxMap.OnFlingListener onFlingListener;
private NavigationOnCameraTrackingChangedListener onTrackingChangedListener;
private NavigationMapboxMapInstanceState mapInstanceState;
private boolean isMapInitialized;
private boolean isSubscribed;
Expand Down Expand Up @@ -170,11 +169,6 @@ public void onRestoreInstanceState(Bundle savedInstanceState) {
* In a {@link android.app.Fragment}, this should be in {@link Fragment#onDestroyView()}.
*/
public void onDestroy() {
if (navigationMap != null) {
navigationMap.removeOnMoveListener(onMoveListener);
navigationMap.removeOnFlingListener(onFlingListener);
}
navigationViewEventDispatcher.onDestroy(navigationViewModel.retrieveNavigation());
shutdown();
}

Expand Down Expand Up @@ -553,8 +547,7 @@ private void initializeNavigation(NavigationViewOptions options) {

if (!isSubscribed) {
initializeClickListeners();
initializeOnMoveListener();
initializeOnFlingListener();
initializeOnCameraTrackingChangedListener();
subscribeViewModels();
}
}
Expand All @@ -565,19 +558,13 @@ private void initializeClickListeners() {
routeOverviewBtn.setOnClickListener(new RouteOverviewBtnClickListener(navigationPresenter));
}

private void initializeOnMoveListener() {
onMoveListener = new NavigationOnMoveListener(navigationPresenter, summaryBehavior);
navigationMap.addOnMoveListener(onMoveListener);
}

private void initializeOnFlingListener() {
onFlingListener = new NavigationOnFlingListener(navigationPresenter, summaryBehavior);
navigationMap.addOnFlingListener(onFlingListener);
private void initializeOnCameraTrackingChangedListener() {
onTrackingChangedListener = new NavigationOnCameraTrackingChangedListener(navigationPresenter, summaryBehavior);
navigationMap.addOnCameraTrackingChangedListener(onTrackingChangedListener);
}

private void establish(NavigationViewOptions options) {
LocaleUtils localeUtils = new LocaleUtils();

establishDistanceFormatter(localeUtils, options);
establishTimeFormat(options);
}
Expand Down Expand Up @@ -640,6 +627,10 @@ private void subscribeViewModels() {
}

private void shutdown() {
if (navigationMap != null) {
navigationMap.removeOnCameraTrackingChangedListener(onTrackingChangedListener);
}
navigationViewEventDispatcher.onDestroy(navigationViewModel.retrieveNavigation());
mapView.onDestroy();
navigationViewModel.onDestroy(isChangingConfigurations());
ImageCoordinator.getInstance().shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerOptions;
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
import com.mapbox.mapboxsdk.plugins.locationlayer.OnCameraTrackingChangedListener;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import com.mapbox.mapboxsdk.style.layers.LineLayer;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
Expand Down Expand Up @@ -464,24 +465,26 @@ public boolean isTrafficVisible() {
return layerInteractor.isLayerVisible(TRAFFIC_LAYER_ID);
}

public void addOnMoveListener(@NonNull MapboxMap.OnMoveListener onMoveListener) {
mapboxMap.addOnMoveListener(onMoveListener);
}

public void addOnFlingListener(MapboxMap.OnFlingListener onFlingListener) {
mapboxMap.addOnFlingListener(onFlingListener);
}

public void removeOnMoveListener(MapboxMap.OnMoveListener onMoveListener) {
if (onMoveListener != null) {
mapboxMap.removeOnMoveListener(onMoveListener);
}
/**
* Add a {@link OnCameraTrackingChangedListener} to the {@link LocationLayerPlugin} that is
* wrapped within this class.
* <p>
* This listener will fire any time camera tracking is dismissed or the camera mode is updated.
*
* @param listener to be added
*/
public void addOnCameraTrackingChangedListener(OnCameraTrackingChangedListener listener) {
locationLayer.addOnCameraTrackingChangedListener(listener);
}

public void removeOnFlingListener(MapboxMap.OnFlingListener onFlingListener) {
if (onFlingListener != null) {
mapboxMap.removeOnFlingListener(onFlingListener);
}
/**
* Remove a {@link OnCameraTrackingChangedListener} from the {@link LocationLayerPlugin} that is
* wrapped within this class.
*
* @param listener to be removed
*/
public void removeOnCameraTrackingChangedListener(OnCameraTrackingChangedListener listener) {
locationLayer.removeOnCameraTrackingChangedListener(listener);
}

public void takeScreenshot(NavigationSnapshotReadyCallback navigationSnapshotReadyCallback) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.mapbox.services.android.navigation.ui.v5;

import android.support.design.widget.BottomSheetBehavior;

import org.junit.Test;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class NavigationOnCameraTrackingChangedListenerTest {

@Test
public void onCameraTrackingDismissed_presenterNotifiedWithVisibleBottomsheet() {
NavigationPresenter presenter = mock(NavigationPresenter.class);
BottomSheetBehavior behavior = mock(BottomSheetBehavior.class);
when(behavior.getState()).thenReturn(BottomSheetBehavior.STATE_EXPANDED);
NavigationOnCameraTrackingChangedListener listener = new NavigationOnCameraTrackingChangedListener(
presenter, behavior
);

listener.onCameraTrackingDismissed();

verify(presenter).onCameraTrackingDismissed();
}

@Test
public void onCameraTrackingDismissed_ignoredWithHiddenBottomsheet() {
NavigationPresenter presenter = mock(NavigationPresenter.class);
BottomSheetBehavior behavior = mock(BottomSheetBehavior.class);
when(behavior.getState()).thenReturn(BottomSheetBehavior.STATE_HIDDEN);
NavigationOnCameraTrackingChangedListener listener = new NavigationOnCameraTrackingChangedListener(
presenter, behavior
);

listener.onCameraTrackingDismissed();

verify(presenter, times(0)).onCameraTrackingDismissed();
}
}

0 comments on commit c7ae8e9

Please sign in to comment.