escodro
03/15/2021, 2:39 PMState
wrapping the default ModalBottomSheetState
and one State
I created to handle which content should be shown for the user.
The implementation is on the comments and I need help to understand if Iām correctly saving the State
in order to use rememberSaveable
with it.
(It is working as expected, but Iām a little afraid of my āinner rememberā inside the Saver
š¬)
Thanks a lot in advance! ā¤ļøescodro
03/15/2021, 2:40 PM@Stable
internal class AlkaaBottomSheetState(
modalState: ModalBottomSheetState,
contentState: BottomSheetContentState
) {
var modalState by mutableStateOf(modalState)
var contentState by mutableStateOf(contentState)
companion object {
fun Saver(modalState: ModalBottomSheetState): Saver<AlkaaBottomSheetState, *> = Saver(
save = { it.contentState },
restore = { AlkaaBottomSheetState(modalState = modalState, contentState = it) }
)
}
}
@OptIn(ExperimentalMaterialApi::class)
@Composable
internal fun rememberBottomSheetState(
modalState: ModalBottomSheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden),
contentState: BottomSheetContentState = BottomSheetContentState.EmptyContentState
): AlkaaBottomSheetState =
rememberSaveable(saver = AlkaaBottomSheetState.Saver(modalState)) {
AlkaaBottomSheetState(modalState = modalState, contentState = contentState)
}
internal sealed class BottomSheetContentState {
@Parcelize
object EmptyContentState : BottomSheetContentState(), Parcelable
@Parcelize
object TaskListContentState : BottomSheetContentState(), Parcelable
@Parcelize
data class CategoryContentState(val category: Category) : BottomSheetContentState(), Parcelable
}
Usage:
val sheetState = rememberBottomSheetState()