Skip to content

Commit

Permalink
Merge branch 'compose_gradle_upgrade' of https://github.com/VeselyJan…
Browse files Browse the repository at this point in the history
…92/ComposeReorderable into VeselyJan92-compose_gradle_upgrade

# Conflicts:
#	android/build.gradle.kts
#	build.gradle.kts
#	gradle.properties
#	gradle/wrapper/gradle-wrapper.properties
  • Loading branch information
bpappin committed Feb 2, 2024
2 parents 3fcb3f3 + 9722e9b commit 9b0471d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 256 deletions.
3 changes: 2 additions & 1 deletion android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0")
implementation("androidx.navigation:navigation-compose:2.7.6")
implementation("io.coil-kt:coil-compose:2.5.0")

}

android {
Expand All @@ -39,7 +40,7 @@ android {
}

kotlinOptions {
jvmTarget = "11" //""1.8"
jvmTarget = "11"
}
namespace = "org.burnoutcrew.android"
buildToolsVersion = "34.0.0"
Expand Down
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
plugins {
`maven-publish`
id("com.android.library") version "8.2.0" apply false
id("org.jetbrains.kotlin.multiplatform") version "1.8.0" apply false
id("org.jetbrains.kotlin.android") version "1.8.0" apply false
id("org.jetbrains.compose") version "1.3.0" apply false
id("org.jetbrains.kotlin.multiplatform") version "1.8.20" apply false
id("org.jetbrains.kotlin.android") version "1.8.20" apply false
id("org.jetbrains.compose") version "1.4.0" apply false
}

ext {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ org.jetbrains.compose.experimental.jscanvas.enabled=true
# by adding android.disableAutomaticComponentCreation=true to the project's gradle.properties file).
# See: https://developer.android.com/build/publish-library
#android.disableAutomaticComponentCreation=true

android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,44 @@
*/
package org.burnoutcrew.reorderable

import androidx.compose.foundation.gestures.awaitDragOrCancellation
import androidx.compose.foundation.gestures.awaitEachGesture
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.forEachGesture
import androidx.compose.foundation.gestures.awaitLongPressOrCancellation
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.AwaitPointerEventScope
import androidx.compose.ui.input.pointer.PointerId
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInWindow

fun Modifier.detectReorder(state: ReorderableState<*>) =
this.then(
Modifier.pointerInput(Unit) {
forEachGesture {
awaitPointerEventScope {
val down = awaitFirstDown(requireUnconsumed = false)
var drag: PointerInputChange?
var overSlop = Offset.Zero
do {
drag = awaitPointerSlopOrCancellation(down.id, down.type) { change, over ->
change.consume()
overSlop = over
}
} while (drag != null && !drag.isConsumed)
if (drag != null) {
state.interactions.trySend(StartDrag(down.id, overSlop))
}
}
}
}
)


fun Modifier.detectReorderAfterLongPress(state: ReorderableState<*>) =
this.then(
Modifier.pointerInput(Unit) {
forEachGesture {
val down = awaitPointerEventScope {
awaitFirstDown(requireUnconsumed = false)
}
awaitLongPressOrCancellation(down)?.also {
state.interactions.trySend(StartDrag(down.id))
}
fun Modifier.detectReorder(state: ReorderableState<*>) = detect(state){
awaitDragOrCancellation(it)
}

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


private fun Modifier.detect(state: ReorderableState<*>, detect: suspend AwaitPointerEventScope.(PointerId)->PointerInputChange?) = composed {

val itemPosition = remember { mutableStateOf(Offset.Zero) }

Modifier.onGloballyPositioned { itemPosition.value = it.positionInWindow() }.pointerInput(Unit) {
awaitEachGesture {
val down = awaitFirstDown(requireUnconsumed = false)
val start = detect(down.id)

if (start != null) {
val relativePosition = itemPosition.value - state.layoutWindowPosition.value + start.position
state.onDragStart(relativePosition.x.toInt(), relativePosition.y.toInt())
}
}
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,38 @@
*/
package org.burnoutcrew.reorderable

import androidx.compose.foundation.gestures.awaitEachGesture
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.drag
import androidx.compose.foundation.gestures.forEachGesture
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.PointerId
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.PointerInputScope
import androidx.compose.ui.input.pointer.changedToUp
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.positionChange
import androidx.compose.ui.util.fastFirstOrNull
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInWindow

fun Modifier.reorderable(
state: ReorderableState<*>
) = then(
Modifier.pointerInput(Unit) {
forEachGesture {
val dragStart = state.interactions.receive()
val down = awaitPointerEventScope {
currentEvent.changes.fastFirstOrNull { it.id == dragStart.id }
}
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) {
awaitEachGesture {
val down = awaitFirstDown(requireUnconsumed = false)

val dragResult = drag(down.id) {
if (state.draggingItemIndex != null){
state.onDrag(it.positionChange().x.toInt(), it.positionChange().y.toInt())
it.consume()
}
detectDrag(
down.id,
onDragEnd = {
state.onDragCanceled()
},
onDragCancel = {
state.onDragCanceled()
},
onDrag = { change, dragAmount ->
change.consume()
state.onDrag(dragAmount.x.toInt(), dragAmount.y.toInt())
})
}
}
})

internal suspend fun PointerInputScope.detectDrag(
down: PointerId,
onDragEnd: () -> Unit = { },
onDragCancel: () -> Unit = { },
onDrag: (change: PointerInputChange, dragAmount: Offset) -> Unit,
) {
awaitPointerEventScope {
if (
drag(down) {
onDrag(it, it.positionChange())
it.consume()
}
) {
// consume up if we quit drag gracefully with the up
currentEvent.changes.forEach {
if (it.changedToUp()) it.consume()
if (dragResult) {
// consume up if we quit drag gracefully with the up
currentEvent.changes.forEach {
if (it.changedToUp()) it.consume()
}
}
onDragEnd()
} else {
onDragCancel()

state.onDragCanceled()
}
}
}

internal data class StartDrag(val id: PointerId, val offset: Offset? = null)
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ abstract class ReorderableState<T>(
private val onDragEnd: ((startIndex: Int, endIndex: Int) -> (Unit))?,
val dragCancelledAnimation: DragCancelledAnimation
) {
var layoutWindowPosition = mutableStateOf(Offset.Zero)

var draggingItemIndex by mutableStateOf<Int?>(null)
private set
val draggingItemKey: Any?
Expand All @@ -61,7 +63,6 @@ abstract class ReorderableState<T>(
protected abstract val firstVisibleItemScrollOffset: Int
protected abstract val viewportStartOffset: Int
protected abstract val viewportEndOffset: Int
internal val interactions = Channel<StartDrag>()
internal val scrollChannel = Channel<Float>()
val draggingItemLeft: Float
get() = if(draggingItemKey!=null) draggingLayoutInfo?.let { item ->
Expand Down

0 comments on commit 9b0471d

Please sign in to comment.