Add timer
This commit is contained in:
@@ -18,6 +18,7 @@ import androidx.compose.material3.OutlinedCard
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -58,12 +59,26 @@ class DisplayClue<C : Clue>(val clue: C) {
|
||||
|
||||
@Composable
|
||||
fun App(modifier: Modifier = Modifier, game: Game = remember { generateGame() }) {
|
||||
val horizontalClues = remember { game.horizontalClues.map { DisplayClue(it) } }
|
||||
val verticalClues = remember { game.verticalClues.map { DisplayClue(it) } }
|
||||
val time = "00:00:00" // TODO
|
||||
val horizontalClues = remember(game) { game.horizontalClues.map { DisplayClue(it) } }
|
||||
val verticalClues = remember(game) { game.verticalClues.map { DisplayClue(it) } }
|
||||
val timer = remember(game) { GameTimer() }
|
||||
val time by timer.elapsedTime.collectAsState("00:00")
|
||||
LaunchedEffect(game) {
|
||||
game.onStart {
|
||||
timer.start()
|
||||
}
|
||||
|
||||
game.onSolved {
|
||||
timer.stop()
|
||||
}
|
||||
}
|
||||
|
||||
Row(modifier = modifier) {
|
||||
PuzzleGrid(
|
||||
modifier = Modifier.aspectRatio(1f).weight(0.6f).fillMaxHeight(),
|
||||
modifier = Modifier
|
||||
.aspectRatio(1f)
|
||||
.weight(0.6f)
|
||||
.fillMaxHeight(),
|
||||
grid = game.grid,
|
||||
onUpdate = {
|
||||
horizontalClues.forEach { it.update(game.grid) }
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package ch.dissem.yaep.ui.common
|
||||
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
class GameTimer {
|
||||
|
||||
private var startTime: Long = 0L
|
||||
private var stopTime: Long = 0L
|
||||
|
||||
val isRunning: Boolean
|
||||
get() = startTime != 0L && stopTime == 0L
|
||||
|
||||
val elapsedSeconds: Flow<Long> = flow {
|
||||
emit(0L)
|
||||
while (startTime == 0L) {
|
||||
delay(100)
|
||||
}
|
||||
var previousSeconds = 0L
|
||||
while (stopTime == 0L) {
|
||||
val elapsedSeconds = System.currentTimeMillis() / 1000 - startTime
|
||||
if (elapsedSeconds != previousSeconds) {
|
||||
emit(elapsedSeconds)
|
||||
}
|
||||
delay(100)
|
||||
previousSeconds = elapsedSeconds
|
||||
}
|
||||
emit(stopTime - startTime)
|
||||
}
|
||||
|
||||
val elapsedTime: Flow<String> = elapsedSeconds.map { seconds ->
|
||||
val minutes = seconds / 60
|
||||
val hours = minutes / 60
|
||||
val remainingMinutes = minutes % 60
|
||||
val remainingSeconds = seconds % 60
|
||||
if (hours == 0L) {
|
||||
"%02d:%02d".format(minutes, remainingSeconds)
|
||||
} else {
|
||||
"%02d:%02d:%02d".format(hours, remainingMinutes, remainingSeconds)
|
||||
}
|
||||
}
|
||||
|
||||
fun start() {
|
||||
if (startTime == 0L) {
|
||||
startTime = System.currentTimeMillis() / 1000
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
if (isRunning) {
|
||||
stopTime = System.currentTimeMillis() / 1000
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user