36 lines
1.0 KiB
Kotlin
36 lines
1.0 KiB
Kotlin
package com.hawhamburg.micr0bu.service
|
|||
|
|
|
||
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||
|
|
import kotlinx.coroutines.flow.StateFlow
|
||
|
|
import kotlinx.coroutines.flow.asStateFlow
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Process-level singleton that bridges [TripRecordingService] (writes) and
|
||
|
|
* [TripRecordingViewModel] (reads) without requiring a bound service.
|
||
|
|
*
|
||
|
|
* The service updates [state] on every relevant change; the ViewModel collects
|
||
|
|
* it as a cold StateFlow.
|
||
|
|
*/
|
||
|
|
object TripServiceBus {
|
||
|
|
|
||
|
|
data class State(
|
||
|
|
val isRecording: Boolean = false,
|
||
|
|
val currentTripId: Long = -1L,
|
||
|
|
val elapsedSeconds: Long = 0L,
|
||
|
|
val currentSpeedMs: Float = 0f,
|
||
|
|
)
|
||
|
|
|
||
|
|
private val _state = MutableStateFlow(State())
|
||
|
|
val state: StateFlow<State> = _state.asStateFlow()
|
||
|
|
|
||
|
|
/** Called exclusively from [TripRecordingService]. */
|
||
|
|
internal fun update(transform: State.() -> State) {
|
||
|
|
_state.value = _state.value.transform()
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Resets to idle — called when recording stops. */
|
||
|
|
internal fun reset() {
|
||
|
|
_state.value = State()
|
||
|
|
}
|
||
|
|
}
|