fix: 小幅度下拉时触发刷新

This commit is contained in:
HuanCheng65 2023-10-06 18:46:23 +08:00
parent 0c09d3690f
commit 03c34924d1
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
3 changed files with 24 additions and 34 deletions

View File

@ -1172,22 +1172,18 @@ private val <T> SwipeableState<T>.LoadPreDownPostUpNestedScrollConnection: Neste
override suspend fun onPreFling(available: Velocity): Velocity {
val toFling = Offset(available.x, available.y).toFloat()
return if (toFling > 0) {
return if (toFling < 0 && offset.value > minBound) {
performFling(velocity = toFling)
// since we go to the anchor with tween settling, consume all for the best UX
// available
Velocity.Zero
available
} else {
Velocity.Zero
}
}
override suspend fun onPostFling(
consumed: Velocity,
available: Velocity,
): Velocity {
override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
performFling(velocity = Offset(available.x, available.y).toFloat())
return Velocity.Zero
return available
}
private fun Float.toOffset(): Offset = Offset(0f, this)

View File

@ -247,22 +247,18 @@ private val <T> SwipeableState<T>.LoadPreUpPostDownNestedScrollConnection: Neste
override suspend fun onPreFling(available: Velocity): Velocity {
val toFling = Offset(available.x, available.y).toFloat()
return if (toFling > 0) {
return if (toFling > 0 && offset.value < maxBound) {
performFling(velocity = toFling)
// since we go to the anchor with tween settling, consume all for the best UX
// available
Velocity.Zero
available
} else {
Velocity.Zero
}
}
override suspend fun onPostFling(
consumed: Velocity,
available: Velocity
): Velocity {
override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
performFling(velocity = Offset(available.x, available.y).toFloat())
return Velocity.Zero
return available
}
private fun Float.toOffset(): Offset = Offset(0f, this)

View File

@ -83,12 +83,12 @@ open class SwipeableState<T>(
val overflow: State<Float> get() = overflowState
// Use `Float.NaN` as a placeholder while the state is uninitialised.
private val offsetState = mutableStateOf(0f)
private val overflowState = mutableStateOf(0f)
private val offsetState = mutableFloatStateOf(0f)
private val overflowState = mutableFloatStateOf(0f)
// the source of truth for the "real"(non ui) position
// basically position in bounds + overflow
private val absoluteOffset = mutableStateOf(0f)
private val absoluteOffset = mutableFloatStateOf(0f)
// current animation target, if animating, otherwise null
private val animationTarget = mutableStateOf<Float?>(null)
@ -110,8 +110,8 @@ open class SwipeableState<T>(
requireNotNull(initialOffset) {
"The initial value must have an associated anchor."
}
offsetState.value = initialOffset
absoluteOffset.value = initialOffset
offsetState.floatValue = initialOffset
absoluteOffset.floatValue = initialOffset
}
}
@ -166,29 +166,29 @@ open class SwipeableState<T>(
internal var thresholds: (Float, Float) -> Float by mutableStateOf({ _, _ -> 0f })
internal var velocityThreshold by mutableStateOf(0f)
internal var velocityThreshold by mutableFloatStateOf(0f)
internal var resistance: ResistanceConfig? by mutableStateOf(null)
internal val draggableState = DraggableState {
val newAbsolute = absoluteOffset.value + it
val newAbsolute = absoluteOffset.floatValue + it
val clamped = newAbsolute.coerceIn(minBound, maxBound)
val overflow = newAbsolute - clamped
val resistanceDelta = resistance?.computeResistance(overflow) ?: 0f
offsetState.value = clamped + resistanceDelta
overflowState.value = overflow
absoluteOffset.value = newAbsolute
offsetState.floatValue = clamped + resistanceDelta
overflowState.floatValue = overflow
absoluteOffset.floatValue = newAbsolute
}
private suspend fun snapInternalToOffset(target: Float) {
draggableState.drag {
dragBy(target - absoluteOffset.value)
dragBy(target - absoluteOffset.floatValue)
}
}
private suspend fun animateInternalToOffset(target: Float, spec: AnimationSpec<Float>) {
draggableState.drag {
var prevValue = absoluteOffset.value
var prevValue = absoluteOffset.floatValue
animationTarget.value = target
isAnimationRunning = true
try {
@ -306,7 +306,7 @@ open class SwipeableState<T>(
}
animateInternalToOffset(targetOffset, anim)
} finally {
val endOffset = absoluteOffset.value
val endOffset = absoluteOffset.floatValue
val endValue = anchors
// fighting rounding error once again, anchor should be as close as 0.5 pixels
.filterKeys { anchorOffset -> abs(anchorOffset - endOffset) < 0.5f }
@ -364,9 +364,9 @@ open class SwipeableState<T>(
* @return the amount of [delta] consumed
*/
fun performDrag(delta: Float): Float {
val potentiallyConsumed = absoluteOffset.value + delta
val potentiallyConsumed = absoluteOffset.floatValue + delta
val clamped = potentiallyConsumed.coerceIn(minBound, maxBound)
val deltaToConsume = clamped - absoluteOffset.value
val deltaToConsume = clamped - absoluteOffset.floatValue
if (abs(deltaToConsume) > 0) {
draggableState.dispatchRawDelta(deltaToConsume)
}
@ -410,9 +410,7 @@ class SwipeProgress<T>(
if (from != other.from) return false
if (to != other.to) return false
if (fraction != other.fraction) return false
return true
return fraction == other.fraction
}
override fun hashCode(): Int {