Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose 1.4, AGP 8.0, refactored forEachGesture with awaitEachGesture #257

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

VeselyJan92
Copy link
Contributor

Updates:

  • Compose 1.4
  • AGP 8.0
  • Refactored forEachGesture with awaitEachGesture

Need to be tested and reviewed. Gradle 8.0 could potentionally break something.

@Monabr
Copy link

Monabr commented Sep 12, 2023

Looks good

if (down != null && state.onDragStart(down.position.x.toInt(), down.position.y.toInt())) {
dragStart.offset?.apply {
state.onDrag(x.toInt(), y.toInt())
Modifier.onGloballyPositioned { state.layoutWindowPosition.value = it.positionInWindow()}.pointerInput(Unit) {
Copy link

@serbelga serbelga Nov 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fun Modifier.reorderable(
    state: ReorderableState<*>
): Modifier = this then Modifier.onGloballyPositioned {
    state.layoutWindowPosition = it.positionInWindow()
}

awaitLongPressOrCancellation(down)?.also {
state.interactions.trySend(StartDrag(down.id))
}
fun Modifier.detectReorder(state: ReorderableState<*>) = detect(state){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fun Modifier.detectReorder(state: ReorderableState<*>): Modifier = composed {
    var itemPosition by remember { mutableStateOf(Offset.Zero) }
    Modifier.onGloballyPositioned {
        itemPosition = it.positionInWindow()
    }.pointerInput(Unit) {
        detectDragGestures(
            onDragStart = { offset ->
                val relativePosition = itemPosition - state.layoutWindowPosition + offset
                state.onDragStart(relativePosition.x.toInt(), relativePosition.y.toInt())
            },
            onDrag = { change, dragAmount ->
                change.consume()
                state.onDrag(dragAmount.x.toInt(), dragAmount.y.toInt())
            },
            onDragCancel = state::onDragCanceled,
            onDragEnd = state::onDragCanceled
        )
    }
}

awaitDragOrCancellation(it)
}

fun Modifier.detectReorderAfterLongPress(state: ReorderableState<*>) = detect(state) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fun Modifier.detectReorderAfterLongPress(state: ReorderableState<*>): Modifier = composed {
    var itemPosition by remember { mutableStateOf(Offset.Zero) }
    Modifier.onGloballyPositioned {
        itemPosition = it.positionInWindow()
    }.pointerInput(Unit) {
        detectDragGesturesAfterLongPress(
            onDragStart = { offset ->
                val relativePosition = itemPosition - state.layoutWindowPosition + offset
                state.onDragStart(relativePosition.x.toInt(), relativePosition.y.toInt())
            },
            onDrag = { change, dragAmount ->
                change.consume()
                state.onDrag(dragAmount.x.toInt(), dragAmount.y.toInt())
            },
            onDragCancel = state::onDragCanceled,
            onDragEnd = state::onDragCanceled
        )
    }
}

@serbelga
Copy link

serbelga commented Nov 11, 2023

You can simplify code in Modifiers using androidx.compose.foundation.gestures detectDragGestures and detectDragGesturesAfterLongPress

@bpappin
Copy link

bpappin commented Feb 2, 2024

This PR was copied to bpappin#3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants