fix: 修复快速滑动时折叠/展开失效,改用 progress 阈值判定

- 移除 velocity 判定:change.position 是本地坐标,卡片移动会导致
  velocityTracker 计算的速度方向错误,快速滑动时被误判方向而回弹
- onDragEnd 改为 progress > 0.3f 判定折叠,onExpandDragEnd 改为
  progress < 0.7f 判定展开
- 修复 onDrag/onExpandDrag 的 race condition:将 _collapseAnimatable.value
  的读取从 launch 外部移到协程内部,避免快速滑动时多回调并发读到旧值

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
xfy 2026-05-20 14:14:18 +08:00
parent df0aa16d8e
commit d0f4980e01
2 changed files with 4 additions and 14 deletions

View File

@ -180,8 +180,8 @@ class CalendarViewModel(
* @param delta 拖拽增量已归一化到 [0,1] 区间
*/
fun onDrag(delta: Float) {
val old = _collapseAnimatable.value
coroutineScope.launch {
val old = _collapseAnimatable.value
val new = (old + delta).coerceIn(0f, 1f)
_collapseAnimatable.snapTo(new)
}
@ -197,11 +197,7 @@ class CalendarViewModel(
fun onDragEnd(velocityDpPerSec: Float = 0f) {
coroutineScope.launch {
val progress = _collapseAnimatable.value
val shouldCollapse = when {
velocityDpPerSec > FLING_VELOCITY_THRESHOLD_DP -> true
velocityDpPerSec < -FLING_VELOCITY_THRESHOLD_DP -> false
else -> progress > COLLAPSE_THRESHOLD
}
val shouldCollapse = progress > 0.3f
if (shouldCollapse) {
_collapseAnimatable.animateTo(
targetValue = 1f,
@ -223,8 +219,8 @@ class CalendarViewModel(
* @param delta 拖拽增量已归一化到 [0,1] 区间
*/
fun onExpandDrag(delta: Float) {
val old = _collapseAnimatable.value
coroutineScope.launch {
val old = _collapseAnimatable.value
val new = (old + delta).coerceIn(0f, 1f)
_collapseAnimatable.snapTo(new)
}
@ -240,11 +236,7 @@ class CalendarViewModel(
fun onExpandDragEnd(velocityDpPerSec: Float = 0f) {
coroutineScope.launch {
val progress = _collapseAnimatable.value
val shouldExpand = when {
velocityDpPerSec < -FLING_VELOCITY_THRESHOLD_DP -> true
velocityDpPerSec > FLING_VELOCITY_THRESHOLD_DP -> false
else -> progress < 1f - COLLAPSE_THRESHOLD
}
val shouldExpand = progress < 0.7f
if (shouldExpand) {
_collapseAnimatable.animateTo(
targetValue = 0f,

View File

@ -72,7 +72,6 @@ fun BottomCard(
detectVerticalDragGestures(
onDragEnd = {
val velocity = velocityTracker.calculateVelocity()
// 上滑为正(折叠方向),下拉为负(展开方向)
val velocityDpPerSec = with(density) { -velocity.y.toDp().value }
viewModel.onExpandDragEnd(velocityDpPerSec)
},
@ -89,7 +88,6 @@ fun BottomCard(
detectVerticalDragGestures(
onDragEnd = {
val velocity = velocityTracker.calculateVelocity()
// 上滑为正(折叠方向),下拉为负(展开方向)
val velocityDpPerSec = with(density) { -velocity.y.toDp().value }
viewModel.onDragEnd(velocityDpPerSec)
},