|
1 | 1 | package com.mapbox.navigation.ui.maps.building |
2 | 2 |
|
3 | 3 | import com.mapbox.bindgen.ExpectedFactory |
| 4 | +import com.mapbox.geojson.Point |
4 | 5 | import com.mapbox.maps.RenderedQueryOptions |
5 | 6 | import com.mapbox.navigation.base.internal.extensions.isLegWaypoint |
| 7 | +import com.mapbox.navigation.base.internal.extensions.isRequestedWaypoint |
6 | 8 | import com.mapbox.navigation.base.internal.utils.internalWaypoints |
| 9 | +import com.mapbox.navigation.base.trip.model.RouteProgress |
7 | 10 | import com.mapbox.navigation.ui.maps.building.model.BuildingError |
8 | 11 | import com.mapbox.navigation.ui.maps.building.model.BuildingValue |
9 | 12 | import kotlin.coroutines.resume |
@@ -48,18 +51,34 @@ internal object BuildingProcessor { |
48 | 51 | fun queryBuildingOnWaypoint( |
49 | 52 | action: BuildingAction.QueryBuildingOnWaypoint |
50 | 53 | ): BuildingResult.GetDestination { |
51 | | - val waypoints = action.progress.navigationRoute.internalWaypoints() |
52 | 54 | val waypointIndex = action.progress.currentLegProgress?.legIndex!! + 1 |
53 | | - val waypoint = waypoints.filter { it.isLegWaypoint() }.getOrNull(waypointIndex) |
54 | | - return BuildingResult.GetDestination(waypoint?.target ?: waypoint?.location) |
| 55 | + val buildingLocation = getBuildingLocation(waypointIndex, action.progress) |
| 56 | + return BuildingResult.GetDestination(buildingLocation) |
55 | 57 | } |
56 | 58 |
|
57 | 59 | fun queryBuildingOnFinalDestination( |
58 | 60 | action: BuildingAction.QueryBuildingOnFinalDestination |
59 | 61 | ): BuildingResult.GetDestination { |
60 | | - val lastWaypoint = action.progress.navigationRoute |
61 | | - .internalWaypoints() |
62 | | - .lastOrNull() |
63 | | - return BuildingResult.GetDestination(lastWaypoint?.target ?: lastWaypoint?.location) |
| 62 | + val waypointIndex = action.progress.navigationRoute.internalWaypoints() |
| 63 | + .indexOfLast { it.isLegWaypoint() } |
| 64 | + val buildingLocation = getBuildingLocation(waypointIndex, action.progress) |
| 65 | + return BuildingResult.GetDestination(buildingLocation) |
| 66 | + } |
| 67 | + |
| 68 | + private fun getBuildingLocation(legWaypointIndex: Int, progress: RouteProgress): Point? { |
| 69 | + val waypoints = progress.navigationRoute.internalWaypoints() |
| 70 | + val legWaypoints = waypoints.filter { it.isLegWaypoint() } |
| 71 | + val waypoint = legWaypoints.getOrNull(legWaypointIndex) |
| 72 | + if (waypoint == null) return null |
| 73 | + if (waypoint.target != null) { |
| 74 | + return waypoint.target |
| 75 | + } |
| 76 | + if (!waypoint.isRequestedWaypoint()) { |
| 77 | + return waypoint.location |
| 78 | + } |
| 79 | + val nonRequestedWaypointsCount = legWaypoints.take(legWaypointIndex + 1) |
| 80 | + .count { !it.isRequestedWaypoint() } |
| 81 | + return progress.navigationRoute.routeOptions.coordinatesList() |
| 82 | + .getOrNull(legWaypointIndex - nonRequestedWaypointsCount) |
64 | 83 | } |
65 | 84 | } |
0 commit comments