Skip to content

Commit

Permalink
Add lifecycle parameter to AmbientAware (google#2481)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: yschimke <[email protected]>
(cherry picked from commit 4cceb8d)
  • Loading branch information
yschimke committed Dec 10, 2024
1 parent d8a4110 commit 2d090e7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
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

0 comments on commit 2d090e7

Please sign in to comment.