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
Show file tree
Hide file tree
Changes from all commits
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,8 +8,10 @@ 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 uniffi.ferrostar.GeographicCoordinate
Expand Down Expand Up @@ -99,18 +101,20 @@ class DefaultNavigationViewModel(
) : ViewModel(), NavigationViewModel {

private var userLocation: UserLocation? = locationProvider.lastLocation
private val muteState: StateFlow<Boolean?> =
spokenInstructionObserver?.muteState ?: MutableStateFlow(null)

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 +138,7 @@ class DefaultNavigationViewModel(
Log.d("NavigationViewModel", "Spoken instruction observer is null, mute operation ignored.")
return
}
spokenInstructionObserver.isMuted = !spokenInstructionObserver.isMuted
spokenInstructionObserver.setMuted(!spokenInstructionObserver.isMuted)
}

// TODO: We can add a hook here to override the current road name.
Expand Down
23 changes: 18 additions & 5 deletions android/core/src/main/java/com/stadiamaps/ferrostar/core/Speech.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package com.stadiamaps.ferrostar.core
import android.content.Context
import android.speech.tts.TextToSpeech
import android.speech.tts.TextToSpeech.OnInitListener
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import uniffi.ferrostar.SpokenInstruction

interface SpokenInstructionObserver {
Expand All @@ -17,7 +21,12 @@ interface SpokenInstructionObserver {
/** Stops speech and clears the queue of spoken utterances. */
fun stopAndClearQueue()

var isMuted: Boolean
fun setMuted(isMuted: Boolean)

val muteState: StateFlow<Boolean>

val isMuted: Boolean
get() = muteState.value
}

/** Observes the status of an [AndroidTtsObserver]. */
Expand Down Expand Up @@ -62,14 +71,18 @@ class AndroidTtsObserver(
private const val TAG = "AndroidTtsObserver"
}

override var isMuted: Boolean = false
set(value) {
field = value
private var _muteState: MutableStateFlow<Boolean> = MutableStateFlow(false)

if (value && tts?.isSpeaking == true) {
override fun setMuted(isMuted: Boolean) {
_muteState.update { _ ->
if (isMuted && tts?.isSpeaking == true) {
tts?.stop()
}
isMuted
}
}

override val muteState: StateFlow<Boolean> = _muteState.asStateFlow()

var tts: TextToSpeech?
private set
Expand Down
Loading