Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust (port / land) Top Banners and add secondary text view #404

Merged
merged 3 commits into from
Oct 20, 2017
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 @@ -11,17 +11,20 @@
import com.mapbox.services.commons.utils.TextUtils;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

import static com.mapbox.services.android.navigation.v5.utils.ManeuverUtils.getManeuverResource;

public class InstructionModel {

private SpannableStringBuilder stepDistanceRemaining;
private String textInstruction;
private String primaryText;
private String secondaryText;
private int maneuverImage;
private String maneuverModifier;
private List<IntersectionLanes> turnLanes;
private boolean isUsingInstruction;

public InstructionModel(RouteProgress progress, DecimalFormat decimalFormat) {
buildInstructionModel(progress, decimalFormat);
Expand All @@ -31,20 +34,28 @@ SpannableStringBuilder getStepDistanceRemaining() {
return stepDistanceRemaining;
}

String getTextInstruction() {
return textInstruction;
String getPrimaryText() {
return primaryText;
}

String getSecondaryText() {
return secondaryText;
}

int getManeuverImage() {
return maneuverImage;
}

String getManeuverModifier() {
return maneuverModifier;
}

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

String getManeuverModifier() {
return maneuverModifier;
boolean isUsingInstruction() {
return isUsingInstruction;
}

private void buildInstructionModel(RouteProgress progress, DecimalFormat decimalFormat) {
Expand All @@ -58,12 +69,12 @@ private void buildInstructionModel(RouteProgress progress, DecimalFormat decimal
private void extractStepResources(LegStep upComingStep) {
maneuverImage = getManeuverResource(upComingStep);
if (hasManeuver(upComingStep)) {
buildTextInstruction(upComingStep);
maneuverModifier = upComingStep.maneuver().modifier();
}
if (hasIntersections(upComingStep)) {
intersectionTurnLanes(upComingStep);
}
buildTextInstructions(upComingStep);
}

private void formatStepDistance(RouteProgress progress, DecimalFormat decimalFormat) {
Expand Down Expand Up @@ -104,38 +115,103 @@ private boolean hasIntersections(LegStep upComingStep) {
&& upComingStep.intersections().get(0) != null;
}

private void buildTextInstruction(LegStep upComingStep) {
if (hasDestinations(upComingStep)) {
destinationInstruction(upComingStep);
private void buildTextInstructions(LegStep upComingStep) {
String exitText = "";

// Extract Exit for later use
if (upComingStep.maneuver() != null) {
if (!TextUtils.isEmpty(upComingStep.exits())) {
exitText = "Exit " + upComingStep.exits();
}
}

// Refs
if (hasRefs(upComingStep)) {
primaryText = StringAbbreviator.deliminator(upComingStep.ref());
if (hasDestination(upComingStep)) {
secondaryText = destinations(upComingStep);
}
return;
}

// Multiple Destinations
if (hasMultipleDestinations(upComingStep)) {
formatMultipleStrings(upComingStep.destinations(), exitText);
return;
}

// Multiple Names
if (hasMultipleNames(upComingStep)) {
formatMultipleStrings(upComingStep.name(), exitText);
return;
}

// Destination or Street Name
if (hasDestination(upComingStep)) {
primaryText = destinations(upComingStep);
return;
} else if (hasName(upComingStep)) {
nameInstruction(upComingStep);
} else if (hasManeuverInstruction(upComingStep)) {
maneuverInstruction(upComingStep);
primaryText = names(upComingStep);
return;
}

// Instruction
if (hasInstruction(upComingStep)) {
primaryText = instruction(upComingStep);
isUsingInstruction = true;
}
}

private void maneuverInstruction(LegStep upComingStep) {
textInstruction = upComingStep.maneuver().instruction();
private boolean hasRefs(LegStep upComingStep) {
return !TextUtils.isEmpty(upComingStep.ref());
}

private boolean hasManeuverInstruction(LegStep upComingStep) {
return !TextUtils.isEmpty(upComingStep.maneuver().instruction());
private String instruction(LegStep upComingStep) {
return upComingStep.maneuver().instruction();
}

private void nameInstruction(LegStep upComingStep) {
textInstruction = upComingStep.name();
private boolean hasInstruction(LegStep upComingStep) {
return upComingStep.maneuver() != null
&& !TextUtils.isEmpty(upComingStep.maneuver().instruction());
}

private String names(LegStep upComingStep) {
String instruction = upComingStep.name().trim();
return StringAbbreviator.deliminator(instruction);
}

private boolean hasName(LegStep upComingStep) {
return !TextUtils.isEmpty(upComingStep.name());
}

private void destinationInstruction(LegStep upComingStep) {
textInstruction = upComingStep.destinations().trim();
textInstruction = StringAbbreviator.deliminator(textInstruction);
private String destinations(LegStep upComingStep) {
String instruction = upComingStep.destinations().trim();
return StringAbbreviator.deliminator(instruction);
}

private boolean hasDestinations(LegStep upComingStep) {
private boolean hasDestination(LegStep upComingStep) {
return !TextUtils.isEmpty(upComingStep.destinations());
}

private boolean hasMultipleDestinations(LegStep upComingStep) {
return !TextUtils.isEmpty(upComingStep.destinations())
&& StringAbbreviator.splitter(upComingStep.destinations()).length > 1;
}

private boolean hasMultipleNames(LegStep upComingStep) {
return !TextUtils.isEmpty(upComingStep.name())
&& StringAbbreviator.splitter(upComingStep.name()).length > 1;
}

private void formatMultipleStrings(String multipleString, String exitText) {
String[] strings = StringAbbreviator.splitter(multipleString);
String[] firstString = Arrays.copyOfRange(strings, 0, 1);
if (!TextUtils.isEmpty(exitText)) {
primaryText = exitText + " " + firstString[0];
} else {
primaryText = firstString[0];
}
String[] remainingStrings = Arrays.copyOfRange(strings, 1, strings.length);
secondaryText = TextUtils.join(" / ", remainingStrings).trim();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import android.support.v4.widget.TextViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
Expand Down Expand Up @@ -59,7 +58,8 @@ public class InstructionView extends RelativeLayout {

private ImageView maneuverImage;
private TextView stepDistanceText;
private TextView stepInstructionText;
private TextView stepPrimaryText;
private TextView stepSecondaryText;
private TextView soundChipText;
private FloatingActionButton soundFab;
private View rerouteLayout;
Expand All @@ -73,9 +73,8 @@ public class InstructionView extends RelativeLayout {
private AnimationSet fadeInSlowOut;

private DecimalFormat decimalFormat;
private String currentInstruction;
private int currentManeuverId;
private SpannableStringBuilder currentDistanceText;
private int primaryTextMaxLines = 1;
private boolean turnLanesHidden;
private boolean isRerouting;
public boolean isMuted;
Expand Down Expand Up @@ -230,7 +229,8 @@ private void init() {
private void bind() {
maneuverImage = findViewById(R.id.maneuverImageView);
stepDistanceText = findViewById(R.id.stepDistanceText);
stepInstructionText = findViewById(R.id.stepInstructionText);
stepPrimaryText = findViewById(R.id.stepPrimaryText);
stepSecondaryText = findViewById(R.id.stepSecondaryText);
soundChipText = findViewById(R.id.soundText);
soundFab = findViewById(R.id.soundFab);
rerouteLayout = findViewById(R.id.rerouteLayout);
Expand Down Expand Up @@ -340,8 +340,12 @@ private void showSoundChip() {
* to automatically re-size based on the length of the text.
*/
private void initInstructionAutoSize() {
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(stepInstructionText,
16, 28, 2, TypedValue.COMPLEX_UNIT_SP);
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(stepPrimaryText,
24, 30, 1, TypedValue.COMPLEX_UNIT_SP);
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(stepSecondaryText,
20, 26, 1, TypedValue.COMPLEX_UNIT_SP);
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(stepDistanceText,
16, 20, 1, TypedValue.COMPLEX_UNIT_SP);
}

/**
Expand Down Expand Up @@ -408,7 +412,7 @@ private void addManeuverImage(InstructionModel model) {
private void addDistanceText(InstructionModel model) {
if (newDistanceText(model)) {
distanceText(model);
} else if (currentDistanceText == null) {
} else if (stepDistanceText.getText().toString().isEmpty()) {
distanceText(model);
}
}
Expand All @@ -419,9 +423,10 @@ private void addDistanceText(InstructionModel model) {
* @param model provides distance text
*/
private boolean newDistanceText(InstructionModel model) {
return currentDistanceText != null
return !stepDistanceText.getText().toString().isEmpty()
&& !TextUtils.isEmpty(model.getStepDistanceRemaining())
&& !currentDistanceText.toString().contentEquals(model.getStepDistanceRemaining().toString());
&& !stepDistanceText.getText().toString()
.contentEquals(model.getStepDistanceRemaining().toString());
}

/**
Expand All @@ -430,7 +435,6 @@ private boolean newDistanceText(InstructionModel model) {
* @param model provides distance text
*/
private void distanceText(InstructionModel model) {
currentDistanceText = model.getStepDistanceRemaining();
stepDistanceText.setText(model.getStepDistanceRemaining());
}

Expand All @@ -441,32 +445,75 @@ private void distanceText(InstructionModel model) {
* @param model provides instruction text
*/
private void addTextInstruction(InstructionModel model) {
if (newTextInstruction(model)) {
textInstruction(model);
} else if (currentInstruction == null) {
textInstruction(model);
updateMaxLines(model);
if (newPrimaryText(model) || newSecondaryText(model)) {
textInstructions(model);
} else if (stepPrimaryText.getText().toString().isEmpty()
|| stepSecondaryText.getText().toString().isEmpty()) {
textInstructions(model);
}
}

/**
* Looks to see if we have a new instruction text.
* Based on a boolean from the model,
* update to 2 lines for primary text.
* <p>
* Track the lines so the max can revert to 1.
*
* @param model provides instruction text
* @param model the cue to go to 2 lines
*/
private void updateMaxLines(InstructionModel model) {
if (model.isUsingInstruction()) {
stepPrimaryText.setMaxLines(2);
primaryTextMaxLines = 2;
} else if (primaryTextMaxLines == 2) {
stepPrimaryText.setMaxLines(1);
}
}

/**
* Looks to see if we have a new primary instruction text.
*
* @param model provides primary instruction text
*/
private boolean newTextInstruction(InstructionModel model) {
return currentInstruction != null
&& !TextUtils.isEmpty(model.getTextInstruction())
&& !currentInstruction.contentEquals(model.getTextInstruction());
private boolean newPrimaryText(InstructionModel model) {
// New primaryText instruction
String currentPrimaryText = stepPrimaryText.getText().toString();
return !currentPrimaryText.isEmpty()
&& !TextUtils.isEmpty(model.getPrimaryText())
&& !currentPrimaryText.contentEquals(model.getPrimaryText());
}

/**
* Looks to see if we have a new secondary instruction text.
*
* @param model provides secondary instruction text
*/
private boolean newSecondaryText(InstructionModel model) {
// New primaryText instruction
String currentSecondaryText = stepSecondaryText.getText().toString();
return !currentSecondaryText.isEmpty()
&& !TextUtils.isEmpty(model.getSecondaryText())
&& !currentSecondaryText.contentEquals(model.getSecondaryText());
}

/**
* Sets current instruction text.
*
* @param model provides instruction text
*/
private void textInstruction(InstructionModel model) {
currentInstruction = model.getTextInstruction();
stepInstructionText.setText(StringAbbreviator.abbreviate(model.getTextInstruction()));
private void textInstructions(InstructionModel model) {
if (!TextUtils.isEmpty(model.getPrimaryText())) {
stepPrimaryText.setText(StringAbbreviator.abbreviate(model.getPrimaryText()));
}
if (!TextUtils.isEmpty(model.getSecondaryText())) {
if (stepSecondaryText.getVisibility() == GONE) {
stepSecondaryText.setVisibility(VISIBLE);
}
stepSecondaryText.setText(StringAbbreviator.abbreviate(model.getSecondaryText()));
} else {
stepSecondaryText.setVisibility(GONE);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="@color/md_grey_200" />

<corners android:bottomRightRadius="8dp"/>

</shape>
Loading