Timer Zurba
unknown
plain_text
5 months ago
7.3 kB
10
Indexable
package com.example.timerapp import android.app.AlertDialog import android.media.MediaPlayer import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.compose.BackHandler import androidx.compose.animation.animateColorAsState import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp import kotlinx.coroutines.delay import kotlinx.coroutines.launch class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { TimerApp() } } } @Composable fun TimerApp() { val scope = rememberCoroutineScope() var timer1 by remember { mutableStateOf(0) } var timer2 by remember { mutableStateOf(0) } var iterations1 by remember { mutableStateOf(1) } var iterations2 by remember { mutableStateOf(1) } var isPerson1Active by remember { mutableStateOf(true) } var isRunning by remember { mutableStateOf(false) } var isButtonDisabled by remember { mutableStateOf(false) } val highlightColor1 by animateColorAsState(if (isPerson1Active) Color.White else Color.Transparent) val highlightColor2 by animateColorAsState(if (!isPerson1Active) Color.White else Color.Transparent) val mediaPlayer1 = remember { MediaPlayer() } // Placeholder for person 1 sound val mediaPlayer2 = remember { MediaPlayer() } // Placeholder for person 2 sound // Function to start timers fun startTimer1() { scope.launch { while (timer1 > 0 && isRunning) { delay(1000) timer1-- } if (timer1 == 0) { mediaPlayer1.start() // Play sound for person 1 isPerson1Active = true } } } fun startTimer2() { scope.launch { while (timer2 > 0 && isRunning) { delay(1000) timer2-- } if (timer2 == 0) { mediaPlayer2.start() // Play sound for person 2 isPerson1Active = false } } } fun onPlusPressed(isPerson1: Boolean) { if (isButtonDisabled) return isButtonDisabled = true scope.launch { delay(10000) // Disable buttons for 10 seconds isButtonDisabled = false } if (isPerson1) { timer1 += 20 iterations1++ isPerson1Active = false startTimer1() } else { timer2 += 20 iterations2++ isPerson1Active = true startTimer2() } } fun onMinusPressed(isPerson1: Boolean) { if (isButtonDisabled) return isButtonDisabled = true scope.launch { delay(10000) // Disable buttons for 10 seconds isButtonDisabled = false } if (isPerson1) { if (timer1 > 20) { timer1 -= 20 iterations1 = maxOf(1, iterations1 - 1) } } else { if (timer2 > 20) { timer2 -= 20 iterations2 = maxOf(1, iterations2 - 1) } } } fun formatTime(seconds: Int): String { val minutes = seconds / 60 val remainingSeconds = seconds % 60 return String.format("%02d:%02d", minutes, remainingSeconds) } Scaffold( topBar = { TopAppBar(title = { Text("Workout Timer") }) } ) { Column( modifier = Modifier .fillMaxSize() .padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally ) { // Person 1 Box( modifier = Modifier .border(BorderStroke(4.dp, highlightColor1), shape = CircleShape) .padding(8.dp) ) { Column(horizontalAlignment = Alignment.CenterHorizontally) { Image( painter = painterResource(id = R.drawable.person_placeholder), contentDescription = "Person 1", modifier = Modifier.size(100.dp) ) Text("Person 1") Text("Time: ${formatTime(timer1)}") Text("Iterations: $iterations1") Row { Button( onClick = { onPlusPressed(true) }, enabled = !isButtonDisabled ) { Text("+") } Spacer(modifier = Modifier.width(8.dp)) Button( onClick = { onMinusPressed(true) }, enabled = !isButtonDisabled ) { Text("-") } } } } Spacer(modifier = Modifier.height(20.dp)) // Person 2 Box( modifier = Modifier .border(BorderStroke(4.dp, highlightColor2), shape = CircleShape) .padding(8.dp) ) { Column(horizontalAlignment = Alignment.CenterHorizontally) { Image( painter = painterResource(id = R.drawable.person_placeholder), contentDescription = "Person 2", modifier = Modifier.size(100.dp) ) Text("Person 2") Text("Time: ${formatTime(timer2)}") Text("Iterations: $iterations2") Row { Button( onClick = { onPlusPressed(false) }, enabled = !isButtonDisabled ) { Text("+") } Spacer(modifier = Modifier.width(8.dp)) Button( onClick = { onMinusPressed(false) }, enabled = !isButtonDisabled ) { Text("-") } } } } Spacer(modifier = Modifier.height(40.dp)) Button(onClick = { isRunning = !isRunning }) { Text(if (isRunning) "Pause" else "Start") } } } // Handle back press BackHandler { AlertDialog.Builder(it) .setTitle("Exit App") .setMessage("Are you sure you want to exit?") .setPositiveButton("Yes") { _, _ -> (it as ComponentActivity).finish() } .setNegativeButton("No", null) .show() } }
Editor is loading...
Leave a Comment