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

Refactored predictive back animatables #667

Merged
merged 1 commit into from
Mar 2, 2024
Merged
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 @@ -52,8 +52,8 @@ private class AndroidPredictiveBackAnimatable(
private val exitProgress: Float by derivedStateOf { exitProgressAnimatable.value }
private val enterProgressAnimatable = Animatable(initialValue = initialEvent.progress.enterProgress())
private val enterProgress: Float by derivedStateOf { enterProgressAnimatable.value }
private val enterFinishProgressAnimatable = Animatable(initialValue = 0F)
private val enterFinishProgress: Float by derivedStateOf { enterFinishProgressAnimatable.value }
private val finishProgressAnimatable = Animatable(initialValue = 0F)
private val finishProgress: Float by derivedStateOf { finishProgressAnimatable.value }
private var edge by mutableStateOf(initialEvent.swipeEdge)

override val exitModifier: Modifier
Expand Down Expand Up @@ -94,18 +94,15 @@ private class AndroidPredictiveBackAnimatable(
}

private fun GraphicsLayerScope.setupEnterGraphicLayer(layoutShape: (progress: Float, edge: BackEvent.SwipeEdge) -> Shape) {
val totalProgress = enterProgress.plusFinishProgress(enterFinishProgress)
val totalProgress = lerp(start = enterProgress, stop = 1F, fraction = finishProgress)
alpha = totalProgress
scaleX = lerp(start = 0.95F, stop = 0.90F, fraction = enterProgress).plusFinishProgress(enterFinishProgress)
scaleX = lerp(start = lerp(start = 0.95F, stop = 0.90F, fraction = enterProgress), stop = 1F, fraction = finishProgress)
scaleY = scaleX
translationX = lerp(start = -size.width * 0.15F, stop = 0F, fraction = totalProgress)
shape = layoutShape(enterFinishProgress, edge)
shape = layoutShape(finishProgress, edge)
clip = true
}

private fun Float.plusFinishProgress(progress: Float): Float =
(this + (1F - this) * progress).coerceIn(0F, 1F)

override suspend fun animate(event: BackEvent) {
edge = event.swipeEdge

Expand All @@ -118,7 +115,7 @@ private class AndroidPredictiveBackAnimatable(
override suspend fun finish() {
awaitAll(
{ exitProgressAnimatable.animateTo(targetValue = 1F) },
{ enterFinishProgressAnimatable.animateTo(targetValue = 1F) },
{ finishProgressAnimatable.animateTo(targetValue = 1F) },
)
}

Expand All @@ -142,7 +139,7 @@ private class AndroidPredictiveBackAnimatable(
if (this < PROGRESS_THRESHOLD) {
0F
} else {
1F - 0.6F * (1F - this) / (1F - PROGRESS_THRESHOLD)
lerp(start = 0.4F, stop = 1F, fraction = (this - PROGRESS_THRESHOLD) / (1F - PROGRESS_THRESHOLD))
}

private companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.arkivanov.decompose.extensions.compose.stack.animation.predictiveback

import androidx.compose.animation.core.Animatable
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
Expand All @@ -14,7 +13,6 @@ import androidx.compose.ui.graphics.GraphicsLayerScope
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.arkivanov.decompose.ExperimentalDecomposeApi
import com.arkivanov.essenty.backhandler.BackEvent
Expand Down Expand Up @@ -57,7 +55,7 @@ private class MaterialPredictiveBackAnimatable(
get() =
if (exitShape == null) {
Modifier.withLayoutCorners { corners ->
graphicsLayer { setupExitGraphicLayer(corners.toShape()) }
graphicsLayer { setupExitGraphicLayer(corners.toShape(progress)) }
}
} else {
Modifier.graphicsLayer {
Expand Down Expand Up @@ -104,17 +102,6 @@ private class MaterialPredictiveBackAnimatable(
clip = true
}

private fun LayoutCorners.toShape(): RoundedCornerShape =
RoundedCornerShape(
topStart = topStart.getProgressRadius(),
topEnd = topEnd.getProgressRadius(),
bottomEnd = bottomEnd.getProgressRadius(),
bottomStart = bottomStart.getProgressRadius(),
)

private fun LayoutCorner.getProgressRadius(): Dp =
if (isFixed) radius else radius * progress

override suspend fun animate(event: BackEvent) {
edge = event.swipeEdge
touchY = event.touchY
Expand Down
Loading