From 03c34924d191e4756a66d8791f96e74f21340d44 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Fri, 6 Oct 2023 18:46:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=8F=E5=B9=85=E5=BA=A6=E4=B8=8B?= =?UTF-8?q?=E6=8B=89=E6=97=B6=E8=A7=A6=E5=8F=91=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tieba/post/ui/page/forum/ForumPage.kt | 12 +++---- .../tieba/post/ui/widgets/compose/LoadMore.kt | 12 +++---- .../post/ui/widgets/compose/Swipeable.kt | 34 +++++++++---------- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt index 14d5dcca..5fd69c7f 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt @@ -1172,22 +1172,18 @@ private val SwipeableState.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) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/LoadMore.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/LoadMore.kt index 276d8a2d..3c6919c9 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/LoadMore.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/LoadMore.kt @@ -247,22 +247,18 @@ private val SwipeableState.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) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/Swipeable.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/Swipeable.kt index 5ceb8a95..67006b96 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/Swipeable.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/Swipeable.kt @@ -83,12 +83,12 @@ open class SwipeableState( val overflow: State 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(null) @@ -110,8 +110,8 @@ open class SwipeableState( 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( 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) { draggableState.drag { - var prevValue = absoluteOffset.value + var prevValue = absoluteOffset.floatValue animationTarget.value = target isAnimationRunning = true try { @@ -306,7 +306,7 @@ open class SwipeableState( } 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( * @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( 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 {