Skip to content

Commit ed26176

Browse files
committed
NAVAND-777: meet code review
1 parent 257caa1 commit ed26176

File tree

9 files changed

+82
-40
lines changed

9 files changed

+82
-40
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Mapbox welcomes participation and contributions from everyone.
44

55
## Unreleased
66
#### Features
7-
- Added `MapboxNavigation#refreshRoutesImmediately` to trigger route refresh request immediately. [#6610](https://github.com/mapbox/mapbox-navigation-android/pull/6610)
87
#### Bug fixes and improvements
98

109
## Mapbox Navigation SDK 2.11.0-alpha.1 - 13 January, 2023
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Added `MapboxNavigation#refreshRoutesImmediately` to trigger route refresh request immediately.

libnavigation-core/src/main/java/com/mapbox/navigation/core/routerefresh/ImmediateRouteRefreshController.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import com.mapbox.navigation.base.route.NavigationRoute
66
@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
77
internal class ImmediateRouteRefreshController(
88
private val routeRefresherExecutor: RouteRefresherExecutor,
9-
private val plannedRefreshController: Pausable,
109
private val stateHolder: RouteRefreshStateHolder,
1110
private val listener: RouteRefresherListener
1211
) {
1312

14-
private val callback = object : RouteRefresherProgressCallback {
13+
private val progressCallback = object : RouteRefresherProgressCallback {
1514

1615
override fun onStarted() {
1716
stateHolder.onStarted()
@@ -22,17 +21,29 @@ internal class ImmediateRouteRefreshController(
2221
stateHolder.onSuccess()
2322
} else {
2423
stateHolder.onFailure(null)
25-
plannedRefreshController.resume()
2624
}
2725
listener.onRoutesRefreshed(routeRefresherResult)
2826
}
2927
}
3028

31-
fun requestRoutesRefresh(routes: List<NavigationRoute>) {
29+
fun requestRoutesRefresh(routes: List<NavigationRoute>, callback: (RouteRefresherResult) -> Unit) {
3230
if (routes.isEmpty()) {
3331
return
3432
}
35-
plannedRefreshController.pause()
36-
routeRefresherExecutor.postRoutesToRefresh(routes, callback)
33+
routeRefresherExecutor.postRoutesToRefresh(routes, wrapCallback(callback))
34+
}
35+
36+
private fun wrapCallback(
37+
callback: (RouteRefresherResult) -> Unit
38+
) = object : RouteRefresherProgressCallback {
39+
40+
override fun onStarted() {
41+
progressCallback.onStarted()
42+
}
43+
44+
override fun onResult(routeRefresherResult: RouteRefresherResult) {
45+
progressCallback.onResult(routeRefresherResult)
46+
callback(routeRefresherResult)
47+
}
3748
}
3849
}

libnavigation-core/src/main/java/com/mapbox/navigation/core/routerefresh/Pausable.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.

libnavigation-core/src/main/java/com/mapbox/navigation/core/routerefresh/PlannedRouteRefreshController.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal class PlannedRouteRefreshController @VisibleForTesting constructor(
1515
private val listener: RouteRefresherListener,
1616
private val cancellableHandler: CancellableHandler,
1717
private val retryStrategy: RetryRouteRefreshStrategy,
18-
) : Pausable {
18+
) {
1919

2020
constructor(
2121
routeRefresherExecutor: RouteRefresherExecutor,
@@ -63,14 +63,14 @@ internal class PlannedRouteRefreshController @VisibleForTesting constructor(
6363
}
6464
}
6565

66-
override fun pause() {
66+
fun pause() {
6767
if (!paused) {
6868
paused = true
6969
cancellableHandler.cancelAll()
7070
}
7171
}
7272

73-
override fun resume() {
73+
fun resume() {
7474
if (paused) {
7575
paused = false
7676
routesToRefresh?.let {

libnavigation-core/src/main/java/com/mapbox/navigation/core/routerefresh/RouteRefresher.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.mapbox.navigation.base.route.NavigationRouterRefreshError
77
import com.mapbox.navigation.core.RouteProgressData
88
import com.mapbox.navigation.core.RouteProgressDataProvider
99
import com.mapbox.navigation.core.directions.session.RouteRefresh
10-
import com.mapbox.navigation.core.ev.EVDynamicDataHolder
10+
import com.mapbox.navigation.core.ev.EVRefreshDataProvider
1111
import com.mapbox.navigation.utils.internal.logE
1212
import com.mapbox.navigation.utils.internal.logI
1313
import kotlinx.coroutines.async
@@ -25,7 +25,7 @@ internal data class RouteRefresherResult(
2525

2626
internal class RouteRefresher(
2727
private val routeProgressDataProvider: RouteProgressDataProvider,
28-
private val evDataHolder: EVDynamicDataHolder,
28+
private val evRefreshDataProvider: EVRefreshDataProvider,
2929
private val routeDiffProvider: DirectionsRouteDiffProvider,
3030
private val routeRefresh: RouteRefresh,
3131
) {
@@ -85,7 +85,7 @@ internal class RouteRefresher(
8585
routeProgressData.legIndex,
8686
routeProgressData.routeGeometryIndex,
8787
routeProgressData.legGeometryIndex,
88-
evDataHolder.currentData(route.routeOptions.unrecognizedJsonProperties ?: emptyMap())
88+
evRefreshDataProvider.get(route.routeOptions)
8989
)
9090
return when (val result = requestRouteRefresh(route, routeRefreshRequestData)) {
9191
is RouteRefreshResult.Fail -> {

libnavigation-core/src/test/java/com/mapbox/navigation/core/routerefresh/CancellableHandlerTest.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ class CancellableHandlerTest {
1818
private val cancellation = mockk<() -> Unit>(relaxed = true)
1919
private val handler = CancellableHandler(coroutineRule.coroutineScope)
2020

21+
@Test
22+
fun postExecutesWithZeroTimeout() = coroutineRule.runBlockingTest {
23+
handler.postDelayed(0, runnable, cancellation)
24+
25+
verify(exactly = 1) {
26+
runnable.run()
27+
}
28+
verify(exactly = 0) {
29+
cancellation.invoke()
30+
}
31+
}
32+
33+
@Test
34+
fun postExecutesWithNegativeTimeout() = coroutineRule.runBlockingTest {
35+
handler.postDelayed(-10, runnable, cancellation)
36+
37+
verify(exactly = 1) {
38+
runnable.run()
39+
}
40+
verify(exactly = 0) {
41+
cancellation.invoke()
42+
}
43+
}
44+
2145
@Test
2246
fun postExecutesAfterTimeout() = coroutineRule.runBlockingTest {
2347
val timeout = 7000L
@@ -50,6 +74,28 @@ class CancellableHandlerTest {
5074
}
5175
}
5276

77+
@Test
78+
fun postWithZeroTimeoutRemovesBlockFromMap() = coroutineRule.runBlockingTest {
79+
handler.postDelayed(0, runnable, cancellation)
80+
81+
handler.cancelAll()
82+
83+
verify(exactly = 0) {
84+
cancellation.invoke()
85+
}
86+
}
87+
88+
@Test
89+
fun postWithNegativeTimeoutRemovesBlockFromMap() = coroutineRule.runBlockingTest {
90+
handler.postDelayed(-10, runnable, cancellation)
91+
92+
handler.cancelAll()
93+
94+
verify(exactly = 0) {
95+
cancellation.invoke()
96+
}
97+
}
98+
5399
@Test
54100
fun cancelAll_noJobs() = coroutineRule.runBlockingTest {
55101
handler.cancelAll()

libnavigation-core/src/test/java/com/mapbox/navigation/core/routerefresh/ImmediateRouteRefreshControllerTest.kt

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,37 @@ import org.junit.Test
1111
class ImmediateRouteRefreshControllerTest {
1212

1313
private val routeRefresherExecutor = mockk<RouteRefresherExecutor>(relaxed = true)
14-
private val plannedRefreshController = mockk<Pausable>(relaxed = true)
1514
private val stateHolder = mockk<RouteRefreshStateHolder>(relaxed = true)
1615
private val listener = mockk<RouteRefresherListener>(relaxed = true)
16+
private val clientCallback = mockk<(RouteRefresherResult) -> Unit>(relaxed = true)
1717
private val routes = listOf<NavigationRoute>(mockk())
1818

1919
private val sut = ImmediateRouteRefreshController(
2020
routeRefresherExecutor,
21-
plannedRefreshController,
2221
stateHolder,
2322
listener
2423
)
2524

2625
@Test
2726
fun requestRoutesRefreshWithEmptyRoutes() {
28-
sut.requestRoutesRefresh(emptyList())
27+
sut.requestRoutesRefresh(emptyList(), clientCallback)
2928

3029
verify(exactly = 0) {
31-
plannedRefreshController.pause()
30+
clientCallback(any())
3231
routeRefresherExecutor.postRoutesToRefresh(any(), any())
3332
}
3433
}
3534

36-
@Test
37-
fun requestRoutesRefreshPausesPlannedController() {
38-
sut.requestRoutesRefresh(routes)
39-
40-
verify(exactly = 1) { plannedRefreshController.pause() }
41-
}
42-
4335
@Test
4436
fun requestRoutesRefreshPostsRefreshRequest() {
45-
sut.requestRoutesRefresh(routes)
37+
sut.requestRoutesRefresh(routes, clientCallback)
4638

4739
verify(exactly = 1) { routeRefresherExecutor.postRoutesToRefresh(routes, any()) }
4840
}
4941

5042
@Test
5143
fun routesRefreshStarted() {
52-
sut.requestRoutesRefresh(routes)
44+
sut.requestRoutesRefresh(routes, clientCallback)
5345
val callback = interceptCallback()
5446

5547
callback.onStarted()
@@ -59,7 +51,7 @@ class ImmediateRouteRefreshControllerTest {
5951

6052
@Test
6153
fun routesRefreshFinishedSuccessfully() {
62-
sut.requestRoutesRefresh(routes)
54+
sut.requestRoutesRefresh(routes, clientCallback)
6355
val callback = interceptCallback()
6456
val result = RouteRefresherResult(
6557
true,
@@ -71,11 +63,12 @@ class ImmediateRouteRefreshControllerTest {
7163

7264
verify(exactly = 1) { stateHolder.onSuccess() }
7365
verify(exactly = 1) { listener.onRoutesRefreshed(result) }
66+
verify(exactly = 1) { clientCallback(result) }
7467
}
7568

7669
@Test
7770
fun routesRefreshFinishedWithFailure() {
78-
sut.requestRoutesRefresh(routes)
71+
sut.requestRoutesRefresh(routes, clientCallback)
7972
val callback = interceptCallback()
8073
val result = RouteRefresherResult(
8174
false,
@@ -86,7 +79,7 @@ class ImmediateRouteRefreshControllerTest {
8679
callback.onResult(result)
8780

8881
verify(exactly = 1) { stateHolder.onFailure(null) }
89-
verify(exactly = 1) { plannedRefreshController.resume() }
82+
verify(exactly = 1) { clientCallback(result) }
9083
verify(exactly = 1) { listener.onRoutesRefreshed(result) }
9184
}
9285

libnavigation-core/src/test/java/com/mapbox/navigation/core/routerefresh/RouteRefresherTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.mapbox.navigation.base.route.NavigationRouterRefreshCallback
66
import com.mapbox.navigation.core.RouteProgressData
77
import com.mapbox.navigation.core.RouteProgressDataProvider
88
import com.mapbox.navigation.core.directions.session.RouteRefresh
9-
import com.mapbox.navigation.core.ev.EVDynamicDataHolder
9+
import com.mapbox.navigation.core.ev.EVRefreshDataProvider
1010
import com.mapbox.navigation.testing.LoggingFrontendTestRule
1111
import com.mapbox.navigation.testing.MainCoroutineRule
1212
import com.mapbox.navigation.testing.factories.createDirectionsRoute
@@ -43,12 +43,12 @@ class RouteRefresherTest {
4343
private val routeProgressDataProvider = mockk<RouteProgressDataProvider>(relaxed = true) {
4444
coEvery { getRouteRefreshRequestDataOrWait() } returns routeProgressData
4545
}
46-
private val evDataHolder = mockk<EVDynamicDataHolder>(relaxed = true)
46+
private val evRefreshDataProvider = mockk<EVRefreshDataProvider>(relaxed = true)
4747
private val routeDiffProvider = mockk<DirectionsRouteDiffProvider>(relaxed = true)
4848
private val routeRefresh = mockk<RouteRefresh>(relaxed = true)
4949
private val sut = RouteRefresher(
5050
routeProgressDataProvider,
51-
evDataHolder,
51+
evRefreshDataProvider,
5252
routeDiffProvider,
5353
routeRefresh,
5454
)

0 commit comments

Comments
 (0)