Hi, folks running into a issue where `collectIsPre...
# compose
s
Hi, folks running into a issue where
collectIsPressedAsState
of InteractionSource claims pressed state even when otherwise when the item is within a scrollable lazy row/column.
Copy code
@Composable
fun InteractionSource.collectIsPressedAsState(): State<Boolean> {
    val isPressed = remember { mutableStateOf(false) }
    LaunchedEffect(this) {
        val pressInteractions = mutableListOf<PressInteraction.Press>()
        interactions.collect { interaction ->
            when (interaction) {
                is PressInteraction.Press -> pressInteractions.add(interaction)
                is PressInteraction.Release -> pressInteractions.remove(interaction.press)
                is PressInteraction.Cancel -> pressInteractions.remove(interaction.press)
            }
            isPressed.value = pressInteractions.isNotEmpty()
        }
    }
    return isPressed
}
At some point during taps,
pressInteractions
retains a Press interaction and isn't removed.
Wondering, what is the rationale of using a list to track press state than just a boolean?
z
The same
MutableInteractionSource
can be passed to multiple components, which can each emit their own
Press
interactions.
👍 1
s
So it looks like the coroutine collecting
interactions
could be canceled before processing the release/cancel event causing the count discrepancy. Is this a known issue?
z
The only way that coroutine can get cancelled is if the remember call is leaving the composition, which means anything writing to it or consuming on it should also be going away, so the count not being zeroed out shouldn't matter at that point.
s
Okay, the case i'm running is when the composabe is within scrollable container and occasionally, the press state is applied even after release. It doesn't use MaterialTheme and uses
DefaultDebugIndication
From the code of
DefaultDebugIndicationInstance
, isPressed should have been true. Any tips to debug this?
Copy code
override fun ContentDrawScope.draw() {
            drawContent()
            if (isPressed) {
                drawRect(color = Color.Black.copy(alpha = 0.3f), size = size)
            } else if (isHovered || isFocused) {
                drawRect(color = Color.Black.copy(alpha = 0.1f), size = size)
            }
        }
@Zach Klippenstein (he/him) [MOD] Found it, it happens only with compose-foundation 1.9.5. It is kind of a race between
handlePressInteractionRelease
and
handlePressInteractionStart
of clickable node when the clickable element is within a scrollable container.
Bug info isn't available, would you be able to check?