Yokan / rebuild
Course 1 8624561

Reorderable.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/components/Reorderable.kt

implementation105 lines
1package eu.friedcutlet.yokan.ui.components23import androidx.compose.foundation.gestures.detectDragGestures4import androidx.compose.foundation.layout.Box5import androidx.compose.foundation.layout.Column6import androidx.compose.foundation.layout.offset7import androidx.compose.runtime.Composable8import androidx.compose.runtime.getValue9import androidx.compose.runtime.mutableStateMapOf10import androidx.compose.runtime.mutableStateOf11import androidx.compose.runtime.remember12import androidx.compose.runtime.rememberUpdatedState13import androidx.compose.runtime.setValue14import androidx.compose.ui.Modifier15import androidx.compose.ui.input.pointer.pointerInput16import androidx.compose.ui.layout.onGloballyPositioned17import androidx.compose.ui.unit.IntOffset18import androidx.compose.ui.zIndex19import kotlin.math.roundToInt2021/**22 * A vertical list whose rows can be reordered by dragging a handle. Replaces23 * the old up/down arrow buttons.24 *25 * Reordering is committed as the dragged row crosses row-height thresholds.26 * Fast drags can move several positions in one gesture update; stores that27 * reorder by offset can use `toIndex - fromIndex` directly.28 *29 * The list is intentionally a plain [Column] (not lazy): reorderable settings30 * lists are short, and a Column sidesteps the index/offset bookkeeping that31 * drag-reorder inside a LazyColumn requires. Drop this as a single `item {}`32 * inside an outer LazyColumn when needed.33 *34 * @param itemContent receives the item, a [Modifier] to attach to the drag35 *   handle, and whether this row is currently being dragged.36 */37@Composable38fun <T> ReorderableColumn(39    items: List<T>,40    key: (T) -> Any,41    onMove: (fromIndex: Int, toIndex: Int) -> Unit,42    modifier: Modifier = Modifier,43    itemContent: @Composable (item: T, dragHandle: Modifier, isDragging: Boolean) -> Unit,44) {45    var draggedKey by remember { mutableStateOf<Any?>(null) }46    var dragOffset by remember { mutableStateOf(0f) }47    val itemHeights = remember { mutableStateMapOf<Any, Int>() }48    val latestItems by rememberUpdatedState(items)49    val latestOnMove by rememberUpdatedState(onMove)5051    Column(modifier = modifier) {52        items.forEach { item ->53            val itemKey = key(item)54            androidx.compose.runtime.key(itemKey) {55                val isDragging = itemKey == draggedKey56                Box(57                    modifier = Modifier58                        .onGloballyPositioned { itemHeights[itemKey] = it.size.height }59                        .zIndex(if (isDragging) 1f else 0f)60                        .then(61                            if (isDragging) {62                                Modifier.offset { IntOffset(0, dragOffset.roundToInt()) }63                            } else {64                                Modifier65                            },66                        ),67                ) {68                    val handle = Modifier.pointerInput(itemKey) {69                        detectDragGestures(70                            onDragStart = {71                                draggedKey = itemKey72                                dragOffset = 0f73                            },74                            onDragEnd = {75                                draggedKey = null76                                dragOffset = 0f77                            },78                            onDragCancel = {79                                draggedKey = null80                                dragOffset = 0f81                            },82                            onDrag = { change, amount ->83                                change.consume()84                                dragOffset += amount.y85                                val currentItems = latestItems86                                val current = currentItems.indexOfFirst { key(it) == draggedKey }87                                if (current < 0) return@detectDragGestures88                                val height = (itemHeights[draggedKey] ?: 0).coerceAtLeast(1)89                                val steps = (dragOffset / height.toFloat()).toInt()90                                if (steps != 0) {91                                    val target = (current + steps).coerceIn(0, currentItems.lastIndex)92                                    if (target != current) {93                                        latestOnMove(current, target)94                                        dragOffset -= (target - current) * height.toFloat()95                                    }96                                }97                            },98                        )99                    }100                    itemContent(item, handle, isDragging)101                }102            }103        }104    }105}106

Search chapters, reference and source filenames.