diff --git a/CollabTableAndroid/app/src/main/java/com/collabtable/app/ui/screens/ListDetailScreen.kt b/CollabTableAndroid/app/src/main/java/com/collabtable/app/ui/screens/ListDetailScreen.kt index e2c72ee..1f96a1a 100644 --- a/CollabTableAndroid/app/src/main/java/com/collabtable/app/ui/screens/ListDetailScreen.kt +++ b/CollabTableAndroid/app/src/main/java/com/collabtable/app/ui/screens/ListDetailScreen.kt @@ -2506,30 +2506,26 @@ fun ManageColumnsDialog( Divider() // Column list with drag-and-drop reordering - // Local extension mirroring the working behavior used in ListsScreen + // Local extension mirroring the working behavior used in ListsScreen, + // adjusting target index when moving downwards to account for prior removal. fun MutableList.move( from: Int, to: Int, ) { if (from == to) return - if (isEmpty()) return - if (from !in indices) return + if (isEmpty() || from !in indices) return val item = removeAt(from) - val insertIndex = - when { - to > size -> size - to < 0 -> 0 - else -> to - } + val insertIndex = (if (to > from) to - 1 else to).coerceIn(0, size) add(insertIndex, item) } val reorderState = rememberReorderableLazyListState( onMove = { from, to -> - // Map from Lazy positions (including dividers) to data indices + // Map from Lazy positions (with dividers) to data indices. + // Insert AFTER the upper item when hovering between rows. val fromData = from.index / 2 - val toData = to.index / 2 + val toData = (to.index + 1) / 2 reorderedFields.move(fromData, toData) }, ) @@ -2546,7 +2542,7 @@ fun ManageColumnsDialog( ) { // Interleave dividers as separate Lazy items so dividers don't move with dragged rows items( - count = reorderedFields.size * 2 - 1, + count = if (reorderedFields.isEmpty()) 0 else reorderedFields.size * 2 - 1, key = { pos -> if (pos % 2 == 0) reorderedFields[pos / 2].id else "divider-$pos" }, diff --git a/CollabTableAndroid/app/src/main/java/com/collabtable/app/ui/screens/ListsScreen.kt b/CollabTableAndroid/app/src/main/java/com/collabtable/app/ui/screens/ListsScreen.kt index 7a6e054..6c6c803 100644 --- a/CollabTableAndroid/app/src/main/java/com/collabtable/app/ui/screens/ListsScreen.kt +++ b/CollabTableAndroid/app/src/main/java/com/collabtable/app/ui/screens/ListsScreen.kt @@ -161,17 +161,11 @@ fun ListsScreen( to: Int, ) { if (from == to) return + if (isEmpty() || from !in indices) return val item = removeAt(from) - add( - if (to > size) { - size - } else if (to < 0) { - 0 - } else { - to - }, - item, - ) + // When moving downwards, the target index shifts left by one after removal + val insertIndex = (if (to > from) to - 1 else to).coerceIn(0, size) + add(insertIndex, item) } val reorderState = @@ -179,9 +173,10 @@ fun ListsScreen( onMove = { from, to -> dragging = true val newList = working.toMutableList() - // Map from Lazy positions (including dividers) to data indices + // Map from Lazy positions (with dividers) to data indices. + // to.index can be odd (between items). Insert AFTER the upper item for better feel. val fromData = from.index / 2 - val toData = to.index / 2 + val toData = (to.index + 1) / 2 newList.move(fromData, toData) working = newList }, @@ -202,7 +197,7 @@ fun ListsScreen( ) { // Interleave dividers as separate list items so they don't move with dragged rows items( - count = working.size * 2 - 1, + count = if (working.isEmpty()) 0 else working.size * 2 - 1, key = { pos -> if (pos % 2 == 0) working[pos / 2].id else "divider-$pos" },