svl
05/21/2023, 8:54 AMdata class HMS(val c1: Int, val c2: Int)
@Composable
fun Closures() {
var state by remember { mutableStateOf(HMS(0, 5)) }
Column {
SomeComponent(state) { state = it }
Text("${state.c1} : ${state.c2}")
}
}
@Composable
fun SomeComponent(state: HMS, onTimeChanged: (HMS) -> Unit) {
Row {
ListComponent( onValueChanged = { onTimeChanged(HMS(it, state.c2)) })
ListComponent( onValueChanged = { onTimeChanged(HMS(state.c1, it)) })
}
}
@Composable
fun ListComponent(onValueChanged: (Int) -> Unit) {
val listState = rememberLazyListState()
LaunchedEffect(listState) {
snapshotFlow { listState.isScrollInProgress }
.collect { if (!it) onValueChanged(listState.firstVisibleItemIndex + 1) }
}
LazyColumn(modifier = Modifier.height(240.dp), state = listState) {
items(10) { Text("$it") }
}
}
This doesn't work. Instead of creating state from current state, it creates state from original (0, 5) state. I think it's due to onValueChanged being executed in a coroutine (LaunchedEffect) and I don't have any ideas to make it work