Created
September 12, 2024 10:56
-
-
Save trix0/f9e8e6efc267fdbab7b0999122ab5814 to your computer and use it in GitHub Desktop.
Kotlin Coroutines & Flows Masterclass, Coroutine basics Assignment #3
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
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
enableEdgeToEdge() | |
setContent { | |
var birdName by remember { | |
mutableStateOf("Coo") | |
} | |
MyApplicationTheme { | |
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> | |
Column(modifier = Modifier | |
.padding(innerPadding) | |
.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally) { | |
when (birdName) { | |
"Coo" -> { | |
CooBird() | |
} | |
"Caw" -> { | |
CawBird() | |
} | |
"Chirp" -> { | |
ChirpBird() | |
} | |
} | |
Column(modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally) { | |
Button(onClick = { birdName = "Coo" }) { | |
Text(text = "Coo") | |
} | |
Button(onClick = { birdName = "Caw" }) { | |
Text(text = "Caw") | |
} | |
Button(onClick = { birdName = "Chirp" }) { | |
Text(text = "Chirp") | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun CooBird() { | |
LaunchedEffect(key1 = true) { | |
while (true) { | |
delay(1000) | |
println("Coo") | |
} | |
} | |
Text( | |
text = "Selected bird Coo!" | |
) | |
} | |
@Composable | |
fun CawBird() { | |
LaunchedEffect(key1 = true) { | |
while (true) { | |
delay(2000) | |
println("Caw") | |
} | |
} | |
Text( | |
text = "Selected bird Caw!" | |
) | |
} | |
@Composable | |
fun ChirpBird() { | |
LaunchedEffect(key1 = true) { | |
while (true) { | |
delay(3000) | |
println("Chirp") | |
} | |
} | |
Text( | |
text = "Selected bird Chirp!" | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment