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, | |
) |
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
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
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
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
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
val itemSize = remember(columns, rows) { | |
val itemWidth = (maxWidth) / rows | |
val itemHeight = (maxHeight) / columns | |
DpSize(itemWidth, itemHeight) | |
} |
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
@Composable | |
fun <ITEM, KEY> AnimatedVerticalGrid( | |
items: List<ITEM>, | |
itemKey: (ITEM) -> KEY, | |
columns: Int, | |
rows: Int | |
) |
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
class ExampleActivity : Activity() { | |
private val viewModel = ViewModel() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
viewModel.state.handleEvent(ExampleEvent) | |
} | |
} |
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
class ViewModel { | |
val state = StateReducerFlow( | |
initialState = State(), | |
reduceState = ::reduceState | |
) | |
private fun reduceState(currentState: State, event: Event): State { | |
return when (event) { | |
is Increment -> currentState.copy(counter = currentState.counter + 1) |
NewerOlder