Skip to content

Commit 30f85c9

Browse files
authored
Merge pull request #689 from square/ray/simpler-compose-overhaul
Overhaul of Compose support.
2 parents 2aaedf7 + e5b5ba9 commit 30f85c9

File tree

55 files changed

+2582
-351
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2582
-351
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android.useAndroidX=true
88
systemProp.org.gradle.internal.publish.checksums.insecure=true
99

1010
GROUP=com.squareup.workflow1
11-
VERSION_NAME=1.7.0-uiUpdate01-SNAPSHOT
11+
VERSION_NAME=1.7.0-uiUpdate02-SNAPSHOT
1212

1313
POM_DESCRIPTION=Square Workflow
1414

samples/compose-samples/src/main/java/com/squareup/sample/compose/hellocompose/HelloBinding.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import androidx.compose.ui.Alignment
88
import androidx.compose.ui.Modifier
99
import com.squareup.sample.compose.hellocompose.HelloWorkflow.Rendering
1010
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
11-
import com.squareup.workflow1.ui.compose.composeViewFactory
11+
import com.squareup.workflow1.ui.compose.composeScreenViewFactory
1212

1313
@OptIn(WorkflowUiExperimentalApi::class)
14-
val HelloBinding = composeViewFactory<Rendering> { rendering, _ ->
14+
val HelloBinding = composeScreenViewFactory<Rendering> { rendering, _ ->
1515
Text(
1616
rendering.message,
1717
modifier = Modifier

samples/compose-samples/src/main/java/com/squareup/sample/compose/hellocompose/HelloWorkflow.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import com.squareup.workflow1.Snapshot
88
import com.squareup.workflow1.StatefulWorkflow
99
import com.squareup.workflow1.action
1010
import com.squareup.workflow1.parse
11+
import com.squareup.workflow1.ui.Screen
12+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
1113

1214
object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
1315
enum class State {
@@ -20,10 +22,11 @@ object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
2022
}
2123
}
2224

25+
@OptIn(WorkflowUiExperimentalApi::class)
2326
data class Rendering(
2427
val message: String,
2528
val onClick: () -> Unit
26-
)
29+
) : Screen
2730

2831
private val helloAction = action {
2932
state = state.theOtherState()

samples/compose-samples/src/main/java/com/squareup/sample/compose/hellocomposebinding/HelloBinding.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import androidx.compose.ui.Modifier
99
import androidx.compose.ui.tooling.preview.Preview
1010
import com.squareup.sample.compose.hellocomposebinding.HelloWorkflow.Rendering
1111
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
12-
import com.squareup.workflow1.ui.compose.composeViewFactory
12+
import com.squareup.workflow1.ui.compose.composeScreenViewFactory
1313
import com.squareup.workflow1.ui.compose.tooling.Preview
1414

1515
@OptIn(WorkflowUiExperimentalApi::class)
16-
val HelloBinding = composeViewFactory<Rendering> { rendering, _ ->
16+
val HelloBinding = composeScreenViewFactory<Rendering> { rendering, _ ->
1717
Text(
1818
rendering.message,
1919
modifier = Modifier

samples/compose-samples/src/main/java/com/squareup/sample/compose/hellocomposebinding/HelloWorkflow.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import com.squareup.workflow1.Snapshot
88
import com.squareup.workflow1.StatefulWorkflow
99
import com.squareup.workflow1.action
1010
import com.squareup.workflow1.parse
11+
import com.squareup.workflow1.ui.Screen
12+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
1113

1214
object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
1315
enum class State {
@@ -20,10 +22,11 @@ object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
2022
}
2123
}
2224

25+
@OptIn(WorkflowUiExperimentalApi::class)
2326
data class Rendering(
2427
val message: String,
2528
val onClick: () -> Unit
26-
)
29+
) : Screen
2730

2831
private val helloAction = action {
2932
state = state.theOtherState()

samples/compose-samples/src/main/java/com/squareup/sample/compose/hellocomposeworkflow/ComposeWorkflow.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import com.squareup.workflow1.StatefulWorkflow
77
import com.squareup.workflow1.Workflow
88
import com.squareup.workflow1.ui.ViewEnvironment
99
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
10-
import com.squareup.workflow1.ui.compose.ComposeRendering
10+
import com.squareup.workflow1.ui.compose.ComposeScreen
1111

1212
/**
1313
* A stateless [Workflow] that [renders][RenderingContent] itself as a [Composable] function.
14-
* Effectively defines an inline [ComposeRendering].
14+
* Effectively defines an inline [ComposeScreen].
1515
*
1616
* This workflow does not have access to a [RenderContext] since render contexts are only valid
1717
* during render passes, and this workflow's [RenderingContent] method is invoked after the render
@@ -28,8 +28,8 @@ import com.squareup.workflow1.ui.compose.ComposeRendering
2828
* comes up.
2929
*/
3030
@WorkflowUiExperimentalApi
31-
public abstract class ComposeWorkflow<in PropsT, out OutputT : Any> :
32-
Workflow<PropsT, OutputT, ComposeRendering> {
31+
abstract class ComposeWorkflow<in PropsT, out OutputT : Any> :
32+
Workflow<PropsT, OutputT, ComposeScreen> {
3333

3434
/**
3535
* Renders [props] by emitting Compose UI. This function will be called to update the UI whenever
@@ -40,21 +40,21 @@ public abstract class ComposeWorkflow<in PropsT, out OutputT : Any> :
4040
* workflow's parent.
4141
* @param viewEnvironment The [ViewEnvironment] passed down through the `ViewBinding` pipeline.
4242
*/
43-
@Composable public abstract fun RenderingContent(
43+
@Composable abstract fun RenderingContent(
4444
props: PropsT,
4545
outputSink: Sink<OutputT>,
4646
viewEnvironment: ViewEnvironment
4747
)
4848

49-
override fun asStatefulWorkflow(): StatefulWorkflow<PropsT, *, OutputT, ComposeRendering> =
49+
override fun asStatefulWorkflow(): StatefulWorkflow<PropsT, *, OutputT, ComposeScreen> =
5050
ComposeWorkflowImpl(this)
5151
}
5252

5353
/**
5454
* Returns a [ComposeWorkflow] that renders itself using the given [render] function.
5555
*/
5656
@WorkflowUiExperimentalApi
57-
public inline fun <PropsT, OutputT : Any> Workflow.Companion.composed(
57+
inline fun <PropsT, OutputT : Any> Workflow.Companion.composed(
5858
crossinline render: @Composable (
5959
props: PropsT,
6060
outputSink: Sink<OutputT>,

samples/compose-samples/src/main/java/com/squareup/sample/compose/hellocomposeworkflow/ComposeWorkflowImpl.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ import com.squareup.workflow1.action
1212
import com.squareup.workflow1.contraMap
1313
import com.squareup.workflow1.ui.ViewEnvironment
1414
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
15-
import com.squareup.workflow1.ui.compose.ComposeRendering
15+
import com.squareup.workflow1.ui.compose.ComposeScreen
1616

1717
@WorkflowUiExperimentalApi
1818
internal class ComposeWorkflowImpl<PropsT, OutputT : Any>(
1919
private val workflow: ComposeWorkflow<PropsT, OutputT>
20-
) : StatefulWorkflow<PropsT, State<PropsT, OutputT>, OutputT, ComposeRendering>() {
20+
) : StatefulWorkflow<PropsT, State<PropsT, OutputT>, OutputT, ComposeScreen>() {
2121

2222
// This doesn't need to be a @Model, it only gets set once, before the composable ever runs.
2323
class SinkHolder<OutputT>(var sink: Sink<OutputT>? = null)
2424

2525
data class State<PropsT, OutputT>(
2626
val propsHolder: MutableState<PropsT>,
2727
val sinkHolder: SinkHolder<OutputT>,
28-
val rendering: ComposeRendering
28+
val rendering: ComposeScreen
2929
)
3030

3131
override fun initialState(
@@ -38,7 +38,7 @@ internal class ComposeWorkflowImpl<PropsT, OutputT : Any>(
3838
return State(
3939
propsHolder,
4040
sinkHolder,
41-
object : ComposeRendering {
41+
object : ComposeScreen {
4242
@Composable override fun Content(viewEnvironment: ViewEnvironment) {
4343
// The sink will get set on the first render pass, which must happen before this is first
4444
// composed, so it should never be null.
@@ -63,7 +63,7 @@ internal class ComposeWorkflowImpl<PropsT, OutputT : Any>(
6363
renderProps: PropsT,
6464
renderState: State<PropsT, OutputT>,
6565
context: RenderContext
66-
): ComposeRendering {
66+
): ComposeScreen {
6767
// The first render pass needs to cache the sink. The sink is reusable, so we can just pass the
6868
// same one every time.
6969
if (renderState.sinkHolder.sink == null) {

samples/compose-samples/src/main/java/com/squareup/sample/compose/hellocomposeworkflow/HelloWorkflow.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import com.squareup.workflow1.StatefulWorkflow
88
import com.squareup.workflow1.action
99
import com.squareup.workflow1.parse
1010
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
11-
import com.squareup.workflow1.ui.compose.ComposeRendering
11+
import com.squareup.workflow1.ui.compose.ComposeScreen
1212

1313
/**
1414
* The root workflow of this sample. Manges the current toggle state and passes it to
1515
* [HelloComposeWorkflow].
1616
*/
1717
@OptIn(WorkflowUiExperimentalApi::class)
18-
object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, ComposeRendering>() {
18+
object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, ComposeScreen>() {
1919
enum class State {
2020
Hello,
2121
Goodbye;
@@ -40,7 +40,7 @@ object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, ComposeRendering>(
4040
renderProps: Unit,
4141
renderState: State,
4242
context: RenderContext
43-
): ComposeRendering =
43+
): ComposeScreen =
4444
context.renderChild(HelloComposeWorkflow, renderState.name) { helloAction }
4545

4646
override fun snapshotState(state: State): Snapshot = Snapshot.of(if (state == Hello) 1 else 0)

samples/compose-samples/src/main/java/com/squareup/sample/compose/inlinerendering/InlineRenderingWorkflow.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@file:Suppress("DEPRECATION", "FunctionName")
21
@file:OptIn(WorkflowUiExperimentalApi::class)
32

43
package com.squareup.sample.compose.inlinerendering
@@ -20,14 +19,14 @@ import androidx.compose.ui.tooling.preview.Preview
2019
import com.squareup.workflow1.Snapshot
2120
import com.squareup.workflow1.StatefulWorkflow
2221
import com.squareup.workflow1.parse
23-
import com.squareup.workflow1.ui.AndroidViewRendering
22+
import com.squareup.workflow1.ui.AndroidScreen
2423
import com.squareup.workflow1.ui.ViewEnvironment
2524
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
26-
import com.squareup.workflow1.ui.compose.ComposeRendering
25+
import com.squareup.workflow1.ui.compose.ComposeScreen
2726
import com.squareup.workflow1.ui.compose.WorkflowRendering
2827
import com.squareup.workflow1.ui.compose.renderAsState
2928

30-
object InlineRenderingWorkflow : StatefulWorkflow<Unit, Int, Nothing, AndroidViewRendering<*>>() {
29+
object InlineRenderingWorkflow : StatefulWorkflow<Unit, Int, Nothing, AndroidScreen<*>>() {
3130

3231
override fun initialState(
3332
props: Unit,
@@ -38,7 +37,7 @@ object InlineRenderingWorkflow : StatefulWorkflow<Unit, Int, Nothing, AndroidVie
3837
renderProps: Unit,
3938
renderState: Int,
4039
context: RenderContext
41-
): AndroidViewRendering<*> = ComposeRendering {
40+
): AndroidScreen<*> = ComposeScreen {
4241
Box {
4342
Button(onClick = context.eventHandler { state += 1 }) {
4443
Text("Counter: ")

samples/compose-samples/src/main/java/com/squareup/sample/compose/nestedrenderings/LegacyRunner.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import androidx.compose.ui.Modifier
88
import androidx.compose.ui.tooling.preview.Preview
99
import com.squareup.sample.compose.databinding.LegacyViewBinding
1010
import com.squareup.sample.compose.nestedrenderings.RecursiveWorkflow.LegacyRendering
11-
import com.squareup.workflow1.ui.LayoutRunner
12-
import com.squareup.workflow1.ui.LayoutRunner.Companion.bind
11+
import com.squareup.workflow1.ui.ScreenViewFactory
12+
import com.squareup.workflow1.ui.ScreenViewRunner
13+
import com.squareup.workflow1.ui.ScreenViewRunner.Companion.bind
1314
import com.squareup.workflow1.ui.ViewEnvironment
14-
import com.squareup.workflow1.ui.ViewFactory
1515
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
1616
import com.squareup.workflow1.ui.compose.tooling.Preview
1717

1818
/**
19-
* A [LayoutRunner] that renders [LegacyRendering]s using the legacy view framework.
19+
* A [ScreenViewRunner] that renders [LegacyRendering]s using the legacy view framework.
2020
*/
2121
@OptIn(WorkflowUiExperimentalApi::class)
22-
class LegacyRunner(private val binding: LegacyViewBinding) : LayoutRunner<LegacyRendering> {
22+
class LegacyRunner(private val binding: LegacyViewBinding) : ScreenViewRunner<LegacyRendering> {
2323

2424
override fun showRendering(
2525
rendering: LegacyRendering,
@@ -28,7 +28,7 @@ class LegacyRunner(private val binding: LegacyViewBinding) : LayoutRunner<Legacy
2828
binding.stub.update(rendering.rendering, viewEnvironment)
2929
}
3030

31-
companion object : ViewFactory<LegacyRendering> by bind(
31+
companion object : ScreenViewFactory<LegacyRendering> by bind(
3232
LegacyViewBinding::inflate, ::LegacyRunner
3333
)
3434
}
@@ -37,7 +37,7 @@ class LegacyRunner(private val binding: LegacyViewBinding) : LayoutRunner<Legacy
3737
@Preview(widthDp = 200, heightDp = 150, showBackground = true)
3838
@Composable private fun LegacyRunnerPreview() {
3939
LegacyRunner.Preview(
40-
rendering = LegacyRendering("child"),
40+
rendering = LegacyRendering(StringRendering("child")),
4141
placeholderModifier = Modifier.fillMaxSize()
4242
)
4343
}

samples/compose-samples/src/main/java/com/squareup/sample/compose/nestedrenderings/RecursiveViewFactory.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import androidx.compose.ui.res.dimensionResource
2323
import androidx.compose.ui.tooling.preview.Preview
2424
import com.squareup.sample.compose.R
2525
import com.squareup.sample.compose.nestedrenderings.RecursiveWorkflow.Rendering
26+
import com.squareup.workflow1.ui.Screen
2627
import com.squareup.workflow1.ui.ViewEnvironment
2728
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2829
import com.squareup.workflow1.ui.compose.WorkflowRendering
29-
import com.squareup.workflow1.ui.compose.composeViewFactory
30+
import com.squareup.workflow1.ui.compose.composeScreenViewFactory
3031
import com.squareup.workflow1.ui.compose.tooling.Preview
3132

3233
/**
@@ -38,7 +39,7 @@ val LocalBackgroundColor = compositionLocalOf<Color> { error("No background colo
3839
* A `ViewFactory` that renders [RecursiveWorkflow.Rendering]s.
3940
*/
4041
@OptIn(WorkflowUiExperimentalApi::class)
41-
val RecursiveViewFactory = composeViewFactory<Rendering> { rendering, viewEnvironment ->
42+
val RecursiveViewFactory = composeScreenViewFactory<Rendering> { rendering, viewEnvironment ->
4243
// Every child should be drawn with a slightly-darker background color.
4344
val color = LocalBackgroundColor.current
4445
val childColor = remember(color) {
@@ -75,9 +76,9 @@ val RecursiveViewFactory = composeViewFactory<Rendering> { rendering, viewEnviro
7576
RecursiveViewFactory.Preview(
7677
Rendering(
7778
children = listOf(
78-
"foo",
79+
StringRendering("foo"),
7980
Rendering(
80-
children = listOf("bar"),
81+
children = listOf(StringRendering("bar")),
8182
onAddChildClicked = {}, onResetClicked = {}
8283
)
8384
),
@@ -90,7 +91,7 @@ val RecursiveViewFactory = composeViewFactory<Rendering> { rendering, viewEnviro
9091

9192
@OptIn(WorkflowUiExperimentalApi::class)
9293
@Composable private fun Children(
93-
children: List<Any>,
94+
children: List<Screen>,
9495
viewEnvironment: ViewEnvironment,
9596
modifier: Modifier
9697
) {

samples/compose-samples/src/main/java/com/squareup/sample/compose/nestedrenderings/RecursiveWorkflow.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.squareup.workflow1.Snapshot
77
import com.squareup.workflow1.StatefulWorkflow
88
import com.squareup.workflow1.action
99
import com.squareup.workflow1.renderChild
10+
import com.squareup.workflow1.ui.Screen
11+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
1012

1113
/**
1214
* A simple workflow that produces [Rendering]s of zero or more children.
@@ -16,7 +18,8 @@ import com.squareup.workflow1.renderChild
1618
* to force it to go through the legacy view layer. This way this sample both demonstrates pass-
1719
* through Composable renderings as well as adapting in both directions.
1820
*/
19-
object RecursiveWorkflow : StatefulWorkflow<Unit, State, Nothing, Any>() {
21+
@OptIn(WorkflowUiExperimentalApi::class)
22+
object RecursiveWorkflow : StatefulWorkflow<Unit, State, Nothing, Screen>() {
2023

2124
data class State(val children: Int = 0)
2225

@@ -28,15 +31,15 @@ object RecursiveWorkflow : StatefulWorkflow<Unit, State, Nothing, Any>() {
2831
* @param onResetClicked Resets [children] to an empty list.
2932
*/
3033
data class Rendering(
31-
val children: List<Any>,
34+
val children: List<Screen>,
3235
val onAddChildClicked: () -> Unit,
3336
val onResetClicked: () -> Unit
34-
)
37+
) : Screen
3538

3639
/**
3740
* Wrapper around a [Rendering] that will be implemented using a legacy view.
3841
*/
39-
data class LegacyRendering(val rendering: Any)
42+
data class LegacyRendering(val rendering: Screen) : Screen
4043

4144
override fun initialState(
4245
props: Unit,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.squareup.sample.compose.nestedrenderings
2+
3+
import com.squareup.workflow1.ui.Screen
4+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
5+
6+
@OptIn(WorkflowUiExperimentalApi::class)
7+
data class StringRendering(val value: String) : Screen

0 commit comments

Comments
 (0)