fix(timer): combine flows to fix a bug with the progress indicator

This commit is contained in:
Nishant Mishra
2025-11-05 12:52:18 +05:30
parent ee5bf647c6
commit d441289e72

View File

@@ -33,7 +33,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
@@ -59,9 +59,8 @@ class TimerViewModel(
val time: StateFlow<Long> = _time.asStateFlow()
val progress = _time.map {
val uiState = timerState.value
(uiState.totalTime.toFloat() - it) / uiState.totalTime
val progress = _time.combine(_timerState) { remainingTime, uiState ->
(uiState.totalTime.toFloat() - remainingTime) / uiState.totalTime
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), 0f)
private var cycles = 0