|
| 1 | +package com.squareup.workflow1.compose |
| 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 com.squareup.workflow1.BaseRenderContext |
| 8 | +import com.squareup.workflow1.Workflow |
| 9 | + |
| 10 | +/** |
| 11 | + * Renders a child [Workflow] from any [WorkflowComposable] (e.g. a [ComposeWorkflow.Rendering] or |
| 12 | + * [BaseRenderContext.renderComposable]) and returns its rendering. |
| 13 | + * |
| 14 | + * @param onOutput An optional function that, if non-null, will be called when the child emits an |
| 15 | + * output. If null, the child's outputs will be ignored. |
| 16 | + */ |
| 17 | +@WorkflowComposable |
| 18 | +@Composable |
| 19 | +fun <ChildPropsT, ChildOutputT, ChildRenderingT> renderChild( |
| 20 | + workflow: Workflow<ChildPropsT, ChildOutputT, ChildRenderingT>, |
| 21 | + props: ChildPropsT, |
| 22 | + onOutput: ((ChildOutputT) -> Unit)? |
| 23 | +): ChildRenderingT { |
| 24 | + val host = LocalWorkflowCompositionHost.current |
| 25 | + return host.renderChild(workflow, props, onOutput) |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Renders a child [Workflow] from any [WorkflowComposable] (e.g. a [ComposeWorkflow.Rendering] or |
| 30 | + * [BaseRenderContext.renderComposable]) and returns its rendering. |
| 31 | + * |
| 32 | + * @param onOutput An optional function that, if non-null, will be called when the child emits an |
| 33 | + * output. If null, the child's outputs will be ignored. |
| 34 | + */ |
| 35 | +@WorkflowComposable |
| 36 | +@Composable |
| 37 | +inline fun <ChildPropsT, ChildRenderingT> renderChild( |
| 38 | + workflow: Workflow<ChildPropsT, Nothing, ChildRenderingT>, |
| 39 | + props: ChildPropsT, |
| 40 | +): ChildRenderingT = renderChild(workflow, props, onOutput = null) |
| 41 | + |
| 42 | +/** |
| 43 | + * Renders a child [Workflow] from any [WorkflowComposable] (e.g. a [ComposeWorkflow.Rendering] or |
| 44 | + * [BaseRenderContext.renderComposable]) and returns its rendering. |
| 45 | + * |
| 46 | + * @param onOutput An optional function that, if non-null, will be called when the child emits an |
| 47 | + * output. If null, the child's outputs will be ignored. |
| 48 | + */ |
| 49 | +@WorkflowComposable |
| 50 | +@Composable |
| 51 | +inline fun <ChildOutputT, ChildRenderingT> renderChild( |
| 52 | + workflow: Workflow<Unit, ChildOutputT, ChildRenderingT>, |
| 53 | + noinline onOutput: ((ChildOutputT) -> Unit)? |
| 54 | +): ChildRenderingT = renderChild(workflow, props = Unit, onOutput) |
| 55 | + |
| 56 | +/** |
| 57 | + * Renders a child [Workflow] from any [WorkflowComposable] (e.g. a [ComposeWorkflow.Rendering] or |
| 58 | + * [BaseRenderContext.renderComposable]) and returns its rendering. |
| 59 | + * |
| 60 | + * @param onOutput An optional function that, if non-null, will be called when the child emits an |
| 61 | + * output. If null, the child's outputs will be ignored. |
| 62 | + */ |
| 63 | +@WorkflowComposable |
| 64 | +@Composable |
| 65 | +inline fun <ChildRenderingT> renderChild( |
| 66 | + workflow: Workflow<Unit, Nothing, ChildRenderingT>, |
| 67 | +): ChildRenderingT = renderChild(workflow, Unit, onOutput = null) |
| 68 | + |
| 69 | +@WorkflowComposable |
| 70 | +@Composable |
| 71 | +fun <ChildPropsT, ChildOutputT, ChildRenderingT> renderChild( |
| 72 | + workflow: ComposeWorkflow<ChildPropsT, ChildOutputT, ChildRenderingT>, |
| 73 | + props: ChildPropsT, |
| 74 | + handler: ((ChildOutputT) -> Unit)? |
| 75 | +): ChildRenderingT { |
| 76 | + val childRendering = remember { mutableStateOf<ChildRenderingT?>(null) } |
| 77 | + // Since this function returns a value, it can't restart without also restarting its parent. |
| 78 | + // IsolateRecomposeScope allows the subtree to restart and only restarts us if the rendering value |
| 79 | + // actually changed. |
| 80 | + RecomposeScopeIsolator( |
| 81 | + child = workflow, |
| 82 | + props = props, |
| 83 | + handler = handler, |
| 84 | + result = childRendering |
| 85 | + ) |
| 86 | + @Suppress("UNCHECKED_CAST") |
| 87 | + return childRendering.value as ChildRenderingT |
| 88 | +} |
| 89 | + |
| 90 | +@Composable |
| 91 | +private fun <PropsT, OutputT, RenderingT> RecomposeScopeIsolator( |
| 92 | + child: ComposeWorkflow<PropsT, OutputT, RenderingT>, |
| 93 | + props: PropsT, |
| 94 | + handler: ((OutputT) -> Unit)?, |
| 95 | + result: MutableState<RenderingT>, |
| 96 | +) { |
| 97 | + result.value = child.Rendering(props, handler ?: {}) |
| 98 | +} |
0 commit comments