AnchoredDraggableState move to true even when finger not released from swiper control

Summary

The issue encountered is with the AnchoredDraggableState in Jetpack Compose, where the state moves to true even when the finger is not released from the swiper control. This happens when the threshold value is reached, causing the swipe control to jump to the extreme right.

Root Cause

The root cause of this issue is due to the positionalThreshold function in the FlingBehavior. This function determines when the state should switch to true. The current implementation switches the state when the distance reached is half of the total distance, which is not the desired behavior.

Why This Happens in Real Systems

This issue occurs in real systems because the AnchoredDraggableState is designed to animate to the nearest anchor point when the threshold is reached. In this case, the threshold is set to half of the total distance, causing the state to switch to true prematurely.

Real-World Impact

The real-world impact of this issue is:

  • Unexpected behavior: The swipe control jumps to the extreme right unexpectedly, which can be confusing for users.
  • Loss of control: The user may feel like they have lost control of the swipe gesture, leading to a poor user experience.
  • Inconsistent interaction: The interaction is inconsistent with other swipe gestures, where the state typically only changes when the finger is released.

Example or Code

val flingBehavior = AnchoredDraggableDefaults.flingBehavior(
    state = swipeState,
    positionalThreshold = { distance -> distance * 0.5f }, // This line causes the issue
    animationSpec = spring(
        dampingRatio = Spring.DampingRatioLowBouncy,
        stiffness = Spring.StiffnessLow
    )
)

How Senior Engineers Fix It

To fix this issue, senior engineers would:

  • Change the threshold calculation: Adjust the positionalThreshold function to only switch the state when the finger is released, regardless of the distance reached.
  • Add a custom threshold check: Implement a custom check to determine when the state should switch to true, taking into account the finger release event.

Why Juniors Miss It

Juniors may miss this issue because:

  • Lack of understanding of AnchoredDraggableState: They may not fully understand how the AnchoredDraggableState works, leading to incorrect assumptions about its behavior.
  • Insufficient testing: They may not test the swipe gesture thoroughly, missing the unexpected behavior when the threshold is reached.
  • Inadequate knowledge of Jetpack Compose: They may not be familiar with the FlingBehavior and positionalThreshold functions, making it difficult to identify the root cause of the issue.