From 6d30451f4425aa8edd566d5eaab4baa58c2ed36e Mon Sep 17 00:00:00 2001 From: Ben Trengrove Date: Tue, 25 Jun 2024 09:23:58 +1000 Subject: [PATCH] Update the LaunchedEffect snipper --- .../sideeffects/SideEffectsSnippets.kt | 52 ++++++------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/sideeffects/SideEffectsSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/sideeffects/SideEffectsSnippets.kt index 4241e4af9..c68791a0f 100644 --- a/compose/snippets/src/main/java/com/example/compose/snippets/sideeffects/SideEffectsSnippets.kt +++ b/compose/snippets/src/main/java/com/example/compose/snippets/sideeffects/SideEffectsSnippets.kt @@ -18,12 +18,13 @@ package com.example.compose.snippets.sideeffects import android.media.Image import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.core.Animatable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.rememberLazyListState -import androidx.compose.material.Button +import androidx.compose.material3.Button import androidx.compose.material3.Scaffold import androidx.compose.material3.SnackbarHost import androidx.compose.material3.SnackbarHostState @@ -50,53 +51,32 @@ import com.example.compose.snippets.interop.FirebaseAnalytics import com.example.compose.snippets.interop.User import com.example.compose.snippets.kotlin.Message import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive import kotlinx.coroutines.launch -// [START android_compose_side_effects_launchedeffect] @Composable -fun MyScreen( - state: UiState>, - snackbarHostState: SnackbarHostState -) { - - // If the UI state contains an error, show snackbar - if (state.hasError) { - - // `LaunchedEffect` will cancel and re-launch if - // `scaffoldState.snackbarHostState` changes - LaunchedEffect(snackbarHostState) { - // Show snackbar using a coroutine, when the coroutine is cancelled the - // snackbar will automatically dismiss. This coroutine will cancel whenever - // `state.hasError` is false, and only start when `state.hasError` is true - // (due to the above if-check), or if `scaffoldState.snackbarHostState` changes. - snackbarHostState.showSnackbar( - message = "Error message", - actionLabel = "Retry message" - ) - } - } - - Scaffold( - snackbarHost = { - SnackbarHost(hostState = snackbarHostState) +fun MyScreen() { +// [START android_compose_side_effects_launchedeffect] + // Allow the pulse rate to be configured, so it can be sped up if the user is running + // out of time + var pulseRateMs by remember { mutableStateOf(3000L) } + val alpha = remember { Animatable(1f) } + LaunchedEffect(pulseRateMs) { // Restart the effect when the pulse rate changes + while (isActive) { + delay(pulseRateMs) // Pulse the alpha every pulseRateMs to alert the user + alpha.animateTo(0f) + alpha.animateTo(1f) } - ) { contentPadding -> - // [START_EXCLUDE] - Box(Modifier.padding(contentPadding)) - // [END_EXCLUDE] } +// [END android_compose_side_effects_launchedeffect] } + // [START_EXCLUDE silent] class Movie { val url = "" val id = "" } -class UiState { - val hasError = true -} // [END_EXCLUDE] -// [END android_compose_side_effects_launchedeffect] - // [START android_compose_side_effects_remembercoroutinescope] @Composable fun MoviesScreen(snackbarHostState: SnackbarHostState) {