Created
March 24, 2021 13:29
-
-
Save vegeta2102/fa98844448518c848b3c973b88f6eb2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override suspend fun startCountDown(count: Int) { | |
// 10 seconds for count down | |
// 100 is a finer number that makes progress run smoother than normal | |
val countTime = count * 100 | |
repeat(countTime) { | |
// In case of 10 seconds count down | |
// The timer will display as 10 -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 | |
// So it must plus 1 | |
val displaySeconds = countTime.minus(it).div(100).plus(1) | |
// Repeat N will run from N-1 to 0 | |
val finish = countTime.minus(it) == 1 | |
// Emit second display and progress state | |
_data.emit(CountDown(time = displaySeconds, progress = it, isProgressFinish = false)) | |
if (finish) { | |
_data.emit(CountDown(time = 0, progress = countTime, isProgressFinish = finish)) | |
} | |
delay(10) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment