Use velocity before snap in drag down animation

This commit is contained in:
Sad Ellie 2023-02-22 19:21:27 +04:00
parent 9553760a72
commit 0bd711c3b9

View File

@ -19,7 +19,8 @@
package com.sadellie.unitto.feature.calculator package com.sadellie.unitto.feature.calculator
import androidx.compose.animation.Crossfade import androidx.compose.animation.Crossfade
import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.Animatable
import androidx.compose.animation.rememberSplineBasedDecay
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable import androidx.compose.foundation.gestures.draggable
@ -46,6 +47,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
@ -67,6 +69,7 @@ import com.sadellie.unitto.feature.calculator.components.CalculatorKeyboard
import com.sadellie.unitto.feature.calculator.components.DragDownView import com.sadellie.unitto.feature.calculator.components.DragDownView
import com.sadellie.unitto.feature.calculator.components.HistoryList import com.sadellie.unitto.feature.calculator.components.HistoryList
import com.sadellie.unitto.feature.calculator.components.InputTextField import com.sadellie.unitto.feature.calculator.components.InputTextField
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
import kotlin.math.abs import kotlin.math.abs
@ -109,9 +112,11 @@ private fun CalculatorScreen(
) { ) {
var showClearHistoryButton by rememberSaveable { mutableStateOf(false) } var showClearHistoryButton by rememberSaveable { mutableStateOf(false) }
var showClearHistoryDialog by rememberSaveable { mutableStateOf(false) } var showClearHistoryDialog by rememberSaveable { mutableStateOf(false) }
var draggedAmount by remember { mutableStateOf(0f) }
val dragAmountAnimated by animateFloatAsState(draggedAmount) val dragAmount = remember { Animatable(0f) }
// val dragAmountAnimated = draggedAmount val dragCoroutineScope = rememberCoroutineScope()
val dragAnimDecay = rememberSplineBasedDecay<Float>()
var textThingyHeight by remember { mutableStateOf(0) } var textThingyHeight by remember { mutableStateOf(0) }
var historyItemHeight by remember { mutableStateOf(0) } var historyItemHeight by remember { mutableStateOf(0) }
@ -144,7 +149,7 @@ private fun CalculatorScreen(
) { paddingValues -> ) { paddingValues ->
DragDownView( DragDownView(
modifier = Modifier.padding(paddingValues), modifier = Modifier.padding(paddingValues),
drag = dragAmountAnimated.roundToInt(), drag = dragAmount.value.roundToInt().coerceAtLeast(0),
historyItemHeight = historyItemHeight, historyItemHeight = historyItemHeight,
historyList = { historyList = {
HistoryList( HistoryList(
@ -169,17 +174,25 @@ private fun CalculatorScreen(
.draggable( .draggable(
orientation = Orientation.Vertical, orientation = Orientation.Vertical,
state = rememberDraggableState { delta -> state = rememberDraggableState { delta ->
draggedAmount = (draggedAmount + delta).coerceAtLeast(0f) dragCoroutineScope.launch {
val draggedAmount = (dragAmount.value + delta).coerceAtLeast(0f)
dragAmount.snapTo(draggedAmount)
}
}, },
onDragStopped = { _ -> onDragStopped = { velocity ->
// Snap to closest anchor (0, one history item, all history items) dragCoroutineScope.launch {
draggedAmount = listOf(0, historyItemHeight, maxDragAmount) dragAmount.animateDecay(velocity, dragAnimDecay)
.minBy { abs(draggedAmount.roundToInt() - it) }
.also { // Snap to closest anchor (0, one history item, all history items)
// Show button only when fully history view is fully expanded val draggedAmount = listOf(0, historyItemHeight, maxDragAmount)
showClearHistoryButton = it == maxDragAmount .minBy { abs(dragAmount.value.roundToInt() - it) }
} .also {
.toFloat() // Show button only when fully history view is fully expanded
showClearHistoryButton = it == maxDragAmount
}
.toFloat()
dragAmount.animateTo(draggedAmount)
}
} }
) )
.padding(top = 8.dp), .padding(top = 8.dp),