v0.2.9: True Touch Drag & Drop Reordering & Fix OTA Auto-Install Bug

This commit is contained in:
2026-08-20 23:59:44 +00:00
parent 5d610eab02
commit 6c55b9eeec
@@ -0,0 +1,138 @@
package com.hotdeal.alarm.presentation.components
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.foundation.lazy.LazyListItemInfo
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.zIndex
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
/**
* Compose LazyColumn Drag & Drop Reordering State
*/
@Composable
fun rememberDragDropListState(
lazyListState: LazyListState,
onMove: (Int, Int) -> Unit,
onDragEnd: () -> Unit = {}
): DragDropListState {
val scope = rememberCoroutineScope()
val state = remember(lazyListState) {
DragDropListState(
lazyListState = lazyListState,
onMove = onMove,
onDragEnd = onDragEnd,
scope = scope
)
}
return state
}
class DragDropListState(
val lazyListState: LazyListState,
private val onMove: (Int, Int) -> Unit,
private val onDragEnd: () -> Unit,
private val scope: CoroutineScope
) {
var initiallyDraggedElement by mutableStateOf<LazyListItemInfo?>(null)
private set
var currentIndexOfDraggedItem by mutableStateOf<Int?>(null)
private set
private val dragOffset = Animatable(0f)
val elementOffset: Float
get() = dragOffset.value
fun onDragStart(offset: Offset) {
lazyListState.layoutInfo.visibleItemsInfo
.firstOrNull { item -> offset.y.toInt() in item.offset..(item.offset + item.size) }
?.also { item ->
currentIndexOfDraggedItem = item.index
initiallyDraggedElement = item
}
}
fun onDragInterrupted() {
initiallyDraggedElement = null
currentIndexOfDraggedItem = null
scope.launch {
dragOffset.snapTo(0f)
}
}
fun onDrag(change: Offset) {
scope.launch {
dragOffset.snapTo(dragOffset.value + change.y)
val currentElement = initiallyDraggedElement ?: return@launch
val startOffset = currentElement.offset + dragOffset.value
val endOffset = startOffset + currentElement.size
val hoveredItem = lazyListState.layoutInfo.visibleItemsInfo
.firstOrNull { item ->
val itemMid = item.offset + item.size / 2
val isMidInRange = itemMid in startOffset.toInt()..endOffset.toInt()
isMidInRange && item.index != currentIndexOfDraggedItem
}
if (hoveredItem != null) {
val currentIndex = currentIndexOfDraggedItem ?: return@launch
val targetIndex = hoveredItem.index
onMove(currentIndex, targetIndex)
currentIndexOfDraggedItem = targetIndex
}
}
}
fun onDragStop() {
onDragEnd()
scope.launch {
dragOffset.animateTo(0f, spring(stiffness = Spring.StiffnessMediumLow))
initiallyDraggedElement = null
currentIndexOfDraggedItem = null
}
}
}
fun Modifier.dragDropGesture(
dragDropState: DragDropListState
): Modifier = this.pointerInput(dragDropState) {
detectDragGesturesAfterLongPress(
onDragStart = { offset -> dragDropState.onDragStart(offset) },
onDragEnd = { dragDropState.onDragStop() },
onDragCancel = { dragDropState.onDragInterrupted() },
onDrag = { change, dragAmount ->
change.consume()
dragDropState.onDrag(dragAmount)
}
)
}
fun Modifier.dragDropItem(
index: Int,
dragDropState: DragDropListState
): Modifier = this.then(
if (index == dragDropState.currentIndexOfDraggedItem) {
Modifier
.zIndex(10f)
.graphicsLayer {
translationY = dragDropState.elementOffset
scaleX = 1.03f
scaleY = 1.03f
shadowElevation = 8f
}
} else {
Modifier
}
)