Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix state flow of mute UI updates on Android #351

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import com.stadiamaps.ferrostar.core.extensions.deviation
import com.stadiamaps.ferrostar.core.extensions.progress
import com.stadiamaps.ferrostar.core.extensions.remainingSteps
import com.stadiamaps.ferrostar.core.extensions.visualInstruction
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import uniffi.ferrostar.GeographicCoordinate
import uniffi.ferrostar.RouteDeviation
import uniffi.ferrostar.RouteStep
Expand Down Expand Up @@ -99,18 +102,20 @@ class DefaultNavigationViewModel(
) : ViewModel(), NavigationViewModel {

private var userLocation: UserLocation? = locationProvider.lastLocation
private var muteState: MutableStateFlow<Boolean?> =
MutableStateFlow(spokenInstructionObserver?.isMuted)
ianthetechie marked this conversation as resolved.
Show resolved Hide resolved

override val uiState =
ferrostarCore.state
.map { coreState ->
combine(ferrostarCore.state, muteState) { a, b -> a to b }
.map { (coreState, muteState) ->
val location = locationProvider.lastLocation
userLocation =
when (coreState.tripState) {
is TripState.Navigating -> coreState.tripState.snappedUserLocation
is TripState.Complete,
TripState.Idle -> locationProvider.lastLocation
}
uiState(coreState, spokenInstructionObserver?.isMuted, location, userLocation)
uiState(coreState, muteState, location, userLocation)
// This awkward dance is required because Kotlin doesn't have a way to map over
// StateFlows
// without converting to a generic Flow in the process.
Expand All @@ -134,7 +139,10 @@ class DefaultNavigationViewModel(
Log.d("NavigationViewModel", "Spoken instruction observer is null, mute operation ignored.")
return
}
spokenInstructionObserver.isMuted = !spokenInstructionObserver.isMuted
muteState.update { oldValue ->
spokenInstructionObserver.isMuted = !spokenInstructionObserver.isMuted
spokenInstructionObserver.isMuted
}
}

// TODO: We can add a hook here to override the current road name.
Expand Down
Loading