Slide 1

Slide 1 text

Sungyong An Material Motion for Compose

Slide 2

Slide 2 text

Sungyong An NAVER WEBTOON GDE fornewid

Slide 3

Slide 3 text

fornewid/material-motion-compose fornewid/naver-map-compose

Slide 4

Slide 4 text

fornewid/material-motion-compose

Slide 5

Slide 5 text

Compose Jetpack

Slide 6

Slide 6 text

Jetpack Compose

Slide 7

Slide 7 text

Example #1 Tab Contents BottomNavigation చਸ ੹ജೞח ҃਋

Slide 8

Slide 8 text

enum class BottomTabs { Albums, Photos, Search } val (selectedTab, setSelectedTab) = remember { mutableStateOf(BottomTabs.Albums) } Scaffold(bottomBar = { BottomNavigation { BottomTabs.values().forEach { tab -> BottomNavigationItem( ... selected = tab == selectedTab, onClick = { setSelectedTab(tab) } ) } } }) { innerPadding -> BottomTabsContents(selectedTab, ...) } Example #1

Slide 9

Slide 9 text

enum class BottomTabs { Albums, Photos, Search } val (selectedTab, setSelectedTab) = remember { mutableStateOf(BottomTabs.Albums) } Scaffold(bottomBar = { BottomNavigation { BottomTabs.values().forEach { tab -> BottomNavigationItem( ... selected = tab == selectedTab, onClick = { setSelectedTab(tab) } ) } } }) { innerPadding -> BottomTabsContents(selectedTab, ...) } Example #1

Slide 10

Slide 10 text

enum class BottomTabs { Albums, Photos, Search } val (selectedTab, setSelectedTab) = remember { mutableStateOf(BottomTabs.Albums) } Scaffold(bottomBar = { BottomNavigation { BottomTabs.values().forEach { tab -> BottomNavigationItem( ... selected = tab == selectedTab, onClick = { setSelectedTab(tab) } ) } } }) { innerPadding -> BottomTabsContents(selectedTab, ...) } Example #1

Slide 11

Slide 11 text

Scaffold(bottomBar = { ... }) { innerPadding -> BottomTabsContents(selectedTab, ...) } Example #1

Slide 12

Slide 12 text

! ചݶਸ ੹ജೡ ٸ, ੗োझۣ૑ ޅೠ ו՝੉ ٜ૑ ঋաਃ? Example #1

Slide 13

Slide 13 text

Choose an animation API Link: h"ps://developer.android.com/develop/ui/compose/animation/choose-api

Slide 14

Slide 14 text

Choose an animation API Link: h"ps://developer.android.com/develop/ui/compose/animation/choose-api

Slide 15

Slide 15 text

Choose an animation API Link: h"ps://developer.android.com/develop/ui/compose/animation/choose-api য়ט੄ ઱ઁ!

Slide 16

Slide 16 text

Link: h"ps://developer.android.com/develop/ui/compose/animation/choose-api Choose an animation API AnimatedContent Crossfade Enter/ExitTransition and more...

Slide 17

Slide 17 text

“Compose Animation API۽ ചݶ ੹ജ ബҗܳ ҳഅ೧ࠁ੗.” Goal

Slide 18

Slide 18 text

Example #1 Scaffold(bottomBar = { ... }) { innerPadding -> BottomTabsContents(selectedTab) }

Slide 19

Slide 19 text

Scaffold(bottomBar = { ... }) { innerPadding -> Crossfade( targetState = selectedTab, modifier = Modifier.padding(innerPadding) ) { currentTab -> BottomTabsContents(currentTab) } } Crossfade

Slide 20

Slide 20 text

Scaffold(bottomBar = { ... }) { innerPadding -> Crossfade( targetState = selectedTab, modifier = Modifier.padding(innerPadding) ) { currentTab -> BottomTabsContents(currentTab) } } Crossfade

Slide 21

Slide 21 text

! যڌѱ ز੘ೞח૑ ҾӘೞ૑ ঋաਃ? Crossfade

Slide 22

Slide 22 text

Compose 1.6 @Composable fun Crossfade( targetState: T, modifier: Modifier = Modifier, animationSpec: FiniteAnimationSpec = tween(), label: String = "Crossfade", content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState, label) transition.Crossfade(modifier, animationSpec, content = content) }

Slide 23

Slide 23 text

Compose 1.6 @Composable fun Crossfade( targetState: T, modifier: Modifier = Modifier, animationSpec: FiniteAnimationSpec = tween(), label: String = "Crossfade", content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState, label) transition.Crossfade(modifier, animationSpec, content = content) }

Slide 24

Slide 24 text

Compose 1.6 @Composable fun Crossfade( targetState: T, modifier: Modifier = Modifier, animationSpec: FiniteAnimationSpec = tween(), label: String = "Crossfade", content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState, label) transition.Crossfade(modifier, animationSpec, content = content) }

Slide 25

Slide 25 text

Compose 1.6 @Composable fun Crossfade( targetState: T, modifier: Modifier = Modifier, animationSpec: FiniteAnimationSpec = tween(), label: String = "Crossfade", content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState, label) transition.Crossfade(modifier, animationSpec, content = content) }

Slide 26

Slide 26 text

Compose 1.6 @Composable fun Crossfade( targetState: T, modifier: Modifier = Modifier, animationSpec: FiniteAnimationSpec = tween(), label: String = "Crossfade", content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState, label) transition.Crossfade(modifier, animationSpec, content = content) }

Slide 27

Slide 27 text

Compose 1.6 @Composable fun Transition.Crossfade( modifier: Modifier = Modifier, animationSpec: FiniteAnimationSpec = tween(), contentKey: (targetState: T) -> Any? = { it }, content: @Composable (targetState: T) -> Unit ) { val currentlyVisible = remember { mutableStateListOf().apply { add(currentState) } } val contentMap = remember { mutableMapOf Unit>() } if (currentState == targetState) { // If not animating, just display the current state if (currentlyVisible.size != 1 || currentlyVisible[0] != targetState) { // Remove all the intermediate items from the list once the animation is finished. currentlyVisible.removeAll { it != targetState } contentMap.clear() } }

Slide 28

Slide 28 text

@Composable fun Transition.Crossfade( modifier: Modifier = Modifier, animationSpec: FiniteAnimationSpec = tween(), contentKey: (targetState: T) -> Any? = { it }, content: @Composable (targetState: T) -> Unit ) { val currentlyVisible = remember { mutableStateListOf().apply { add(currentState) } } val contentMap = remember { mutableMapOf Unit>() } if (currentState == targetState) { // If not animating, just display the current state if (currentlyVisible.size != 1 || currentlyVisible[0] != targetState) { // Remove all the intermediate items from the list once the animation is finished. currentlyVisible.removeAll { it != targetState } contentMap.clear() } } Compose 1.6

Slide 29

Slide 29 text

val currentlyVisible = remember { mutableStateListOf().apply { add(currentState) } } val contentMap = remember { mutableMapOf Unit>() } if (currentState == targetState) { // If not animating, just display the current state if (currentlyVisible.size != 1 || currentlyVisible[0] != targetState) { // Remove all the intermediate items from the list once the animation is finished. currentlyVisible.removeAll { it != targetState } contentMap.clear() } } if (!contentMap.contains(targetState)) { // Replace target with the same key if any val replacementId = currentlyVisible.indexOfFirst { contentKey(it) == contentKey(targetState) } if (replacementId == -1) { currentlyVisible.add(targetState) } else { currentlyVisible[replacementId] = targetState } contentMap.clear() Compose 1.6

Slide 30

Slide 30 text

} if (replacementId == -1) { currentlyVisible.add(targetState) } else { currentlyVisible[replacementId] = targetState } contentMap.clear() currentlyVisible.fastForEach { stateForContent -> contentMap[stateForContent] = { val alpha by animateFloat( transitionSpec = { animationSpec } ) { if (it == stateForContent) 1f else 0f } Box(Modifier.graphicsLayer { this.alpha = alpha }) { content(stateForContent) } } } } Box(modifier) { currentlyVisible.fastForEach { key(contentKey(it)) { contentMap[it]?.invoke() Compose 1.6

Slide 31

Slide 31 text

} if (replacementId == -1) { currentlyVisible.add(targetState) } else { currentlyVisible[replacementId] = targetState } contentMap.clear() currentlyVisible.fastForEach { stateForContent -> contentMap[stateForContent] = { val alpha by animateFloat( transitionSpec = { animationSpec } ) { if (it == stateForContent) 1f else 0f } Box(Modifier.graphicsLayer { this.alpha = alpha }) { content(stateForContent) } } } } Box(modifier) { currentlyVisible.fastForEach { key(contentKey(it)) { contentMap[it]?.invoke() Compose 1.6

Slide 32

Slide 32 text

} if (replacementId == -1) { currentlyVisible.add(targetState) } else { currentlyVisible[replacementId] = targetState } contentMap.clear() currentlyVisible.fastForEach { stateForContent -> contentMap[stateForContent] = { val alpha by animateFloat( transitionSpec = { animationSpec } ) { if (it == stateForContent) 1f else 0f } Box(Modifier.graphicsLayer { this.alpha = alpha }) { content(stateForContent) } } } } Box(modifier) { currentlyVisible.fastForEach { key(contentKey(it)) { contentMap[it]?.invoke() Compose 1.6

Slide 33

Slide 33 text

contentMap[stateForContent] = { val alpha by animateFloat( transitionSpec = { animationSpec } ) { if (it == stateForContent) 1f else 0f } Box(Modifier.graphicsLayer { this.alpha = alpha }) { content(stateForContent) } } } } Box(modifier) { currentlyVisible.fastForEach { key(contentKey(it)) { contentMap[it]?.invoke() } } } } Compose 1.6

Slide 34

Slide 34 text

contentMap[stateForContent] = { val alpha by animateFloat( transitionSpec = { animationSpec } ) { if (it == stateForContent) 1f else 0f } Box(Modifier.graphicsLayer { this.alpha = alpha }) { content(stateForContent) } } } } Box(modifier) { currentlyVisible.fastForEach { key(contentKey(it)) { contentMap[it]?.invoke() } } } } Compose 1.6 복잡해보이지만,간단합니다. Box(modifier) { Box(Modifier.graphicsLayer { alpha = 1f -> 0f }) { BottomTabsContents(previousTab) } Box(Modifier.graphicsLayer { alpha = 0f -> 1f }) { BottomTabsContents(currentTab) } }

Slide 35

Slide 35 text

Box Box (alpha) Box (alpha) Crossfade

Slide 36

Slide 36 text

! 전환효과가생겼지만, 뭔가아쉽지않나요? Crossfade

Slide 37

Slide 37 text

MaterialDesign의MotionSystem에서는 구성요소또는화면간전환에 4가지패턴을제공합니다. Material Motion

Slide 38

Slide 38 text

Container Transform Shared Axis Fade Through Fade Material Motion

Slide 39

Slide 39 text

Fade Through Material Motion BottomNavigation

Slide 40

Slide 40 text

스펙문서에Transition의 세부정보가적혀있습니다. Fade Through

Slide 41

Slide 41 text

Fade Through

Slide 42

Slide 42 text

Crossfade ... contentMap[stateForContent] = { val alpha by animateFloat( transitionSpec = { animationSpec } ) { if (it == stateForContent) 1f else 0f } Box(Modifier.graphicsLayer { this.alpha = alpha }) { content(stateForContent) } } ...

Slide 43

Slide 43 text

... contentMap[stateForContent] = { val scale by animateFloat( transitionSpec = { animationSpec } ) { if (it == stateForContent) 1f else 0.92f } val alpha by animateFloat( transitionSpec = { animationSpec } ) { if (it == stateForContent) 1f else 0f } Box(Modifier.graphicsLayer { this.alpha = alpha this.scale = scale }) { content(stateForContent) } } ... Fade Through

Slide 44

Slide 44 text

... contentMap[stateForContent] = { val scale by animateFloat( transitionSpec = { animationSpec } ) { if (it == stateForContent) 1f else 0.92f } val alpha by animateFloat( transitionSpec = { animationSpec } ) { if (it == stateForContent) 1f else 0f } Box(Modifier.graphicsLayer { this.alpha = alpha this.scale = scale }) { content(stateForContent) } } ... Fade Through

Slide 45

Slide 45 text

Scaffold(bottomBar = { ... }) { innerPadding -> MaterialFadeThrough( targetState = selectedTab, modifier = Modifier.padding(innerPadding) ) { currentTab -> BottomTabsContents(currentTab) } } Fade Through Box(modifier) { Box(Modifier.graphicsLayer { alpha = 1f -> 0f, scale = 1f }) { BottomTabsContents(previousTab) } Box(Modifier.graphicsLayer { alpha = 0f -> 1f, scale = 0.92f -> 1f }) { BottomTabsContents(currentTab) } }

Slide 46

Slide 46 text

Copy Crossfade ✅ Example #1 Fade Through

Slide 47

Slide 47 text

! Example #2 ݾ۾ਸ ੿۳ೞѢա, Grid/Linear ੹ജೞח ҃਋

Slide 48

Slide 48 text

Example #2

Slide 49

Slide 49 text

Example #2 @Composable fun LibraryContents(state: LibraryState, ...) { Surface(modifier = Modifier.fillMaxSize()) { val sortType = state.sortType val items = remember(sortType) { when (sortType) { SortType.A_TO_Z -> MusicData.albums SortType.Z_TO_A -> MusicData.albums.asReversed() } } when (state.listType) { ListType.Grid -> LibraryGridContents(items, ...) ListType.Linear -> LibraryLinearContents(items, ...) } } }

Slide 50

Slide 50 text

Example #2 @Composable fun LibraryScreen(...) { val (state, onStateChanged) = remember { mutableStateOf(LibraryState(SortType.A_TO_Z, ListType.Grid)) } Scaffold(...) { LibraryContents(state, ...) } }

Slide 51

Slide 51 text

@Composable fun LibraryScreen(...) { val (state, onStateChanged) = remember { mutableStateOf(LibraryState(SortType.A_TO_Z, ListType.Grid)) } Scaffold(...) { AnimatedContent( targetState = state, transitionSpec = { ... }, modifier = Modifier.padding(innerPadding), ) { currentDestination -> LibraryContents(state, ...) } } } AnimatedContent

Slide 52

Slide 52 text

@Composable fun AnimatedContent( targetState: S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentTransitionScope.() -> ContentTransform = { ... }, contentAlignment: Alignment = Alignment.TopStart, label: String = "AnimatedContent", contentKey: (targetState: S) -> Any? = { it }, content: @Composable AnimatedContentScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState, label = label) transition.AnimatedContent( modifier, transitionSpec, contentAlignment, contentKey, content = content ) } Compose 1.6

Slide 53

Slide 53 text

@Composable fun AnimatedContent( targetState: S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentTransitionScope.() -> ContentTransform = { ... }, contentAlignment: Alignment = Alignment.TopStart, label: String = "AnimatedContent", contentKey: (targetState: S) -> Any? = { it }, content: @Composable AnimatedContentScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState, label = label) transition.AnimatedContent( modifier, transitionSpec, contentAlignment, contentKey, content = content ) } Compose 1.6

Slide 54

Slide 54 text

@Composable fun AnimatedContent( targetState: S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentTransitionScope.() -> ContentTransform = { ... }, contentAlignment: Alignment = Alignment.TopStart, label: String = "AnimatedContent", contentKey: (targetState: S) -> Any? = { it }, content: @Composable AnimatedContentScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState, label = label) transition.AnimatedContent( modifier, transitionSpec, contentAlignment, contentKey, content = content ) } Compose 1.6

Slide 55

Slide 55 text

@Composable fun AnimatedContent( targetState: S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentTransitionScope.() -> ContentTransform = { ... }, contentAlignment: Alignment = Alignment.TopStart, label: String = "AnimatedContent", contentKey: (targetState: S) -> Any? = { it }, content: @Composable AnimatedContentScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState, label = label) transition.AnimatedContent( modifier, transitionSpec, contentAlignment, contentKey, content = content ) } Compose 1.6

Slide 56

Slide 56 text

@Composable fun AnimatedContent( targetState: S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentTransitionScope.() -> ContentTransform = { ... }, contentAlignment: Alignment = Alignment.TopStart, label: String = "AnimatedContent", contentKey: (targetState: S) -> Any? = { it }, content: @Composable AnimatedContentScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState, label = label) transition.AnimatedContent( modifier, transitionSpec, contentAlignment, contentKey, content = content ) } Compose 1.6

Slide 57

Slide 57 text

@Composable fun Transition.AnimatedContent( modifier: Modifier = Modifier, transitionSpec: AnimatedContentTransitionScope.() -> ContentTransform = { ... }, contentAlignment: Alignment = Alignment.TopStart, contentKey: (targetState: S) -> Any? = { it }, content: @Composable AnimatedContentScope.(targetState: S) -> Unit ) { val layoutDirection = LocalLayoutDirection.current val rootScope = remember(this) { AnimatedContentTransitionScopeImpl(this, contentAlignment, layoutDirection) } // TODO: remove screen as soon as they are animated out val currentlyVisible = remember(this) { mutableStateListOf(currentState) } val contentMap = remember(this) { mutableMapOf Unit>() } // This is needed for tooling because it could change currentState directly, // as opposed to changing target only. When that happens we need to clear all the // visible content and only display the content for the new current state and target state. if (!currentlyVisible.contains(currentState)) { Compose 1.6

Slide 58

Slide 58 text

contentKey: (targetState: S) -> Any? = { it }, content: @Composable AnimatedContentScope.(targetState: S) -> Unit ) { val layoutDirection = LocalLayoutDirection.current val rootScope = remember(this) { AnimatedContentTransitionScopeImpl(this, contentAlignment, layoutDirection) } // TODO: remove screen as soon as they are animated out val currentlyVisible = remember(this) { mutableStateListOf(currentState) } val contentMap = remember(this) { mutableMapOf Unit>() } // This is needed for tooling because it could change currentState directly, // as opposed to changing target only. When that happens we need to clear all the // visible content and only display the content for the new current state and target state. if (!currentlyVisible.contains(currentState)) { currentlyVisible.clear() currentlyVisible.add(currentState) } if (currentState == targetState) { if (currentlyVisible.size != 1 || currentlyVisible[0] != currentState) { currentlyVisible.clear() currentlyVisible.add(currentState) } if (contentMap.size != 1 || contentMap.containsKey(currentState)) { Compose 1.6

Slide 59

Slide 59 text

if (currentState != targetState && !currentlyVisible.contains(targetState)) { // Replace the target with the same key if any val id = currentlyVisible.indexOfFirst { contentKey(it) == contentKey(targetState) } if (id == -1) { currentlyVisible.add(targetState) } else { currentlyVisible[id] = targetState } } if (!contentMap.containsKey(targetState) || !contentMap.containsKey(currentState)) { contentMap.clear() currentlyVisible.fastForEach { stateForContent -> contentMap[stateForContent] = { val specOnEnter = remember { transitionSpec(rootScope) } // NOTE: enter and exit for this AnimatedVisibility will be using different spec, // naturally. val exit = remember(segment.targetState == stateForContent) { if (segment.targetState == stateForContent) { ExitTransition.None } else { rootScope.transitionSpec().initialContentExit } Compose 1.6

Slide 60

Slide 60 text

if (currentState != targetState && !currentlyVisible.contains(targetState)) { // Replace the target with the same key if any val id = currentlyVisible.indexOfFirst { contentKey(it) == contentKey(targetState) } if (id == -1) { currentlyVisible.add(targetState) } else { currentlyVisible[id] = targetState } } if (!contentMap.containsKey(targetState) || !contentMap.containsKey(currentState)) { contentMap.clear() currentlyVisible.fastForEach { stateForContent -> contentMap[stateForContent] = { val specOnEnter = remember { transitionSpec(rootScope) } // NOTE: enter and exit for this AnimatedVisibility will be using different spec, // naturally. val exit = remember(segment.targetState == stateForContent) { if (segment.targetState == stateForContent) { ExitTransition.None } else { rootScope.transitionSpec().initialContentExit } Compose 1.6

Slide 61

Slide 61 text

if (currentState != targetState && !currentlyVisible.contains(targetState)) { // Replace the target with the same key if any val id = currentlyVisible.indexOfFirst { contentKey(it) == contentKey(targetState) } if (id == -1) { currentlyVisible.add(targetState) } else { currentlyVisible[id] = targetState } } if (!contentMap.containsKey(targetState) || !contentMap.containsKey(currentState)) { contentMap.clear() currentlyVisible.fastForEach { stateForContent -> contentMap[stateForContent] = { val specOnEnter = remember { transitionSpec(rootScope) } // NOTE: enter and exit for this AnimatedVisibility will be using different spec, // naturally. val exit = remember(segment.targetState == stateForContent) { if (segment.targetState == stateForContent) { ExitTransition.None } else { rootScope.transitionSpec().initialContentExit } Compose 1.6

Slide 62

Slide 62 text

} else { rootScope.transitionSpec().initialContentExit } } val childData = remember { AnimatedContentTransitionScopeImpl.ChildData(stateForContent == targetState) } // TODO: Will need a custom impl of this to: 1) get the signal for when // the animation is finished, 2) get the target size properly AnimatedEnterExitImpl( this, { it == stateForContent }, enter = specOnEnter.targetContentEnter, exit = exit, modifier = Modifier .layout { measurable, constraints -> val placeable = measurable.measure(constraints) layout(placeable.width, placeable.height) { placeable.place(0, 0, zIndex = specOnEnter.targetContentZIndex) } } .then(childData.apply { isTarget = stateForContent == targetState }), shouldDisposeBlock = { currentState, targetState -> Compose 1.6

Slide 63

Slide 63 text

!exit.data.hold } ) { // TODO: Should Transition.AnimatedVisibility have an end listener? DisposableEffect(this) { onDispose { currentlyVisible.remove(stateForContent) rootScope.targetSizeMap.remove(stateForContent) } } rootScope.targetSizeMap[stateForContent] = (this as AnimatedVisibilityScopeImpl).targetSize with(remember { AnimatedContentScopeImpl(this) }) { content(stateForContent) } } } } } val contentTransform = remember(rootScope, segment) { transitionSpec(rootScope) } val sizeModifier = rootScope.createSizeAnimationModifier(contentTransform) Layout( modifier = modifier.then(sizeModifier), Compose 1.6

Slide 64

Slide 64 text

with(remember { AnimatedContentScopeImpl(this) }) { content(stateForContent) } } } } } val contentTransform = remember(rootScope, segment) { transitionSpec(rootScope) } val sizeModifier = rootScope.createSizeAnimationModifier(contentTransform) Layout( modifier = modifier.then(sizeModifier), content = { currentlyVisible.fastForEach { key(contentKey(it)) { contentMap[it]?.invoke() } } }, measurePolicy = remember { AnimatedContentMeasurePolicy(rootScope) } ) } Compose 1.6

Slide 65

Slide 65 text

with(remember { AnimatedContentScopeImpl(this) }) { content(stateForContent) } } } } } val contentTransform = remember(rootScope, segment) { transitionSpec(rootScope) } val sizeModifier = rootScope.createSizeAnimationModifier(contentTransform) Layout( modifier = modifier.then(sizeModifier), content = { currentlyVisible.fastForEach { key(contentKey(it)) { contentMap[it]?.invoke() } } }, measurePolicy = remember { AnimatedContentMeasurePolicy(rootScope) } ) } Compose 1.6 Layout(modifier) { AnimatedEnterExitImpl(exitTransition) { LibraryContents(previousState) } AnimatedEnterExitImpl(enterTransition) { LibraryContents(currentState) } }

Slide 66

Slide 66 text

@Composable fun LibraryScreen(...) { val (state, onStateChanged) = remember { mutableStateOf(LibraryState(SortType.A_TO_Z, ListType.Grid)) } Scaffold(...) { val slideDistance = rememberSlideDistance() AnimatedContent( targetState = state, transitionSpec = { if (initialState.listType != targetState.listType) { materialFadeThrough() } else { materialSharedAxisY(forward = true, slideDistance) } }, modifier = Modifier.padding(innerPadding), ) { currentDestination -> LibraryContents(state, ...) } } } Enter/Exit Transition

Slide 67

Slide 67 text

@Composable fun AnimatedContent( targetState: S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentTransitionScope.() -> ContentTransform = { ... }, contentAlignment: Alignment = Alignment.TopStart, label: String = "AnimatedContent", contentKey: (targetState: S) -> Any? = { it }, content: @Composable AnimatedContentScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState, label = label) transition.AnimatedContent( modifier, transitionSpec, contentAlignment, contentKey, content = content ) } Compose 1.6

Slide 68

Slide 68 text

class ContentTransform( val targetContentEnter: EnterTransition, val initialContentExit: ExitTransition, targetContentZIndex: Float = 0f, sizeTransform: SizeTransform? = SizeTransform() ) { var targetContentZIndex by mutableFloatStateOf(targetContentZIndex) var sizeTransform: SizeTransform? = sizeTransform internal set } Compose 1.6

Slide 69

Slide 69 text

class ContentTransform( val targetContentEnter: EnterTransition, val initialContentExit: ExitTransition, targetContentZIndex: Float = 0f, sizeTransform: SizeTransform? = SizeTransform() ) { var targetContentZIndex by mutableFloatStateOf(targetContentZIndex) var sizeTransform: SizeTransform? = sizeTransform internal set } Compose 1.6

Slide 70

Slide 70 text

@Immutable sealed class EnterTransition { internal abstract val data: TransitionData } @Immutable sealed class ExitTransition { internal abstract val data: TransitionData } @Immutable internal data class TransitionData( val fade: Fade? = null, val slide: Slide? = null, val changeSize: ChangeSize? = null, val scale: Scale? = null, val hold: Boolean = false, val effectsMap: Map, TransitionEffect> = emptyMap() ) Compose 1.6

Slide 71

Slide 71 text

@Immutable sealed class EnterTransition { internal abstract val data: TransitionData } @Immutable sealed class ExitTransition { internal abstract val data: TransitionData } @Immutable internal data class TransitionData( val fade: Fade? = null, val slide: Slide? = null, val changeSize: ChangeSize? = null, val scale: Scale? = null, val hold: Boolean = false, val effectsMap: Map, TransitionEffect> = emptyMap() ) Compose 1.6

Slide 72

Slide 72 text

@Composable fun LibraryScreen(...) { val (state, onStateChanged) = remember { mutableStateOf(LibraryState(SortType.A_TO_Z, ListType.Grid)) } Scaffold(...) { val slideDistance = rememberSlideDistance() AnimatedContent( targetState = state, transitionSpec = { if (initialState.listType != targetState.listType) { materialFadeThrough() } else { materialSharedAxisY(forward = true, slideDistance) } }, modifier = Modifier.padding(innerPadding), ) { currentDestination -> LibraryContents(state, ...) } } } Fade Through

Slide 73

Slide 73 text

public fun materialFadeThrough( durationMillis: Int = 300, ): ContentTransform = materialFadeThroughIn( durationMillis = durationMillis, ) togetherWith materialFadeThroughOut( durationMillis = durationMillis, ) Fade Through

Slide 74

Slide 74 text

public fun materialFadeThrough( durationMillis: Int = 300, ): ContentTransform = materialFadeThroughIn( durationMillis = durationMillis, ) togetherWith materialFadeThroughOut( durationMillis = durationMillis, ) Fade Through

Slide 75

Slide 75 text

public fun materialFadeThrough( durationMillis: Int = 300, ): ContentTransform = materialFadeThroughIn( durationMillis = durationMillis, ) togetherWith materialFadeThroughOut( durationMillis = durationMillis, ) infix fun EnterTransition.togetherWith(exit: ExitTransition) = ContentTransform(this, exit) Fade Through

Slide 76

Slide 76 text

Fade Through public fun materialFadeThroughOut( durationMillis: Int = 300, ): ExitTransition = fadeOut( animationSpec = tween( durationMillis = durationMillis.ForOutgoing, delayMillis = 0, easing = FastOutLinearInEasing, ), )

Slide 77

Slide 77 text

Fade Through public fun materialFadeThroughIn( initialScale: Float = 0.92f, durationMillis: Int = 300, ): EnterTransition = fadeIn( animationSpec = tween( delayMillis = durationMillis.ForOutgoing, durationMillis = durationMillis.ForIncoming, easing = LinearOutSlowInEasing, ), ) + scaleIn( animationSpec = tween( delayMillis = durationMillis.ForOutgoing, durationMillis = durationMillis.ForIncoming, easing = LinearOutSlowInEasing, ), initialScale = initialScale, )

Slide 78

Slide 78 text

Fade Through public fun materialFadeThroughIn( initialScale: Float = 0.92f, durationMillis: Int = 300, ): EnterTransition = fadeIn( animationSpec = tween( delayMillis = durationMillis.ForOutgoing, durationMillis = durationMillis.ForIncoming, easing = LinearOutSlowInEasing, ), ) + scaleIn( animationSpec = tween( delayMillis = durationMillis.ForOutgoing, durationMillis = durationMillis.ForIncoming, easing = LinearOutSlowInEasing, ), initialScale = initialScale, )

Slide 79

Slide 79 text

@Composable fun LibraryScreen(...) { val (state, onStateChanged) = remember { mutableStateOf(LibraryState(SortType.A_TO_Z, ListType.Grid)) } Scaffold(...) { val slideDistance = rememberSlideDistance() AnimatedContent( targetState = state, transitionSpec = { if (initialState.listType != targetState.listType) { materialFadeThrough() } else { materialSharedAxisY(forward = true, slideDistance) } }, modifier = Modifier.padding(innerPadding), ) { currentDestination -> LibraryContents(state, ...) } } } Shared Axis

Slide 80

Slide 80 text

public fun materialSharedAxisY ( forward: Boolean, slideDistance: Int, durationMillis: Int = 300, ): ContentTransform = materialSharedAxisYIn( forward = forward, slideDistance = slideDistance, durationMillis = durationMillis, ) togetherWith materialSharedAxisYOut( forward = forward, slideDistance = slideDistance, durationMillis = durationMillis, ) Shared Axis

Slide 81

Slide 81 text

public fun materialSharedAxisY ( forward: Boolean, slideDistance: Int, durationMillis: Int = 300, ): ContentTransform = materialSharedAxisYIn( forward = forward, slideDistance = slideDistance, durationMillis = durationMillis, ) togetherWith materialSharedAxisYOut( forward = forward, slideDistance = slideDistance, durationMillis = durationMillis, ) Shared Axis

Slide 82

Slide 82 text

public fun materialSharedAxisYOut( forward: Boolean, slideDistance: Int, durationMillis: Int = 300, ): ExitTransition = slideOutVertically( animationSpec = tween( durationMillis = durationMillis, easing = FastOutSlowInEasing, ), targetOffsetY = { if (forward) -slideDistance else slideDistance }, ) + fadeOut( animationSpec = tween( durationMillis = durationMillis.ForOutgoing, delayMillis = 0, easing = FastOutLinearInEasing, ), ) Shared Axis

Slide 83

Slide 83 text

public fun materialSharedAxisYIn( forward: Boolean, slideDistance: Int, durationMillis: Int = 300, ): EnterTransition = slideInVertically( animationSpec = tween( durationMillis = durationMillis, easing = FastOutSlowInEasing, ), initialOffsetY = { if (forward) slideDistance else -slideDistance }, ) + fadeIn( animationSpec = tween( durationMillis = durationMillis.ForIncoming, delayMillis = durationMillis.ForOutgoing, easing = LinearOutSlowInEasing, ), ) Shared Axis

Slide 84

Slide 84 text

public fun materialSharedAxisYIn( forward: Boolean, slideDistance: Int, durationMillis: Int = 300, ): EnterTransition = slideInVertically( animationSpec = tween( durationMillis = durationMillis, easing = FastOutSlowInEasing, ), initialOffsetY = { if (forward) slideDistance else -slideDistance }, ) + fadeIn( animationSpec = tween( durationMillis = durationMillis.ForIncoming, delayMillis = durationMillis.ForOutgoing, easing = LinearOutSlowInEasing, ), ) Shared Axis

Slide 85

Slide 85 text

@Composable fun LibraryScreen(...) { val (state, onStateChanged) = remember { mutableStateOf(LibraryState(SortType.A_TO_Z, ListType.Grid)) } Scaffold(...) { val slideDistance = rememberSlideDistance() AnimatedContent( targetState = state, transitionSpec = { if (initialState.listType != targetState.listType) { materialFadeThrough() } else { materialSharedAxisY(forward = true, slideDistance) } }, modifier = Modifier.padding(innerPadding), ) { currentDestination -> LibraryContents(state, ...) } } } Shared Axis

Slide 86

Slide 86 text

Enter/Exit Transition AnimatedContent Example #2 ✅

Slide 87

Slide 87 text

! List-Detail ಁఢ Example #3

Slide 88

Slide 88 text

Example #3

Slide 89

Slide 89 text

@Composable fun DemoScreen() { var albumId by remember { ... } if (albumId != null) { AlbumScreen(albumId) } else { LibraryScreen(onItemClick = { albumId = it.id }) } } Example #3

Slide 90

Slide 90 text

@Composable fun DemoScreen() { var albumId by remember { ... } AnimatedContent( targetState = albumId, transitionSpec = { ... }, ) { currentId -> if (currentId != null) { AlbumScreen(currentId) } else { LibraryScreen(onItemClick = { albumId = it.id }) } } } AnimatedContent

Slide 91

Slide 91 text

@Composable fun DemoScreen() { var albumId by remember { ... } AnimatedContent( targetState = albumId, transitionSpec = { if (targetState != null) { translateYIn { it } togetherWith holdOut() } else { holdIn() togetherWith translateYOut { it } } }, ) { currentId -> ... } } AnimatedContent

Slide 92

Slide 92 text

! غجইয়ח গפݫ੉࣌ য়ܨ... Problem

Slide 93

Slide 93 text

... AnimatedContent( targetState = albumId, transitionSpec = { if (targetState != null) { ContentTransform( translateYIn { it }, holdOut(), targetContentZIndex = 1f ) } else { ContentTransform( holdIn(), translateYOut { it }, targetContentZIndex = 0f ) } }, ) { currentId -> ... } ... Z-Index

Slide 94

Slide 94 text

Example #3 # ೧஖ਛա...?

Slide 95

Slide 95 text

غجইয়ݶ ࢚కо ୡӝചػ׮... $ Problem

Slide 96

Slide 96 text

@Composable fun DemoScreen() { var albumId by remember { ... } AnimatedContent( targetState = albumId, transitionSpec = { ... }, ) { currentId -> if (currentId != null) { AlbumScreen(currentId) } else { LibraryScreen(onItemClick = { albumId = it.id }) } } } Issue Point #1

Slide 97

Slide 97 text

@Composable fun LibraryScreen(...) { val (state, ...) = remember { mutableStateOf(LibraryState(SortType.A_TO_Z, ListType.Grid)) } Scaffold(...) { AnimatedContent(...) { LibraryContents(...) } } } Issue Point #1

Slide 98

Slide 98 text

@Composable fun LibraryScreen(...) { val (state, ...) = rememberSaveable(stateSaver = Saver) { mutableStateOf(LibraryState(SortType.A_TO_Z, ListType.Grid)) } Scaffold(...) { AnimatedContent(...) { LibraryContents(...) } } } Saveable val Saver = run { val sortTypeKey = "SortType" val listTypeKey = "ListType" mapSaver( save = { mapOf( sortTypeKey to it.sortType, listTypeKey to it.listType, )}, restore = { LibraryState( it[sortTypeKey] as SortType, it[listTypeKey] as ListType, )} )

Slide 99

Slide 99 text

@Composable fun DemoScreen() { var albumId by remember { ... } AnimatedContent(...) { currentId -> if (currentId != null) { AlbumScreen(currentId) } else { LibraryScreen(onItemClick = { albumId = it.id }) } } } Issue Point #2

Slide 100

Slide 100 text

@Composable fun DemoScreen() { val saveableStateHolder = rememberSaveableStateHolder() var albumId by remember { ... } AnimatedContent(...) { currentId -> saveableStateHolder.SaveableStateProvider(currentId) { if (currentId != null) { AlbumScreen(currentId) } else { LibraryScreen(onItemClick = { albumId = it.id }) } } } } Saveable

Slide 101

Slide 101 text

@Composable fun DemoScreen() { val saveableStateHolder = rememberSaveableStateHolder() var albumId by remember { ... } AnimatedContent(...) { currentId -> saveableStateHolder.SaveableStateProvider(currentId) { if (currentId != null) { AlbumScreen(currentId) } else { LibraryScreen(onItemClick = { albumId = it.id }) } } } } Saveable

Slide 102

Slide 102 text

private class SaveableStateHolderImpl( private val savedStates: MutableMap>> = mutableMapOf() ) : SaveableStateHolder { private val registryHolders = mutableMapOf() var parentSaveableStateRegistry: SaveableStateRegistry? = null @Composable override fun SaveableStateProvider(key: Any, content: @Composable () -> Unit) { ReusableContent(key) { val registryHolder = remember { ... RegistryHolder(key) } CompositionLocalProvider( LocalSaveableStateRegistry provides registryHolder.registry, content = content ) ... } } ... Compose 1.6

Slide 103

Slide 103 text

૒੽ Saveableਸ ׮ܖח Ѫ੉ ബਯ੸ੋ ߑߨੌө...? Example #3 !

Slide 104

Slide 104 text

Link: h"ps://developer.android.com/develop/ui/compose/animation/choose-api Choose an animation API

Slide 105

Slide 105 text

@Composable fun DemoScreen() { val saveableStateHolder = rememberSaveableStateHolder() var albumId by remember { ... } AnimatedContent(...) { currentId -> saveableStateHolder.SaveableStateProvider(currentId) { if (currentId != null) { AlbumScreen(currentId) } else { LibraryScreen(onItemClick = { albumId = it.id }) } } } } AnimatedContent

Slide 106

Slide 106 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", enterTransition = { holdIn() }, exitTransition = { holdOut() }, ) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", arguments = listOf(navArgument("albumId") { ... }), enterTransition = { translateYIn { it } }, exitTransition = { translateYOut { it } }, ) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) } } } NavHost

Slide 107

Slide 107 text

@Composable public fun NavHost(...) { ... val saveableStateHolder = rememberSaveableStateHolder() ... if (backStackEntry != null) { ... val transition = updateTransition(backStackEntry, label = "entry") transition.AnimatedContent( modifier, transitionSpec = { ... }, contentAlignment, contentKey = { it.id } ) { val currentEntry = visibleEntries.lastOrNull { entry -> it == entry } currentEntry?.LocalOwnersProvider(saveableStateHolder) { (currentEntry.destination as ComposeNavigator.Destination).content(this, currentEntry) } } Navigation 2.7

Slide 108

Slide 108 text

@Composable public fun NavHost(...) { ... val saveableStateHolder = rememberSaveableStateHolder() ... if (backStackEntry != null) { ... val transition = updateTransition(backStackEntry, label = "entry") transition.AnimatedContent( modifier, transitionSpec = { ... }, contentAlignment, contentKey = { it.id } ) { val currentEntry = visibleEntries.lastOrNull { entry -> it == entry } currentEntry?.LocalOwnersProvider(saveableStateHolder) { (currentEntry.destination as ComposeNavigator.Destination).content(this, currentEntry) } } Navigation 2.7

Slide 109

Slide 109 text

@Composable public fun NavHost(...) { ... val saveableStateHolder = rememberSaveableStateHolder() ... if (backStackEntry != null) { ... val transition = updateTransition(backStackEntry, label = "entry") transition.AnimatedContent( modifier, transitionSpec = { ... }, contentAlignment, contentKey = { it.id } ) { val currentEntry = visibleEntries.lastOrNull { entry -> it == entry } currentEntry?.LocalOwnersProvider(saveableStateHolder) { (currentEntry.destination as ComposeNavigator.Destination).content(this, currentEntry) } } Navigation 2.7

Slide 110

Slide 110 text

@Composable public fun NavBackStackEntry.LocalOwnersProvider( saveableStateHolder: SaveableStateHolder, content: @Composable () -> Unit ) { CompositionLocalProvider(...) { saveableStateHolder.SaveableStateProvider(content) } } @Composable private fun SaveableStateHolder.SaveableStateProvider(content: @Composable () -> Unit) { val viewModel = viewModel() viewModel.saveableStateHolderRef = WeakReference(this) SaveableStateProvider(viewModel.id, content) } Navigation 2.7

Slide 111

Slide 111 text

@Composable public fun NavBackStackEntry.LocalOwnersProvider( saveableStateHolder: SaveableStateHolder, content: @Composable () -> Unit ) { CompositionLocalProvider(...) { saveableStateHolder.SaveableStateProvider(content) } } @Composable private fun SaveableStateHolder.SaveableStateProvider(content: @Composable () -> Unit) { val viewModel = viewModel() viewModel.saveableStateHolderRef = WeakReference(this) SaveableStateProvider(viewModel.id, content) } Navigation 2.7

Slide 112

Slide 112 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", enterTransition = { holdIn() }, exitTransition = { holdOut() }, ) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", arguments = listOf(navArgument("albumId") { ... }), enterTransition = { translateYIn { it } }, exitTransition = { translateYOut { it } }, ) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) } } } NavHost

Slide 113

Slide 113 text

NavHost Example #3 Enter/Exit Transition ✅

Slide 114

Slide 114 text

Predictive Back Gesture...? Example #4 !

Slide 115

Slide 115 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", ...) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) } } } NavHost

Slide 116

Slide 116 text

// build.gradle.kts dependencies { implementation("androidx.navigation:navigation-compose:2.8.0-beta05") } // AndroidManifest.xml ... Navigation 2.8

Slide 117

Slide 117 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", ...) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) } } } NavHost

Slide 118

Slide 118 text

Navigation 2.8 @Composable public fun NavHost(...) { ... val currentBackStack by composeNavigator.backStack.collectAsState() var progress by remember { mutableFloatStateOf(0f) } var inPredictiveBack by remember { mutableStateOf(false) } PredictiveBackHandler(currentBackStack.size > 1) { backEvent -> progress = 0f val currentBackStackEntry = currentBackStack.lastOrNull() composeNavigator.prepareForTransition(currentBackStackEntry!!) val previousEntry = currentBackStack[currentBackStack.size - 2] composeNavigator.prepareForTransition(previousEntry) try { backEvent.collect { inPredictiveBack = true progress = it.progress } inPredictiveBack = false

Slide 119

Slide 119 text

Navigation 2.8 @Composable public fun NavHost(...) { ... val currentBackStack by composeNavigator.backStack.collectAsState() var progress by remember { mutableFloatStateOf(0f) } var inPredictiveBack by remember { mutableStateOf(false) } PredictiveBackHandler(currentBackStack.size > 1) { backEvent -> progress = 0f val currentBackStackEntry = currentBackStack.lastOrNull() composeNavigator.prepareForTransition(currentBackStackEntry!!) val previousEntry = currentBackStack[currentBackStack.size - 2] composeNavigator.prepareForTransition(previousEntry) try { backEvent.collect { inPredictiveBack = true progress = it.progress } inPredictiveBack = false

Slide 120

Slide 120 text

val currentBackStack by composeNavigator.backStack.collectAsState() var progress by remember { mutableFloatStateOf(0f) } var inPredictiveBack by remember { mutableStateOf(false) } PredictiveBackHandler(currentBackStack.size > 1) { backEvent -> progress = 0f val currentBackStackEntry = currentBackStack.lastOrNull() composeNavigator.prepareForTransition(currentBackStackEntry!!) val previousEntry = currentBackStack[currentBackStack.size - 2] composeNavigator.prepareForTransition(previousEntry) try { backEvent.collect { inPredictiveBack = true progress = it.progress } inPredictiveBack = false composeNavigator.popBackStack(currentBackStackEntry, false) } catch (e: CancellationException) { inPredictiveBack = false } } ... if (backStackEntry != null) { Navigation 2.8

Slide 121

Slide 121 text

val currentBackStack by composeNavigator.backStack.collectAsState() var progress by remember { mutableFloatStateOf(0f) } var inPredictiveBack by remember { mutableStateOf(false) } PredictiveBackHandler(currentBackStack.size > 1) { backEvent -> progress = 0f val currentBackStackEntry = currentBackStack.lastOrNull() composeNavigator.prepareForTransition(currentBackStackEntry!!) val previousEntry = currentBackStack[currentBackStack.size - 2] composeNavigator.prepareForTransition(previousEntry) try { backEvent.collect { inPredictiveBack = true progress = it.progress } inPredictiveBack = false composeNavigator.popBackStack(currentBackStackEntry, false) } catch (e: CancellationException) { inPredictiveBack = false } } ... if (backStackEntry != null) { Navigation 2.8

Slide 122

Slide 122 text

} catch (e: CancellationException) { inPredictiveBack = false } } ... if (backStackEntry != null) { ... val transitionState = remember { SeekableTransitionState(backStackEntry) } val transition = rememberTransition(transitionState, label = "entry") if (inPredictiveBack) { LaunchedEffect(progress) { val previousEntry = currentBackStack[currentBackStack.size - 2] transitionState.seekTo(progress, previousEntry) } } else { LaunchedEffect(backStackEntry) { if (transitionState.currentState != backStackEntry) { transitionState.animateTo(backStackEntry) } else { ... Navigation 2.8

Slide 123

Slide 123 text

} catch (e: CancellationException) { inPredictiveBack = false } } ... if (backStackEntry != null) { ... val transitionState = remember { SeekableTransitionState(backStackEntry) } val transition = rememberTransition(transitionState, label = "entry") if (inPredictiveBack) { LaunchedEffect(progress) { val previousEntry = currentBackStack[currentBackStack.size - 2] transitionState.seekTo(progress, previousEntry) } } else { LaunchedEffect(backStackEntry) { if (transitionState.currentState != backStackEntry) { transitionState.animateTo(backStackEntry) } else { ... Navigation 2.8

Slide 124

Slide 124 text

if (backStackEntry != null) { ... val transitionState = remember { SeekableTransitionState(backStackEntry) } val transition = rememberTransition(transitionState, label = "entry") if (inPredictiveBack) { LaunchedEffect(progress) { val previousEntry = currentBackStack[currentBackStack.size - 2] transitionState.seekTo(progress, previousEntry) } } else { LaunchedEffect(backStackEntry) { if (transitionState.currentState != backStackEntry) { transitionState.animateTo(backStackEntry) } else { ... } } } transition.AnimatedContent(...) { ... } Navigation 2.8

Slide 125

Slide 125 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", ...) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) BackHandler { if (collapse && listState.isScrollInProgress.not()) { // scroll to top item } else { upPress() } } } } } Custom BackHandler

Slide 126

Slide 126 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", ...) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) BackHandler { if (collapse && listState.isScrollInProgress.not()) { // scroll to top item } else { upPress() } } } } } Custom BackHandler

Slide 127

Slide 127 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", ...) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) BackHandler( enabled = collapse && listState.isScrollInProgress.not() ) { // scroll to top item } } } } BackHandler Enabled

Slide 128

Slide 128 text

Example #4 Pre-release NavHost Enter/Exit Transition ✅

Slide 129

Slide 129 text

material-motion-compose % Material Motion

Slide 130

Slide 130 text

Material Motion Shared Axis (X, Y, Z) Fade Through Elevation Scale Hold Fade

Slide 131

Slide 131 text

val enterTransition: EnterTransition = ... val exitTransition: ExitTransition = ... AnimatedContent( targetState = state, transitionSpec = { enterTransition to exitTransition }, ) { currentState -> // composable according to screen } materialSharedAxis(Axis.X, forward = true) materialFadeThrough() materialFade() materialElevationScale(growing = false) hold() ... Axis.Y Axis.Z Material Motion Link: h"ps://github.com/fornewid/material-motion-compose

Slide 132

Slide 132 text

Container Transform ! Example #5

Slide 133

Slide 133 text

Example #5 "key" "key"

Slide 134

Slide 134 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", ...) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) } } } NavHost

Slide 135

Slide 135 text

// build.gradle.kts dependencies { implementation("androidx.compose.animation:animation:1.7.0-beta05") } Compose 1.7

Slide 136

Slide 136 text

@Composable fun DemoScreen() { val navController = rememberNavController() SharedTransitionLayout { NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen(...) } composable("album/{albumId}", ...) { AlbumScreen(...) } } } } Shared Elements

Slide 137

Slide 137 text

@Composable fun LibraryGridItem(...) { Card( modifier = Modifier.sharedBounds( rememberSharedContentState(key = "album-${album.id}"), animatedVisibilityScope = animatedVisibilityScope, ), ) { ... } } Shared Elements

Slide 138

Slide 138 text

@Composable fun LibraryGridItem(...) { Card( modifier = Modifier.sharedBounds( rememberSharedContentState(key = "album-${album.id}"), animatedVisibilityScope = animatedVisibilityScope, ), ) { ... } } Shared Elements

Slide 139

Slide 139 text

@Composable fun LibraryGridItem(...) { Card( modifier = Modifier.sharedBounds( rememberSharedContentState(key = "album-${album.id}"), animatedVisibilityScope = animatedVisibilityScope, ), ) { ... } } Shared Elements

Slide 140

Slide 140 text

@Composable fun LibraryGridItem(...) { Card( modifier = Modifier.sharedBounds( rememberSharedContentState(key = "album-${album.id}"), animatedVisibilityScope = animatedVisibilityScope, ), ) { ... } } @Composable fun AlbumScreen(...) { Box( modifier = Modifier.sharedBounds( rememberSharedContentState(key = "album-${album.id}"), animatedVisibilityScope = animatedVisibilityScope, ), ) { ... } } Shared Elements

Slide 141

Slide 141 text

@Composable fun SharedTransitionLayout( modifier: Modifier = Modifier, content: @Composable SharedTransitionScope.() -> Unit ) { SharedTransitionScope { sharedTransitionModifier -> // Put shared transition modifier *after* user provided modifier to support user provided // modifiers to influence the overlay's size, position, clipping, etc. Box(modifier.then(sharedTransitionModifier)) { content() } } } Compose 1.7

Slide 142

Slide 142 text

@Composable fun AnimatedContent( ... content: @Composable() AnimatedContentScope.(targetState: S) -> Unit ) { ... } sealed interface AnimatedContentScope : AnimatedVisibilityScope Compose 1.7

Slide 143

Slide 143 text

@Composable fun AnimatedContent( ... content: @Composable() AnimatedContentScope.(targetState: S) -> Unit ) { ... } sealed interface AnimatedContentScope : AnimatedVisibilityScope Compose 1.7

Slide 144

Slide 144 text

@Stable interface SharedTransitionScope : LookaheadScope { fun Modifier.sharedBounds( sharedContentState: SharedContentState, animatedVisibilityScope: AnimatedVisibilityScope, ... ): Modifier ... Compose 1.7

Slide 145

Slide 145 text

@Stable interface SharedTransitionScope : LookaheadScope { fun Modifier.sharedBounds( sharedContentState: SharedContentState, animatedVisibilityScope: AnimatedVisibilityScope, ... ): Modifier ... Compose 1.7

Slide 146

Slide 146 text

SharedTransitionLayout { AnimatedContent { Box( modifier = Modifier.sharedBounds( rememberSharedContentState("album-${album.id}"), animatedVisibilityScope = this@AnimatedContent, ), ) } } Shared Elements

Slide 147

Slide 147 text

SharedTransitionLayout { AnimatedContent { Box( modifier = Modifier.sharedBounds( rememberSharedContentState("album-${album.id}"), animatedVisibilityScope = this@AnimatedContent, ), ) } } Shared Elements

Slide 148

Slide 148 text

SharedTransitionLayout { AnimatedContent { AnimatedContent { Box( modifier = Modifier.sharedBounds( rememberSharedContentState("album-${album.id}"), animatedVisibilityScope = this@AnimatedContent, ), ) } } } Shared Elements ⚠

Slide 149

Slide 149 text

SharedTransitionLayout { AnimatedContent { val parentScope: AnimatedVisibilityScope = this AnimatedContent { Box( modifier = Modifier.sharedBounds( rememberSharedContentState("album-${album.id}"), animatedVisibilityScope = parentScope, ), ) } } } Shared Elements

Slide 150

Slide 150 text

AnimatedContent Shared Elements ' Example #5

Slide 151

Slide 151 text

Recommended Link: h"ps://www.youtube.com/watch?v=PR6rz1QUkAM

Slide 152

Slide 152 text

Compose Multiplatform

Slide 153

Slide 153 text

Desktop Android iOS Server

Slide 154

Slide 154 text

Desktop Android iOS Server Kotlin/JVM Kotlin/JVM Kotlin/Native Kotlin/JVM

Slide 155

Slide 155 text

Desktop Android iOS Server Domain Kotlin Multiplatform

Slide 156

Slide 156 text

Desktop Android iOS Server Data Domain Kotlin Multiplatform

Slide 157

Slide 157 text

Server Data Domain UI Kotlin Multiplatform

Slide 158

Slide 158 text

Server Data Domain UI Compose Multiplatform

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

Supported platforms Link: jb.gg/compose Compose Multiplatform

Slide 161

Slide 161 text

forked from androidx/androidx Compose Multiplatform Core Link: h"ps://github.com/JetBrains/compose-multipla$orm-core commonMain androidMain jbMain ... nativeMain desktopMain … webMain or skikoMain Jetpack Compose Compose Multiplatform Compose Multiplatform

Slide 162

Slide 162 text

// Jetpack Compose dependencies { implementation("androidx. compose.runtime :runtime:1.6.7") implementation("androidx. compose.ui :ui:1.6.7") implementation("androidx. compose.foundation :foundation:1.6.7") implementation("androidx. compose.animation :animation:1.6.7") implementation("androidx. compose.material :material:1.6.7") implementation("androidx. compose.material3 :material3:1.2.1") } Link: h"ps://developer.android.com/jetpack/androidx/releases/compose#versions

Slide 163

Slide 163 text

// Compose Multiplatform commonMain.dependencies { implementation("org.jetbrains. compose.runtime :runtime:1.6.11") implementation("org.jetbrains. compose.ui :ui:1.6.11") implementation("org.jetbrains. compose.foundation :foundation:1.6.11") implementation("org.jetbrains. compose.animation :animation:1.6.11") implementation("org.jetbrains. compose.material :material:1.6.11") implementation("org.jetbrains. compose.material3 :material3:1.6.11") } Link: h"ps://www.jetbrains.com/help/kotlin-multipla$orm-dev/compose-compatibility-and-versioning.html

Slide 164

Slide 164 text

Material Motion Link: h"ps://speakerdeck.com/fornewid/kotlin-multipla$orm-library-baepohagi

Slide 165

Slide 165 text

// core/src/commonMain/kotlin/soup/compose/material/motion/CircularReveal.kt import androidx.annotation.FloatRange private class CircularRevealShape( @FloatRange(from = 0.0, to = 1.0) // Compose Web X private val progress: Float, private val center: (fullSize: Size) -> Offset = { Offset.Unspecified }, ) : Shape { ... #1. Annotation Link: h"ps://github.com/fornewid/material-motion-compose/pull/190/%les#di&

Slide 166

Slide 166 text

// core/src/commonMain/kotlin/soup/compose/material/motion/CircularReveal.kt import androidx.annotation.FloatRange private class CircularRevealShape( @FloatRange(from = 0.0, to = 1.0) private val progress: Float, private val center: (fullSize: Size) -> Offset = { Offset.Unspecified }, ) : Shape { init { require(progress !in 0.0..1.0) } ... Link: h"ps://github.com/fornewid/material-motion-compose/pull/190/%les#di& A. Preconditions

Slide 167

Slide 167 text

JetBrains Internal ۄ੉࠳۞ܻ androidx.annotation:annotation ✅ ✅ ✅ ❌ androidx.collection:collection ✅ ✅ ✅ ❌ org.jetbrains.compose.annotation-internal:annotation ✅ ✅ ✅ ✅ org.jetbrains.compose.collection-internal:collection ✅ ✅ ✅ ✅ Android iOS Desktop Web module B. Jetbrains Internal

Slide 168

Slide 168 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", ...) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) BackHandler( enabled = collapse && listState.isScrollInProgress.not() ) { // scroll to top item } } } } #2. BackHandler

Slide 169

Slide 169 text

Activity Compose @Composable public fun BackHandler(enabled: Boolean = true, onBack: () -> Unit) { // Safely update the current `onBack` lambda when a new one is provided val currentOnBack by rememberUpdatedState(onBack) // Remember in Composition a back callback that calls the `onBack` lambda val backCallback = remember { object : OnBackPressedCallback(enabled) { override fun handleOnBackPressed() { currentOnBack() } } } // On every successful composition, update the callback with the `enabled` value SideEffect { backCallback.isEnabled = enabled } val backDispatcher = checkNotNull(LocalOnBackPressedDispatcherOwner.current) { "No OnBackPressedDispatcherOwner was provided via LocalOnBackPressedDispatcherOwner” }.onBackPressedDispatcher

Slide 170

Slide 170 text

Activity Compose @Composable public fun BackHandler(enabled: Boolean = true, onBack: () -> Unit) { // Safely update the current `onBack` lambda when a new one is provided val currentOnBack by rememberUpdatedState(onBack) // Remember in Composition a back callback that calls the `onBack` lambda val backCallback = remember { object : OnBackPressedCallback(enabled) { // Android only override fun handleOnBackPressed() { currentOnBack() } } } // On every successful composition, update the callback with the `enabled` value SideEffect { backCallback.isEnabled = enabled } val backDispatcher = checkNotNull(LocalOnBackPressedDispatcherOwner.current) { "No OnBackPressedDispatcherOwner was provided via LocalOnBackPressedDispatcherOwner” }.onBackPressedDispatcher

Slide 171

Slide 171 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", ...) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) // BackHandler( // enabled = collapse && listState.isScrollInProgress.not() // ) { // // scroll to top item // } } } } A. Comment Out

Slide 172

Slide 172 text

B. expect - actual commonMain/ androidMain/ iosMain/ desktopMain/ expect actual actual actual Link: h"ps://kotlinlang.org/docs/multipla$orm-expect-actual.html

Slide 173

Slide 173 text

Sample // src/commonMain/kotlin/BackHandler.kt @Composable expect fun BackHandler(enabled: Boolean = true, onBack: () -> Unit) // src/androidMain/kotlin/BackHandler.kt @Composable actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) { androidx.activity.compose.BackHandler(enabled = enabled, onBack = onBack) } // src/iosMain/kotlin/BackHandler.kt @Composable actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) { // do nothing! }

Slide 174

Slide 174 text

Sample // src/commonMain/kotlin/BackHandler.kt @Composable expect fun BackHandler(enabled: Boolean = true, onBack: () -> Unit) // src/androidMain/kotlin/BackHandler.kt @Composable actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) { androidx.activity.compose.BackHandler(enabled = enabled, onBack = onBack) } // src/iosMain/kotlin/BackHandler.kt @Composable actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) { // do nothing! }

Slide 175

Slide 175 text

Sample // src/commonMain/kotlin/BackHandler.kt @Composable expect fun BackHandler(enabled: Boolean = true, onBack: () -> Unit) // src/androidMain/kotlin/BackHandler.kt @Composable actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) { androidx.activity.compose.BackHandler(enabled = enabled, onBack = onBack) } // src/iosMain/kotlin/BackHandler.kt @Composable actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) { // do nothing! }

Slide 176

Slide 176 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", ...) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) BackHandler( enabled = collapse && listState.isScrollInProgress.not() ) { // scroll to top item } } } } B. expect - actual

Slide 177

Slide 177 text

@Composable fun DemoScreen() { val navController = rememberNavController() NavHost(navController, startDestination = "library") { composable("library", ...) { LibraryScreen { navController.navigate("album/${it.id}") } } composable("album/{albumId}", ...) { backStackEntry -> AlbumScreen(backStackEntry.arguments?.getLong("albumId")) } } } #3. Predictive Back

Slide 178

Slide 178 text

// build.gradle.kts dependencies { implementation("androidx.navigation:navigation-compose:2.8.0-beta05") } // AndroidManifest.xml ... Jetpack Navigation 2.8

Slide 179

Slide 179 text

@Composable public fun NavHost(...) { ... val currentBackStack by composeNavigator.backStack.collectAsState() var progress by remember { mutableFloatStateOf(0f) } var inPredictiveBack by remember { mutableStateOf(false) } PredictiveBackHandler(currentBackStack.size > 1) { backEvent -> // Android only ... Jetpack Navigation 2.8

Slide 180

Slide 180 text

// build.gradle.kts commonMain.dependencies { implementation("org.jetbrains.androidx.navigation:navigation-compose:2.8.0-alpha08") } // AndroidManifest.xml ... Jetbrains Navigation 2.8

Slide 181

Slide 181 text

@Composable public fun NavHost(...) { ... val currentBackStack by composeNavigator.backStack.collectAsState() var progress by remember { mutableFloatStateOf(0f) } var inPredictiveBack by remember { mutableStateOf(false) } /* TODO: Support PredictiveBackHandler on multiplatform PredictiveBackHandler(currentBackStack.size > 1) { backEvent -> ... */ ... Link: h"ps://github.com/JetBrains/compose-multipla$orm/issues/4419 Jetbrains Navigation 2.8 '

Slide 182

Slide 182 text

Material Motion ✅

Slide 183

Slide 183 text

Summary Multiplatform BackHandler ❌ Compose*, Annotation * expect - actual ContainerTransform AnimatedContent Shared Elements Predictive Back Pre-release NavHost Enter/Exit Transition List-Detail NavHost Enter/Exit Transition List Sort/Type Enter/Exit Transition AnimatedContent Tab-Contents Crossfade Fade Through or

Slide 184

Slide 184 text

Thank You Sungyong An @fornewid Android GDE