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

Added shared element transition between contact row and chat screen #74

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -20,7 +20,9 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.google.android.samples.socialite.ui.Bubble
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class BubbleActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
38 changes: 25 additions & 13 deletions app/src/main/java/com/google/android/samples/socialite/ui/Bubble.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,37 @@

package com.google.android.samples.socialite.ui

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionLayout
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.google.android.samples.socialite.ui.chat.ChatScreen

@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
fun Bubble(chatId: Long) {
SocialTheme {
ChatScreen(
chatId = chatId,
foreground = false,
onBackPressed = null,
// TODO (donovanfm): Hook up camera button in the Bubble composable
onCameraClick = {},
// TODO (jolandaverhoef): Hook up play video button in the Bubble composable
onVideoClick = {},
// TODO (mayurikhin): Hook up camera button in the Bubble composable
onPhotoPickerClick = {},
modifier = Modifier.fillMaxSize(),
)
SharedTransitionLayout {
AnimatedContent(targetState = 1) { _ ->
Copy link
Contributor

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.

Copy link
Contributor

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.

SocialTheme {
ChatScreen(
chatId = chatId,
foreground = false,
onBackPressed = null,
// TODO (donovanfm): Hook up camera button in the Bubble composable
onCameraClick = {},
// TODO (jolandaverhoef): Hook up play video button in the Bubble composable
onVideoClick = {},
// TODO (mayurikhin): Hook up camera button in the Bubble composable
onPhotoPickerClick = {},
modifier = Modifier.fillMaxSize(),
sharedTransitionScope = this@SharedTransitionLayout,
animatedContentScope = this@AnimatedContent
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"),
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
)
}
}
}
}
Loading
Loading