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

Cache route options / calculate remaining waypoints #680

Merged
merged 10 commits into from
Feb 16, 2018

Conversation

danesfeder
Copy link
Contributor

Closes #609

cc @ericrwolfe

@danesfeder danesfeder added the bug Defect to be fixed. label Jan 29, 2018
@danesfeder danesfeder added this to the v0.10.0 milestone Jan 29, 2018
@danesfeder danesfeder self-assigned this Jan 29, 2018
@danesfeder
Copy link
Contributor Author

@devotaaabel @Guardiola31337 this is also good for 👀

}
}

private void cacheRouteOptions(RouteOptions routeOptions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really need it's own method? I'm not sure cacheRouteDestination() fits under the name of this method either

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about encapsulating all cache-related calls included in extractRouteFromOptions method

cacheRouteOptions(route.routeOptions());
cacheRouteProfile(options);
cacheRouteLanguage(options, route);
into a cache method and fixing the naming of these methods (concrete names) so that they reflect clearer what they're doing in this context?

private void cacheRouteProfile(NavigationViewOptions options, DirectionsRoute route) {
String profile = options.directionsProfile();
routeProfile = profile != null ? profile : route.routeOptions().profile();
private void cacheRouteProfile(NavigationViewOptions options) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this needs it's own method either since the logic has been removed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't see why not if it makes the intention clearer and the code easy to read and understand.

@@ -240,4 +234,21 @@ private boolean validRouteResponse(Response<DirectionsResponse> response) {
return response.body() != null
&& !response.body().routes().isEmpty();
}

private void addWaypoints(List<Point> remainingCoordinates, NavigationRoute.Builder builder) {
if (!remainingCoordinates.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isEmpty check is redundant

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we're already checking that right before calling addWaypoints 👀

if (!remainingWaypoints.isEmpty()) {
builder.destination(remainingWaypoints.remove(remainingWaypoints.size() - 1));
addWaypoints(remainingWaypoints, builder);
}

We should remove from either here or fetchRouteFromOffRouteEvent, the latter preferable (making public fetchRouteFromOffRouteEvent method cleaner).

@@ -35,15 +36,15 @@ public void fetchRoute(Point origin, RouteProgress routeProgress) {
this.routeProgress = routeProgress;

// Calculate remaining waypoints
List<Point> coordinates = new ArrayList<>(routeProgress.directionsRoute().routeOptions().coordinates());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method seems really similar to the one in RouteViewModel, maybe some of it could be extracted into NavigationRoute? Or maybe somewhere else

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah definitely a lot of duplicate here, good call

@@ -74,14 +76,36 @@ public static boolean isLastLeg(RouteProgress routeProgress) {
return currentLeg.equals(legs.get(legs.size() - 1));
}

/**
* Given a {@link RouteProgress}, this method will calculate the remaining coordinates
* along the given route based on total route coordinates and the progress remaining waypoints.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to javadoc cases that it returns null

Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 good opportunity to apply The Boy Scout Rule
Let's do it 😉

if (newOrigin != null) {
public void onChanged(@Nullable OffRouteEvent offRouteEvent) {
if (offRouteEvent != null) {
Point newOrigin = offRouteEvent.getNewOrigin();
if (navigationViewEventDispatcher.allowRerouteFrom(newOrigin)) {
// Send off route event with new origin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not adding any value, it should be removed.
IMO looking at navigationViewEventDispatcher.onOffRoute(newOrigin) you know what's going on making the comment superfluous and not needed.

if (newOrigin != null) {
public void onChanged(@Nullable OffRouteEvent offRouteEvent) {
if (offRouteEvent != null) {
Point newOrigin = offRouteEvent.getNewOrigin();
if (navigationViewEventDispatcher.allowRerouteFrom(newOrigin)) {
// Send off route event with new origin
navigationViewEventDispatcher.onOffRoute(newOrigin);
// Fetch a new route with the given origin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you update the functionality but not the comment, the comment becomes out of date and it starts lying. This is an example of why I'm not a big fan of comments :trollface:

if (navigationViewEventDispatcher.allowRerouteFrom(newOrigin)) {
// Send off route event with new origin
navigationViewEventDispatcher.onOffRoute(newOrigin);
// Fetch a new route with the given origin
routeViewModel.fetchRouteNewOrigin(newOrigin);
routeViewModel.fetchRouteFromOffRouteEvent(offRouteEvent);
// To prevent from firing on rotation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to make the intention clearer, what about extracting this call into a private method and give it a name based on the comment? This way onChanged will be easier to read and understand and comment won't be necessary + anyone who looks at the code will know what's going on at a glance.

private void fetchRoute(Point origin, Point destination) {
if (origin != null && destination != null) {

public void fetchRouteFromOffRouteEvent(OffRouteEvent offRouteEvent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO fetchRouteFromOffRouteEvent is getting too long.
What about extracting blocks of code into well-named (based on the comments) private methods and simplifying conditional expressions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT OffRouteEvent offRouteEvent is kind of repetitive. Calling it just event should be fine, IMO it's clear based on the context that it's an OffRouteEvent.

.accessToken(Mapbox.getAccessToken())
.origin(origin)
.destination(destination);
// Add any overridden values based on NavigationViewOptions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not adding any value, it should be removed.
IMO looking at addNavigationViewOptions(builder) you know what's going on making the comment superfluous and not needed.

@@ -240,4 +234,21 @@ private boolean validRouteResponse(Response<DirectionsResponse> response) {
return response.body() != null
&& !response.body().routes().isEmpty();
}

private void addWaypoints(List<Point> remainingCoordinates, NavigationRoute.Builder builder) {
if (!remainingCoordinates.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we're already checking that right before calling addWaypoints 👀

if (!remainingWaypoints.isEmpty()) {
builder.destination(remainingWaypoints.remove(remainingWaypoints.size() - 1));
addWaypoints(remainingWaypoints, builder);
}

We should remove from either here or fetchRouteFromOffRouteEvent, the latter preferable (making public fetchRouteFromOffRouteEvent method cleaner).

@@ -35,15 +36,15 @@ public void fetchRoute(Point origin, RouteProgress routeProgress) {
this.routeProgress = routeProgress;

// Calculate remaining waypoints
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not adding any value, it should be removed.

return;
}
// Remove any waypoints that have been passed
coordinates.subList(0, routeProgress.remainingWaypoints()).clear();

// Get the destination waypoint (last in the list)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we extract waypoints.size() - 1 into an explaining variable called last and remove the comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it's kinda confusing that calling remove returns the element removed so maybe it's preferable to extract waypoints.remove(waypoints.size() - 1) into a method reflecting that. What do you think?

@@ -52,7 +53,7 @@ public void fetchRoute(Point origin, RouteProgress routeProgress) {
.routeOptions(currentOptions);

// Add waypoints with the remaining coordinate values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about fixing waypoints

List<Point> waypoints = RouteUtils.calculateRemainingWaypoints(routeProgress);
name into something like remaining and remove both comments (here and L38)?

if (coordinates.size() < routeProgress.remainingWaypoints()) {
return null;
}
// Remove any waypoints that have been passed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment needed?

Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job refactoring the code 🙇

There is still room for improvement (minor comments below) but definitely not blocking the PR.

@Override
public void onChanged(@Nullable Point newOrigin) {
if (newOrigin != null) {
public void onChanged(@Nullable OffRouteEvent offRouteEvent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT OffRouteEvent offRouteEvent is kind of repetitive. Calling it just event should be fine, IMO it's clear based on the context that it's an OffRouteEvent.

private void fetchRoute(Point origin, Point destination) {
if (origin != null && destination != null) {

public void fetchRouteFromOffRouteEvent(OffRouteEvent offRouteEvent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT OffRouteEvent offRouteEvent is kind of repetitive. Calling it just event should be fine, IMO it's clear based on the context that it's an OffRouteEvent.


@Test
public void addRouteOptionsIncludedInRequest() throws Exception {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT Empty line

RouteProgress progress) {
RouteOptions options = progress.directionsRoute().routeOptions();
NavigationRoute.Builder builder = NavigationRoute.builder()
.origin(origin, bearing, 90d)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic number

}

private static Point retrieveDestinationWaypoint(List<Point> remainingWaypoints) {
int lastWaypoint = remainingWaypoints.size() - 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic number

private void cacheRouteDestination() {
if (routeOptions != null && !routeOptions.coordinates().isEmpty()) {
List<Point> coordinates = routeOptions.coordinates();
Point destinationPoint = coordinates.get(coordinates.size() - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic number

@danesfeder danesfeder merged commit 48f2bf0 into master Feb 16, 2018
@danesfeder danesfeder deleted the dan-dropped-waypoints branch February 16, 2018 17:27
@danesfeder danesfeder mentioned this pull request Feb 26, 2018
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Defect to be fixed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants