From e9103f6af725427ef570ccf66e51dd1691d3f293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9luchu?= Date: Thu, 6 Jul 2023 10:19:06 +0200 Subject: [PATCH] Improve Drag and Drop in LazyColumn --- .../foundation/lists/LazyColumnDragAndDrop.kt | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/foundation/lists/LazyColumnDragAndDrop.kt b/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/foundation/lists/LazyColumnDragAndDrop.kt index 2fceeb1c..ea379b2b 100644 --- a/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/foundation/lists/LazyColumnDragAndDrop.kt +++ b/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/foundation/lists/LazyColumnDragAndDrop.kt @@ -125,10 +125,12 @@ class DragDropState internal constructor( draggingItem.index != item.index } if (targetItem != null) { - val scrollToIndex = when { - targetItem.index == state.firstVisibleItemIndex -> draggingItem.index - draggingItem.index == state.firstVisibleItemIndex -> targetItem.index - else -> null + val scrollToIndex = if (targetItem.index == state.firstVisibleItemIndex) { + draggingItem.index + } else if (draggingItem.index == state.firstVisibleItemIndex) { + targetItem.index + } else { + null } if (scrollToIndex != null) { scope.launch { @@ -172,7 +174,7 @@ fun Modifier.dragContainer(dragDropState: DragDropState): Modifier { } } -@ExperimentalFoundationApi +@OptIn(ExperimentalFoundationApi::class) @Composable fun LazyItemScope.DraggableItem( dragDropState: DragDropState, @@ -181,22 +183,19 @@ fun LazyItemScope.DraggableItem( content: @Composable ColumnScope.(isDragging: Boolean) -> Unit ) { val dragging = index == dragDropState.draggingItemIndex - val draggingModifier = when { - dragging -> { - Modifier - .zIndex(1f) - .graphicsLayer { - translationY = dragDropState.draggingItemOffset - } - } - index == dragDropState.previousIndexOfDraggedItem -> { - Modifier - .zIndex(1f) - .graphicsLayer { - translationY = dragDropState.previousItemOffset.value - } - } - else -> Modifier.animateItemPlacement() + val draggingModifier = if (dragging) { + Modifier + .zIndex(1f) + .graphicsLayer { + translationY = dragDropState.draggingItemOffset + } + } else if (index == dragDropState.previousIndexOfDraggedItem) { + Modifier.zIndex(1f) + .graphicsLayer { + translationY = dragDropState.previousItemOffset.value + } + } else { + Modifier.animateItemPlacement() } Column(modifier = modifier.then(draggingModifier)) { content(dragging)