escodro
04/27/2021, 12:58 PMDisposableEffect
. I have a ModalBottomSheetLayout
with different contents based on user interaction.
My idea is to clean all the content and clear the focus when the BottomSheet is hidden.
My current implementation is:
DisposableEffect(modalSheetState.currentValue) {
onDispose {
if (modalSheetState.isVisible.not()) {
focusManager.clearFocus()
sheetContentState = SheetContentState.Empty
}
}
}
The code is working, but in all samples I see there is a code before onDispose()
, like a callback register.
Is it a correct implementation of DisposableEffect
?
Thanks a lot for your help! â¤ď¸Zach Klippenstein (he/him) [MOD]
04/27/2021, 1:10 PMfocusManager
is probably safe to do that with (assuming you are also requesting focus at some point), but not sure.escodro
04/27/2021, 1:35 PMZach Klippenstein (he/him) [MOD]
04/27/2021, 1:44 PMescodro
04/27/2021, 1:48 PMModalBottomSheetLayout
, each type of BottomSheet
has a code similar to:
val focusRequester = remember { FocusRequester() }
LaunchedEffect(Unit) {
delay(600)
focusRequester.requestFocus()
}
TaskInputTextField(
text = taskInputText,
onTextChange = { text -> taskInputText = text },
modifier = Modifier.focusRequester(focusRequester)
)
I needed to add the delay due to the BottomSheet
reveal animation.Adam Powell
04/27/2021, 1:52 PMAdam Powell
04/27/2021, 1:53 PMAdam Powell
04/27/2021, 1:54 PMsheetContentState = SheetContentState.Empty
looks like the sort of thing where you're expecting sheetContentState
to be consumed somewhere else in the same recomposition where the DisposableEffect
changes and thereby "fires"Adam Powell
04/27/2021, 1:55 PMsheetContentState
are out of sync.Adam Powell
04/27/2021, 1:57 PMescodro
04/27/2021, 1:59 PMModalBottomSheetLayout
that changes the BottomSheet
content when the tabs in the BottomAppBar
changes. Also, I want to keep the BottomSheet
opened when the user rotates the screen.
So I tried to create a mechanism where the Scaffold
content communicates with the ModalBottomSheetLayout
, updating the SheetContentState
to be possible to have multiple BottomSheet
.
The complete (also a little confusing) code is this:
https://github.com/igorescodro/alkaa/blob/ff0878e63730d70b60d6a3c74d01285fe03a323d/app/src/main/java/com/escodro/alkaa/presentation/home/Home.kt#L76escodro
04/27/2021, 2:00 PMBottomSheet
is opened.Adam Powell
04/27/2021, 2:02 PMescodro
04/27/2021, 2:02 PMDisposableEffect
to close the keyboard every time the BottomSheet
is closed.
Now I added a new one to clean all the content, setting to Empty
again.Adam Powell
04/27/2021, 2:04 PMAdam Powell
04/27/2021, 2:06 PMAdam Powell
04/27/2021, 2:06 PMAdam Powell
04/27/2021, 2:09 PMLaunchedEffect(modalSheetState, focusManager) {
snapshotFlow { modalSheetState.isVisible }
...
.collect {
// take action
}
}
so that the flow collection scoped to the composition via LaunchedEffect
is what drives the action, rather than a recomposed effect itself. This will prevent the one frame delay/jank, but it won't resolve any other potential data flow/distribution of responsibility questions present hereAdam Powell
04/27/2021, 2:11 PMsheetContentState
in particular looks like it wants to be driven somewhere else and not by a presentation like this going awayAdam Powell
04/27/2021, 2:12 PMescodro
04/27/2021, 2:29 PMScaffold
and ModalBottomSheetLayout
, but I need to study more before develop a better solution. đ
Sean McQuillan [G]
04/30/2021, 6:31 PMSean McQuillan [G]
04/30/2021, 6:34 PM