Skip to content

Commit

Permalink
Added InstructionViewCallback to allow views to be alerted when the i…
Browse files Browse the repository at this point in the history
…nstruction list is shown or hidden. This fixes the bugs where the re-center button and other widgets are shown on top of the instruction list when it's expanded
  • Loading branch information
Devota Aabel committed Jun 8, 2018
1 parent 7b6ba9a commit d9e0809
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.mapbox.services.android.navigation.ui.v5.NavigationView;
import com.mapbox.services.android.navigation.ui.v5.NavigationViewOptions;
import com.mapbox.services.android.navigation.ui.v5.OnNavigationReadyCallback;
import com.mapbox.services.android.navigation.ui.v5.listeners.InstructionListListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;
import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener;
Expand All @@ -31,14 +32,15 @@
import retrofit2.Response;

public class EmbeddedNavigationActivity extends AppCompatActivity implements OnNavigationReadyCallback,
NavigationListener, ProgressChangeListener {
NavigationListener, ProgressChangeListener, InstructionListListener {

private NavigationView navigationView;
private View spacer;
private TextView speedWidget;
private static final Point ORIGIN = Point.fromLngLat(-77.03194990754128, 38.909664963450105);
private static final Point DESTINATION = Point.fromLngLat(-77.0270025730133, 38.91057077063121);
private boolean bottomSheetVisible = true;
private boolean instructionListShown = false;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -78,7 +80,8 @@ private void startNavigation(DirectionsRoute directionsRoute) {
.navigationListener(this)
.directionsRoute(directionsRoute)
.shouldSimulateRoute(true)
.progressChangeListener(this);
.progressChangeListener(this)
.instructionListListener(this);
setBottomSheetCallback(options);

navigationView.startNavigation(options.build());
Expand Down Expand Up @@ -220,6 +223,14 @@ private void setSpeed(Location location) {
0, string.length() - 3, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

speedWidget.setText(spannableString);
speedWidget.setVisibility(View.VISIBLE);
if (!instructionListShown) {
speedWidget.setVisibility(View.VISIBLE);
}
}

@Override
public void onInstructionListVisibilityChanged(boolean shown) {
instructionListShown = shown;
speedWidget.setVisibility(shown ? View.GONE : View.VISIBLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ void onNavigationLocationUpdate(Location location) {
}
view.updateNavigationMap(location);
}

public void onInstructionListVisibilityChanged(boolean visible) {
if (visible) {
view.hideRecenterBtn();
} else {
view.showRecenterBtn();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.services.android.navigation.ui.v5.instruction.ImageCoordinator;
import com.mapbox.services.android.navigation.ui.v5.instruction.InstructionView;
import com.mapbox.services.android.navigation.ui.v5.listeners.InstructionListListener;
import com.mapbox.services.android.navigation.ui.v5.map.NavigationMapboxMap;
import com.mapbox.services.android.navigation.ui.v5.summary.SummaryBottomSheet;
import com.mapbox.services.android.navigation.ui.v5.utils.ViewUtils;
Expand Down Expand Up @@ -380,6 +381,7 @@ private void initializeView() {
initializeSummaryBottomSheet();
initializeNavigationEventDispatcher();
initializeNavigationPresenter();
initializeInstructionListListener();
}

private void bind() {
Expand Down Expand Up @@ -419,6 +421,16 @@ private void initializeNavigationEventDispatcher() {
navigationViewModel.initializeEventDispatcher(navigationViewEventDispatcher);
}

private void initializeInstructionListListener() {
instructionView.setInstructionListListener(new InstructionListListener() {
@Override
public void onInstructionListVisibilityChanged(boolean visible) {
navigationPresenter.onInstructionListVisibilityChanged(visible);
navigationViewEventDispatcher.onInstructionListVisibilityChanged(visible);
}
});
}

/**
* 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 @@ -9,6 +9,7 @@
import com.mapbox.geojson.Point;
import com.mapbox.services.android.navigation.ui.v5.feedback.FeedbackItem;
import com.mapbox.services.android.navigation.ui.v5.listeners.FeedbackListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.InstructionListListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.RouteListener;
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation;
Expand All @@ -24,6 +25,7 @@ class NavigationViewEventDispatcher {
private NavigationListener navigationListener;
private RouteListener routeListener;
private BottomSheetCallback bottomSheetCallback;
private InstructionListListener instructionListListener;

/**
* Initializes the listeners in the dispatcher, as well as the listeners in the {@link MapboxNavigation}
Expand All @@ -37,6 +39,7 @@ void initializeListeners(NavigationViewOptions navigationViewOptions, MapboxNavi
assignBottomSheetCallback(navigationViewOptions.bottomSheetCallback());
assignProgressChangeListener(navigationViewOptions, navigation);
assignMilestoneEventListener(navigationViewOptions, navigation);
assignInstructionListListener(navigationViewOptions.instructionListListener());
}

void assignFeedbackListener(@Nullable FeedbackListener feedbackListener) {
Expand All @@ -55,6 +58,10 @@ void assignBottomSheetCallback(@Nullable BottomSheetCallback bottomSheetCallback
this.bottomSheetCallback = bottomSheetCallback;
}

void assignInstructionListListener(@Nullable InstructionListListener instructionListListener) {
this.instructionListListener = instructionListListener;
}

/*
* Feedback listeners
*/
Expand Down Expand Up @@ -141,6 +148,12 @@ void onBottomSheetStateChanged(View bottomSheet, int newState) {
}
}

void onInstructionListVisibilityChanged(boolean shown) {
if (instructionListListener != null) {
instructionListListener.onInstructionListVisibilityChanged(shown);
}
}

private void assignProgressChangeListener(NavigationViewOptions navigationViewOptions, MapboxNavigation navigation) {
if (navigationViewOptions.progressChangeListener() != null) {
navigation.addProgressChangeListener(navigationViewOptions.progressChangeListener());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mapbox.api.directions.v5.DirectionsCriteria;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.services.android.navigation.ui.v5.listeners.FeedbackListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.InstructionListListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.RouteListener;
import com.mapbox.services.android.navigation.v5.milestone.Milestone;
Expand Down Expand Up @@ -42,6 +43,9 @@ public abstract class NavigationViewOptions extends NavigationUiOptions {
@Nullable
public abstract BottomSheetCallback bottomSheetCallback();

@Nullable
public abstract InstructionListListener instructionListListener();

@AutoValue.Builder
public abstract static class Builder {

Expand Down Expand Up @@ -73,6 +77,8 @@ public abstract static class Builder {

public abstract Builder bottomSheetCallback(BottomSheetCallback bottomSheetCallback);

public abstract Builder instructionListListener(InstructionListListener instructionListListener);

public abstract NavigationViewOptions build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.mapbox.services.android.navigation.ui.v5.feedback.FeedbackItem;
import com.mapbox.services.android.navigation.ui.v5.instruction.maneuver.ManeuverView;
import com.mapbox.services.android.navigation.ui.v5.instruction.turnlane.TurnLaneAdapter;
import com.mapbox.services.android.navigation.ui.v5.listeners.InstructionListListener;
import com.mapbox.services.android.navigation.ui.v5.summary.list.InstructionListAdapter;
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants;
import com.mapbox.services.android.navigation.v5.navigation.metrics.FeedbackEvent;
Expand Down Expand Up @@ -104,6 +105,7 @@ public class InstructionView extends RelativeLayout implements FeedbackBottomShe
private AnimationSet fadeInSlowOut;
private LegStep currentStep;
private NavigationViewModel navigationViewModel;
private InstructionListListener instructionListListener;

private String language = "";
private String unitType = "";
Expand All @@ -123,6 +125,10 @@ public InstructionView(Context context, @Nullable AttributeSet attrs, int defSty
initialize();
}

public void setInstructionListListener(InstructionListListener instructionListListener) {
this.instructionListListener = instructionListListener;
}

/**
* Once this view has finished inflating, it will bind the views.
* <p>
Expand Down Expand Up @@ -299,6 +305,7 @@ public void hideInstructionList() {
updateLandscapeConstraintsTo(R.layout.instruction_layout);
}
instructionListLayout.setVisibility(GONE);
onInstructionListVisibilityChanged(false);
}

/**
Expand All @@ -308,6 +315,7 @@ public void hideInstructionList() {
* can be animated appropriately.
*/
public void showInstructionList() {
onInstructionListVisibilityChanged(true);
beginDelayedTransition();
int orientation = getContext().getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Expand Down Expand Up @@ -549,6 +557,12 @@ private void initializeAnimations() {
fadeInSlowOut.addAnimation(fadeOut);
}

private void onInstructionListVisibilityChanged(boolean visible) {
if (instructionListListener != null) {
instructionListListener.onInstructionListVisibilityChanged(visible);
}
}

private void addBottomSheetListener() {
FragmentManager fragmentManager = obtainSupportFragmentManger();
if (fragmentManager != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mapbox.services.android.navigation.ui.v5.listeners;

/**
* A listener that is triggered when the instruction list in InstructionView is shown or hidden.
*/
public interface InstructionListListener {
/**
* Triggered when the instruction list is shown or hidden.
*
* @param visible whether the list is shown or hidden
* @since 0.15.0
*/
void onInstructionListVisibilityChanged(boolean visible);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mapbox.geojson.Point;
import com.mapbox.services.android.navigation.ui.v5.feedback.FeedbackItem;
import com.mapbox.services.android.navigation.ui.v5.listeners.FeedbackListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.InstructionListListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.RouteListener;

Expand Down Expand Up @@ -278,4 +279,15 @@ public void onFeedbackListenerNotSet_feedbackSentIsNotCalled() throws Exception

verify(feedbackListener, times(0)).onFeedbackSent(item);
}

@Test
public void onInstructionListShown() {
NavigationViewEventDispatcher eventDispatcher = new NavigationViewEventDispatcher();
InstructionListListener instructionListListener = mock(InstructionListListener.class);
eventDispatcher.assignInstructionListListener(instructionListListener);

eventDispatcher.onInstructionListVisibilityChanged(true);

verify(instructionListListener, times(1)).onInstructionListVisibilityChanged(true);
}
}

0 comments on commit d9e0809

Please sign in to comment.