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

(WIP) Add sample for ContainerTransform #216

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ android {
dependencies {
implementation project(':core')

implementation libs.compose.animation
implementation libs.compose.ui.ui
implementation libs.compose.ui.tooling
implementation libs.compose.material
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import soup.compose.material.motion.sample.ui.theme.Purple200
fun AlbumScaffold(
upPress: () -> Unit,
collapse: Boolean,
modifier: Modifier = Modifier,
content: @Composable () -> Unit,
) {
val backgroundColor = if (collapse) {
Expand All @@ -75,7 +76,7 @@ fun AlbumScaffold(
} else {
Color.Transparent
}
Box(modifier = Modifier.fillMaxSize()) {
Box(modifier = modifier) {
Surface(modifier = Modifier.fillMaxSize(), content = content)
TopAppBar(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
*/
package soup.compose.material.motion.sample.ui.demo

import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
Expand All @@ -32,6 +36,8 @@ import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

context(SharedTransitionScope, AnimatedVisibilityScope)
@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
fun AlbumScreen(album: MusicData.Album, upPress: () -> Unit) {
val density = LocalDensity.current
Expand Down Expand Up @@ -62,9 +68,17 @@ fun AlbumScreen(album: MusicData.Album, upPress: () -> Unit) {
}
AlbumScaffold(
upPress = upPress,
collapse = collapse
collapse = collapse,
modifier = Modifier
.fillMaxSize()
.containerTransform(
rememberSharedContentState(key = "album-${album.id}"),
this@AnimatedVisibilityScope,
),
) {
LazyColumn(state = listState) {
LazyColumn(
state = listState,
) {
item {
AlbumHeader(album, showFab)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
@file:OptIn(ExperimentalSharedTransitionApi::class)

package soup.compose.material.motion.sample.ui.demo

import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.animation.SharedTransitionScope.SharedContentState
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.ui.Modifier
import soup.compose.material.motion.MotionConstants

context(SharedTransitionScope)
fun Modifier.containerTransform(
state: SharedContentState,
animatedVisibilityScope: AnimatedVisibilityScope,
placeHolderSize: SharedTransitionScope.PlaceHolderSize = SharedTransitionScope.PlaceHolderSize.contentSize,
renderInOverlayDuringTransition: Boolean = true,
zIndexInOverlay: Float = 0f,
): Modifier = sharedBounds(
sharedContentState = state,
animatedVisibilityScope = animatedVisibilityScope,
enter = containerTransformIn(),
exit = containerTransformOut(),
placeHolderSize = placeHolderSize,
renderInOverlayDuringTransition = renderInOverlayDuringTransition,
zIndexInOverlay = zIndexInOverlay,
)

private const val ProgressThreshold = 0.35f

private val Int.ForOutgoing: Int
get() = (this * ProgressThreshold).toInt()

private val Int.ForIncoming: Int
get() = this - this.ForOutgoing

private fun containerTransformIn(
durationMillis: Int = MotionConstants.DefaultMotionDuration,
): EnterTransition = fadeIn(
animationSpec = tween(
durationMillis = durationMillis.ForIncoming,
delayMillis = durationMillis.ForOutgoing,
easing = LinearOutSlowInEasing
)
)

private fun containerTransformOut(
durationMillis: Int = MotionConstants.DefaultMotionDuration,
): ExitTransition = fadeOut(
animationSpec = tween(
durationMillis = durationMillis.ForOutgoing,
delayMillis = 0,
easing = FastOutLinearInEasing
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,48 @@ package soup.compose.material.motion.sample.ui.demo

import android.content.res.Configuration
import androidx.activity.compose.BackHandler
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionLayout
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import soup.compose.material.motion.animation.holdIn
import soup.compose.material.motion.animation.holdOut
import soup.compose.material.motion.animation.translateYIn
import soup.compose.material.motion.animation.translateYOut
import soup.compose.material.motion.sample.ui.theme.SampleTheme

@OptIn(ExperimentalAnimationApi::class)
@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
fun DemoScreen(upPress: () -> Unit) {
val navController = rememberNavController()
NavHost(navController, startDestination = "library") {
composable(
"library",
enterTransition = { holdIn() },
exitTransition = { holdOut() },
) {
BackHandler {
upPress()
}
LibraryScreen(
onItemClick = {
navController.navigate("album/${it.id}")
}
)
}
composable(
"album/{albumId}",
arguments = listOf(navArgument("albumId") { type = NavType.LongType }),
enterTransition = { translateYIn { it } },
exitTransition = { translateYOut { it } },
) { backStackEntry ->
val currentId = backStackEntry.arguments?.getLong("albumId")
val album = MusicData.albums.first { it.id == currentId }
AlbumScreen(
album,
upPress = {
navController.popBackStack()
SharedTransitionLayout {
NavHost(navController, startDestination = "library") {
composable(
"library",
) {
BackHandler {
upPress()
}
)
LibraryScreen(
onItemClick = {
navController.navigate("album/${it.id}")
}
)
}
composable(
"album/{albumId}",
arguments = listOf(navArgument("albumId") { type = NavType.LongType }),
) { backStackEntry ->
val currentId = backStackEntry.arguments?.getLong("albumId")
val album = MusicData.albums.first { it.id == currentId }
AlbumScreen(
album,
upPress = {
navController.popBackStack()
}
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(ExperimentalSharedTransitionApi::class)

package soup.compose.material.motion.sample.ui.demo

import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -48,6 +53,7 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
fun LibraryGridContents(
items: List<MusicData.Album>,
Expand All @@ -65,6 +71,7 @@ fun LibraryGridContents(
}
}

context(SharedTransitionScope, AnimatedVisibilityScope)
@OptIn(ExperimentalMaterialApi::class)
@Composable
private fun LibraryGridItem(
Expand All @@ -76,6 +83,10 @@ private fun LibraryGridItem(
modifier = Modifier
.fillMaxWidth()
.padding(4.dp)
.containerTransform(
rememberSharedContentState(key = "album-${album.id}"),
this@AnimatedVisibilityScope,
),
) {
Column {
Image(
Expand Down Expand Up @@ -107,6 +118,7 @@ private fun LibraryGridItem(
}
}

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
fun LibraryLinearContents(
items: List<MusicData.Album>,
Expand All @@ -124,6 +136,7 @@ fun LibraryLinearContents(
}
}

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
private fun LibraryLinearItem(
album: MusicData.Album,
Expand All @@ -134,7 +147,11 @@ private fun LibraryLinearItem(
.fillMaxWidth()
.requiredHeight(88.dp)
.clickable { onItemClick(album) }
.padding(horizontal = 16.dp),
.padding(horizontal = 16.dp)
.containerTransform(
rememberSharedContentState(key = "album-${album.id}"),
this@AnimatedVisibilityScope,
),
verticalAlignment = Alignment.CenterVertically
) {
Image(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(ExperimentalSharedTransitionApi::class)

package soup.compose.material.motion.sample.ui.demo

import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
Expand Down Expand Up @@ -82,6 +87,7 @@ private data class LibraryState(
}
}

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
fun LibraryScreen(onItemClick: (MusicData.Album) -> Unit) {
val (state, onStateChanged) = rememberSaveable(stateSaver = Saver) {
Expand Down Expand Up @@ -120,20 +126,22 @@ fun LibraryScreen(onItemClick: (MusicData.Album) -> Unit) {
onListTypeChange(newType)
},
) { innerPadding ->
val slideDistance = rememberSlideDistance()
MaterialMotion(
targetState = state,
transitionSpec = {
when (targetState.motionSpecType) {
MotionSpecType.SharedAxis -> materialSharedAxisY(forward = true, slideDistance)
MotionSpecType.FadeThrough -> materialFadeThrough()
}
},
modifier = Modifier.padding(innerPadding),
pop = false
) { currentDestination ->
LibraryContents(currentDestination, onItemClick)
}
//FIXME: not working
// val slideDistance = rememberSlideDistance()
// MaterialMotion(
// targetState = state,
// transitionSpec = {
// when (targetState.motionSpecType) {
// MotionSpecType.SharedAxis -> materialSharedAxisY(forward = true, slideDistance)
// MotionSpecType.FadeThrough -> materialFadeThrough()
// }
// },
// modifier = Modifier.padding(innerPadding),
// pop = false
// ) { currentDestination ->
// LibraryContents(currentDestination, onItemClick)
// }
LibraryContents(state, onItemClick)
}
}

Expand Down Expand Up @@ -167,6 +175,7 @@ fun LibraryScaffold(
)
}

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
private fun LibraryContents(
state: LibraryState,
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ subprojects {
jvmTarget = "1.8"
// Allow use of @OptIn
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
freeCompilerArgs += "-Xcontext-receivers"
}
}

Expand Down
Loading