@@ -18,12 +18,13 @@ package com.example.compose.snippets.sideeffects
18
18
19
19
import android.media.Image
20
20
import androidx.compose.animation.AnimatedVisibility
21
+ import androidx.compose.animation.core.Animatable
21
22
import androidx.compose.foundation.layout.Box
22
23
import androidx.compose.foundation.layout.Column
23
24
import androidx.compose.foundation.layout.padding
24
25
import androidx.compose.foundation.lazy.LazyColumn
25
26
import androidx.compose.foundation.lazy.rememberLazyListState
26
- import androidx.compose.material .Button
27
+ import androidx.compose.material3 .Button
27
28
import androidx.compose.material3.Scaffold
28
29
import androidx.compose.material3.SnackbarHost
29
30
import androidx.compose.material3.SnackbarHostState
@@ -50,53 +51,32 @@ import com.example.compose.snippets.interop.FirebaseAnalytics
50
51
import com.example.compose.snippets.interop.User
51
52
import com.example.compose.snippets.kotlin.Message
52
53
import kotlinx.coroutines.delay
54
+ import kotlinx.coroutines.isActive
53
55
import kotlinx.coroutines.launch
54
56
55
- // [START android_compose_side_effects_launchedeffect]
56
57
@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 )
82
69
}
83
- ) { contentPadding ->
84
- // [START_EXCLUDE]
85
- Box (Modifier .padding(contentPadding))
86
- // [END_EXCLUDE]
87
70
}
71
+ // [END android_compose_side_effects_launchedeffect]
88
72
}
73
+
89
74
// [START_EXCLUDE silent]
90
75
class Movie {
91
76
val url = " "
92
77
val id = " "
93
78
}
94
- class UiState <T > {
95
- val hasError = true
96
- }
97
79
// [END_EXCLUDE]
98
- // [END android_compose_side_effects_launchedeffect]
99
-
100
80
// [START android_compose_side_effects_remembercoroutinescope]
101
81
@Composable
102
82
fun MoviesScreen (snackbarHostState : SnackbarHostState ) {
0 commit comments