Skip to content

Commit

Permalink
Improve predictive back on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
arkivanov committed Dec 14, 2024
1 parent 4b6857e commit f109595
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ compose-material3 = "1.3.1"
compose-ui-tooling = "1.4.0"
composeWindowSize = "0.5.0"
credentials = "1.5.0-beta01"
decompose = "3.2.2"
decompose = "3.3.0-alpha02"
essenty = "2.4.0"
googleid = "1.1.1"
horologist = "0.7.7-alpha"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package dev.johnoreilly.confetti.ui

import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.FiniteAnimationSpec
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.layout
import com.arkivanov.decompose.ExperimentalDecomposeApi
import com.arkivanov.decompose.extensions.compose.experimental.stack.animation.*
import com.arkivanov.decompose.extensions.compose.experimental.stack.animation.PredictiveBackParams
import com.arkivanov.decompose.extensions.compose.experimental.stack.animation.StackAnimation
import com.arkivanov.decompose.extensions.compose.experimental.stack.animation.StackAnimator
import com.arkivanov.decompose.extensions.compose.experimental.stack.animation.stackAnimation
import com.arkivanov.decompose.extensions.compose.experimental.stack.animation.stackAnimator
import com.arkivanov.decompose.extensions.compose.stack.animation.isFront
import com.arkivanov.essenty.backhandler.BackHandler

Expand All @@ -15,18 +23,27 @@ internal actual fun <C : Any, T : Any> predictiveBackAnimation(
onBack: () -> Unit,
): StackAnimation<C, T> =
stackAnimation(
animator = iosLikeSlide(),
predictiveBackParams = {
PredictiveBackParams(
backHandler = backHandler,
onBack = onBack,
)
},
selector = { _, _, _, isPredictiveBack ->
iosLikeSlide(iosAnimationSpec(isPredictiveBack = isPredictiveBack))
},
)

private fun iosAnimationSpec(isPredictiveBack: Boolean): FiniteAnimationSpec<Float> =
if (isPredictiveBack) {
tween(easing = LinearEasing, durationMillis = 150)
} else {
tween(easing = FastOutSlowInEasing)
}

@ExperimentalDecomposeApi
private fun iosLikeSlide(): StackAnimator =
stackAnimator { factor, direction ->
private fun iosLikeSlide(animationSpec: FiniteAnimationSpec<Float>): StackAnimator =
stackAnimator(animationSpec = animationSpec) { factor, direction ->
Modifier
.then(if (direction.isFront) Modifier else Modifier.fade(factor + 1F))
.offsetXFactor(factor = if (direction.isFront) factor else factor * 0.5F)
Expand Down

0 comments on commit f109595

Please sign in to comment.