Skip to content

Commit

Permalink
simplify find current banner and voice instructions algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
dsilvera authored and Guardiola31337 committed Jul 18, 2018
1 parent f52ebe7 commit 360d69a
Showing 1 changed file with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import com.mapbox.services.android.navigation.v5.milestone.Milestone;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

Expand All @@ -30,6 +31,7 @@ public class RouteUtils {
private static final String FORCED_LOCATION = "Forced Location";
private static final int FIRST_COORDINATE = 0;
private static final int ZERO_INSTRUCTIONS = 0;
private static final int FIRST_INSTRUCTION = 0;
private static final Set<String> VALID_PROFILES = new HashSet<String>() {
{
add(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC);
Expand Down Expand Up @@ -177,25 +179,34 @@ public boolean isValidRouteProfile(String routeProfile) {
*/
@Nullable
public BannerInstructions findCurrentBannerInstructions(LegStep currentStep, double stepDistanceRemaining) {
if (isValidStep(currentStep) && hasInstructions(currentStep.bannerInstructions())) {
int roundedDistanceRemaining = (int) stepDistanceRemaining;
List<BannerInstructions> instructions = new ArrayList<>(currentStep.bannerInstructions());
Iterator<BannerInstructions> instructionsIterator = instructions.iterator();
while (instructionsIterator.hasNext()) {
BannerInstructions instruction = instructionsIterator.next();
if (isValidBannerInstructions(currentStep)) {
List<BannerInstructions> instructions = sortBannerInstructions(currentStep.bannerInstructions());
for (BannerInstructions instruction : instructions) {
double distanceAlongGeometry = instruction.distanceAlongGeometry();
if (distanceAlongGeometry < roundedDistanceRemaining) {
instructionsIterator.remove();
if (distanceAlongGeometry >= stepDistanceRemaining) {
return instruction;
}
}
int instructionIndex = checkValidIndex(instructions);
if (instructions.size() > ZERO_INSTRUCTIONS) {
return instructions.get(instructionIndex);
}
return instructions.get(FIRST_INSTRUCTION);
}
return null;
}

private boolean isValidBannerInstructions(LegStep currentStep) {
return isValidStep(currentStep) && hasInstructions(currentStep.bannerInstructions());
}

private List<BannerInstructions> sortBannerInstructions(List<BannerInstructions> instructions) {
List<BannerInstructions> sortedInstructions = new ArrayList<>(instructions);
Collections.sort(sortedInstructions, new Comparator<BannerInstructions>() {
@Override
public int compare(BannerInstructions instructions, BannerInstructions nextInstructions) {
return Double.compare(instructions.distanceAlongGeometry(), nextInstructions.distanceAlongGeometry());
}
});
return sortedInstructions;
}

/**
* This method returns the current {@link BannerText} based on the currentStep distance
* remaining.
Expand Down Expand Up @@ -229,24 +240,34 @@ public BannerText findCurrentBannerText(LegStep currentStep, double stepDistance
*/
@Nullable
public VoiceInstructions findCurrentVoiceInstructions(LegStep currentStep, double stepDistanceRemaining) {
if (isValidStep(currentStep) && hasInstructions(currentStep.voiceInstructions())) {
List<VoiceInstructions> instructions = new ArrayList<>(currentStep.voiceInstructions());
Iterator<VoiceInstructions> instructionsIterator = instructions.iterator();
while (instructionsIterator.hasNext()) {
VoiceInstructions instruction = instructionsIterator.next();
if (isValidVoiceInstructions(currentStep)) {
List<VoiceInstructions> instructions = sortVoiceInstructions(currentStep.voiceInstructions());
for (VoiceInstructions instruction : instructions) {
double distanceAlongGeometry = instruction.distanceAlongGeometry();
if (distanceAlongGeometry < stepDistanceRemaining) {
instructionsIterator.remove();
if (distanceAlongGeometry >= stepDistanceRemaining) {
return instruction;
}
}
int instructionIndex = checkValidIndex(instructions);
if (instructions.size() > ZERO_INSTRUCTIONS) {
return instructions.get(instructionIndex);
}
return instructions.get(FIRST_INSTRUCTION);
}
return null;
}

private boolean isValidVoiceInstructions(LegStep currentStep) {
return isValidStep(currentStep) && hasInstructions(currentStep.voiceInstructions());
}

private List<VoiceInstructions> sortVoiceInstructions(List<VoiceInstructions> instructions) {
List<VoiceInstructions> sortedInstructions = new ArrayList<>(instructions);
Collections.sort(sortedInstructions, new Comparator<VoiceInstructions>() {
@Override
public int compare(VoiceInstructions instructions, VoiceInstructions nextInstructions) {
return Double.compare(instructions.distanceAlongGeometry(), nextInstructions.distanceAlongGeometry());
}
});
return sortedInstructions;
}

private boolean upcomingStepIsArrivalManeuverType(@NonNull RouteProgress routeProgress) {
return routeProgress.currentLegProgress().upComingStep() != null
&& routeProgress.currentLegProgress().upComingStep().maneuver().type().contains(STEP_MANEUVER_TYPE_ARRIVE);
Expand Down

0 comments on commit 360d69a

Please sign in to comment.