@@ -19,20 +19,20 @@ import android.os.SystemClock
1919import androidx.lifecycle.DefaultLifecycleObserver
2020import androidx.lifecycle.LifecycleOwner
2121import kotlinx.coroutines.flow.MutableStateFlow
22- import kotlinx.coroutines.flow.asStateFlow
22+ import kotlinx.coroutines.flow.StateFlow
2323
2424class AnswerTimer : DefaultLifecycleObserver {
25- private val _state = MutableStateFlow <AnswerTimerState >( AnswerTimerState . Hidden )
26- val state = _state .asStateFlow( )
25+ val state : StateFlow <AnswerTimerState >
26+ field = MutableStateFlow < AnswerTimerState >( AnswerTimerState . Hidden )
2727
2828 fun configureForCard (
2929 shouldShow : Boolean ,
3030 limitMs : Int ,
3131 ) {
3232 if (! shouldShow) {
33- _state .value = AnswerTimerState .Hidden
33+ state .value = AnswerTimerState .Hidden
3434 } else {
35- _state .value =
35+ state .value =
3636 AnswerTimerState .Running (
3737 baseTime = SystemClock .elapsedRealtime(),
3838 limitMs = limitMs,
@@ -42,18 +42,18 @@ class AnswerTimer : DefaultLifecycleObserver {
4242
4343 /* * Permanently stops the timer. */
4444 fun stop () {
45- when (val currentState = _state .value) {
45+ when (val currentState = state .value) {
4646 is AnswerTimerState .Running -> {
4747 val elapsed = (SystemClock .elapsedRealtime() - currentState.baseTime).coerceAtMost(currentState.limitMs.toLong())
4848
49- _state .value =
49+ state .value =
5050 AnswerTimerState .Stopped (
5151 elapsedTimeMs = elapsed,
5252 limitMs = currentState.limitMs,
5353 )
5454 }
5555 is AnswerTimerState .Paused -> {
56- _state .value =
56+ state .value =
5757 AnswerTimerState .Stopped (
5858 elapsedTimeMs = currentState.elapsedTimeMs,
5959 limitMs = currentState.limitMs,
@@ -65,14 +65,14 @@ class AnswerTimer : DefaultLifecycleObserver {
6565
6666 /* * Temporarily pauses the timer. */
6767 override fun onPause (owner : LifecycleOwner ) {
68- val currentState = _state .value
68+ val currentState = state .value
6969 if (currentState is AnswerTimerState .Running ) {
7070 val rawElapsed = SystemClock .elapsedRealtime() - currentState.baseTime
7171 // If the timer has a limit and we've passed it, clamp the elapsed time
7272 // to the limit. This matches the UI behavior where the timer visually stops.
7373 val effectiveElapsed = rawElapsed.coerceAtMost(currentState.limitMs.toLong())
7474
75- _state .value =
75+ state .value =
7676 AnswerTimerState .Paused (
7777 elapsedTimeMs = effectiveElapsed,
7878 limitMs = currentState.limitMs,
@@ -82,17 +82,17 @@ class AnswerTimer : DefaultLifecycleObserver {
8282
8383 /* * Resumes the timer if it was paused and the limit hasn't been exceeded. */
8484 override fun onResume (owner : LifecycleOwner ) {
85- val currentState = _state .value
85+ val currentState = state .value
8686 if (currentState is AnswerTimerState .Paused ) {
8787 if (currentState.elapsedTimeMs < currentState.limitMs) {
88- _state .value =
88+ state .value =
8989 AnswerTimerState .Running (
9090 baseTime = SystemClock .elapsedRealtime() - currentState.elapsedTimeMs,
9191 limitMs = currentState.limitMs,
9292 )
9393 } else {
9494 // Limit reached while paused, permanently stop it.
95- _state .value =
95+ state .value =
9696 AnswerTimerState .Stopped (
9797 elapsedTimeMs = currentState.elapsedTimeMs,
9898 limitMs = currentState.limitMs,
@@ -102,6 +102,6 @@ class AnswerTimer : DefaultLifecycleObserver {
102102 }
103103
104104 fun hide () {
105- _state .value = AnswerTimerState .Hidden
105+ state .value = AnswerTimerState .Hidden
106106 }
107107}
0 commit comments