package eu.friedcutlet.yokan.ui.components import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.offset import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateMapOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.zIndex import kotlin.math.roundToInt /** * A vertical list whose rows can be reordered by dragging a handle. Replaces * the old up/down arrow buttons. * * Reordering is committed as the dragged row crosses row-height thresholds. * Fast drags can move several positions in one gesture update; stores that * reorder by offset can use `toIndex - fromIndex` directly. * * The list is intentionally a plain [Column] (not lazy): reorderable settings * lists are short, and a Column sidesteps the index/offset bookkeeping that * drag-reorder inside a LazyColumn requires. Drop this as a single `item {}` * inside an outer LazyColumn when needed. * * @param itemContent receives the item, a [Modifier] to attach to the drag * handle, and whether this row is currently being dragged. */ @Composable fun ReorderableColumn( items: List, key: (T) -> Any, onMove: (fromIndex: Int, toIndex: Int) -> Unit, modifier: Modifier = Modifier, itemContent: @Composable (item: T, dragHandle: Modifier, isDragging: Boolean) -> Unit, ) { var draggedKey by remember { mutableStateOf(null) } var dragOffset by remember { mutableStateOf(0f) } val itemHeights = remember { mutableStateMapOf() } val latestItems by rememberUpdatedState(items) val latestOnMove by rememberUpdatedState(onMove) Column(modifier = modifier) { items.forEach { item -> val itemKey = key(item) androidx.compose.runtime.key(itemKey) { val isDragging = itemKey == draggedKey Box( modifier = Modifier .onGloballyPositioned { itemHeights[itemKey] = it.size.height } .zIndex(if (isDragging) 1f else 0f) .then( if (isDragging) { Modifier.offset { IntOffset(0, dragOffset.roundToInt()) } } else { Modifier }, ), ) { val handle = Modifier.pointerInput(itemKey) { detectDragGestures( onDragStart = { draggedKey = itemKey dragOffset = 0f }, onDragEnd = { draggedKey = null dragOffset = 0f }, onDragCancel = { draggedKey = null dragOffset = 0f }, onDrag = { change, amount -> change.consume() dragOffset += amount.y val currentItems = latestItems val current = currentItems.indexOfFirst { key(it) == draggedKey } if (current < 0) return@detectDragGestures val height = (itemHeights[draggedKey] ?: 0).coerceAtLeast(1) val steps = (dragOffset / height.toFloat()).toInt() if (steps != 0) { val target = (current + steps).coerceIn(0, currentItems.lastIndex) if (target != current) { latestOnMove(current, target) dragOffset -= (target - current) * height.toFloat() } } }, ) } itemContent(item, handle, isDragging) } } } } }