Is there anyway to observe `BottomSheetScaffoldSta...
# compose
z
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
You must remember the
BottomSheetState:
Copy code
val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
    bottomSheetState = rememberBottomSheetState(BottomSheetValue.Expanded)
)
👀 1
r
Copy code
LaunchedEffect(bottomSheetScaffoldState.bottomSheetState){
     if (bottomSheetScaffoldState.bottomSheetState.isCollapsed) {
        onBackPress()
    }
 }
z
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
Should be this
Copy code
LaunchedEffect(bottomSheetScaffoldState.bottomSheetState.isCollapsed){
    if (bottomSheetScaffoldState.bottomSheetState.isCollapsed) {
        onBackPress()
    }
}
👀 1
🔥 1
z
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
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
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