Skip to content

Commit 59b1992

Browse files
committed
remove MapboxTripStarterState
1 parent 584e1f7 commit 59b1992

2 files changed

Lines changed: 19 additions & 42 deletions

File tree

libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/MapboxTripStarter.kt

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ import kotlinx.coroutines.cancel
2020
import kotlinx.coroutines.flow.Flow
2121
import kotlinx.coroutines.flow.MutableStateFlow
2222
import kotlinx.coroutines.flow.collect
23-
import kotlinx.coroutines.flow.distinctUntilChanged
2423
import kotlinx.coroutines.flow.flatMapLatest
25-
import kotlinx.coroutines.flow.map
2624
import kotlinx.coroutines.flow.onEach
2725
import kotlinx.coroutines.flow.update
2826
import kotlinx.coroutines.launch
@@ -40,7 +38,11 @@ import kotlinx.coroutines.launch
4038
@ExperimentalPreviewMapboxNavigationAPI
4139
class MapboxTripStarter internal constructor() : MapboxNavigationObserver {
4240

43-
private val stateFlow = MutableStateFlow(MapboxTripStarterState())
41+
private val tripType = MutableStateFlow(MAPBOX_TRIP_STARTER_MAP_MATCHING)
42+
private val replayRouteSessionOptions = MutableStateFlow(
43+
ReplayRouteSessionOptions.Builder().build()
44+
)
45+
private val isLocationPermissionGranted = MutableStateFlow(false)
4446
private var replayRouteTripSession: ReplayRouteSession? = null
4547
private var mapboxNavigation: MapboxNavigation? = null
4648

@@ -57,7 +59,7 @@ class MapboxTripStarter internal constructor() : MapboxNavigationObserver {
5759
// Initialize the options to be aware of the location permissions
5860
val context = mapboxNavigation.navigationOptions.applicationContext
5961
val granted = PermissionsManager.areLocationPermissionsGranted(context)
60-
stateFlow.update { it.copy(isLocationPermissionGranted = granted) }
62+
isLocationPermissionGranted.update { granted }
6163

6264
// Observe changes to state
6365
coroutineScope.launch { observeStateFlow(mapboxNavigation).collect() }
@@ -81,7 +83,7 @@ class MapboxTripStarter internal constructor() : MapboxNavigationObserver {
8183
fun refreshLocationPermissions() = apply {
8284
mapboxNavigation?.navigationOptions?.applicationContext?.let { context ->
8385
val granted = PermissionsManager.areLocationPermissionsGranted(context)
84-
stateFlow.value = stateFlow.value.copy(isLocationPermissionGranted = granted)
86+
isLocationPermissionGranted.value = granted
8587
}
8688
}
8789

@@ -91,18 +93,17 @@ class MapboxTripStarter internal constructor() : MapboxNavigationObserver {
9193
* effect on the experience.
9294
*/
9395
fun enableMapMatching() = apply {
94-
if (!stateFlow.value.isLocationPermissionGranted) {
96+
if (!isLocationPermissionGranted.value) {
9597
refreshLocationPermissions()
9698
}
97-
stateFlow.update { it.copy(tripType = MAPBOX_TRIP_STARTER_MAP_MATCHING) }
99+
tripType.update { MAPBOX_TRIP_STARTER_MAP_MATCHING }
98100
}
99101

100102
/**
101103
* Get the current [ReplayRouteSessionOptions]. This can be used with [enableReplayRoute] to
102104
* make minor adjustments to the current options.
103105
*/
104-
fun getReplayRouteSessionOptions(): ReplayRouteSessionOptions =
105-
stateFlow.value.replayRouteSessionOptions
106+
fun getReplayRouteSessionOptions(): ReplayRouteSessionOptions = replayRouteSessionOptions.value
106107

107108
/**
108109
* Enables a mode where the primary route is simulated by an artificial driver. Set the route
@@ -114,26 +115,22 @@ class MapboxTripStarter internal constructor() : MapboxNavigationObserver {
114115
fun enableReplayRoute(
115116
options: ReplayRouteSessionOptions? = null
116117
) = apply {
117-
stateFlow.update {
118-
it.copy(
119-
tripType = MAPBOX_TRIP_STARTER_REPLAY_ROUTE,
120-
replayRouteSessionOptions = options ?: it.replayRouteSessionOptions
121-
)
122-
}
118+
options?.let { options -> replayRouteSessionOptions.update { options } }
119+
tripType.update { MAPBOX_TRIP_STARTER_REPLAY_ROUTE }
123120
}
124121

125122
@OptIn(ExperimentalCoroutinesApi::class)
126123
private fun observeStateFlow(mapboxNavigation: MapboxNavigation): Flow<*> {
127-
return stateFlow.map { it.tripType }.distinctUntilChanged().flatMapLatest { tripType ->
124+
return tripType.flatMapLatest { tripType ->
128125
when (tripType) {
129126
MAPBOX_TRIP_STARTER_REPLAY_ROUTE ->
130-
stateFlow
131-
.map { it.replayRouteSessionOptions }.distinctUntilChanged()
132-
.onEach { options -> onReplayTripEnabled(mapboxNavigation, options) }
127+
replayRouteSessionOptions.onEach { options ->
128+
onReplayTripEnabled(mapboxNavigation, options)
129+
}
133130
MAPBOX_TRIP_STARTER_MAP_MATCHING ->
134-
stateFlow
135-
.map { it.isLocationPermissionGranted }.distinctUntilChanged()
136-
.onEach { granted -> onMapMatchingEnabled(mapboxNavigation, granted) }
131+
isLocationPermissionGranted.onEach { granted ->
132+
onMapMatchingEnabled(mapboxNavigation, granted)
133+
}
137134
else -> throw NotImplementedError("$LOG_CATEGORY type is not supported $tripType")
138135
}
139136
}

libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/MapboxTripStarterState.kt

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

0 commit comments

Comments
 (0)