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 all commits
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
36 changes: 23 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,35 @@

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

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionLayout
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,6 +45,7 @@ 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,
Expand All @@ -61,7 +67,7 @@ fun ChatRow(
Image(
painter = rememberIconPainter(contentUri = contact.iconUri),
contentDescription = null,
modifier = Modifier
modifier = Modifier.Companion
.size(48.dp)
.clip(CircleShape)
.background(Color.LightGray),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.app.Activity
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.tween
Expand Down Expand Up @@ -60,6 +61,7 @@ fun Main(
}
}

@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
fun MainNavigation(
modifier: Modifier,
Expand All @@ -78,7 +80,6 @@ fun MainNavigation(
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
}

NavHost(
navController = navController,
startDestination = "home",
Expand All @@ -94,8 +95,8 @@ fun MainNavigation(
route = "home",
) {
Home(
modifier = Modifier.fillMaxSize(),
onChatClicked = { chatId -> navController.navigate("chat/$chatId") },
modifier = Modifier.fillMaxSize()
)
}
composable(
Expand All @@ -116,12 +117,12 @@ fun MainNavigation(
ChatScreen(
chatId = chatId,
foreground = true,
modifier = Modifier.fillMaxSize(),
onBackPressed = { navController.popBackStack() },
onCameraClick = { navController.navigate("chat/$chatId/camera") },
onPhotoPickerClick = { navController.navigateToPhotoPicker(chatId) },
onVideoClick = { uri -> navController.navigate("videoPlayer?uri=$uri") },
prefilledText = text,
modifier = Modifier.fillMaxSize(),
)
}
composable(
Expand Down Expand Up @@ -187,7 +188,6 @@ fun MainNavigation(
)
}
}

if (shortcutParams != null) {
val chatId = extractChatId(shortcutParams.shortcutId)
val text = shortcutParams.text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -108,6 +113,7 @@ import kotlinx.coroutines.withContext

private const val TAG = "ChatUI"

@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
fun ChatScreen(
chatId: Long,
Expand All @@ -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)
Expand All @@ -143,7 +149,7 @@ fun ChatScreen(
onPhotoPickerClick = onPhotoPickerClick,
onVideoClick = onVideoClick,
modifier = modifier
.clip(RoundedCornerShape(5)),
.clip(RoundedCornerShape(5))
)
}
LifecycleEffect(
Expand Down Expand Up @@ -175,7 +181,7 @@ private fun LifecycleEffect(
}
}

@OptIn(ExperimentalMaterial3Api::class)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalSharedTransitionApi::class)
@Composable
private fun ChatContent(
chat: ChatDetail,
Expand All @@ -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)
Expand Down Expand Up @@ -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 = {
Expand All @@ -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,
Expand Down Expand Up @@ -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) {_ ->
Copy link
Contributor

Choose a reason for hiding this comment

The 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 = {}
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.samples.socialite.ui.home

import android.annotation.SuppressLint
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -39,13 +40,13 @@ import com.google.android.samples.socialite.R
import com.google.android.samples.socialite.model.ChatDetail
import com.google.android.samples.socialite.ui.ChatRow

@OptIn(ExperimentalPermissionsApi::class)
@OptIn(ExperimentalPermissionsApi::class, ExperimentalSharedTransitionApi::class)
@Composable
internal fun ChatList(
chats: List<ChatDetail>,
contentPadding: PaddingValues,
onChatClicked: (chatId: Long) -> Unit,
modifier: Modifier = Modifier,
modifier: Modifier = Modifier
) {
@SuppressLint("InlinedApi") // Granted at install time on API <33.
val notificationPermissionState = rememberPermissionState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.samples.socialite.ui.home

import androidx.annotation.StringRes
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ChatBubbleOutline
Expand Down Expand Up @@ -48,11 +49,13 @@ import com.google.android.samples.socialite.R
import com.google.android.samples.socialite.ui.AnimationConstants
import com.google.android.samples.socialite.ui.home.timeline.Timeline

@OptIn(ExperimentalMaterial3AdaptiveNavigationSuiteApi::class)
@OptIn(ExperimentalMaterial3AdaptiveNavigationSuiteApi::class,
ExperimentalSharedTransitionApi::class
)
@Composable
fun Home(
onChatClicked: (chatId: Long) -> Unit,
modifier: Modifier = Modifier,
modifier: Modifier = Modifier
) {
var currentDestination by rememberSaveable { mutableStateOf(Destination.Chats) }
NavigationSuiteScaffold(
Expand All @@ -75,14 +78,19 @@ fun Home(
)
}
},
) { HomeContent(currentDestination, modifier, onChatClicked) }
) { HomeContent(
currentDestination,
modifier,
onChatClicked
) }
}

@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
private fun HomeContent(
currentDestination: Destination,
modifier: Modifier,
onChatClicked: (chatId: Long) -> Unit,
onChatClicked: (chatId: Long) -> Unit
) {
Scaffold(
modifier = modifier,
Expand Down Expand Up @@ -116,7 +124,7 @@ private fun HomeContent(
chats = chats,
contentPadding = innerPadding,
onChatClicked = onChatClicked,
modifier = modifier,
modifier = modifier
)
}
composable(
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cameraViewfinderCompose = "1.0.0-SNAPSHOT"
coil = "2.4.0"
compose_bom = "2024.04.00"
composeCompiler = "1.5.4" # Used in app/build.gradle.kts
compose-foundation = "1.6.0-beta03"
compose-foundation = "1.7.0-beta01"
concurrent = "1.1.0"
core = "1.12.0"
core-splashscreen = "1.0.1"
Expand Down
Loading