-
Notifications
You must be signed in to change notification settings - Fork 319
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
Conversation
@devotaaabel @Guardiola31337 this is also good for 👀 |
} | ||
} | ||
|
||
private void cacheRouteOptions(RouteOptions routeOptions) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Lines 159 to 161 in cb1af2f
cacheRouteOptions(route.routeOptions()); | |
cacheRouteProfile(options); | |
cacheRouteLanguage(options, route); |
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isEmpty
check is redundant
There was a problem hiding this comment.
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
👀
Lines 117 to 120 in cb1af2f
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()); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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
There was a problem hiding this 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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
👀
Lines 117 to 120 in cb1af2f
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about fixing waypoints
Line 39 in cb1af2f
List<Point> waypoints = RouteUtils.calculateRemainingWaypoints(routeProgress); |
remaining
and remove both comments (here and L38)?
if (coordinates.size() < routeProgress.remainingWaypoints()) { | ||
return null; | ||
} | ||
// Remove any waypoints that have been passed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment needed?
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 { | ||
|
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Magic number
Closes #609
cc @ericrwolfe