-
Notifications
You must be signed in to change notification settings - Fork 2
feat(top-app-bar): top app bar avatar should be a component with its own states #981
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
Merged
paulinea
merged 10 commits into
develop
from
977-top-app-bar-avatar-should-be-a-component-with-its-own-states
Apr 9, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8602c4a
Add OudsAvatar composable and use it in OudsTopAppBar
florentmaitre 0b1d752
Several component states and methods are now private
florentmaitre 6f41740
Add snapshot tests for OudsAvatar
florentmaitre 0433d91
Add ripple effect when avatar is pressed
florentmaitre 3a66a77
Minor fix
florentmaitre b635e9b
Avatar is now displayed over a minimal button
florentmaitre 9c1cd35
Fix bug with user click on the avatar
florentmaitre 780d21d
Minor fix
florentmaitre 8f5489b
Review: Avatar now scales with the font
florentmaitre 8c68998
Fix accessibility issue with avatar
florentmaitre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
238 changes: 238 additions & 0 deletions
238
core/src/main/java/com/orange/ouds/core/component/OudsAvatar.kt
This file contains hidden or 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,238 @@ | ||
| /* | ||
| * Software Name: OUDS Android | ||
| * SPDX-FileCopyrightText: Copyright (c) Orange SA | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * This software is distributed under the MIT license, | ||
| * the text of which is available at https://opensource.org/license/MIT/ | ||
| * or see the "LICENSE" file for more details. | ||
| * | ||
| * Software description: Android library of reusable graphical components | ||
| */ | ||
|
|
||
| package com.orange.ouds.core.component | ||
|
|
||
| import androidx.compose.foundation.Image | ||
| import androidx.compose.foundation.background | ||
| import androidx.compose.foundation.interaction.MutableInteractionSource | ||
| import androidx.compose.foundation.isSystemInDarkTheme | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.size | ||
| import androidx.compose.foundation.shape.CircleShape | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.draw.alpha | ||
| import androidx.compose.ui.draw.clip | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.graphics.ImageBitmap | ||
| import androidx.compose.ui.graphics.painter.ColorPainter | ||
| import androidx.compose.ui.graphics.painter.Painter | ||
| import androidx.compose.ui.graphics.vector.ImageVector | ||
| import androidx.compose.ui.layout.ContentScale | ||
| import androidx.compose.ui.platform.LocalConfiguration | ||
| import androidx.compose.ui.semantics.Role | ||
| import androidx.compose.ui.semantics.clearAndSetSemantics | ||
| import androidx.compose.ui.semantics.onClick | ||
| import androidx.compose.ui.semantics.role | ||
| import androidx.compose.ui.semantics.semantics | ||
| import androidx.compose.ui.tooling.preview.PreviewLightDark | ||
| import androidx.compose.ui.tooling.preview.PreviewParameter | ||
| import androidx.compose.ui.unit.dp | ||
| import com.orange.ouds.core.theme.OudsTheme | ||
| import com.orange.ouds.core.utilities.OudsPreview | ||
| import com.orange.ouds.core.utilities.PreviewCheckerboardPainter | ||
| import com.orange.ouds.core.utilities.PreviewEnumEntries | ||
| import com.orange.ouds.core.utilities.getPreviewTheme | ||
| import com.orange.ouds.foundation.utilities.BasicPreviewParameterProvider | ||
| import com.orange.ouds.theme.OudsThemeContract | ||
|
|
||
| @Composable | ||
| internal fun OudsAvatar( | ||
| painter: Painter, | ||
| onClick: (() -> Unit)?, | ||
| modifier: Modifier = Modifier, | ||
| interactionSource: MutableInteractionSource? = null | ||
| ) { | ||
| OudsAvatar( | ||
| graphicsObject = painter, | ||
| monogram = null, | ||
| monogramColor = null, | ||
| monogramBackgroundColor = null, | ||
| onClick = onClick, | ||
| modifier = modifier, | ||
| interactionSource = interactionSource | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| internal fun OudsAvatar( | ||
| imageVector: ImageVector, | ||
| onClick: (() -> Unit)?, | ||
| modifier: Modifier = Modifier, | ||
| interactionSource: MutableInteractionSource? = null | ||
| ) { | ||
| OudsAvatar( | ||
| graphicsObject = imageVector, | ||
| monogram = null, | ||
| monogramColor = null, | ||
| monogramBackgroundColor = null, | ||
| onClick = onClick, | ||
| modifier = modifier, | ||
| interactionSource = interactionSource | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| internal fun OudsAvatar( | ||
| bitmap: ImageBitmap, | ||
| onClick: (() -> Unit)?, | ||
| modifier: Modifier = Modifier, | ||
| interactionSource: MutableInteractionSource? = null | ||
| ) { | ||
| OudsAvatar( | ||
| graphicsObject = bitmap, | ||
| monogram = null, | ||
| monogramColor = null, | ||
| monogramBackgroundColor = null, | ||
| onClick = onClick, | ||
| modifier = modifier, | ||
| interactionSource = interactionSource | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| internal fun OudsAvatar( | ||
| monogram: Char, | ||
| color: Color, | ||
| backgroundColor: Color, | ||
| onClick: (() -> Unit)?, | ||
| modifier: Modifier = Modifier, | ||
| interactionSource: MutableInteractionSource? = null | ||
| ) { | ||
| OudsAvatar( | ||
| graphicsObject = null, | ||
| monogram = monogram, | ||
| monogramColor = color, | ||
| monogramBackgroundColor = backgroundColor, | ||
| onClick = onClick, | ||
| modifier = modifier, | ||
| interactionSource = interactionSource | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| internal fun OudsAvatar( | ||
| graphicsObject: Any?, | ||
| monogram: Char?, | ||
| monogramColor: Color?, | ||
| monogramBackgroundColor: Color?, | ||
| onClick: (() -> Unit)?, | ||
| modifier: Modifier = Modifier, | ||
| interactionSource: MutableInteractionSource? = null | ||
| ) { | ||
| @Suppress("NAME_SHADOWING") val interactionSource = interactionSource ?: remember { MutableInteractionSource() } | ||
|
|
||
| Box( | ||
| modifier = modifier.semantics(mergeDescendants = true) { | ||
| if (onClick != null) { | ||
| role = Role.Button | ||
| onClick(null) { | ||
| onClick() | ||
| true | ||
| } | ||
| } | ||
| }, | ||
| contentAlignment = Alignment.Center | ||
| ) { | ||
| OudsButton( | ||
| modifier = Modifier.alpha(if (onClick != null) 1f else 0f), // If onClick is null, draw the button with alpha 0 anyway to get a consistent size for the avatar | ||
| icon = OudsButtonIcon(ColorPainter(Color.Transparent), ""), // Use transparent painter to scale the button with the font | ||
| onClick = { onClick?.invoke() }, | ||
| appearance = OudsButtonAppearance.Minimal, | ||
| interactionSource = interactionSource | ||
| ) | ||
|
|
||
| val scale = LocalConfiguration.current.fontScale | ||
| val contentModifier = Modifier | ||
| .clip(CircleShape) | ||
| .size(AvatarSize * scale) | ||
| if (graphicsObject != null) { | ||
| val contentScale = ContentScale.Crop | ||
| when (graphicsObject) { | ||
| is Painter -> Image( | ||
| modifier = contentModifier, | ||
| painter = graphicsObject, | ||
| contentDescription = null, | ||
| contentScale = contentScale | ||
| ) | ||
| is ImageVector -> Image( | ||
| modifier = contentModifier, | ||
| imageVector = graphicsObject, | ||
| contentDescription = null, | ||
| contentScale = contentScale | ||
| ) | ||
| is ImageBitmap -> Image( | ||
| modifier = contentModifier, | ||
| bitmap = graphicsObject, | ||
| contentDescription = null, | ||
| contentScale = contentScale | ||
| ) | ||
| } | ||
| } else if (monogram != null && monogramColor != null && monogramBackgroundColor != null) { | ||
| Box( | ||
| modifier = contentModifier.background(monogramBackgroundColor), | ||
| contentAlignment = Alignment.Center, | ||
| ) { | ||
| Text( | ||
| modifier = Modifier.clearAndSetSemantics {}, | ||
| text = monogram.uppercase(), | ||
| color = monogramColor, | ||
| style = MaterialTheme.typography.titleMedium, // This looks like the most accurate style according to Material specs at https://m3.material.io/components/app-bars/specs#606c6564-ce7d-489d-8852-af2b3b478bc6 | ||
| fontFamily = OudsTheme.typography.fontFamily | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private val AvatarSize = 32.dp | ||
|
|
||
| @PreviewLightDark | ||
| @Composable | ||
| @Suppress("PreviewShouldNotBeCalledRecursively") | ||
| private fun PreviewOudsAvatar(@PreviewParameter(OudsAvatarPreviewParameterProvider::class) isMonogram: Boolean) { | ||
| PreviewOudsAvatar(theme = getPreviewTheme(), darkThemeEnabled = isSystemInDarkTheme(), isMonogram = isMonogram) | ||
| } | ||
|
|
||
| @Composable | ||
| internal fun PreviewOudsAvatar( | ||
| theme: OudsThemeContract, | ||
| darkThemeEnabled: Boolean, | ||
| isMonogram: Boolean | ||
| ) = OudsPreview(theme = theme, darkThemeEnabled = darkThemeEnabled) { | ||
| PreviewEnumEntries<OudsButtonState>(filter = { it !in listOf(OudsButtonState.Loading, OudsButtonState.Disabled) }) { | ||
| if (isMonogram) { | ||
| OudsAvatar( | ||
| monogram = 'A', | ||
| color = Color.White, | ||
| backgroundColor = Color(0xffd5204e), | ||
| onClick = {} | ||
| ) | ||
| } else { | ||
| OudsAvatar( | ||
| painter = PreviewCheckerboardPainter( | ||
| squareSize = 6.dp, | ||
| primaryColor = Color(0xff247a85), | ||
| secondaryColor = Color(0xfffbcd00) | ||
| ), | ||
| onClick = {} | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal class OudsAvatarPreviewParameterProvider : BasicPreviewParameterProvider<Boolean>(false, true) | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.