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

Adding shared elements to Jetsnack 🎉 #1314

Open
wants to merge 45 commits into
base: compose-latest
Choose a base branch
from

Conversation

riggaroo
Copy link
Collaborator

@riggaroo riggaroo commented Apr 4, 2024

Adding shared elements between Home feed and SnackDetail.

Introduced SnackSharedElementKey to create a unique key for the shared element to match.

Using Modifier.sharedBounds() on the Card container, and Modifier.sharedElement() on the Image.

Using Modifier.sharedBounds() with Modifier.animateEnterExit() on Text.

The above modifiers require being in an AnimatedVisibilityScope and SharedTransitionScope, in order to use the correct scope at the correct point, we save the scopes in a CompositionLocal and retrieve it when required.

jetsnack.mp4

@riggaroo riggaroo changed the title Adding Shared elements to Jetsnack Adding shared elements to Jetsnack 🎉 Apr 5, 2024
@riggaroo riggaroo marked this pull request as ready for review April 22, 2024 14:31
@riggaroo riggaroo requested a review from a team as a code owner April 22, 2024 14:31
@riggaroo riggaroo changed the base branch from main to compose-latest April 29, 2024 13:36
@@ -41,183 +42,183 @@ val snacks = listOf(
price = 299
),
Snack(
id = 2L,
id = Random.nextLong(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cupcake above still has a fixed ID. Do these IDs need to be random? Also, isn't it now possible (albeit very improbable) that two snacks end up with the same ID?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its for preview purposes to be able to query for a certain snack. yes it is somewhat possible, but this is fake mock data so I dont think its worth the hassle of making it more robust.

Comment on lines +237 to +246
.sharedBounds(
rememberSharedContentState(
key = SnackSharedElementKey(
snackId = snack.id,
origin = snackCollectionId.toString(),
type = SnackSharedElementType.Image
)
),
animatedVisibilityScope = animatedVisibilityScope,
boundsTransform = snackDetailBoundsTransform
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question. 🤔

I think sharedBounds() was used in SnackItem to maintain the visual continuity of space and size, as it involves both an image and text. If there were only an image, would it be appropriate to use sharedElement() instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends, if you have an Async image like this in this case, you'd want to use shared bounds as you may be loading up a higher resolution image. If you are using a standard image that you dont expect to be different, then yes you can use sharedElement :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I interpret it such that sharedBounds is used when the resolution of a component is determined dynamically, and sharedElement is employed when it's static?

Additionally, as I have a keen interest in this topic, would you kindly recommend any references I could consult?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly,
sharedBounds() is for content that is visually different but should share the same area between states, whereas sharedElement() expects the content to be the same.

See the new documentation for shared elements here:

https://developer.android.com/develop/ui/compose/animation/shared-elements#shared-bounds

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if you were to apply some other transformations to the image, for example a black and white filter, you'd need to use sharedBounds.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riggaroo Thank you for your assistance. 😀
I understand the difference.

@yongsuk44
Copy link
Contributor

Will values be added to the 'element' tab later, or is it normal for it to be empty. In my opinion, it would be convenient for debugging to display the values applied to parameters other than the key in sharedContentState.

스크린샷 2024-05-21 오후 5 34 40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants