Last active
December 11, 2023 18:39
-
-
Save PaulKlauser/8c3a36bb621670333a11f235ced11b1d to your computer and use it in GitHub Desktop.
Attempt at using a state holder instead of a snapshot flow for https://gist.github.com/heatcreep/08ec25591b53b517ffb664856e4b8f10
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
@Composable | |
fun MonolithCollapsableColumn(listState: LazyListState, content: @Composable () -> Unit) { | |
val showingState = rememberShowingState(listState = listState) | |
Column { | |
AnimatedVisibility(visible = showingState.show) { | |
Column( | |
modifier = Modifier | |
.fillMaxWidth(), | |
verticalArrangement = Arrangement.spacedBy(16.dp) | |
) { | |
content() | |
} | |
} | |
} | |
} | |
@Composable | |
fun rememberShowingState(listState: LazyListState): ShowingState = | |
remember { | |
ShowingState(listState) | |
} | |
class ShowingState( | |
private val listState: LazyListState | |
) { | |
private var previousIndex = listState.firstVisibleItemIndex | |
private var previousShow = true | |
val show by derivedStateOf { | |
if (previousIndex != listState.firstVisibleItemIndex) { | |
previousShow = (previousIndex > listState.firstVisibleItemIndex) | |
previousIndex = listState.firstVisibleItemIndex | |
} | |
previousShow | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment