@@ -6,6 +6,7 @@ import androidx.compose.ui.graphics.GraphicsLayerScope
66import androidx.compose.ui.unit.IntSize
77import androidx.compose.ui.unit.Velocity
88import kotlin.math.absoluteValue
9+ import kotlin.math.pow
910
1011/* *
1112 * Represents the possible directions for card swiping gestures.
@@ -111,8 +112,6 @@ internal fun calculateSwipeProgress(offset: Offset, velocityThresholdPx: Float):
111112 * Calculates the scale factor for each card in the stack.
112113 *
113114 * @param index The index of the card in the stack
114- * @param itemCount Total number of cards
115- * @param stackDragProgress The current drag progress of the stack
116115 * @return The calculated scale factor
117116 **/
118117internal fun calculateScales (index : Int , itemCount : Int , stackDragProgress : Float ): Float {
@@ -165,52 +164,40 @@ internal fun calculateDragBasedOffset(
165164 spacingPx : Float ,
166165 itemCount : Int ,
167166 cardAlignment : CardAlignment
168- ): Offset = when {
169- index == itemCount - 1 && stackDragProgress < 0f -> {
170- val progress = stackDragProgress * (itemCount - index)
171- when (cardAlignment) {
172- CardAlignment .TOP -> Offset (0f , - (spacingPx * progress) * progress)
173- CardAlignment .BOTTOM -> Offset (0f , (spacingPx * progress) * progress)
174- CardAlignment .TOP_START , CardAlignment .BOTTOM_START ->
175- Offset (
176- (spacingPx * progress) * progress,
177- (spacingPx * progress) * progress * if (cardAlignment == CardAlignment .BOTTOM_START ) 1 else - 1
178- )
179-
180- CardAlignment .TOP_END , CardAlignment .BOTTOM_END ->
181- Offset (
182- - (spacingPx * progress) * progress,
183- (spacingPx * progress) * progress * if (cardAlignment == CardAlignment .BOTTOM_END ) 1 else - 1
184- )
185-
186- CardAlignment .START -> Offset ((spacingPx * progress) * progress, 0f )
187- CardAlignment .END -> Offset (- (spacingPx * progress) * progress, 0f )
188- }
167+ ): Offset {
168+ val progress = if (index == itemCount - 1 && stackDragProgress < 0f ) {
169+ (- stackDragProgress).coerceIn(0f , 1f )
170+ } else {
171+ (stackDragProgress * (1f - (index.toFloat() / itemCount))).coerceIn(- 1f , 1f )
189172 }
190173
191- index > 0 && stackDragProgress != 0f -> {
192- val progress = (stackDragProgress * (itemCount - index)).coerceIn(0f , 1f )
193- when (cardAlignment) {
194- CardAlignment .TOP -> Offset (0f , - spacingPx * progress)
195- CardAlignment .BOTTOM -> Offset (0f , spacingPx * progress)
196- CardAlignment .TOP_START , CardAlignment .BOTTOM_START ->
197- Offset (
198- spacingPx * progress,
199- spacingPx * progress * if (cardAlignment == CardAlignment .BOTTOM_START ) 1 else - 1
200- )
201-
202- CardAlignment .TOP_END , CardAlignment .BOTTOM_END ->
203- Offset (
204- - spacingPx * progress,
205- spacingPx * progress * if (cardAlignment == CardAlignment .BOTTOM_END ) 1 else - 1
206- )
207-
208- CardAlignment .START -> Offset (spacingPx * progress, 0f )
209- CardAlignment .END -> Offset (- spacingPx * progress, 0f )
210- }
174+ val easedProgress = progress * progress * (3 - 2 * progress)
175+
176+ return when (cardAlignment) {
177+ CardAlignment .TOP -> Offset (
178+ 0f ,
179+ spacingPx * easedProgress * if (index == itemCount - 1 && stackDragProgress < 0f ) 1f else - 1f
180+ )
181+ CardAlignment .BOTTOM -> Offset (0f , spacingPx * easedProgress)
182+ CardAlignment .TOP_START -> Offset (
183+ spacingPx * easedProgress,
184+ - spacingPx * easedProgress
185+ )
186+ CardAlignment .BOTTOM_START -> Offset (
187+ spacingPx * easedProgress,
188+ spacingPx * easedProgress
189+ )
190+ CardAlignment .TOP_END -> Offset (
191+ - spacingPx * easedProgress,
192+ - spacingPx * easedProgress
193+ )
194+ CardAlignment .BOTTOM_END -> Offset (
195+ - spacingPx * easedProgress,
196+ spacingPx * easedProgress
197+ )
198+ CardAlignment .START -> Offset (spacingPx * easedProgress, 0f )
199+ CardAlignment .END -> Offset (- spacingPx * easedProgress, 0f )
211200 }
212-
213- else -> Offset .Zero
214201}
215202
216203/* *
0 commit comments