|
1 | 1 | package com.squareup.workflow1.compose
|
2 | 2 |
|
3 | 3 | import androidx.compose.runtime.Composable
|
| 4 | +import androidx.compose.runtime.MutableState |
4 | 5 | import androidx.compose.runtime.Stable
|
| 6 | +import androidx.compose.runtime.collectAsState |
| 7 | +import androidx.compose.runtime.getValue |
| 8 | +import androidx.compose.runtime.mutableIntStateOf |
| 9 | +import androidx.compose.runtime.mutableStateOf |
5 | 10 | import androidx.compose.runtime.remember
|
| 11 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 12 | +import androidx.compose.runtime.setValue |
6 | 13 | import com.squareup.workflow1.BaseRenderContext
|
| 14 | +import com.squareup.workflow1.Snapshot |
7 | 15 | import com.squareup.workflow1.StatefulWorkflow
|
8 | 16 | import com.squareup.workflow1.Workflow
|
9 | 17 | import com.squareup.workflow1.WorkflowAction
|
| 18 | +import com.squareup.workflow1.WorkflowExperimentalApi |
| 19 | +import com.squareup.workflow1.compose.SampleComposeWorkflow.Rendering |
| 20 | +import kotlinx.coroutines.flow.StateFlow |
10 | 21 |
|
11 | 22 | /**
|
12 |
| - * A [Workflow]-like interface that participates in a workflow tree via its [Rendering] composable. |
| 23 | + * A [Workflow]-like interface that participates in a workflow tree via its [produceRendering] |
| 24 | + * composable. See the docs on [produceRendering] for more information on writing composable |
| 25 | + * workflows. |
| 26 | + * |
| 27 | + * @sample SampleComposeWorkflow |
13 | 28 | */
|
| 29 | +@WorkflowExperimentalApi |
14 | 30 | @Stable
|
15 |
| -public interface ComposeWorkflow< |
| 31 | +public abstract class ComposeWorkflow< |
16 | 32 | in PropsT,
|
17 | 33 | out OutputT,
|
18 | 34 | out RenderingT
|
19 |
| - > { |
| 35 | + > : Workflow<PropsT, OutputT, RenderingT> { |
20 | 36 |
|
21 | 37 | /**
|
22 | 38 | * The main composable of this workflow that consumes some [props] from its parent and may emit
|
23 |
| - * an output via [emitOutput]. |
| 39 | + * an output via [emitOutput]. Equivalent to [StatefulWorkflow.render]. |
24 | 40 | *
|
25 |
| - * Equivalent to [StatefulWorkflow.render]. |
| 41 | + * To render child workflows (composable or otherwise) from this method, call [renderWorkflow]. |
| 42 | + * |
| 43 | + * Any compose snapshot state that is read in this method or any methods it calls, that is later |
| 44 | + * changed, will trigger a re-render of the workflow tree. See |
| 45 | + * [BaseRenderContext.renderComposable] for more details on how composition is tied to the |
| 46 | + * workflow lifecycle. |
| 47 | + * |
| 48 | + * To save state when the workflow tree is restored, use [rememberSaveable]. This is equivalent |
| 49 | + * to implementing [StatefulWorkflow.snapshotState]. |
| 50 | + * |
| 51 | + * @param props The [PropsT] value passed in from the parent workflow. |
| 52 | + * @param emitOutput A function that can be called to emit an [OutputT] value to the parent |
| 53 | + * workflow. Calling this method is analogous to sending an action to |
| 54 | + * [BaseRenderContext.actionSink] that calls |
| 55 | + * [setOutput][com.squareup.workflow1.WorkflowAction.Updater.setOutput]. If this function is |
| 56 | + * called from the `onOutput` callback of a [renderWorkflow], then it is equivalent to returning |
| 57 | + * an action from [BaseRenderContext.renderChild]'s `handler` parameter. |
| 58 | + * |
| 59 | + * @sample SampleComposeWorkflow.produceRendering |
26 | 60 | */
|
27 | 61 | @WorkflowComposable
|
28 | 62 | @Composable
|
29 |
| - fun Rendering( |
| 63 | + protected abstract fun produceRendering( |
30 | 64 | props: PropsT,
|
31 | 65 | emitOutput: (OutputT) -> Unit
|
32 | 66 | ): RenderingT
|
33 |
| -} |
34 | 67 |
|
35 |
| -fun < |
36 |
| - PropsT, StateT, OutputT, |
37 |
| - ChildPropsT, ChildOutputT, ChildRenderingT |
38 |
| - > BaseRenderContext<PropsT, StateT, OutputT>.renderChild( |
39 |
| - child: ComposeWorkflow<ChildPropsT, ChildOutputT, ChildRenderingT>, |
40 |
| - props: ChildPropsT, |
41 |
| - key: String = "", |
42 |
| - handler: (ChildOutputT) -> WorkflowAction<PropsT, StateT, OutputT> |
43 |
| -): ChildRenderingT = renderComposable(key = key) { |
44 |
| - // Explicitly remember the output function since we know that actionSink is stable even though |
45 |
| - // Compose might not know that. |
46 |
| - val emitOutput: (ChildOutputT) -> Unit = remember(actionSink) { |
47 |
| - { output -> |
48 |
| - val action = handler(output) |
49 |
| - actionSink.send(action) |
| 68 | + /** |
| 69 | + * Render this workflow as a child of another [WorkflowComposable], ensuring that the workflow's |
| 70 | + * [produceRendering] method is a separate recompose scope from the caller. |
| 71 | + */ |
| 72 | + @Composable |
| 73 | + internal fun renderWithRecomposeBoundary( |
| 74 | + props: PropsT, |
| 75 | + onOutput: ((OutputT) -> Unit)? |
| 76 | + ): RenderingT { |
| 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 |
| 79 | + // value actually changed. |
| 80 | + val renderingState = remember { mutableStateOf<RenderingT?>(null) } |
| 81 | + RecomposeScopeIsolator( |
| 82 | + props = props, |
| 83 | + onOutput = onOutput, |
| 84 | + result = renderingState |
| 85 | + ) |
| 86 | + |
| 87 | + // The value is guaranteed to have been set at least once by RecomposeScopeIsolator so this cast |
| 88 | + // will never fail. Note we can't use !! since RenderingT itself might nullable, so null is |
| 89 | + // still a potentially valid rendering value. |
| 90 | + @Suppress("UNCHECKED_CAST") |
| 91 | + return renderingState.value as RenderingT |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Creates an isolated recompose scope that separates a non-restartable caller ([render]) from |
| 96 | + * a non-restartable function call ([produceRendering]). This is accomplished simply by this |
| 97 | + * function having a [Unit] return type and being not inline. |
| 98 | + * |
| 99 | + * **It MUST have a [Unit] return type to do its job.** |
| 100 | + */ |
| 101 | + @Composable |
| 102 | + private fun RecomposeScopeIsolator( |
| 103 | + props: PropsT, |
| 104 | + onOutput: ((OutputT) -> Unit)?, |
| 105 | + result: MutableState<RenderingT?>, |
| 106 | + ) { |
| 107 | + result.value = produceRendering(props, onOutput ?: {}) |
| 108 | + } |
| 109 | + |
| 110 | + private var statefulImplCache: ComposeWorkflowWrapper? = null |
| 111 | + final override fun asStatefulWorkflow(): StatefulWorkflow<PropsT, *, OutputT, RenderingT> = |
| 112 | + statefulImplCache ?: ComposeWorkflowWrapper().also { statefulImplCache = it } |
| 113 | + |
| 114 | + /** |
| 115 | + * Exposes this [ComposeWorkflow] as a [StatefulWorkflow]. |
| 116 | + */ |
| 117 | + private inner class ComposeWorkflowWrapper : |
| 118 | + StatefulWorkflow<PropsT, Unit, OutputT, RenderingT>() { |
| 119 | + |
| 120 | + override fun initialState( |
| 121 | + props: PropsT, |
| 122 | + snapshot: Snapshot? |
| 123 | + ) { |
| 124 | + // Noop |
| 125 | + } |
| 126 | + |
| 127 | + override fun render( |
| 128 | + renderProps: PropsT, |
| 129 | + renderState: Unit, |
| 130 | + context: RenderContext |
| 131 | + ): RenderingT = context.renderComposable { |
| 132 | + // Explicitly remember the output function since we know that actionSink is stable even though |
| 133 | + // Compose might not know that. |
| 134 | + val emitOutput: (OutputT) -> Unit = remember(context.actionSink) { |
| 135 | + { output -> context.actionSink.send(OutputAction(output)) } |
| 136 | + } |
| 137 | + |
| 138 | + // Since we're composing directly from renderComposable, we don't need to isolate the |
| 139 | + // recompose boundary again. This root composable is already a recompose boundary, and we |
| 140 | + // don't need to create a redundant rendering state holder. |
| 141 | + return@renderComposable produceRendering( |
| 142 | + props = renderProps, |
| 143 | + emitOutput = emitOutput |
| 144 | + ) |
50 | 145 | }
|
| 146 | + |
| 147 | + override fun snapshotState(state: Unit): Snapshot? = null |
| 148 | + |
| 149 | + private inner class OutputAction( |
| 150 | + private val output: OutputT |
| 151 | + ) : WorkflowAction<PropsT, Unit, OutputT>() { |
| 152 | + override fun Updater.apply() { |
| 153 | + setOutput(output) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +@OptIn(WorkflowExperimentalApi::class) |
| 160 | +private class SampleComposeWorkflow |
| 161 | +// In real code, this constructor would probably be injected by Dagger or something. |
| 162 | +constructor( |
| 163 | + private val injectedService: Service, |
| 164 | + private val child: Workflow<String, String, String> |
| 165 | +) : ComposeWorkflow< |
| 166 | + /* PropsT */ String, |
| 167 | + /* OutputT */ String, |
| 168 | + /* RenderingT */ Rendering |
| 169 | + >() { |
| 170 | + |
| 171 | + // In real code, this would not be defined in the workflow itself but somewhere else in the |
| 172 | + // codebase. |
| 173 | + interface Service { |
| 174 | + val values: StateFlow<String> |
51 | 175 | }
|
52 |
| - child.Rendering( |
53 |
| - props = props, |
54 |
| - emitOutput = emitOutput |
| 176 | + |
| 177 | + data class Rendering( |
| 178 | + val label: String, |
| 179 | + val onClick: () -> Unit |
55 | 180 | )
|
| 181 | + |
| 182 | + @Composable |
| 183 | + override fun produceRendering( |
| 184 | + props: String, |
| 185 | + emitOutput: (String) -> Unit |
| 186 | + ): Rendering { |
| 187 | + // ComposeWorkflows use native compose idioms to manage state, including saving state to be |
| 188 | + // restored later. |
| 189 | + var clickCount by rememberSaveable { mutableIntStateOf(0) } |
| 190 | + |
| 191 | + // They also use native compose idioms to work with Flows and perform effects. |
| 192 | + val serviceValue by injectedService.values.collectAsState() |
| 193 | + |
| 194 | + // And they can render child workflows, just like traditional workflows. This is equivalent to |
| 195 | + // calling BaseRenderContext.renderChild(). |
| 196 | + // Note that there's no explicit key: the child key is tied to where it's called in the |
| 197 | + // composition, the same way other composable state is keyed. |
| 198 | + val childRendering = renderWorkflow( |
| 199 | + workflow = child, |
| 200 | + props = "child props", |
| 201 | + // This is equivalent to the handler parameter on renderChild(). |
| 202 | + onOutput = { |
| 203 | + emitOutput("child emitted output: $it") |
| 204 | + } |
| 205 | + ) |
| 206 | + |
| 207 | + return Rendering( |
| 208 | + // Reading clickCount and serviceValue here mean that when those values are changed, it will |
| 209 | + // trigger a render pass in the hosting workflow tree, which will recompose this method. |
| 210 | + label = "props=$props, " + |
| 211 | + "clickCount=$clickCount, " + |
| 212 | + "serviceValue=$serviceValue, " + |
| 213 | + "childRendering=$childRendering", |
| 214 | + onClick = { |
| 215 | + // Instead of using WorkflowAction's state property, you can just update snapshot state |
| 216 | + // objects directly. |
| 217 | + clickCount++ |
| 218 | + |
| 219 | + // This is equivalent to calling setOutput from a WorkflowAction. |
| 220 | + emitOutput("clicked!") |
| 221 | + } |
| 222 | + ) |
| 223 | + } |
56 | 224 | }
|
0 commit comments