From 9566d206afa4a9ddd3afa6107804b87f561a1ba8 Mon Sep 17 00:00:00 2001 From: garanj Date: Thu, 14 Sep 2023 20:04:12 +0100 Subject: [PATCH] AmbientAware isAlwaysOnScreen flag (#1679) * Adds support for enabling or disabling always-on mode. This is useful if different screens in the app may need or not need this behaviour, for example a workout screen vs an end-of-workout summary. --- compose-layout/api/current.api | 2 +- .../compose/ambient/AmbientAware.kt | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/compose-layout/api/current.api b/compose-layout/api/current.api index f8ba34d978..c041fb723d 100644 --- a/compose-layout/api/current.api +++ b/compose-layout/api/current.api @@ -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 block); + method @androidx.compose.runtime.Composable public static void AmbientAware(optional boolean isAlwaysOnScreen, kotlin.jvm.functions.Function1 block); } public final class AmbientAwareTimeKt { diff --git a/compose-layout/src/main/java/com/google/android/horologist/compose/ambient/AmbientAware.kt b/compose-layout/src/main/java/com/google/android/horologist/compose/ambient/AmbientAware.kt index 6c1a9742cb..4795bc3880 100644 --- a/compose-layout/src/main/java/com/google/android/horologist/compose/ambient/AmbientAware.kt +++ b/compose-layout/src/main/java/com/google/android/horologist/compose/ambient/AmbientAware.kt @@ -39,21 +39,30 @@ import androidx.wear.ambient.AmbientLifecycleObserver * https://developer.android.com/training/wearables/views/always-on). * * It should therefore be used high up in the tree of composables. + * + * @param isAlwaysOnScreen If supplied, this indicates whether always-on should be enabled. This can + * be used to ensure that some screens display an ambient-mode version, whereas others do not, for + * example, a workout screen vs a end-of-workout summary screen. + * @param block Lambda that will be used for building the UI, which is passed the current ambient + * state. */ @Composable -fun AmbientAware(block: @Composable (AmbientStateUpdate) -> Unit) { +fun AmbientAware( + isAlwaysOnScreen: Boolean = true, + block: @Composable (AmbientStateUpdate) -> Unit, +) { val activity = LocalContext.current.findActivityOrNull() // 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. - if (activity == null) { - AmbientAwareNoActivity(block) + if (activity != null && isAlwaysOnScreen) { + AmbientAwareEnabled(activity, block) } else { - AmbientAwareWithActivity(activity, block) + AmbientAwareDisabled(block) } } @Composable -private fun AmbientAwareWithActivity( +private fun AmbientAwareEnabled( activity: Activity, block: @Composable (AmbientStateUpdate) -> Unit, ) { @@ -101,7 +110,7 @@ private fun AmbientAwareWithActivity( } @Composable -private fun AmbientAwareNoActivity(block: @Composable (AmbientStateUpdate) -> Unit) { +private fun AmbientAwareDisabled(block: @Composable (AmbientStateUpdate) -> Unit) { val staticAmbientState by remember { mutableStateOf(AmbientStateUpdate(AmbientState.Interactive)) } block(staticAmbientState) }