Initial Commit
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
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 brakingCount: Int = 0,
|
||||
val turningCount: Int = 0,
|
||||
val stoppingCount: Int = 0,
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user