Skip to content

Commit

Permalink
Fix state flow of mute UI updates on Android (#351)
Browse files Browse the repository at this point in the history
* Fix state flow of mute UI updates on Android

* Update android/core/src/main/java/com/stadiamaps/ferrostar/core/NavigationViewModel.kt

Co-authored-by: Jacob Fielding <[email protected]>

* Apply automatic changes

* Revert change; can't work outside a Composable context

* Refactor speech observer data flow

---------

Co-authored-by: Jacob Fielding <[email protected]>
Co-authored-by: ianthetechie <[email protected]>
  • Loading branch information
3 people authored Nov 6, 2024
1 parent b0545b9 commit fbbc52e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
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

0 comments on commit fbbc52e

Please sign in to comment.