-
Notifications
You must be signed in to change notification settings - Fork 344
Migrate remaining Compose side-effects snippets #832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -43,6 +43,7 @@ import androidx.compose.runtime.remember | |||||
| import androidx.compose.runtime.rememberCoroutineScope | ||||||
| import androidx.compose.runtime.rememberUpdatedState | ||||||
| import androidx.compose.runtime.setValue | ||||||
| import androidx.compose.runtime.snapshotFlow | ||||||
| import androidx.compose.ui.Modifier | ||||||
| import androidx.lifecycle.Lifecycle | ||||||
| import androidx.lifecycle.LifecycleEventObserver | ||||||
|
|
@@ -52,6 +53,9 @@ 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.flow.distinctUntilChanged | ||||||
| import kotlinx.coroutines.flow.filter | ||||||
| import kotlinx.coroutines.flow.map | ||||||
| import kotlinx.coroutines.isActive | ||||||
| import kotlinx.coroutines.launch | ||||||
|
|
||||||
|
|
@@ -263,3 +267,54 @@ fun DerivedStateOfWrongUsage() { | |||||
| val fullNameCorrect = "$firstName $lastName" // This is correct | ||||||
| // [END android_compose_side_effects_derivedstateof_wrong] | ||||||
| } | ||||||
|
|
||||||
| @Composable | ||||||
| private fun SnapshotFlowExample() { | ||||||
| // [START android_compose_side_effects_snapshotflow] | ||||||
| val listState = rememberLazyListState() | ||||||
|
|
||||||
| LazyColumn(state = listState) { | ||||||
| // ... | ||||||
| } | ||||||
|
|
||||||
| LaunchedEffect(listState) { | ||||||
| snapshotFlow { listState.firstVisibleItemIndex } | ||||||
| .map { index -> index > 0 } | ||||||
| .distinctUntilChanged() | ||||||
| .filter { it == true } | ||||||
| .collect { | ||||||
| MyAnalyticsService.sendScrolledPastFirstItemEvent() | ||||||
| } | ||||||
| } | ||||||
| // [END android_compose_side_effects_snapshotflow] | ||||||
| } | ||||||
|
|
||||||
| private object MyAnalyticsService { | ||||||
| fun sendScrolledPastFirstItemEvent() {} | ||||||
| } | ||||||
|
|
||||||
| private object RestartingEffectsSnippet { | ||||||
| // [START android_compose_side_effects_restarting_effects] | ||||||
| @Composable | ||||||
| fun HomeScreen( | ||||||
| lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, | ||||||
| onStart: () -> Unit, // Send the 'started' analytics event | ||||||
| onStop: () -> Unit // Send the 'stopped' analytics event | ||||||
| ) { | ||||||
| // These values never change in Composition | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is misleading. The values held by
Suggested change
|
||||||
| val currentOnStart by rememberUpdatedState(onStart) | ||||||
| val currentOnStop by rememberUpdatedState(onStop) | ||||||
|
|
||||||
| DisposableEffect(lifecycleOwner) { | ||||||
| val observer = LifecycleEventObserver { _, event -> | ||||||
| /* ... */ | ||||||
| } | ||||||
|
|
||||||
| lifecycleOwner.lifecycle.addObserver(observer) | ||||||
| onDispose { | ||||||
| lifecycleOwner.lifecycle.removeObserver(observer) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| // [END android_compose_side_effects_restarting_effects] | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For boolean flows,
filter { it == true }can be simplified tofilter { it }for better readability and conciseness. This is a common idiomatic expression in Kotlin.