mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-22 10:10:30 +02:00
Use velocity before snap in drag down animation
This commit is contained in:
parent
9553760a72
commit
0bd711c3b9
@ -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 ->
|
||||||
|
dragCoroutineScope.launch {
|
||||||
|
dragAmount.animateDecay(velocity, dragAnimDecay)
|
||||||
|
|
||||||
// Snap to closest anchor (0, one history item, all history items)
|
// Snap to closest anchor (0, one history item, all history items)
|
||||||
draggedAmount = listOf(0, historyItemHeight, maxDragAmount)
|
val draggedAmount = listOf(0, historyItemHeight, maxDragAmount)
|
||||||
.minBy { abs(draggedAmount.roundToInt() - it) }
|
.minBy { abs(dragAmount.value.roundToInt() - it) }
|
||||||
.also {
|
.also {
|
||||||
// Show button only when fully history view is fully expanded
|
// Show button only when fully history view is fully expanded
|
||||||
showClearHistoryButton = it == maxDragAmount
|
showClearHistoryButton = it == maxDragAmount
|
||||||
}
|
}
|
||||||
.toFloat()
|
.toFloat()
|
||||||
|
dragAmount.animateTo(draggedAmount)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.padding(top = 8.dp),
|
.padding(top = 8.dp),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user