This file contains hidden or 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
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() | |
} |
This file contains hidden or 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
typealias ItemOffset = Animatable<DpOffset, AnimationVector2D> | |
fun ItemOffset(offset: DpOffset): ItemOffset = Animatable(offset, DpOffset.VectorConverter) |
This file contains hidden or 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
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() | |
} |
This file contains hidden or 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
items.forEach { item -> | |
val offset = itemsOffsets.getValue(itemKey(item)).value | |
Box( | |
modifier = Modifier | |
.size(itemSize) | |
.offset(offset.x, offset.y) | |
) { | |
itemContent(item) | |
} | |
} |
This file contains hidden or 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
LaunchedEffect(itemKeys) { | |
items.forEachIndexed { index, item -> | |
val newOffset = gridOffsets[index] | |
val itemOffset = itemsOffsets.getValue(itemKey(item)) | |
launch { itemOffset.animateTo(newOffset, animationSpec) } | |
} | |
} |
This file contains hidden or 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
// 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, | |
) |
OlderNewer