|
| 1 | +package com.squareup.workflow1 |
| 2 | + |
| 3 | +import androidx.compose.runtime.Composable |
| 4 | +import androidx.compose.runtime.MutableState |
| 5 | +import androidx.compose.runtime.mutableStateOf |
| 6 | +import androidx.compose.runtime.remember |
| 7 | +import androidx.compose.runtime.staticCompositionLocalOf |
| 8 | + |
| 9 | +internal val LocalWorkflowComposableRenderer = |
| 10 | + staticCompositionLocalOf<WorkflowComposableRenderer> { error("No renderer") } |
| 11 | + |
| 12 | +internal interface WorkflowComposableRenderer { |
| 13 | + @Composable |
| 14 | + fun <ChildPropsT, ChildOutputT, ChildRenderingT> Child( |
| 15 | + workflow: Workflow<ChildPropsT, ChildOutputT, ChildRenderingT>, |
| 16 | + props: ChildPropsT, |
| 17 | + onOutput: ((ChildOutputT) -> Unit)? |
| 18 | + ): ChildRenderingT |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Renders a child [Workflow] from any [WorkflowComposable] (e.g. a [ComposeWorkflow.Rendering] or |
| 23 | + * [BaseRenderContext.renderComposable]) and returns its rendering. |
| 24 | + * |
| 25 | + * @param handler An optional function that, if non-null, will be called when the child emits an |
| 26 | + * output. If null, the child's outputs will be ignored. |
| 27 | + */ |
| 28 | +@WorkflowComposable |
| 29 | +@Composable |
| 30 | +fun <ChildPropsT, ChildOutputT, ChildRenderingT> Child( |
| 31 | + workflow: Workflow<ChildPropsT, ChildOutputT, ChildRenderingT>, |
| 32 | + props: ChildPropsT, |
| 33 | + onOutput: ((ChildOutputT) -> Unit)? |
| 34 | +): ChildRenderingT { |
| 35 | + val renderer = LocalWorkflowComposableRenderer.current |
| 36 | + return renderer.Child(workflow, props, onOutput) |
| 37 | +} |
| 38 | + |
| 39 | +@WorkflowComposable |
| 40 | +@Composable |
| 41 | +fun <ChildPropsT, ChildOutputT, ChildRenderingT> Child( |
| 42 | + workflow: ComposeWorkflow<ChildPropsT, ChildOutputT, ChildRenderingT>, |
| 43 | + props: ChildPropsT, |
| 44 | + handler: ((ChildOutputT) -> Unit)? |
| 45 | +): ChildRenderingT { |
| 46 | + val childRendering = remember { mutableStateOf<ChildRenderingT?>(null) } |
| 47 | + // Since this function returns a value, it can't restart without also restarting its parent. |
| 48 | + // IsolateRecomposeScope allows the subtree to restart and only restarts us if the rendering value |
| 49 | + // actually changed. |
| 50 | + IsolateRecomposeScope( |
| 51 | + child = workflow, |
| 52 | + props = props, |
| 53 | + handler = handler, |
| 54 | + result = childRendering |
| 55 | + ) |
| 56 | + @Suppress("UNCHECKED_CAST") |
| 57 | + return childRendering.value as ChildRenderingT |
| 58 | +} |
| 59 | + |
| 60 | +@Composable |
| 61 | +private fun <PropsT, OutputT, RenderingT> IsolateRecomposeScope( |
| 62 | + child: ComposeWorkflow<PropsT, OutputT, RenderingT>, |
| 63 | + props: PropsT, |
| 64 | + handler: ((OutputT) -> Unit)?, |
| 65 | + result: MutableState<RenderingT>, |
| 66 | +) { |
| 67 | + result.value = child.Rendering(props, handler ?: {}) |
| 68 | +} |
0 commit comments