Skip to content
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

Add lifecycle parameter to AmbientAware #2481

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compose-layout/api/current.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package com.google.android.horologist.compose.ambient {

public final class AmbientAwareKt {
method @androidx.compose.runtime.Composable public static void AmbientAware(kotlin.jvm.functions.Function1<? super com.google.android.horologist.compose.ambient.AmbientState,kotlin.Unit> content);
method @androidx.compose.runtime.Composable public static void AmbientAware(optional androidx.lifecycle.Lifecycle lifecycle, kotlin.jvm.functions.Function1<? super com.google.android.horologist.compose.ambient.AmbientState,kotlin.Unit> content);
method public static androidx.compose.runtime.ProvidableCompositionLocal<com.google.android.horologist.compose.ambient.AmbientState> getLocalAmbientState();
property public static final androidx.compose.runtime.ProvidableCompositionLocal<com.google.android.horologist.compose.ambient.AmbientState> LocalAmbientState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ import androidx.wear.ambient.AmbientLifecycleObserver
*
* It should be used within each individual screen inside nav routes.
*
* @param lifecycle The [Lifecycle] of the activity or current owner such as NavBackStackEntry.
* @param content Lambda that will be used for building the UI, which is passed the current ambient
* state.
*/
@Composable
fun AmbientAware(
lifecycle: Lifecycle = LocalLifecycleOwner.current.lifecycle,
content: @Composable (AmbientState) -> Unit,
) {
// Using AmbientAware correctly relies on there being an Activity context. If there isn't, then
// gracefully allow the composition of [block], but no ambient-mode functionality is enabled.
val activity = LocalContext.current.findActivityOrNull()
val lifecycle = LocalLifecycleOwner.current.lifecycle

val ambientState = rememberAmbientState(activity, lifecycle)

Expand Down
Loading