Florian
12/14/2021, 10:33 AMrunBlockingTest
because TICK_DELAY
is skipped so the CPU doesn't have time to execute something else? Normally, I leave this loop by collecting the value in another place and cancelling the coroutine around it.
while (true) {
delay(TICK_DELAY)
val totalElapsedTime = systemClockProvider.getElapsedRealTime() - startTime
tasksDataSource.increaseMillisCompletedToday(
selectedTask.id,
totalElapsedTime - lastReportedTime
)
lastReportedTime = totalElapsedTime
}
Joffrey
12/14/2021, 11:14 AMFlorian
12/14/2021, 11:38 AMstartTimer
starts the timer and the coroutine in the init
block observes and stops it. But in my tests, I get stuck in an infinite while
loop:
init {
applicationScope.launch {
combine(selectedTask, activeDay) { selectedTask, activeDay ->
Pair(selectedTask, activeDay)
}.collect { (selectedTask, activeDay) ->
if (selectedTask == null || activeDay == null ||
!selectedTask.isActiveOnDay(activeDay)
) {
stopTimer()
} else if (selectedTask.dailyGoalCompleted && timerRunning.first()) {
// Task has just finished
stopTimer()
shaveOffOvershootTime(selectedTask)
notificationHelper.showTaskCompletedNotification(selectedTask)
stopTimer()
}
}
}
}
fun startTimer() {
timerJob?.cancel()
timerJob = applicationScope.launch {
val selectedTask = selectedTask.first()
if (selectedTask != null) {
tasksDataSource.updateLastActiveTimestamp(selectedTask.id, System.currentTimeMillis())
startTimerService()
timerRunningFlow.value = true
val startTime = SystemClock.elapsedRealtime()
var lastReportedTime = 0L
while (true) {
delay(TICK_DELAY)
val totalElapsedTime = SystemClock.elapsedRealtime() - startTime
tasksDataSource.increaseMillisCompletedToday(selectedTask.id, totalElapsedTime - lastReportedTime)
lastReportedTime = totalElapsedTime
}
}
}
}
ephemient
12/14/2021, 4:27 PMFlorian
12/15/2021, 7:38 AM