diff --git a/sharedUI/src/commonMain/kotlin/com/livrets/ui/SwipeBack.kt b/sharedUI/src/commonMain/kotlin/com/livrets/ui/SwipeBack.kt new file mode 100644 index 0000000..8166a3a --- /dev/null +++ b/sharedUI/src/commonMain/kotlin/com/livrets/ui/SwipeBack.kt @@ -0,0 +1,15 @@ +package com.livrets.ui + +import androidx.compose.ui.unit.dp + +object SwipeBackGesture { + val EdgeZone = 24.dp + const val CommitRatio = 0.4f + const val CommitVelocityDpPerSec = 1000f + + fun shouldCommit(offsetPx: Float, containerWidthPx: Float, velocityDpPerSec: Float): Boolean { + if (containerWidthPx <= 0f) return false + return offsetPx / containerWidthPx > CommitRatio || + velocityDpPerSec >= CommitVelocityDpPerSec + } +} diff --git a/sharedUI/src/commonTest/kotlin/com/livrets/ui/SwipeBackGestureTest.kt b/sharedUI/src/commonTest/kotlin/com/livrets/ui/SwipeBackGestureTest.kt new file mode 100644 index 0000000..2c9a035 --- /dev/null +++ b/sharedUI/src/commonTest/kotlin/com/livrets/ui/SwipeBackGestureTest.kt @@ -0,0 +1,52 @@ +package com.livrets.ui + +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class SwipeBackGestureTest { + + @Test + fun `commit si l offset depasse 40 pourcent de la largeur`() { + assertTrue( + SwipeBackGesture.shouldCommit( + offsetPx = 401f, + containerWidthPx = 1000f, + velocityDpPerSec = 0f, + ) + ) + } + + @Test + fun `pas de commit si offset et velocite sous les seuils`() { + assertFalse( + SwipeBackGesture.shouldCommit( + offsetPx = 399f, + containerWidthPx = 1000f, + velocityDpPerSec = 999f, + ) + ) + } + + @Test + fun `commit par velocite seule meme avec offset nul`() { + assertTrue( + SwipeBackGesture.shouldCommit( + offsetPx = 0f, + containerWidthPx = 1000f, + velocityDpPerSec = 1000f, + ) + ) + } + + @Test + fun `pas de commit si la largeur n est pas connue`() { + assertFalse( + SwipeBackGesture.shouldCommit( + offsetPx = 500f, + containerWidthPx = 0f, + velocityDpPerSec = 2000f, + ) + ) + } +}