Skip to content

Instantly share code, notes, and snippets.

@Ahmed-Sellami
Created Sep 30, 2021
Embed
What would you like to do?
@ExperimentalAnimationApi
@Composable
fun Home() {
/* ... */
Scaffold(/* ... */) { innerPadding ->
ShoesList(
modifier = Modifier.padding(innerPadding),
shoesArticles = shoesArticles,
slideStates = slideStates,
updateSlidedState = { shoesArticle, slideState -> slideStates[shoesArticle] = slideState },
updateItemPosition = { currentIndex, destinationIndex ->
val shoesArticle = shoesArticles[currentIndex]
shoesArticles.removeAt(currentIndex)
shoesArticles.add(destinationIndex, shoesArticle)
slideStates.apply {
shoesArticles.map { shoesArticle ->
shoesArticle to SlideState.NONE
}.toMap().also {
putAll(it)
}
}
}
)
}
}
@ExperimentalAnimationApi
@Composable
fun ShoesList(
modifier: Modifier,
shoesArticles: MutableList<ShoesArticle>,
slideStates: Map<ShoesArticle, SlideState>,
updateSlidedState: (shoesArticle: ShoesArticle, slideState: SlideState) -> Unit,
updateItemPosition: (currentIndex: Int, destinationIndex: Int) -> Unit
) {
val lazyListState = rememberLazyListState()
LazyColumn(
state = lazyListState,
modifier = modifier.padding(top = dimensionResource(id = R.dimen.list_top_padding))
) {
items(shoesArticles.size) { index ->
val shoesArticle = shoesArticles.getOrNull(index)
if (shoesArticle != null) {
key(shoesArticle) {
val slideState = slideStates[shoesArticle] ?: SlideState.NONE
ShoesCard(
shoesArticle = shoesArticle,
slideState = slideState,
shoesArticles = shoesArticles,
updateSlidedState = updateSlidedState,
updateItemPosition = updateItemPosition
)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment