Skip to content

Commit

Permalink
Fixed timer not stopping when paused
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Oct 8, 2024
1 parent f8ac7c3 commit 9687873
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ private class TimerStateRepo(
takeAndChangeState { state ->
when (state) {
is State.Running -> state::stop
is State.NotRunning -> state.remain()
is State.NotRunning.Paused -> state::stop
is State.NotRunning.Finished -> state.remain()
}
}
}
Expand Down Expand Up @@ -196,6 +197,8 @@ private class TimerStateRepo(
coroutineScope = coroutineScope,
finishCallback = finishCallback,
)

internal fun stop(): Finished = Finished(elapsedSoFar)
}

/** Timer is finished. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,24 @@ class RecurringTimerTest {
assertTrue(timerScope.isActive)
timer.state.assertEmits("timer is not running after start") { it is Timer.State.Running }
timer.stop()
timer.state.assertEmits("timer is not running after start") { it is Timer.State.NotRunning.Finished }
timer.state.assertEmits("timer is not stopped after stop") { it is Timer.State.NotRunning.Finished }
assertFalse(timer.start())
assertFalse(timerScope.isActive)
}

@Test
fun stopBeforeStart(): Unit = runBlocking {
val timerScope = CoroutineScope(Dispatchers.Default)
val timer = RecurringTimer(
duration = 1.seconds,
interval = 10.milliseconds,
coroutineScope = timerScope,
)
timer.state.assertEmits("timer was not paused after creation") { it is Timer.State.NotRunning.Paused }
timer.stop()
timer.state.assertEmits("timer is not stopped after stop") { it is Timer.State.NotRunning.Finished }
}

@Test
fun elapsedFlow(): Unit = runBlocking {
val timerScope = CoroutineScope(Dispatchers.Default)
Expand Down

0 comments on commit 9687873

Please sign in to comment.