-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
108 additions
and
32 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
compose-reordering/src/main/kotlin/it/vfsfitvnm/reordering/AnimatablesPool.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package it.vfsfitvnm.reordering | ||
|
||
import androidx.compose.animation.core.Animatable | ||
import androidx.compose.animation.core.AnimationVector | ||
import androidx.compose.animation.core.TwoWayConverter | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
|
||
class AnimatablesPool<T, V : AnimationVector>( | ||
private val size: Int, | ||
private val initialValue: T, | ||
typeConverter: TwoWayConverter<T, V> | ||
) { | ||
private val values = MutableList(size) { | ||
Animatable(initialValue = initialValue, typeConverter = typeConverter) | ||
} | ||
|
||
private val mutex = Mutex() | ||
|
||
init { | ||
require(size > 0) | ||
} | ||
|
||
suspend fun acquire(): Animatable<T, V> { | ||
return mutex.withLock { | ||
require(values.isNotEmpty()) | ||
values.removeFirst() | ||
} | ||
} | ||
|
||
suspend fun release(animatable: Animatable<T, V>) { | ||
mutex.withLock { | ||
require(values.size < size) | ||
animatable.snapTo(initialValue) | ||
values.add(animatable) | ||
} | ||
} | ||
} |
28 changes: 19 additions & 9 deletions
28
compose-reordering/src/main/kotlin/it/vfsfitvnm/reordering/DraggedItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,34 @@ | ||
package it.vfsfitvnm.reordering | ||
|
||
import android.annotation.SuppressLint | ||
import androidx.compose.foundation.gestures.Orientation | ||
import androidx.compose.foundation.layout.offset | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.composed | ||
import androidx.compose.ui.unit.IntOffset | ||
import androidx.compose.ui.zIndex | ||
|
||
@SuppressLint("UnnecessaryComposedModifier") | ||
fun Modifier.draggedItem( | ||
reorderingState: ReorderingState, | ||
index: Int | ||
): Modifier = composed { | ||
val translation by reorderingState.translationFor(index) | ||
|
||
offset { | ||
): Modifier = when (reorderingState.draggingIndex) { | ||
-1 -> this | ||
index -> offset { | ||
when (reorderingState.lazyListState.layoutInfo.orientation) { | ||
Orientation.Vertical -> IntOffset(0, reorderingState.offset.value) | ||
Orientation.Horizontal -> IntOffset(reorderingState.offset.value, 0) | ||
} | ||
}.zIndex(1f) | ||
else -> offset { | ||
val offset = when (index) { | ||
in reorderingState.indexesToAnimate -> reorderingState.indexesToAnimate.getValue(index).value | ||
in (reorderingState.draggingIndex + 1)..reorderingState.reachedIndex -> -reorderingState.draggingItemSize | ||
in reorderingState.reachedIndex until reorderingState.draggingIndex -> reorderingState.draggingItemSize | ||
else -> 0 | ||
} | ||
when (reorderingState.lazyListState.layoutInfo.orientation) { | ||
Orientation.Vertical -> IntOffset(0, translation) | ||
Orientation.Horizontal -> IntOffset(translation, 0) | ||
Orientation.Vertical -> IntOffset(0, offset) | ||
Orientation.Horizontal -> IntOffset(offset, 0) | ||
} | ||
} | ||
.zIndex(if (reorderingState.draggingIndex == index) 1f else 0f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters