Skip to content

Commit

Permalink
ItemPosition for reorder callbacks
Browse files Browse the repository at this point in the history
Bump gradle from 4.2.2 to 7.0.3
  • Loading branch information
aclassen committed Oct 12, 2021
1 parent afb03ab commit 10d3547
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fun HorizontalReorderList(
modifier: Modifier = Modifier,
items: List<ItemData>,
state: ReorderableState = rememberReorderState(),
onMove: (fromPos: Int, toPos: Int) -> (Unit),
onMove: (fromPos: ItemPosition, toPos: ItemPosition) -> (Unit),
) {
LazyRow(
state = state.listState,
Expand Down Expand Up @@ -95,8 +95,8 @@ fun VerticalReorderList(
modifier: Modifier = Modifier,
items: List<ItemData>,
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion reorderable/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ android {
}

sourceSets {
val main by getting {
named("main") {
manifest.srcFile("src/androidMain/AndroidManifest.xml")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.burnoutcrew.reorderable

data class ItemPosition(val index: Int, val key: Any)
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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<LazyListItemInfo>,
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) {
Expand All @@ -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
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 10d3547

Please sign in to comment.