Skip to content

Commit b51c25f

Browse files
authored
add option to hide or show alternative routes (#6100)
1 parent 73f0ea7 commit b51c25f

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

libnavui-androidauto/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Mapbox welcomes participation and contributions from everyone.
55
## Unreleased
66
#### Features
77
#### Bug fixes and improvements
8+
- Added an option to hide or show alternative routes during route preview and active guidance. [#6100](https://github.com/mapbox/mapbox-navigation-android/pull/6100)
89

910
## androidauto-v0.5.0 - Jul 22, 2022
1011
### Changelog

libnavui-androidauto/src/main/java/com/mapbox/androidauto/car/MainCarContext.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.mapbox.search.SearchEngine
1414
import kotlinx.coroutines.CoroutineScope
1515
import kotlinx.coroutines.Dispatchers
1616
import kotlinx.coroutines.SupervisorJob
17+
import kotlinx.coroutines.flow.MutableStateFlow
1718

1819
@OptIn(MapboxExperimental::class)
1920
class MainCarContext(
@@ -37,6 +38,8 @@ class MainCarContext(
3738
MapboxManeuverApi(distanceFormatter)
3839
}
3940

41+
val routeAlternativesEnabled = MutableStateFlow(value = true)
42+
4043
fun getJobControl(): JobControl {
4144
val supervisorJob = SupervisorJob()
4245
val scope = CoroutineScope(supervisorJob + Dispatchers.Main)

libnavui-androidauto/src/main/java/com/mapbox/androidauto/car/navigation/speedlimit/CarSpeedLimitRenderer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class CarSpeedLimitRenderer(
6565
logAndroidAuto("CarSpeedLimitRenderer carMapSurface detached")
6666
mainCarContext.mapboxNavigation.unregisterLocationObserver(locationObserver)
6767
speedLimitWidget?.let { mapboxCarMapSurface.mapSurface.removeWidget(it) }
68+
speedLimitWidget = null
6869
}
6970

7071
override fun onVisibleAreaChanged(visibleArea: Rect, edgeInsets: EdgeInsets) {

libnavui-androidauto/src/main/java/com/mapbox/androidauto/car/preview/CarRouteLine.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mapbox.androidauto.car.preview
33
import com.mapbox.androidauto.car.MainCarContext
44
import com.mapbox.androidauto.car.internal.extensions.getStyle
55
import com.mapbox.androidauto.car.internal.extensions.handleStyleOnAttached
6+
import com.mapbox.androidauto.car.internal.extensions.handleStyleOnDetached
67
import com.mapbox.androidauto.car.routes.NavigationRoutesProvider
78
import com.mapbox.androidauto.car.routes.RoutesListener
89
import com.mapbox.androidauto.car.routes.RoutesProvider
@@ -25,6 +26,12 @@ import com.mapbox.navigation.ui.maps.route.line.model.MapboxRouteLineOptions
2526
import com.mapbox.navigation.ui.maps.route.line.model.NavigationRouteLine
2627
import com.mapbox.navigation.ui.maps.route.line.model.RouteLineColorResources
2728
import com.mapbox.navigation.ui.maps.route.line.model.RouteLineResources
29+
import kotlinx.coroutines.CoroutineScope
30+
import kotlinx.coroutines.MainScope
31+
import kotlinx.coroutines.cancel
32+
import kotlinx.coroutines.cancelChildren
33+
import kotlinx.coroutines.flow.collect
34+
import kotlinx.coroutines.launch
2835

2936
/**
3037
* This class is to simplify the interaction with [MapboxRouteLineApi], [MapboxRouteArrowView]
@@ -53,6 +60,8 @@ class CarRouteLine internal constructor(
5360
private lateinit var routeArrowApi: MapboxRouteArrowApi
5461
private lateinit var routeArrowView: MapboxRouteArrowView
5562

63+
private var styleScope: CoroutineScope? = null
64+
5665
private val onPositionChangedListener = OnIndicatorPositionChangedListener { point ->
5766
val result = routeLineApi.updateTraveledRouteLine(point)
5867
mainCarContext.mapboxCarMap.carMapSurface?.getStyle()?.let {
@@ -98,6 +107,7 @@ class CarRouteLine internal constructor(
98107
override fun onAttached(mapboxCarMapSurface: MapboxCarMapSurface) {
99108
logAndroidAuto("CarRouteLine carMapSurface loaded $mapboxCarMapSurface")
100109
val locationPlugin = mapboxCarMapSurface.mapSurface.location
110+
val scope = MainScope().also { styleScope = it }
101111
styleLoadedListener = mapboxCarMapSurface.handleStyleOnAttached { style ->
102112
val routeLineOptions = getMapboxRouteLineOptions(style)
103113
routeLineView = MapboxRouteLineView(routeLineOptions)
@@ -114,15 +124,29 @@ class CarRouteLine internal constructor(
114124
locationPlugin.addOnIndicatorPositionChangedListener(onPositionChangedListener)
115125
mainCarContext.mapboxNavigation.registerRouteProgressObserver(routeProgressObserver)
116126
routesProvider.registerRoutesListener(routesListener)
127+
128+
scope.coroutineContext.cancelChildren()
129+
scope.launch {
130+
mainCarContext.routeAlternativesEnabled.collect { enabled ->
131+
if (enabled) {
132+
routeLineView.showAlternativeRoutes(style)
133+
} else {
134+
routeLineView.hideAlternativeRoutes(style)
135+
}
136+
}
137+
}
117138
}
118139
}
119140

120141
override fun onDetached(mapboxCarMapSurface: MapboxCarMapSurface) {
121142
logAndroidAuto("CarRouteLine carMapSurface detached $mapboxCarMapSurface")
122143
val mapSurface = mapboxCarMapSurface.mapSurface
144+
mapboxCarMapSurface.handleStyleOnDetached(styleLoadedListener)
123145
mapSurface.location.removeOnIndicatorPositionChangedListener(onPositionChangedListener)
124146
mainCarContext.mapboxNavigation.unregisterRouteProgressObserver(routeProgressObserver)
125147
routesProvider.unregisterRoutesListener(routesListener)
148+
styleScope?.cancel()
149+
styleScope = null
126150
}
127151

128152
private fun getMapboxRouteLineOptions(style: Style): MapboxRouteLineOptions {

libnavui-androidauto/src/main/java/com/mapbox/androidauto/car/routes/NavigationRoutesProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal class NavigationRoutesProvider(private val mapboxNavigation: MapboxNavi
1818
private data class RoutesObserverAdapter(val listener: RoutesListener) : RoutesObserver {
1919

2020
override fun onRoutesChanged(result: RoutesUpdatedResult) {
21-
listener.onRoutesChanged(result.navigationRoutes.take(1))
21+
listener.onRoutesChanged(result.navigationRoutes)
2222
}
2323
}
2424
}

0 commit comments

Comments
 (0)