@@ -27,6 +27,7 @@ import kotlinx.coroutines.flow.filter
27
27
import kotlinx.coroutines.flow.first
28
28
import kotlinx.coroutines.flow.flatMapConcat
29
29
import kotlinx.coroutines.flow.flatMapLatest
30
+ import kotlinx.coroutines.flow.flatMapMerge
30
31
import kotlinx.coroutines.flow.flowOf
31
32
import kotlinx.coroutines.flow.map
32
33
import kotlinx.coroutines.flow.updateAndGet
@@ -123,12 +124,12 @@ internal constructor(
123
124
/* *
124
125
* Top level flow that will switch based on the language and muted state.
125
126
*/
126
- @OptIn(ExperimentalCoroutinesApi ::class , FlowPreview :: class )
127
+ @OptIn(ExperimentalCoroutinesApi ::class )
127
128
private fun audioGuidanceFlow (
128
129
mapboxNavigation : MapboxNavigation
129
130
): Flow <MapboxAudioGuidanceState > {
130
- return mapboxNavigation.audioGuidanceVoice().flatMapLatest { audioGuidance ->
131
- var lastPlayedInstructions : VoiceInstructions ? = null
131
+ var lastPlayedInstructions : VoiceInstructions ? = null
132
+ return mapboxNavigation.audioGuidanceVoice().flatMapMerge { audioGuidance ->
132
133
mutedStateFlow.flatMapLatest { isMuted ->
133
134
val voiceInstructions = mapboxVoiceInstructions.voiceInstructions()
134
135
.map { state ->
@@ -146,18 +147,15 @@ internal constructor(
146
147
} else {
147
148
voiceInstructions
148
149
.filter { it.voiceInstructions != lastPlayedInstructions }
149
- .flatMapConcat {
150
+ .map {
150
151
lastPlayedInstructions = it.voiceInstructions
151
152
val announcement = audioGuidance.speak(it.voiceInstructions)
152
- flowOf(announcement)
153
- }
154
- .map { speechAnnouncement ->
155
- internalStateFlow.updateAndGet {
153
+ internalStateFlow.updateAndGet { state ->
156
154
MapboxAudioGuidanceState (
157
- isPlayable = it .isPlayable,
158
- isMuted = it .isMuted,
159
- voiceInstructions = it .voiceInstructions,
160
- speechAnnouncement = speechAnnouncement
155
+ isPlayable = state .isPlayable,
156
+ isMuted = state .isMuted,
157
+ voiceInstructions = state .voiceInstructions,
158
+ speechAnnouncement = announcement
161
159
)
162
160
}
163
161
}
0 commit comments