-
Notifications
You must be signed in to change notification settings - Fork 74
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
Added shared element transition between contact row and chat screen #74
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,11 @@ | |
|
||
package com.google.android.samples.socialite.ui | ||
|
||
import androidx.compose.animation.AnimatedContentScope | ||
import androidx.compose.animation.ExperimentalSharedTransitionApi | ||
import androidx.compose.animation.SharedTransitionScope | ||
import androidx.compose.animation.fadeIn | ||
import androidx.compose.animation.fadeOut | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.clickable | ||
|
@@ -40,60 +45,81 @@ import androidx.compose.ui.unit.sp | |
import com.google.android.samples.socialite.data.utils.toReadableString | ||
import com.google.android.samples.socialite.model.ChatDetail | ||
|
||
@OptIn(ExperimentalSharedTransitionApi::class) | ||
@Composable | ||
fun ChatRow( | ||
chat: ChatDetail, | ||
onClick: (() -> Unit)?, | ||
modifier: Modifier = Modifier, | ||
sharedTransitionScope: SharedTransitionScope, | ||
animatedContentScope: AnimatedContentScope | ||
) { | ||
Row( | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.then( | ||
if (onClick != null) Modifier.clickable(onClick = onClick) else Modifier, | ||
) | ||
.padding(16.dp), | ||
verticalAlignment = Alignment.CenterVertically, | ||
horizontalArrangement = Arrangement.spacedBy(16.dp), | ||
) { | ||
// This only supports DM for now. | ||
val contact = chat.attendees.first() | ||
Image( | ||
painter = rememberIconPainter(contentUri = contact.iconUri), | ||
contentDescription = null, | ||
modifier = Modifier | ||
.size(48.dp) | ||
.clip(CircleShape) | ||
.background(Color.LightGray), | ||
) | ||
Column( | ||
modifier = Modifier.weight(1f), | ||
verticalArrangement = Arrangement.SpaceEvenly, | ||
horizontalAlignment = Alignment.Start, | ||
) { | ||
Text( | ||
text = contact.name, | ||
style = MaterialTheme.typography.bodyLarge, | ||
fontSize = 16.sp, | ||
) | ||
Text( | ||
text = chat.chatWithLastMessage.text, | ||
style = MaterialTheme.typography.bodySmall, | ||
maxLines = 1, | ||
overflow = TextOverflow.Ellipsis, | ||
modifier = Modifier.padding(top = 4.dp), | ||
fontWeight = FontWeight.Light, | ||
fontSize = 14.sp, | ||
) | ||
} | ||
Column( | ||
verticalArrangement = Arrangement.SpaceEvenly, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
with(sharedTransitionScope) { | ||
Row( | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.then( | ||
if (onClick != null) Modifier.clickable(onClick = onClick) else Modifier, | ||
) | ||
.padding(16.dp) | ||
.sharedBounds( | ||
sharedTransitionScope.rememberSharedContentState(key = "Bounds${chat.chatWithLastMessage.id}"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this shared element is quite complex - you might want to consider introducing a class that represents the key - For example like this https://developer.android.com/develop/ui/compose/animation/shared-elements#unique-keys. It means it'll be strongly typed, easier to navigate the codebase and find the corresponding keys, and harder to override if you were to add another shared element on the same screen. |
||
animatedVisibilityScope = animatedContentScope, | ||
enter = fadeIn(), | ||
exit = fadeOut(), | ||
), | ||
verticalAlignment = Alignment.CenterVertically, | ||
horizontalArrangement = Arrangement.spacedBy(16.dp), | ||
) { | ||
Text( | ||
text = chat.chatWithLastMessage.timestamp.toReadableString(), | ||
fontSize = 14.sp, | ||
// This only supports DM for now. | ||
val contact = chat.attendees.first() | ||
Image( | ||
painter = rememberIconPainter(contentUri = contact.iconUri), | ||
contentDescription = null, | ||
modifier = Modifier.Companion | ||
.sharedElement( | ||
sharedTransitionScope.rememberSharedContentState(key = chat.chatWithLastMessage.id), | ||
animatedVisibilityScope = animatedContentScope | ||
) | ||
.size(48.dp) | ||
.clip(CircleShape) | ||
.background(Color.LightGray), | ||
) | ||
Column( | ||
modifier = Modifier.weight(1f), | ||
verticalArrangement = Arrangement.SpaceEvenly, | ||
horizontalAlignment = Alignment.Start, | ||
) { | ||
Text( | ||
text = contact.name, | ||
style = MaterialTheme.typography.bodyLarge, | ||
fontSize = 16.sp, | ||
modifier = Modifier.Companion.sharedBounds( | ||
sharedTransitionScope.rememberSharedContentState(key = "Text${chat.chatWithLastMessage.id}"), | ||
animatedVisibilityScope = animatedContentScope, | ||
enter = fadeIn(), | ||
exit = fadeOut(), | ||
) | ||
) | ||
Text( | ||
text = chat.chatWithLastMessage.text, | ||
style = MaterialTheme.typography.bodySmall, | ||
maxLines = 1, | ||
overflow = TextOverflow.Ellipsis, | ||
modifier = Modifier.padding(top = 4.dp), | ||
fontWeight = FontWeight.Light, | ||
fontSize = 14.sp, | ||
) | ||
} | ||
Column( | ||
verticalArrangement = Arrangement.SpaceEvenly, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
Text( | ||
text = chat.chatWithLastMessage.timestamp.toReadableString(), | ||
fontSize = 14.sp, | ||
) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a preview? Or where is this used?
If shared elements aren't required in these examples, you could consider creating a
Modifier.trySharedElement()
that doesn't nessecarily need the scopes and just does a no-op when scopes are not present, and adds the modifiers when scopes are present. Then in screens where its not used, you wouldn't provide the scopes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise I'd change this to
AnimatedVisibility(true)
for a bit of a cleaner description.