svl
05/19/2023, 9:30 AMcurioustechizen
05/19/2023, 9:40 AMsvl
05/19/2023, 9:43 AMJakub Syty
05/19/2023, 9:46 AMsvl
05/19/2023, 9:47 AMval state = rememberDatePickerState(initialDisplayMode = DisplayMode.Input)
DatePicker(state = state, modifier = Modifier.padding(16.dp))
Text("Entered date timestamp: ${state.selectedDateMillis ?: "no input"}")
I would expect it to be Datepicker(state = state, onStateChange = { state = it }) or something similarcurioustechizen
05/19/2023, 10:14 AMHorizontal/VerticalPager
which accepts a PagerState
parameter. You don't get a callback when a page in the pager is selected. Instead you need to observe properties on PagerState
itself (like currentPage
). You also use the same state to perform actions like scroll to a specific page.
2. ModalBottomSheetLayout
which accepts a ModalBottomSheetState
. You don't get callbacks when the bottom sheet is expanded/hidden. You observe properties for this. Again, you use the same state object if you want to perform actions like expand/collapse the sheet.Jakub Syty
05/19/2023, 10:32 AMStylianos Gakis
05/19/2023, 10:43 AMsvl
05/19/2023, 11:05 AMStylianos Gakis
05/19/2023, 11:06 AMsvl
05/19/2023, 11:09 AMStylianos Gakis
05/19/2023, 11:11 AMsvl
05/19/2023, 11:12 AMStylianos Gakis
05/19/2023, 11:12 AMsvl
05/19/2023, 11:14 AMStylianos Gakis
05/19/2023, 11:15 AMit just changes and the ui will change with it, but you aren't be able to react to it in any way
You can by observing the state it exposes
svl
05/19/2023, 11:16 AMStylianos Gakis
05/19/2023, 11:17 AMcurioustechizen
05/19/2023, 11:47 AMLandry Norris
05/19/2023, 12:15 PMStylianos Gakis
05/19/2023, 12:17 PMLandry Norris
05/19/2023, 12:18 PMStylianos Gakis
05/19/2023, 12:19 PMOleksandr Balan
05/19/2023, 12:43 PMBasicTextField2
also has only state
argument without onChange
lambda 🤔
Maybe it is new “this is the way” :mandalorian: 🤔
https://developer.android.com/reference/kotlin/androidx/compose/foundation/text2/package-summaryStylianos Gakis
05/19/2023, 12:51 PMforEachTextValue
which allows you to do something per new state change. Which internally is literally just textAsFlow().collectLatest(block)
where textAsFlow()
is just fun TextFieldState.textAsFlow(): Flow<TextFieldCharSequence> = snapshotFlow { text }
.
So exactly what we discussed above here too, taking the state that the date picker exposes, and observe its changes. SnapshotFlow and flows in general give you this super clean api on how to do this.curioustechizen
05/19/2023, 1:04 PMStylianos Gakis
05/19/2023, 1:07 PMcurioustechizen
05/19/2023, 1:07 PMStylianos Gakis
05/19/2023, 1:11 PMval fooState = rememberFooState(
initialState,
{ newState ->
useNewState(newState)
}
)
vs
val fooState = rememberFooState(initialState)
LaunchedEffect(Unit) {
snapshotFlow { fooState.internalState }.collect { newState ->
useNewState(newState)
}
}
curioustechizen
05/19/2023, 1:19 PMDo you find that doing a snapshotFlow on the state and collecting those changes something that is too complicated vs a lambda?It is fine in isolation in one place but I fear it might become tedious and repetitive if you have several components all of which expect you to LaunchedEffect + snapshotFlow + collect every time you want to react to a simple change.
Alex Vanyo
05/19/2023, 5:16 PMLaunchedEffect
comes in.
And you could replace “on disk” with “over the network” and now the LaunchedEffect
becomes even more important. The “awkwardness” raises important questions like “how often should I try to update the value?” “what happens if the update fails due to some I/O error?” “what happens if the value was updated somewhere else?” “what scope should the LaunchedEffect
be launched in which impacts when an update is cancelled?”
A lot of those considerations can depend on your exact use case. Maybe you can synchronize the value immediately always, or maybe you have an “OK” and “Cancel” to confirm a change