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

Refactor: AmbientAware API change #2472

Merged
merged 7 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import android.content.Context
import android.content.ContextWrapper
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.wear.ambient.AmbientLifecycleObserver
Expand All @@ -49,44 +48,44 @@ import androidx.wear.ambient.AmbientLifecycleObserver
@Composable
fun AmbientAware(
isAlwaysOnScreen: Boolean = true,
block: @Composable (AmbientStateUpdate) -> Unit,
block: @Composable (State<AmbientStateUpdate>) -> Unit,
) {
var ambientUpdate by remember(isAlwaysOnScreen) {
mutableStateOf(if (isAlwaysOnScreen) null else AmbientStateUpdate(AmbientState.Interactive))
}

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 && isAlwaysOnScreen) {
val lifecycle = LocalLifecycleOwner.current.lifecycle
val observer = remember {
val callback = object : AmbientLifecycleObserver.AmbientLifecycleCallback {
override fun onEnterAmbient(ambientDetails: AmbientLifecycleObserver.AmbientDetails) {
ambientUpdate = AmbientStateUpdate(AmbientState.Ambient(ambientDetails))
}
val activity = LocalContext.current.findActivityOrNull()
val lifecycle = LocalLifecycleOwner.current.lifecycle

var ambientState = remember {
mutableStateOf<AmbientStateUpdate>(AmbientStateUpdate(AmbientState.Interactive))
}

override fun onExitAmbient() {
ambientUpdate = AmbientStateUpdate(AmbientState.Interactive)
}
val observer = remember {
if (activity != null) {
AmbientLifecycleObserver(
activity,
object : AmbientLifecycleObserver.AmbientLifecycleCallback {
override fun onEnterAmbient(ambientDetails: AmbientLifecycleObserver.AmbientDetails) {
ambientState.value = AmbientStateUpdate(AmbientState.Ambient(ambientDetails))
}

override fun onUpdateAmbient() {
val lastAmbientDetails =
(ambientUpdate?.ambientState as? AmbientState.Ambient)?.ambientDetails
ambientUpdate = AmbientStateUpdate(AmbientState.Ambient(lastAmbientDetails))
}
}
AmbientLifecycleObserver(activity, callback).also {
// Necessary to populate the initial value
val initialAmbientState = if (it.isAmbient) {
AmbientState.Ambient(null)
} else {
AmbientState.Interactive
}
ambientUpdate = AmbientStateUpdate(initialAmbientState)
}
override fun onExitAmbient() {
ambientState.value = AmbientStateUpdate(AmbientState.Interactive)
}

override fun onUpdateAmbient() {
val lastAmbientDetails =
(ambientState.value.ambientState as? AmbientState.Ambient)?.ambientDetails
ambientState.value =
AmbientStateUpdate(AmbientState.Ambient(lastAmbientDetails))
}
},
)
} else {
null
}
}

if (observer != null) {
DisposableEffect(Unit) {
lifecycle.addObserver(observer)

Expand All @@ -96,8 +95,8 @@ fun AmbientAware(
}
}

ambientUpdate?.let {
block(it)
if (isAlwaysOnScreen || ambientState.value.ambientState is AmbientState.Interactive) {
block(ambientState)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ import androidx.wear.ambient.AmbientLifecycleObserver
data class AmbientStateUpdate(
val ambientState: AmbientState,
val changeTimeMillis: Long = System.currentTimeMillis(),
)
) {
override fun toString(): String {
return "${ambientState.javaClass.simpleName}[$changeTimeMillis]"
}

val isInteractive: Boolean
get() = ambientState is AmbientState.Interactive

val isAmbient: Boolean
get() = ambientState is AmbientState.Ambient
}

sealed interface AmbientState {
data class Ambient(val ambientDetails: AmbientLifecycleObserver.AmbientDetails? = null) :
AmbientState

object Interactive : AmbientState
data object Interactive : AmbientState
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,33 @@ package com.google.android.horologist.scratch
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.State
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.toRect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ColorMatrix
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.withSaveLayer
import androidx.wear.compose.material.Text
import androidx.wear.compose.material.TimeText
import androidx.wear.compose.navigation.rememberSwipeDismissableNavController
import androidx.wear.compose.ui.tooling.preview.WearPreviewLargeRound
import com.google.android.horologist.compose.ambient.AmbientAware
import com.google.android.horologist.compose.ambient.AmbientStateUpdate
import com.google.android.horologist.compose.layout.AppScaffold
import com.google.android.horologist.compose.layout.ScreenScaffold
import com.google.android.horologist.compose.nav.SwipeDismissableNavHost
import com.google.android.horologist.compose.nav.composable
import kotlinx.serialization.Serializable

class ScratchActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -34,8 +59,85 @@ class ScratchActivity : ComponentActivity() {

@Composable
fun WearApp() {
val navController = rememberSwipeDismissableNavController()
AmbientAware { update ->
AppScaffold(timeText = {
if (update.value.isInteractive) {
TimeText()
}
}) {
SideEffect {
println("App - Ambient state update: $update")
}

Box(modifier = Modifier.fillMaxSize()) {
SwipeDismissableNavHost(navController, Preparing) {
composable<Preparing> {
PreparingScreen(update, modifier = Modifier.ambientGray(update))
}
composable<Exercise> {
ExerciseScreen(update, modifier = Modifier.ambientGray(update))
}
}
}
}
}
}

private val grayscale = Paint().apply {
colorFilter = ColorFilter.colorMatrix(
ColorMatrix().apply {
setToSaturation(0.5f)
},
)
isAntiAlias = false
}

internal fun Modifier.ambientGray(ambientState: State<AmbientStateUpdate>): Modifier =
if (ambientState.value.isAmbient) {
graphicsLayer {
scaleX = 0.9f
scaleY = 0.9f
}.drawWithContent {
drawIntoCanvas {
it.withSaveLayer(size.toRect(), grayscale) {
drawContent()
}
}
}
} else {
this
}

@Composable
fun ExerciseScreen(ambientState: State<AmbientStateUpdate>, modifier: Modifier = Modifier) {
ScreenScaffold {
}
}

@Composable
fun PreparingScreen(update: State<AmbientStateUpdate>, modifier: Modifier = Modifier) {
SideEffect {
println("PreparingScreen - Ambient state update: $update")
}

ScreenScaffold {
Box(modifier = modifier.fillMaxSize()) {
Text(
"Preparing: ${update.value}",
color = Color.Blue,
modifier = Modifier.align(Alignment.Center),
)
}
}
}

@Serializable
object Preparing

@Serializable
object Exercise

@WearPreviewLargeRound
@Composable
fun WearAppPreview() {
Expand Down
Loading