Detect swipe gesture between Composable in Jetpack Compose

189 Views Asked by At

I'm currently working on an app that one of its functionality is to detect defect in touchscreen. There are Grids in which user have to touch to confirm that section of the touchscreen is working. Click event is not a problem, but swipe gesture is. I havent been able to detect swipe that start from another Composable. For example:

Supposed i have 5*5 grids and position is indicated by its X and Y index (1-1, 1-2, 1-3, and so on). I want to detect every grid that get swiped by. So, if i swipe from grid 4-5 to grid 4-4 to grid 4,4. I want all 3 of them is registered. I have tried varios method inside pointer input modifier but looks like the drag does not get passed to other composable, so i only managed to register the first grid (where onDragStart happens). I tried to use coordinate matching, but turns out the pointerChangesEvent only gives relative position instead of absolute position.

So how do i detect which Composable that get swiped by from the start to end swipe?

Current Code:

val screenWidth = LocalConfiguration.current.screenWidthDp.dp
var selectedIndex by remember { mutableStateOf(emptySet<Int>()) }

LazyHorizontalGrid(
    rows = GridCells.Fixed(20),
    modifier = Modifier
        .fillMaxSize(),
) {
    items(240) { index ->
        val isSelected by remember {
            derivedStateOf { index in selectedIndex }
        }

        Box(
            modifier = Modifier
                .fillMaxHeight()
                .width(screenWidth / 12)
                .background(if (isSelected) Colors.white else Colors.colorPrimary)
                .border(Dimensions.SIZE_1, Color.White)
                .pointerInput(Unit) {
                    this.awaitPointerEventScope {
                        val event = awaitPointerEvent()
                        when (event.type) {
                            PointerEventType.Release -> {
                                // do something
                            }
                            else -> {
                                selectedIndex += index
                            }
                        }
                    }
                }
        )
    }
}
0

There are 0 best solutions below