-
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
Open
DAGalpin
wants to merge
2
commits into
android:main
Choose a base branch
from
DAGalpin:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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
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
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 |
---|---|---|
|
@@ -20,6 +20,12 @@ import android.graphics.Bitmap | |
import android.media.MediaMetadataRetriever | ||
import android.net.Uri | ||
import android.util.Log | ||
import androidx.compose.animation.AnimatedContent | ||
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.border | ||
|
@@ -94,7 +100,6 @@ import androidx.hilt.navigation.compose.hiltViewModel | |
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import coil.compose.AsyncImage | ||
import coil.request.ImageRequest | ||
import com.google.android.samples.socialite.R | ||
|
@@ -108,6 +113,7 @@ import kotlinx.coroutines.withContext | |
|
||
private const val TAG = "ChatUI" | ||
|
||
@OptIn(ExperimentalSharedTransitionApi::class) | ||
@Composable | ||
fun ChatScreen( | ||
chatId: Long, | ||
|
@@ -118,7 +124,7 @@ fun ChatScreen( | |
onPhotoPickerClick: () -> Unit, | ||
onVideoClick: (uri: String) -> Unit, | ||
prefilledText: String? = null, | ||
viewModel: ChatViewModel = hiltViewModel(), | ||
viewModel: ChatViewModel = hiltViewModel() | ||
) { | ||
LaunchedEffect(chatId) { | ||
viewModel.setChatId(chatId) | ||
|
@@ -143,7 +149,7 @@ fun ChatScreen( | |
onPhotoPickerClick = onPhotoPickerClick, | ||
onVideoClick = onVideoClick, | ||
modifier = modifier | ||
.clip(RoundedCornerShape(5)), | ||
.clip(RoundedCornerShape(5)) | ||
) | ||
} | ||
LifecycleEffect( | ||
|
@@ -175,7 +181,7 @@ private fun LifecycleEffect( | |
} | ||
} | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalSharedTransitionApi::class) | ||
@Composable | ||
private fun ChatContent( | ||
chat: ChatDetail, | ||
|
@@ -188,7 +194,7 @@ private fun ChatContent( | |
onCameraClick: () -> Unit, | ||
onPhotoPickerClick: () -> Unit, | ||
onVideoClick: (uri: String) -> Unit, | ||
modifier: Modifier = Modifier, | ||
modifier: Modifier = Modifier | ||
) { | ||
val topAppBarState = rememberTopAppBarState() | ||
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(topAppBarState) | ||
|
@@ -242,13 +248,14 @@ private fun PaddingValues.copy( | |
bottom = bottom ?: calculateBottomPadding(), | ||
) | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalSharedTransitionApi::class) | ||
@Composable | ||
private fun ChatAppBar( | ||
chat: ChatDetail, | ||
scrollBehavior: TopAppBarScrollBehavior, | ||
onBackPressed: (() -> Unit)?, | ||
modifier: Modifier = Modifier, | ||
|
||
) { | ||
TopAppBar( | ||
title = { | ||
|
@@ -258,8 +265,17 @@ private fun ChatAppBar( | |
) { | ||
// This only supports DM for now. | ||
val contact = chat.attendees.first() | ||
SmallContactIcon(iconUri = contact.iconUri, size = 32.dp) | ||
Text(text = contact.name) | ||
Image( | ||
painter = rememberIconPainter(contact.iconUri), | ||
contentDescription = null, | ||
modifier = Modifier.Companion | ||
.size(32.dp) | ||
.clip(CircleShape) | ||
.background(Color.LightGray), | ||
) | ||
Text( | ||
text = contact.name, | ||
) | ||
} | ||
}, | ||
modifier = modifier, | ||
|
@@ -500,27 +516,32 @@ private fun PreviewInputBar() { | |
} | ||
} | ||
|
||
@OptIn(ExperimentalSharedTransitionApi::class) | ||
@Preview | ||
@Composable | ||
private fun PreviewChatContent() { | ||
SocialTheme { | ||
ChatContent( | ||
chat = ChatDetail(ChatWithLastMessage(0L), listOf(Contact.CONTACTS[0])), | ||
messages = listOf( | ||
ChatMessage("Hi!", null, null, 0L, false, null), | ||
ChatMessage("Hello", null, null, 0L, true, null), | ||
ChatMessage("world", null, null, 0L, true, null), | ||
ChatMessage("!", null, null, 0L, true, null), | ||
ChatMessage("Hello, world!", null, null, 0L, true, null), | ||
), | ||
input = "Hello", | ||
sendEnabled = true, | ||
onBackPressed = {}, | ||
onInputChanged = {}, | ||
onSendClick = {}, | ||
onCameraClick = {}, | ||
onPhotoPickerClick = {}, | ||
onVideoClick = {}, | ||
) | ||
SharedTransitionScope { | ||
AnimatedContent(targetState = 1) {_ -> | ||
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. Rather use AnimatedVisibility(true) here instead of AnimatedContent for previews. |
||
SocialTheme { | ||
ChatContent( | ||
chat = ChatDetail(ChatWithLastMessage(0L), listOf(Contact.CONTACTS[0])), | ||
messages = listOf( | ||
ChatMessage("Hi!", null, null, 0L, false, null), | ||
ChatMessage("Hello", null, null, 0L, true, null), | ||
ChatMessage("world", null, null, 0L, true, null), | ||
ChatMessage("!", null, null, 0L, true, null), | ||
ChatMessage("Hello, world!", null, null, 0L, true, null), | ||
), | ||
input = "Hello", | ||
sendEnabled = true, | ||
onBackPressed = {}, | ||
onInputChanged = {}, | ||
onSendClick = {}, | ||
onCameraClick = {}, | ||
onPhotoPickerClick = {}, | ||
onVideoClick = {} | ||
) | ||
} | ||
} | ||
} | ||
} |
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
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
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.
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.