Last active
May 4, 2021 01:31
-
-
Save alexjlockwood/868ddaa80d8724923ffc3ce7030297c2 to your computer and use it in GitHub Desktop.
Example of a TimerButton component that manages its own timer state (video: https://youtu.be/-qbJRDDwB8M)
This file contains hidden or 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
package com.alexjlockwood.composetimerdemo | |
import android.os.Bundle | |
import android.widget.Toast | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.isSystemInDarkTheme | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.material.* | |
import androidx.compose.material.icons.Icons | |
import androidx.compose.material.icons.filled.Favorite | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.rememberCoroutineScope | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.platform.LocalContext | |
import androidx.compose.ui.unit.dp | |
import kotlinx.coroutines.CancellationException | |
import kotlinx.coroutines.launch | |
import kotlin.math.roundToInt | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
MaterialTheme(if (isSystemInDarkTheme()) darkColors() else lightColors()) { | |
Surface { | |
ScreenContent() | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
private fun ScreenContent() { | |
val context = LocalContext.current | |
fun showToast(message: String) = Toast.makeText(context, message, Toast.LENGTH_SHORT).show() | |
val timerState = rememberTimerState(2000) | |
suspend fun startTimer() { | |
try { | |
timerState.startTimer() | |
showToast("Timer completed") | |
} catch (e: CancellationException) { | |
showToast("Timer cancelled") | |
} | |
} | |
suspend fun stopTimer() = timerState.stopTimer() | |
// Uncomment this code to auto-start the timer when the UI is first shown | |
// LaunchedEffect(Unit) { | |
// startTimer() | |
// } | |
val scope = rememberCoroutineScope() | |
Column( | |
modifier = Modifier.padding(16.dp), | |
verticalArrangement = Arrangement.spacedBy(16.dp), | |
) { | |
Box( | |
modifier = Modifier.fillMaxWidth().weight(1f), | |
contentAlignment = Alignment.Center, | |
) { | |
TimerButton( | |
state = timerState, | |
onClick = { /* ... */ }, | |
) { | |
Icon( | |
imageVector = Icons.Default.Favorite, | |
contentDescription = null, | |
) | |
} | |
} | |
Text(text = "Timer running: ${timerState.isRunning}") | |
Text(text = "Timer value: ${(timerState.timerValue * 100).roundToInt() / 100f}") | |
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { | |
Button( | |
modifier = Modifier.weight(1f), | |
onClick = { scope.launch { startTimer() } }, | |
) { | |
Text("Start") | |
} | |
Button( | |
modifier = Modifier.weight(1f), | |
onClick = { scope.launch { stopTimer() } }, | |
) { | |
Text("Stop") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment