-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Handle ambient mode in Exercise screens (#317)
* Refactor: Handle ambient mode in Exercise screens This commit introduces changes to handle ambient mode within individual exercise screens instead of globally. - An AmbientAware composable is added to each screen to control ambient behavior. - Modifiers for ambient mode (ambientGray, ambientBlank) are applied within the screen composables. - Removed the ambientState parameter from composables and adjusted accordingly.
- Loading branch information
Showing
45 changed files
with
367 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...ose/app/src/main/java/com/example/exercisesamplecompose/presentation/ambient/AmbientUI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.example.exercisesamplecompose.presentation.ambient | ||
|
||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.drawWithContent | ||
import androidx.compose.ui.geometry.toRect | ||
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 com.google.android.horologist.compose.ambient.AmbientState | ||
|
||
/** | ||
* A Paint object configured to apply a grayscale effect. | ||
* | ||
* This is achieved by using a ColorMatrix to set the saturation to 0, | ||
* effectively removing all color information from the image. | ||
* Anti-aliasing is disabled for this paint to potentially improve performance. | ||
*/ | ||
private val grayscale = Paint().apply { | ||
colorFilter = ColorFilter.colorMatrix( | ||
ColorMatrix().apply { | ||
setToSaturation(0f) | ||
} | ||
) | ||
isAntiAlias = false | ||
} | ||
|
||
/** | ||
* Applies a grayscale effect and scales down the content when in ambient mode. | ||
* | ||
* This modifier checks the provided [AmbientState] to determine if the device is | ||
* in ambient mode. If it is, the content is scaled down by 10% and a grayscale | ||
* filter is applied. When not in ambient mode, the content is rendered normally. | ||
*/ | ||
fun Modifier.ambientGray(ambientState: AmbientState): Modifier = | ||
graphicsLayer { | ||
if (ambientState.isAmbient) { | ||
scaleX = 0.9f | ||
scaleY = 0.9f | ||
} | ||
}.drawWithContent { | ||
if (ambientState.isAmbient) { | ||
drawIntoCanvas { | ||
it.withSaveLayer(size.toRect(), grayscale) { | ||
drawContent() | ||
} | ||
} | ||
} else { | ||
drawContent() | ||
} | ||
} | ||
|
||
/** | ||
* This modifier conditionally draws the content based on the state provided by an [AmbientState]. | ||
* | ||
* If the `isInteractive` property of the provided [ambientState] is true, the content will be drawn. | ||
* Otherwise, the content will not be drawn, effectively leaving the area blank. | ||
*/ | ||
fun Modifier.ambientBlank(ambientState: AmbientState): Modifier = | ||
drawWithContent { | ||
if (ambientState.isInteractive) { | ||
drawContent() | ||
} | ||
} |
76 changes: 0 additions & 76 deletions
76
...app/src/main/java/com/example/exercisesamplecompose/presentation/component/ProgressBar.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.