Last active
December 4, 2023 14:22
-
-
Save bmc08gt/5fafd5bc888fccbd63cc4bb3a6479494 to your computer and use it in GitHub Desktop.
Parent shared component with own VM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun ActionMenuContentDecorator( | |
modifier: Modifier = Modifier, | |
viewModel: ActionMenuViewModel = hiltViewModel(), | |
content: @Composable () -> Unit, // A | |
// content: @Composable (PaddingValues) -> Unit, // B | |
) { | |
val state by viewModel.stateFlow.collectAsStateWithLifecycle() | |
// some parent container type (Scaffold, ModalBottomSheetLayout, etc.) | |
// or lay things out with Column/Row/Box/Subcompose/Lookahead/etc | |
Scaffold( | |
modifier = modifier, | |
topBar = { | |
YourActionElements( | |
modifier = Modifier.windowInsetsPadding(WindowInsets.statusBars), | |
state = state, | |
dispatch = viewModel::dispatchEvent | |
) | |
} | |
) { padding -> | |
// either expose content Padding to child or hide it | |
// A) | |
// Box(modifier = Modifier.padding(padding) { | |
// content() | |
// } | |
// | |
// or B | |
// content(padding) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun SomeScreen( | |
viewModel: SomeScreenViewModel = hiltViewModel(), | |
) { | |
ActionMenuContentDecorator( | |
modifier = Modifier.fillMaxSize(), | |
) { | |
// your screen content | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment