From 10d35470f2187ac128eefd51049ec9c5891fe195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=20Cla=C3=9Fen?= Date: Wed, 13 Oct 2021 00:49:22 +0200 Subject: [PATCH] ItemPosition for reorder callbacks Bump gradle from 4.2.2 to 7.0.3 --- .../android/ui/reorderlist/ReorderList.kt | 6 ++--- .../ui/reorderlist/ReorderListViewModel.kt | 11 ++++---- build.gradle.kts | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- reorderable/build.gradle.kts | 2 +- .../burnoutcrew/reorderable/ItemPosition.kt | 3 +++ .../burnoutcrew/reorderable/ReorderLogic.kt | 25 +++++++++---------- .../burnoutcrew/reorderable/Reorderable.kt | 4 +-- 8 files changed, 29 insertions(+), 26 deletions(-) create mode 100644 reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ItemPosition.kt diff --git a/android/src/main/kotlin/org/burnoutcrew/android/ui/reorderlist/ReorderList.kt b/android/src/main/kotlin/org/burnoutcrew/android/ui/reorderlist/ReorderList.kt index a201780..4202c74 100644 --- a/android/src/main/kotlin/org/burnoutcrew/android/ui/reorderlist/ReorderList.kt +++ b/android/src/main/kotlin/org/burnoutcrew/android/ui/reorderlist/ReorderList.kt @@ -62,7 +62,7 @@ fun HorizontalReorderList( modifier: Modifier = Modifier, items: List, state: ReorderableState = rememberReorderState(), - onMove: (fromPos: Int, toPos: Int) -> (Unit), + onMove: (fromPos: ItemPosition, toPos: ItemPosition) -> (Unit), ) { LazyRow( state = state.listState, @@ -95,8 +95,8 @@ fun VerticalReorderList( modifier: Modifier = Modifier, items: List, state: ReorderableState = rememberReorderState(), - onMove: (fromPos: Int, toPos: Int) -> (Unit), - canDragOver: ((index: Int) -> Boolean), + onMove: (fromPos: ItemPosition, toPos: ItemPosition) -> (Unit), + canDragOver: ((pos: ItemPosition) -> Boolean), ) { LazyColumn( state = state.listState, diff --git a/android/src/main/kotlin/org/burnoutcrew/android/ui/reorderlist/ReorderListViewModel.kt b/android/src/main/kotlin/org/burnoutcrew/android/ui/reorderlist/ReorderListViewModel.kt index dcf3e82..75a5646 100644 --- a/android/src/main/kotlin/org/burnoutcrew/android/ui/reorderlist/ReorderListViewModel.kt +++ b/android/src/main/kotlin/org/burnoutcrew/android/ui/reorderlist/ReorderListViewModel.kt @@ -17,6 +17,7 @@ package org.burnoutcrew.android.ui.reorderlist import androidx.compose.runtime.toMutableStateList import androidx.lifecycle.ViewModel +import org.burnoutcrew.reorderable.ItemPosition import org.burnoutcrew.reorderable.move @@ -26,13 +27,13 @@ class ReorderListViewModel : ViewModel() { if (it.mod(10) == 0) ItemData("Locked", "id$it", true) else ItemData("Dog $it", "id$it") }.toMutableStateList() - fun moveCat(from: Int, to: Int) { - cats.move(from, to) + fun moveCat(from: ItemPosition, to: ItemPosition) { + cats.move(from.index, to.index) } - fun moveDog(from: Int, to: Int) { - dogs.move(from, to) + fun moveDog(from: ItemPosition, to: ItemPosition) { + dogs.move(from.index, to.index) } - fun isDogDragEnabled(idx: Int) = dogs.getOrNull(idx)?.isLocked != true + fun isDogDragEnabled(pos: ItemPosition) = dogs.getOrNull(pos.index)?.isLocked != true } \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index e07f133..147dc2a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ buildscript { dependencies { classpath("org.jetbrains.compose:compose-gradle-plugin:1.0.0-alpha3") - classpath("com.android.tools.build:gradle:4.2.2") + classpath("com.android.tools.build:gradle:7.0.3") classpath(kotlin("gradle-plugin", version = "1.5.21")) } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 549d844..0f80bbf 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/reorderable/build.gradle.kts b/reorderable/build.gradle.kts index d97b5bf..0926258 100644 --- a/reorderable/build.gradle.kts +++ b/reorderable/build.gradle.kts @@ -41,7 +41,7 @@ android { } sourceSets { - val main by getting { + named("main") { manifest.srcFile("src/androidMain/AndroidManifest.xml") } } diff --git a/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ItemPosition.kt b/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ItemPosition.kt new file mode 100644 index 0000000..d243094 --- /dev/null +++ b/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ItemPosition.kt @@ -0,0 +1,3 @@ +package org.burnoutcrew.reorderable + +data class ItemPosition(val index: Int, val key: Any) \ No newline at end of file diff --git a/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ReorderLogic.kt b/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ReorderLogic.kt index bd9eaa8..5708d5d 100644 --- a/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ReorderLogic.kt +++ b/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/ReorderLogic.kt @@ -25,8 +25,8 @@ import kotlin.math.sign internal class ReorderLogic( private val state: ReorderableState, - private val onMove: (fromIndex: Int, toIndex: Int) -> (Unit), - private val canDragOver: ((index: Int) -> Boolean)? = null, + private val onMove: (fromIndex: ItemPosition, toIndex: ItemPosition) -> (Unit), + private val canDragOver: ((index: ItemPosition) -> Boolean)? = null, private val onDragEnd: ((startIndex: Int, endIndex: Int) -> (Unit))? = null, ) { fun startDrag(key: Any) = @@ -79,29 +79,28 @@ internal class ReorderLogic( val end = (start + selected.size) .coerceIn(viewportStartOffset, viewportEndOffset + selected.size) state.draggedIndex?.also { draggedItem -> - chooseDropIndex( + chooseDropItem( state.listState.layoutInfo.visibleItemsInfo .filterNot { it.offsetEnd() < start || it.offset > end || it.index == draggedItem } - .filter { canDragOver?.invoke(it.index) != false }, + .filter { canDragOver?.invoke(ItemPosition(it.index, it.key)) != false }, start, end )?.also { targetIdx -> - onMove(draggedItem, targetIdx) - state.draggedIndex = targetIdx + onMove(ItemPosition(draggedItem, selected.key), ItemPosition(targetIdx.index, targetIdx.key)) + state.draggedIndex = targetIdx.index state.listState.scrollToItem(state.listState.firstVisibleItemIndex, state.listState.firstVisibleItemScrollOffset) - } } } } - private fun chooseDropIndex( + private fun chooseDropItem( items: List, curStart: Float, curEnd: Float, - ): Int? = + ): LazyListItemInfo? = draggedItem.let { draggedItem -> - var targetIndex: Int? = null + var targetItem: LazyListItemInfo? = null if (draggedItem != null) { val distance = curStart - draggedItem.offset if (distance != 0f) { @@ -118,14 +117,14 @@ internal class ReorderLogic( ?.takeIf { it > targetDiff } ?.also { targetDiff = it - targetIndex = item.index + targetItem = item } } } } else if (state.draggedIndex != null) { - targetIndex = items.lastOrNull()?.index + targetItem = items.lastOrNull() } - targetIndex + targetItem } diff --git a/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/Reorderable.kt b/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/Reorderable.kt index 5b6318f..f8fd2ec 100644 --- a/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/Reorderable.kt +++ b/reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/Reorderable.kt @@ -72,8 +72,8 @@ class ReorderableState(val listState: LazyListState) { @OptIn(ExperimentalCoroutinesApi::class) fun Modifier.reorderable( state: ReorderableState, - onMove: (fromPos: Int, toPos: Int) -> (Unit), - canDragOver: ((index: Int) -> Boolean)? = null, + onMove: (fromPos: ItemPosition, toPos: ItemPosition) -> (Unit), + canDragOver: ((index: ItemPosition) -> Boolean)? = null, onDragEnd: ((startIndex: Int, endIndex: Int) -> (Unit))? = null, orientation: Orientation = Orientation.Vertical, maxScrollPerFrame: Dp = 20.dp,