Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Components
- class Main(
- val onFinished: () -> Unit,
- val onItemSelected: (String) -> Unit,
- ) {
- val list: State<List<String>> = mutableStateOf(List(100) { "Item $it" })
- // ...
- }
- class Details(
- val text: String,
- val onFinished: () -> Unit,
- ) {
- // ...
- }
- class Root(
- componentContext: ComponentContext,
- private val onFinished: () -> Unit,
- ) : ComponentContext by componentContext {
- private val router: Router<Config, Child> =
- router(
- initialConfiguration = Config.Main,
- childFactory = ::createChild,
- )
- val routerState: Value<RouterState<*, Child>> = router.state
- private fun createChild(config: Config, componentContext: ComponentContext): Child =
- when (config) {
- is Config.Main -> MainChild(main())
- is Config.Details -> DetailsChild(details(config))
- }
- private fun main(): Main =
- Main(
- onFinished = onFinished,
- onItemSelected = { router.push(Config.Details(text = it)) },
- )
- private fun details(config: Config.Details): Details =
- Details(
- text = config.text,
- onFinished = { router.navigate { it - config } },
- )
- sealed interface Child
- class MainChild(val main: Main) : Child
- class DetailsChild(val details: Details) : Child
- private sealed interface Config : Parcelable {
- @Parcelize
- object Main : Config
- @Parcelize
- data class Details(val text: String) : Config
- }
- }
- // Composables
- @Composable
- fun DetailsContent(component: Details, modifier: Modifier = Modifier) {
- Text(text = component.text, modifier = modifier)
- }
- @Composable
- fun RootContent(component: Root) {
- val routerState by component.routerState.subscribeAsState()
- ChildWindows(routerState.getInstanceStack())
- }
- @Composable
- fun ChildWindows(stack: List<Root.Child>) {
- val child = stack.first()
- Window(
- isModal = child !is Root.MainChild,
- title = when (child) {
- is Root.MainChild -> "Main screen"
- is Root.DetailsChild -> "Details screen"
- },
- onCloseRequest = when (child) {
- is Root.MainChild -> child.main.onFinished
- is Root.DetailsChild -> child.details.onFinished
- },
- ) {
- ChildContent(child)
- stack.drop(1).takeUnless(List<*>::isEmpty)?.also {
- ChildWindows(it)
- }
- }
- }
- @Composable
- fun ChildContent(child: Root.Child) {
- Surface(modifier = Modifier.fillMaxSize()) {
- MaterialTheme {
- CompositionLocalProvider(LocalScrollbarStyle provides defaultScrollbarStyle()) {
- when (child) {
- is Root.MainChild -> MainContent(component = child.main, modifier = Modifier.fillMaxSize())
- is Root.DetailsChild -> DetailsContent(component = child.details, modifier = Modifier.fillMaxSize())
- }
- }
- }
- }
- }
- @Composable
- fun Window(
- isModal: Boolean,
- onCloseRequest: () -> Unit,
- title: String,
- content: @Composable WindowScope.() -> Unit,
- ) {
- if (isModal) {
- Dialog(onCloseRequest = onCloseRequest, title = title, content = content)
- } else {
- Window(onCloseRequest = onCloseRequest, title = title, content = content)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement