kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
refactor: validate drag-and-drop by fixed `contentType`
rodzic
cdf7639c4a
commit
9869c9e861
|
@ -148,14 +148,13 @@ internal constructor(
|
||||||
internal var previousItemOffset = Animatable(0f)
|
internal var previousItemOffset = Animatable(0f)
|
||||||
private set
|
private set
|
||||||
|
|
||||||
internal fun onDragStart(offset: Offset) {
|
internal fun onDragStart(offset: Offset): LazyListItemInfo? = state.layoutInfo.visibleItemsInfo
|
||||||
state.layoutInfo.visibleItemsInfo
|
.filter { it.contentType == DragDropContentType }
|
||||||
.firstOrNull { item -> offset.y.toInt() in item.offset..(item.offset + item.size) }
|
.firstOrNull { item -> offset.y.toInt() in item.offset..(item.offset + item.size) }
|
||||||
?.also {
|
?.also {
|
||||||
draggingItemIndex = it.index
|
draggingItemIndex = it.index
|
||||||
draggingItemInitialOffset = it.offset
|
draggingItemInitialOffset = it.offset
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
internal fun onDragInterrupted() {
|
internal fun onDragInterrupted() {
|
||||||
if (draggingItemIndex != null) {
|
if (draggingItemIndex != null) {
|
||||||
|
@ -240,7 +239,7 @@ fun Modifier.dragContainer(
|
||||||
dragDropState.onDrag(offset = offset)
|
dragDropState.onDrag(offset = offset)
|
||||||
},
|
},
|
||||||
onDragStart = { offset ->
|
onDragStart = { offset ->
|
||||||
dragDropState.onDragStart(offset)
|
dragDropState.onDragStart(offset) ?: return@detectDragGesturesAfterLongPress
|
||||||
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
|
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
|
||||||
},
|
},
|
||||||
onDragEnd = { dragDropState.onDragInterrupted() },
|
onDragEnd = { dragDropState.onDragInterrupted() },
|
||||||
|
@ -272,6 +271,8 @@ fun LazyItemScope.DraggableItem(
|
||||||
Column(modifier = modifier.then(draggingModifier)) { content(dragging) }
|
Column(modifier = modifier.then(draggingModifier)) { content(dragging) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const val DragDropContentType = "drag-and-drop"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension function for [LazyListScope] with drag-and-drop functionality for indexed items.
|
* Extension function for [LazyListScope] with drag-and-drop functionality for indexed items.
|
||||||
*
|
*
|
||||||
|
@ -282,12 +283,11 @@ inline fun <T> LazyListScope.dragDropItemsIndexed(
|
||||||
items: List<T>,
|
items: List<T>,
|
||||||
dragDropState: DragDropState,
|
dragDropState: DragDropState,
|
||||||
noinline key: ((index: Int, item: T) -> Any)? = null,
|
noinline key: ((index: Int, item: T) -> Any)? = null,
|
||||||
crossinline contentType: (index: Int, item: T) -> Any? = { _, _ -> null },
|
|
||||||
crossinline itemContent: @Composable LazyItemScope.(index: Int, item: T, isDragging: Boolean) -> Unit
|
crossinline itemContent: @Composable LazyItemScope.(index: Int, item: T, isDragging: Boolean) -> Unit
|
||||||
) = itemsIndexed(
|
) = itemsIndexed(
|
||||||
items = items,
|
items = items,
|
||||||
key = key,
|
key = key,
|
||||||
contentType = contentType,
|
contentType = { _, _ -> DragDropContentType },
|
||||||
itemContent = { index, item ->
|
itemContent = { index, item ->
|
||||||
DraggableItem(
|
DraggableItem(
|
||||||
dragDropState = dragDropState,
|
dragDropState = dragDropState,
|
||||||
|
|
Ładowanie…
Reference in New Issue