https://kotlinlang.org logo
#compose
Title
# compose
z

ziv kesten

03/13/2022, 10:29 AM
Is there anyway to observe
BottomSheetScaffoldState
? i would like to perform an action when its state is
BottomSheetValue.Collapsed
I tried using LaunchedEffect but it is not triggered
Copy code
val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
                bottomSheetState = BottomSheetState(BottomSheetValue.Expanded)
            )

            SomeComposableWithBottomSheet(state: BottomSheetScaffoldState)

            LaunchedEffect(bottomSheetScaffoldState){
                if (bottomSheetScaffoldState.bottomSheetState.isCollapsed) {
                    onBackPress()
                }
            }
a

Albert Chang

03/13/2022, 10:42 AM
You must remember the
BottomSheetState:
Copy code
val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
    bottomSheetState = rememberBottomSheetState(BottomSheetValue.Expanded)
)
👀 1
r

Rafs

03/13/2022, 10:44 AM
Copy code
LaunchedEffect(bottomSheetScaffoldState.bottomSheetState){
     if (bottomSheetScaffoldState.bottomSheetState.isCollapsed) {
        onBackPress()
    }
 }
z

ziv kesten

03/13/2022, 10:55 AM
I tried:
Copy code
val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
                bottomSheetState = rememberBottomSheetState(BottomSheetValue.Expanded)
            )
            SomeComposableWithBottomSheet(bottomSheetScaffoldState)
            LaunchedEffect(bottomSheetScaffoldState.bottomSheetState){
                Log.d("TAG", "bottomSheetState ${bottomSheetScaffoldState.bottomSheetState}")
                if (bottomSheetScaffoldState.bottomSheetState.isCollapsed) {
                    onBackPress()
                }
            }
But i still dont get the log in the launched effect
a

Albert Chang

03/13/2022, 11:38 AM
Should be this
Copy code
LaunchedEffect(bottomSheetScaffoldState.bottomSheetState.isCollapsed){
    if (bottomSheetScaffoldState.bottomSheetState.isCollapsed) {
        onBackPress()
    }
}
👀 1
🔥 1
z

ziv kesten

03/13/2022, 11:40 AM
Yes, that is it. To make it more readable and generic i should be looking at
bottomSheetScaffoldState.bottomSheetState.currentValue
Like this:
Copy code
LaunchedEffect(bottomSheetScaffoldState.bottomSheetState.currentValue){
                if (bottomSheetScaffoldState.bottomSheetState.isCollapsed) {
                    close(visibilityState)
                }
            }
Thank you very much!
a

Albert Chang

03/13/2022, 11:43 AM
I don't see how it is more readable and generic. Actually you are doing extra work with that code as a new coroutine will be launched even if the state change isn't from or to collapsed state.
z

ziv kesten

03/13/2022, 11:46 AM
The reason was that i want to do more with current state than just that condition. I was simplifying the code for the thread but i would like to respond to expanded as well as collapsed states
30 Views