Skip to content

Reorderable states should expose onDragStart and onDragEnd #106

@MFlisar

Description

@MFlisar

My current solution is following:

@Composable
fun rememberReorderableLazyListState(
    lazyListState: LazyListState,
    scrollThresholdPadding: PaddingValues = PaddingValues(0.dp),
    scrollThreshold: Dp = ReorderableLazyCollectionDefaults.ScrollThreshold,
    onMove: suspend CoroutineScope.(from: LazyListItemInfo, to: LazyListItemInfo) -> Unit,
    onDragStart: suspend (from: Int) -> Unit = {},
    onDragEnd: suspend (from: Int, to: Int) -> Unit = { _, _ -> }
) : ReorderableLazyListState {
    val fromFirst = rememberSaveable { mutableStateOf<Int?>(null)}
    val toLast = rememberSaveable { mutableStateOf<Int?>(null)}
    val dragDropState = rememberReorderableLazyListState(
        lazyListState = lazyListState,
        scrollThresholdPadding = scrollThresholdPadding,
        scrollThreshold = scrollThreshold,
        //scroller = scroller,
        onMove = { from, to ->
            if (fromFirst.value == null) {
                fromFirst.value = from.index
                onDragStart(from.index)
            }
            toLast.value = to.index
            onMove(from, to)
        }
    )
    LaunchedEffect(dragDropState.isAnyItemDragging) {
        val from = fromFirst.value
        val to = toLast.value
        if (!dragDropState.isAnyItemDragging) {
            fromFirst.value = null
            toLast.value = null
            if (from != null && to != null)
                onDragEnd(from, to)
        }
    }
    return dragDropState
}

You can see I'd like to react to onDragStart and onDrafgEnd events. First one has side effects, second one undos those side effects and persists changes to the database. Those things seem like a very common use case to me.

Problems in the solution above only triggers onDragStart as soon as the first move is occuring... I must use a work around through the draggableHandle modifier which his cumbersome... I could solve this easily if dragDropState.draggingItemKey would be accessible to me.

Why I need that

I want to collapse expandable items and/or hide some items when dragging starts

Suggestion

Either expose draggingItemKey so I can immediately react to it or provide callbacks for onDragStart and onDragEnd to your state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions