Skip to content

Commit 1ef8542

Browse files
authored
Update the LaunchedEffect snipper (#285)
1 parent fcc7775 commit 1ef8542

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/sideeffects/SideEffectsSnippets.kt

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ package com.example.compose.snippets.sideeffects
1818

1919
import android.media.Image
2020
import androidx.compose.animation.AnimatedVisibility
21+
import androidx.compose.animation.core.Animatable
2122
import androidx.compose.foundation.layout.Box
2223
import androidx.compose.foundation.layout.Column
2324
import androidx.compose.foundation.layout.padding
2425
import androidx.compose.foundation.lazy.LazyColumn
2526
import androidx.compose.foundation.lazy.rememberLazyListState
26-
import androidx.compose.material.Button
27+
import androidx.compose.material3.Button
2728
import androidx.compose.material3.Scaffold
2829
import androidx.compose.material3.SnackbarHost
2930
import androidx.compose.material3.SnackbarHostState
@@ -50,53 +51,32 @@ import com.example.compose.snippets.interop.FirebaseAnalytics
5051
import com.example.compose.snippets.interop.User
5152
import com.example.compose.snippets.kotlin.Message
5253
import kotlinx.coroutines.delay
54+
import kotlinx.coroutines.isActive
5355
import kotlinx.coroutines.launch
5456

55-
// [START android_compose_side_effects_launchedeffect]
5657
@Composable
57-
fun MyScreen(
58-
state: UiState<List<Movie>>,
59-
snackbarHostState: SnackbarHostState
60-
) {
61-
62-
// If the UI state contains an error, show snackbar
63-
if (state.hasError) {
64-
65-
// `LaunchedEffect` will cancel and re-launch if
66-
// `scaffoldState.snackbarHostState` changes
67-
LaunchedEffect(snackbarHostState) {
68-
// Show snackbar using a coroutine, when the coroutine is cancelled the
69-
// snackbar will automatically dismiss. This coroutine will cancel whenever
70-
// `state.hasError` is false, and only start when `state.hasError` is true
71-
// (due to the above if-check), or if `scaffoldState.snackbarHostState` changes.
72-
snackbarHostState.showSnackbar(
73-
message = "Error message",
74-
actionLabel = "Retry message"
75-
)
76-
}
77-
}
78-
79-
Scaffold(
80-
snackbarHost = {
81-
SnackbarHost(hostState = snackbarHostState)
58+
fun MyScreen() {
59+
// [START android_compose_side_effects_launchedeffect]
60+
// Allow the pulse rate to be configured, so it can be sped up if the user is running
61+
// out of time
62+
var pulseRateMs by remember { mutableStateOf(3000L) }
63+
val alpha = remember { Animatable(1f) }
64+
LaunchedEffect(pulseRateMs) { // Restart the effect when the pulse rate changes
65+
while (isActive) {
66+
delay(pulseRateMs) // Pulse the alpha every pulseRateMs to alert the user
67+
alpha.animateTo(0f)
68+
alpha.animateTo(1f)
8269
}
83-
) { contentPadding ->
84-
// [START_EXCLUDE]
85-
Box(Modifier.padding(contentPadding))
86-
// [END_EXCLUDE]
8770
}
71+
// [END android_compose_side_effects_launchedeffect]
8872
}
73+
8974
// [START_EXCLUDE silent]
9075
class Movie {
9176
val url = ""
9277
val id = ""
9378
}
94-
class UiState<T> {
95-
val hasError = true
96-
}
9779
// [END_EXCLUDE]
98-
// [END android_compose_side_effects_launchedeffect]
99-
10080
// [START android_compose_side_effects_remembercoroutinescope]
10181
@Composable
10282
fun MoviesScreen(snackbarHostState: SnackbarHostState) {

0 commit comments

Comments
 (0)