Skip to content

Commit 4caf1be

Browse files
committed
- Added Waypoints to NavigationRoute; - RouteOptionsUpdater refactored;
1 parent a43c3a4 commit 4caf1be

File tree

15 files changed

+709
-182
lines changed

15 files changed

+709
-182
lines changed

libnavigation-base/src/main/java/com/mapbox/navigation/base/internal/NativeRouteParserWrapper.kt

+15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ interface SDKRouteParser {
1212
request: String,
1313
routerOrigin: RouterOrigin,
1414
): Expected<String, List<RouteInterface>>
15+
16+
fun parseDirectionsRoutes(
17+
directionsRoutes: String,
18+
request: String,
19+
routerOrigin: RouterOrigin,
20+
): Expected<String, List<RouteInterface>>
1521
}
1622

1723
object NativeRouteParserWrapper : SDKRouteParser {
@@ -23,4 +29,13 @@ object NativeRouteParserWrapper : SDKRouteParser {
2329
RouteParser.parseDirectionsResponse(
2430
response, request, routerOrigin.mapToNativeRouteOrigin()
2531
)
32+
33+
override fun parseDirectionsRoutes(
34+
directionsRoutes: String,
35+
request: String,
36+
routerOrigin: RouterOrigin,
37+
): Expected<String, List<RouteInterface>> =
38+
RouteParser.parseDirectionsRoutes(
39+
directionsRoutes, request, routerOrigin.mapToNativeRouteOrigin()
40+
)
2641
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@file:JvmName("WaypointEx")
2+
3+
package com.mapbox.navigation.base.internal.extensions
4+
5+
import com.mapbox.api.directions.v5.models.DirectionsRoute
6+
import com.mapbox.navigation.base.internal.route.Waypoint
7+
import com.mapbox.navigation.base.trip.model.RouteProgress
8+
9+
/**
10+
* Return waypoints that are requested explicitly
11+
*/
12+
fun List<Waypoint>.requestedWaypoints(): List<Waypoint> =
13+
this.filter { it.type != Waypoint.EV_CHARGING }
14+
15+
/**
16+
* Return waypoints that tracking in [RouteProgress.currentLegProgress]#legIndex and based on
17+
* [DirectionsRoute.legs] index
18+
*/
19+
fun List<Waypoint>.legsWaypoints(): List<Waypoint> =
20+
this.filter { it.type != Waypoint.SILENT }
21+
22+
fun indexOfNextCoordinate(
23+
waypoints: List<Waypoint>,
24+
remainingWaypoints: Int
25+
): Int? {
26+
if (remainingWaypoints > waypoints.size) {
27+
return null
28+
}
29+
val nextRequestedWaypoint = waypoints
30+
.subList(waypoints.size - remainingWaypoints, waypoints.size)
31+
.requestedWaypoints()
32+
.firstOrNull()
33+
?: return null
34+
35+
return waypoints
36+
.requestedWaypoints()
37+
.indexOf(nextRequestedWaypoint)
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.mapbox.navigation.base.internal.route
2+
3+
import androidx.annotation.IntDef
4+
import com.mapbox.geojson.Point
5+
6+
data class Waypoint(
7+
val location: Point,
8+
@Type val type: Int,
9+
val name: String,
10+
val target: Point?,
11+
) {
12+
companion object {
13+
const val REGULAR = 1
14+
const val SILENT = 2
15+
const val EV_CHARGING = 3
16+
}
17+
18+
@Target(
19+
AnnotationTarget.PROPERTY,
20+
AnnotationTarget.VALUE_PARAMETER,
21+
AnnotationTarget.FUNCTION,
22+
AnnotationTarget.TYPE
23+
)
24+
@Retention(AnnotationRetention.BINARY)
25+
@IntDef(REGULAR, SILENT, EV_CHARGING)
26+
annotation class Type
27+
}

libnavigation-base/src/main/java/com/mapbox/navigation/base/internal/utils/DirectionsRouteEx.kt

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.mapbox.navigation.base.internal.utils
44

55
import com.mapbox.api.directions.v5.models.DirectionsRoute
66
import com.mapbox.api.directions.v5.models.LegStep
7+
import com.mapbox.navigation.base.internal.route.Waypoint
78
import com.mapbox.navigation.base.utils.ifNonNull
89

910
/**
@@ -36,3 +37,21 @@ private fun DirectionsRoute.stepsNamesAsString(): String? =
3637
?.joinToString { leg ->
3738
leg.steps()?.joinToString { step -> step.name() ?: "" } ?: ""
3839
}
40+
41+
internal fun List<com.mapbox.navigator.Waypoint>.mapToSkd(): List<Waypoint> =
42+
map { nativeWaypoint ->
43+
Waypoint(
44+
location = nativeWaypoint.location,
45+
type = nativeWaypoint.type.mapToSdk(),
46+
name = nativeWaypoint.name,
47+
target = nativeWaypoint.target,
48+
)
49+
}
50+
51+
@Waypoint.Type
52+
private fun com.mapbox.navigator.WaypointType.mapToSdk(): Int =
53+
when (this) {
54+
com.mapbox.navigator.WaypointType.REGULAR -> Waypoint.REGULAR
55+
com.mapbox.navigator.WaypointType.SILENT -> Waypoint.SILENT
56+
com.mapbox.navigator.WaypointType.EV_CHARGING -> Waypoint.EV_CHARGING
57+
}

libnavigation-base/src/main/java/com/mapbox/navigation/base/internal/utils/RouterEx.kt

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mapbox.navigation.base.internal.utils
22

3+
import com.mapbox.navigation.base.internal.route.Waypoint
4+
import com.mapbox.navigation.base.route.NavigationRoute
35
import com.mapbox.navigator.RouterOrigin
46

57
fun RouterOrigin.mapToSdkRouteOrigin(): com.mapbox.navigation.base.route.RouterOrigin =
@@ -15,3 +17,5 @@ fun com.mapbox.navigation.base.route.RouterOrigin.mapToNativeRouteOrigin(): Rout
1517
com.mapbox.navigation.base.route.RouterOrigin.Onboard -> RouterOrigin.ONBOARD
1618
is com.mapbox.navigation.base.route.RouterOrigin.Custom -> RouterOrigin.CUSTOM
1719
}
20+
21+
fun NavigationRoute._waypoints(): List<Waypoint> = waypoints

libnavigation-base/src/main/java/com/mapbox/navigation/base/route/NavigationRoute.kt

+4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import com.mapbox.geojson.Point
1616
import com.mapbox.navigation.base.internal.NativeRouteParserWrapper
1717
import com.mapbox.navigation.base.internal.SDKRouteParser
1818
import com.mapbox.navigation.base.internal.route.RouteCompatibilityCache
19+
import com.mapbox.navigation.base.internal.route.Waypoint
1920
import com.mapbox.navigation.base.internal.utils.mapToSdkRouteOrigin
21+
import com.mapbox.navigation.base.internal.utils.mapToSkd
2022
import com.mapbox.navigation.utils.internal.ThreadController
2123
import com.mapbox.navigation.utils.internal.logE
2224
import com.mapbox.navigation.utils.internal.logI
@@ -273,6 +275,8 @@ class NavigationRoute internal constructor(
273275
.routeOptions(routeOptions)
274276
.build()
275277

278+
internal val waypoints: List<Waypoint> = nativeRoute.waypoints.mapToSkd()
279+
276280
/**
277281
* Indicates whether some other object is "equal to" this one.
278282
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
package com.mapbox.navigation.base.internal.extensions
2+
3+
import com.mapbox.navigation.base.internal.route.Waypoint
4+
import io.mockk.every
5+
import io.mockk.mockk
6+
import junit.framework.Assert.assertEquals
7+
import junit.framework.Assert.assertTrue
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
import org.junit.runners.Parameterized
11+
12+
class WaypointExTest {
13+
14+
@RunWith(Parameterized::class)
15+
class FilterTest(
16+
private val waypoints: List<Waypoint>,
17+
private val requestedWaypointsExpected: List<@Waypoint.Type Int>,
18+
private val legsWaypointsExpected: List<@Waypoint.Type Int>
19+
) {
20+
companion object {
21+
@JvmStatic
22+
@Parameterized.Parameters
23+
fun data() = listOf(
24+
arrayOf(
25+
provideWaypoints(Waypoint.REGULAR, Waypoint.SILENT, Waypoint.REGULAR),
26+
listOf(Waypoint.REGULAR, Waypoint.SILENT, Waypoint.REGULAR),
27+
listOf(Waypoint.REGULAR, Waypoint.REGULAR),
28+
),
29+
arrayOf(
30+
provideWaypoints(
31+
Waypoint.REGULAR,
32+
Waypoint.SILENT,
33+
Waypoint.EV_CHARGING,
34+
Waypoint.SILENT,
35+
Waypoint.REGULAR
36+
),
37+
listOf(Waypoint.REGULAR, Waypoint.SILENT, Waypoint.SILENT, Waypoint.REGULAR),
38+
listOf(Waypoint.REGULAR, Waypoint.EV_CHARGING, Waypoint.REGULAR),
39+
),
40+
arrayOf(
41+
provideWaypoints(
42+
Waypoint.REGULAR,
43+
Waypoint.EV_CHARGING,
44+
Waypoint.EV_CHARGING,
45+
Waypoint.EV_CHARGING,
46+
Waypoint.REGULAR
47+
),
48+
listOf(Waypoint.REGULAR, Waypoint.REGULAR),
49+
listOf(
50+
Waypoint.REGULAR,
51+
Waypoint.EV_CHARGING,
52+
Waypoint.EV_CHARGING,
53+
Waypoint.EV_CHARGING,
54+
Waypoint.REGULAR
55+
),
56+
),
57+
arrayOf(
58+
provideWaypoints(
59+
Waypoint.REGULAR,
60+
Waypoint.SILENT,
61+
Waypoint.REGULAR,
62+
Waypoint.REGULAR,
63+
Waypoint.EV_CHARGING,
64+
Waypoint.REGULAR
65+
),
66+
listOf(
67+
Waypoint.REGULAR,
68+
Waypoint.SILENT,
69+
Waypoint.REGULAR,
70+
Waypoint.REGULAR,
71+
Waypoint.REGULAR
72+
),
73+
listOf(
74+
Waypoint.REGULAR,
75+
Waypoint.REGULAR,
76+
Waypoint.REGULAR,
77+
Waypoint.EV_CHARGING,
78+
Waypoint.REGULAR
79+
),
80+
),
81+
)
82+
83+
fun checkWaypoints(
84+
expectedWaypoints: List<@Waypoint.Type Int>,
85+
modified: List<Waypoint>,
86+
original: List<Waypoint>,
87+
) {
88+
89+
assertEquals(expectedWaypoints.size, modified.size)
90+
var bufferIndex = -1
91+
modified.forEachIndexed { index, waypoint ->
92+
assertEquals(expectedWaypoints[index], waypoint.type)
93+
assertTrue(original.contains(waypoint))
94+
val idx = original.indexOf(waypoint)
95+
assertTrue(idx > bufferIndex)
96+
bufferIndex = idx
97+
}
98+
}
99+
}
100+
101+
@Test
102+
fun testCases() {
103+
checkWaypoints(requestedWaypointsExpected, waypoints.requestedWaypoints(), waypoints)
104+
checkWaypoints(legsWaypointsExpected, waypoints.legsWaypoints(), waypoints)
105+
}
106+
}
107+
108+
@RunWith(Parameterized::class)
109+
class IndexOfNextCoordinateTest(
110+
private val testDescription: String,
111+
private val waypoints: List<Waypoint>,
112+
private val remainingWaypoints: Int,
113+
private val expectedIndex: Int?
114+
) {
115+
116+
companion object {
117+
@JvmStatic
118+
@Parameterized.Parameters(name = "{0}")
119+
fun data() = listOf(
120+
arrayOf(
121+
"Next index: 1 for 2 relevant waypoints and remaining waypoint 1",
122+
provideWaypoints(
123+
Waypoint.REGULAR, Waypoint.REGULAR
124+
),
125+
1,
126+
1,
127+
),
128+
arrayOf(
129+
"Next index: 1 for 3 relevant waypoints and remaining waypoint 2",
130+
provideWaypoints(
131+
Waypoint.REGULAR, Waypoint.SILENT, Waypoint.REGULAR
132+
),
133+
2,
134+
1,
135+
),
136+
arrayOf(
137+
"Next index: 2 for 3 relevant waypoints and remaining waypoint 1",
138+
provideWaypoints(
139+
Waypoint.REGULAR, Waypoint.SILENT, Waypoint.REGULAR
140+
),
141+
1,
142+
2,
143+
),
144+
arrayOf(
145+
"Next index: 3 for 4 relevant waypoints and remaining waypoint 1",
146+
provideWaypoints(
147+
Waypoint.REGULAR, Waypoint.SILENT, Waypoint.SILENT, Waypoint.REGULAR
148+
),
149+
1,
150+
3,
151+
),
152+
arrayOf(
153+
"Next index: 1 for 2 relevant waypoints (1 is EV) and remaining waypoint 2",
154+
provideWaypoints(
155+
Waypoint.REGULAR, Waypoint.EV_CHARGING, Waypoint.REGULAR
156+
),
157+
2,
158+
1,
159+
),
160+
arrayOf(
161+
"Next index: 1 for 3 relevant waypoints (2 is EV) and remaining waypoint 4",
162+
provideWaypoints(
163+
Waypoint.REGULAR,
164+
Waypoint.EV_CHARGING,
165+
Waypoint.SILENT,
166+
Waypoint.EV_CHARGING,
167+
Waypoint.REGULAR
168+
),
169+
4,
170+
1,
171+
),
172+
arrayOf(
173+
"Next index: 1 for 3 relevant waypoints (2 is EV) and remaining waypoint 2",
174+
provideWaypoints(
175+
Waypoint.REGULAR,
176+
Waypoint.EV_CHARGING,
177+
Waypoint.SILENT,
178+
Waypoint.EV_CHARGING,
179+
Waypoint.REGULAR
180+
),
181+
2,
182+
2,
183+
),
184+
arrayOf(
185+
"Next index: null for non-valid case - 3 relevant waypoints (2 is EV) and " +
186+
"remaining waypoint 7",
187+
provideWaypoints(
188+
Waypoint.REGULAR,
189+
Waypoint.EV_CHARGING,
190+
Waypoint.SILENT,
191+
Waypoint.EV_CHARGING,
192+
Waypoint.REGULAR
193+
),
194+
7,
195+
null,
196+
),
197+
arrayOf(
198+
"Next index: 0 for 3 relevant waypoints (2 is EV) and remaining waypoint 5",
199+
provideWaypoints(
200+
Waypoint.REGULAR,
201+
Waypoint.EV_CHARGING,
202+
Waypoint.SILENT,
203+
Waypoint.EV_CHARGING,
204+
Waypoint.REGULAR
205+
),
206+
5,
207+
0,
208+
),
209+
)
210+
}
211+
212+
@Test
213+
fun testCases() {
214+
assertEquals(
215+
testDescription,
216+
expectedIndex,
217+
indexOfNextCoordinate(waypoints, remainingWaypoints)
218+
)
219+
}
220+
}
221+
}
222+
223+
private fun provideWaypoints(@Waypoint.Type vararg waypointType: Int): List<Waypoint> =
224+
waypointType.map { mapToType ->
225+
mockk { every { type } returns mapToType }
226+
}

0 commit comments

Comments
 (0)