RevealSwipe icon indicating copy to clipboard operation
RevealSwipe copied to clipboard

Remember RevealState with additional key

Open zak01011996 opened this issue 9 months ago • 1 comments

Hello, it will be great to have "key" argument in rememberRevealState function. Due to directions are now being handled by state I didn't find a way to update this set dynamically (in case if entry related to this card has been changed for example).

The problem is that there are no ways to "reload" the state by some condition. So here is my workaround for that:

@Composable
fun rememberRevealState(
    key: Any? = Unit,
    maxRevealDp: Dp = 75.dp,
    directions: Set<RevealDirection> = setOf(RevealDirection.StartToEnd, RevealDirection.EndToStart),
    initialValue: RevealValue = RevealValue.Default,
    positionalThreshold: (totalDistance: Float) -> Float = { distance: Float -> distance * 0.5f },
    velocityThreshold: (() -> Float)? = null,
    snapAnimationSpec: AnimationSpec<Float> = tween(),
    decayAnimationSpec: DecayAnimationSpec<Float> = exponentialDecay(),
    confirmValueChange: (newValue: RevealValue) -> Boolean = { true }
): RevealState {
    val density = LocalDensity.current
    return remember(key) {
        RevealState(
            maxRevealDp = maxRevealDp,
            directions = directions,
            density = density,
            initialValue = initialValue,
            positionalThreshold = positionalThreshold,
            velocityThreshold = velocityThreshold ?: { with(density) { 100.dp.toPx() } },
            snapAnimationSpec = snapAnimationSpec,
            decayAnimationSpec = decayAnimationSpec,
            confirmValueChange = confirmValueChange
        )
    }
}

Usage example: In this example if an entry is published I should provide options via StartToEnd direction. If it's not published yet options are on the end of the card to publish this entry first.

   val entry = MyObjectEntry(...)

    val directions = setOf(
        when (entry.isPublished()) {
            true -> RevealDirection.StartToEnd
            false -> RevealDirection.EndToStart
        }
    )

    val state = rememberRevealState(
        key = entry,
        directions = directions,
        maxRevealDp = 100.dp
    )

zak01011996 avatar Mar 22 '25 08:03 zak01011996

Looks good. Could you create a pull request?

ch4rl3x avatar Mar 30 '25 09:03 ch4rl3x