Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,18 @@

import android.text.SpannableString;

import com.mapbox.api.directions.v5.models.IntersectionLanes;
import com.mapbox.api.directions.v5.models.LegStep;
import com.mapbox.api.directions.v5.models.StepIntersection;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
import com.mapbox.services.android.navigation.v5.utils.DistanceFormatter;

import java.util.List;

public class InstructionModel {

private RouteProgress progress;
private SpannableString stepDistanceRemaining;
private List<IntersectionLanes> turnLanes;
private String upcomingManeuverModifier;

public InstructionModel(DistanceFormatter distanceFormatter, RouteProgress progress) {
this.progress = progress;
double distanceRemaining = progress.currentLegProgress().currentStepProgress().distanceRemaining();
stepDistanceRemaining = distanceFormatter.formatDistance(distanceRemaining);
extractStepResources(progress);
}

RouteProgress retrieveProgress() {
Expand All @@ -31,51 +23,4 @@ RouteProgress retrieveProgress() {
SpannableString retrieveStepDistanceRemaining() {
return stepDistanceRemaining;
}

String retrieveUpcomingManeuverModifier() {
return upcomingManeuverModifier;
}

List<IntersectionLanes> retrieveTurnLanes() {
return turnLanes;
}

private void extractStepResources(RouteProgress progress) {
LegStep upcomingStep = progress.currentLegProgress().upComingStep();
if (upcomingStep != null) {
if (hasIntersections(upcomingStep)) {
intersectionTurnLanes(upcomingStep);
}
upcomingManeuverModifier = upcomingStep.maneuver().modifier();
}
}

private void intersectionTurnLanes(LegStep step) {
StepIntersection intersection = step.intersections().get(0);
List<IntersectionLanes> lanes = intersection.lanes();
if (checkForNoneIndications(lanes)) {
turnLanes = null;
return;
}
turnLanes = lanes;
}

private boolean checkForNoneIndications(List<IntersectionLanes> lanes) {
if (lanes == null) {
return true;
}
for (IntersectionLanes lane : lanes) {
for (String indication : lane.indications()) {
if (indication.contains("none")) {
return true;
}
}
}
return false;
}

private boolean hasIntersections(LegStep step) {
return step.intersections() != null
&& step.intersections().get(0) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mapbox.api.directions.v5.models.BannerComponents;
import com.mapbox.api.directions.v5.models.BannerInstructions;
import com.mapbox.api.directions.v5.models.BannerText;
import com.mapbox.api.directions.v5.models.IntersectionLanes;
import com.mapbox.api.directions.v5.models.LegStep;
import com.mapbox.services.android.navigation.ui.v5.FeedbackButton;
import com.mapbox.services.android.navigation.ui.v5.NavigationButton;
Expand All @@ -54,8 +54,6 @@
import com.mapbox.services.android.navigation.v5.utils.LocaleUtils;
import com.mapbox.services.android.navigation.v5.utils.RouteUtils;

import java.util.List;

import timber.log.Timber;

/**
Expand All @@ -74,7 +72,7 @@
*/
public class InstructionView extends RelativeLayout implements FeedbackBottomSheetListener {

private static final double VALID_DURATION_REMAINING = 70d;
private static final String COMPONENT_TYPE_LANE = "lane";

private ManeuverView upcomingManeuverView;
private TextView upcomingDistanceText;
Expand Down Expand Up @@ -190,7 +188,7 @@ public void onChanged(@Nullable BannerInstructionModel model) {
updateManeuverView(model.retrievePrimaryManeuverType(), model.retrieveSecondaryManeuverModifier(),
model.retrievePrimaryRoundaboutAngle());
updateDataFromBannerText(model.retrievePrimaryBannerText(), model.retrieveSecondaryBannerText());
updateSubStep(model.retrieveSubBannerText());
updateSubStep(model.retrieveSubBannerText(), model.retrievePrimaryManeuverType());
}
}
});
Expand Down Expand Up @@ -246,9 +244,10 @@ public void updateBannerInstructionsWith(Milestone milestone) {
return;
}
BannerText primary = instructions.primary();
updateManeuverView(primary.type(), primary.modifier(), primary.degrees());
String primaryManeuverModifier = primary.modifier();
updateManeuverView(primary.type(), primaryManeuverModifier, primary.degrees());
updateDataFromBannerText(primary, instructions.secondary());
updateSubStep(instructions.sub());
updateSubStep(instructions.sub(), primaryManeuverModifier);
}
}

Expand Down Expand Up @@ -605,63 +604,7 @@ private boolean newStep(RouteProgress routeProgress) {
return newStep;
}

/**
* Looks for turn lane data and populates / shows the turn lane view if found.
* If not, hides the turn lane view.
*
* @param model created with new {@link RouteProgress} holding turn lane data
*/
private void updateTurnLanes(InstructionModel model) {
List<IntersectionLanes> turnLanes = model.retrieveTurnLanes();
String maneuverViewModifier = model.retrieveUpcomingManeuverModifier();
double durationRemaining = model.retrieveProgress().currentLegProgress().currentStepProgress().durationRemaining();

if (shouldShowTurnLanes(turnLanes, maneuverViewModifier, durationRemaining)) {
if (turnLaneLayout.getVisibility() == GONE) {
turnLaneAdapter.addTurnLanes(turnLanes, maneuverViewModifier);
showTurnLanes();
}
} else {
hideTurnLanes();
}
}

private boolean shouldShowTurnLanes(List<IntersectionLanes> turnLanes,
String maneuverViewModifier, double durationRemaining) {
return subStepLayout.getVisibility() != VISIBLE
&& turnLanes != null
&& !TextUtils.isEmpty(maneuverViewModifier)
&& durationRemaining <= VALID_DURATION_REMAINING;
}

/**
* Shows turn lane view
*/
private void showTurnLanes() {
if (turnLaneLayout.getVisibility() == GONE) {
beginDelayedTransition();
turnLaneLayout.setVisibility(VISIBLE);
}
}

/**
* Hides turn lane view
*/
private void hideTurnLanes() {
if (turnLaneLayout.getVisibility() == VISIBLE) {
beginDelayedTransition();
turnLaneLayout.setVisibility(GONE);
}
}

/**
* Check if the the sub step should be shown.
* If true, update the "then" maneuver and the "then" step text.
* If false, hide the then layout.
*
* @param subText to determine if the then step layout should be shown
*/
private void updateSubStep(BannerText subText) {
private void updateSubStep(BannerText subText, String primaryManeuverModifier) {
if (shouldShowSubStep(subText)) {
String maneuverType = subText.type();
String maneuverModifier = subText.modifier();
Expand All @@ -675,15 +618,23 @@ private void updateSubStep(BannerText subText) {
instructionLoader.loadInstruction();
}
showSubLayout();
return;
} else {
hideSubLayout();
}

if (shouldShowTurnLanes(subText, primaryManeuverModifier)) {
turnLaneAdapter.addTurnLanes(subText.components(), primaryManeuverModifier);
showTurnLanes();
} else {
hideTurnLanes();
}
}

private boolean shouldShowSubStep(@Nullable BannerText subText) {
return turnLaneLayout.getVisibility() != VISIBLE
&& subText != null
&& subText.type() != null;
return subText != null
&& subText.type() != null
&& !subText.type().contains(COMPONENT_TYPE_LANE);
}

private void showSubLayout() {
Expand All @@ -700,6 +651,32 @@ private void hideSubLayout() {
}
}

private boolean shouldShowTurnLanes(BannerText subText, String maneuverModifier) {
if (!hasComponents(subText) || TextUtils.isEmpty(maneuverModifier)) {
return false;
}
for (BannerComponents components : subText.components()) {
if (components.type().equals(COMPONENT_TYPE_LANE)) {
return true;
}
}
return false;
}

private void showTurnLanes() {
if (turnLaneLayout.getVisibility() == GONE) {
beginDelayedTransition();
turnLaneLayout.setVisibility(VISIBLE);
}
}

private void hideTurnLanes() {
if (turnLaneLayout.getVisibility() == VISIBLE) {
beginDelayedTransition();
turnLaneLayout.setVisibility(GONE);
}
}

@Nullable
private FragmentManager obtainSupportFragmentManager() {
try {
Expand Down Expand Up @@ -743,7 +720,6 @@ private void cancelDelayedTransition() {
private void updateDataFromInstruction(InstructionModel model) {
updateDistanceText(model);
updateInstructionList(model);
updateTurnLanes(model);
if (newStep(model.retrieveProgress())) {
LegStep upComingStep = model.retrieveProgress().currentLegProgress().upComingStep();
ImageCoordinator.getInstance().prefetchImageCache(upComingStep);
Expand Down
Loading