Skip to content

Instantly share code, notes, and snippets.

View linean's full-sized avatar

Maciej Sady linean

View GitHub Profile
val gridOffsets = remember(columns, rows, itemSize) {
(0 until columns).map { column ->
(0 until rows).map { row ->
DpOffset(
x = itemSize.width * row,
y = itemSize.height * column,
)
}
}.flatten()
}
typealias ItemOffset = Animatable<DpOffset, AnimationVector2D>
fun ItemOffset(offset: DpOffset): ItemOffset = Animatable(offset, DpOffset.VectorConverter)
var itemsOffsets by remember { mutableStateOf(mapOf<KEY, ItemOffset>()) }
key(itemKeys) {
itemsOffsets = items.mapIndexed { index, item ->
val key = itemKey(item)
key to when {
itemsOffsets.containsKey(key) -> itemsOffsets.getValue(key)
else -> ItemOffset(gridOffsets[index])
}
}.toMap()
}
items.forEach { item ->
val offset = itemsOffsets.getValue(itemKey(item)).value
Box(
modifier = Modifier
.size(itemSize)
.offset(offset.x, offset.y)
) {
itemContent(item)
}
}
LaunchedEffect(itemKeys) {
items.forEachIndexed { index, item ->
val newOffset = gridOffsets[index]
val itemOffset = itemsOffsets.getValue(itemKey(item))
launch { itemOffset.animateTo(newOffset, animationSpec) }
}
}
@linean
linean / StateMachineDebounceExample.kt
Created February 9, 2023 10:49
Simple example how events can be debounced on StateMachine side
// Keep in mind this is just simplified example
class Example {
private val scope = CoroutineScope(Job())
private val debouncingChannel = Channel<UserEvent>(
capacity = RENDEZVOUS,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)