Skip to content

Commit 6fb63df

Browse files
committed
BREAKING: Compose overhaul makes ViewEnvironment less in your face.
* Introduces `LocalWorkflowEnvironment` and eliminates explicit `ViewEnvironment` parameters from `ScreenComposableFactory.Content`, `ComposeScreen.Content`, etc. It is put in place automatically by the default implementation of `ScreenComposableFactoryFinder`. * Adds optional `CompositionRoot` argument to `ViewEnvironment.withComposeInteropSupport()`. `CompositionRoot` is our existing hook to ensure `CompositionLocal`s (e.g. for UI themes) are put in play above the `@Composable Content()` function invoked for any `Screen`. * Replaces `ViewEnvironment.withCompositionRoot` with `@Composable fun ViewEnvironment.RootScreen`, as our preferred Compose-friendly alternative to `WorkflowLayout` An `Activity` that uses `setContent {}` instead of `setContentView()` can now kick things off like so: ```kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val environment : ViewEnvironment = ViewEnvironment.EMPTY + // ... .withComposeInteropSupport() val rootScreen by RootWorkflow.renderAsState( props = Unit, onOutput = {}, ) setContent { environment.RootScreen(rootScreen) } } ```
1 parent daf192e commit 6fb63df

File tree

26 files changed

+230
-205
lines changed

26 files changed

+230
-205
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ private val viewEnvironment = ViewEnvironment.EMPTY.withComposeInteropSupport()
3030
)
3131
WorkflowRendering(
3232
rendering,
33-
viewEnvironment,
3433
Modifier.border(
3534
shape = RoundedCornerShape(10.dp),
3635
width = 10.dp,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import androidx.compose.runtime.Composable
88
import androidx.compose.ui.Alignment
99
import androidx.compose.ui.Modifier
1010
import androidx.compose.ui.tooling.preview.Preview
11-
import com.squareup.workflow1.ui.ViewEnvironment
1211
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
1312
import com.squareup.workflow1.ui.compose.ComposeScreen
1413
import com.squareup.workflow1.ui.compose.tooling.Preview
@@ -18,7 +17,7 @@ data class HelloComposeScreen(
1817
val message: String,
1918
val onClick: () -> Unit
2019
) : ComposeScreen {
21-
@Composable override fun Content(viewEnvironment: ViewEnvironment) {
20+
@Composable override fun Content() {
2221
Text(
2322
message,
2423
modifier = Modifier

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import com.squareup.workflow1.ui.compose.ScreenComposableFactory
1313
import com.squareup.workflow1.ui.compose.tooling.Preview
1414

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

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import com.squareup.workflow1.ui.ViewRegistry
1818
import com.squareup.workflow1.ui.WorkflowLayout
1919
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2020
import com.squareup.workflow1.ui.compose.withComposeInteropSupport
21-
import com.squareup.workflow1.ui.compose.withCompositionRoot
2221
import com.squareup.workflow1.ui.plus
2322
import com.squareup.workflow1.ui.renderWorkflowIn
2423
import com.squareup.workflow1.ui.withEnvironment
@@ -27,10 +26,9 @@ import kotlinx.coroutines.flow.StateFlow
2726
@OptIn(WorkflowUiExperimentalApi::class)
2827
private val viewEnvironment =
2928
(ViewEnvironment.EMPTY + ViewRegistry(HelloBinding))
30-
.withCompositionRoot { content ->
29+
.withComposeInteropSupport { content ->
3130
MaterialTheme(content = content)
3231
}
33-
.withComposeInteropSupport()
3432

3533
/**
3634
* Demonstrates how to create and display a view factory with

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import com.squareup.workflow1.Sink
77
import com.squareup.workflow1.StatefulWorkflow
88
import com.squareup.workflow1.Workflow
99
import com.squareup.workflow1.WorkflowIdentifier
10-
import com.squareup.workflow1.ui.ViewEnvironment
1110
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
1211
import com.squareup.workflow1.ui.compose.ComposeScreen
1312

@@ -35,17 +34,15 @@ abstract class ComposeWorkflow<in PropsT, out OutputT : Any> :
3534

3635
/**
3736
* Renders [props] by emitting Compose UI. This function will be called to update the UI whenever
38-
* the [props] or [viewEnvironment] change.
37+
* the [props] change.
3938
*
4039
* @param props The data to render.
4140
* @param outputSink A [Sink] that can be used from UI event handlers to send outputs to this
4241
* workflow's parent.
43-
* @param viewEnvironment The [ViewEnvironment] passed down through the `ViewBinding` pipeline.
4442
*/
4543
@Composable abstract fun RenderingContent(
4644
props: PropsT,
4745
outputSink: Sink<OutputT>,
48-
viewEnvironment: ViewEnvironment
4946
)
5047

5148
override fun asStatefulWorkflow(): StatefulWorkflow<PropsT, *, OutputT, ComposeScreen> =
@@ -62,14 +59,12 @@ inline fun <PropsT, OutputT : Any> Workflow.Companion.composed(
6259
crossinline render: @Composable (
6360
props: PropsT,
6461
outputSink: Sink<OutputT>,
65-
environment: ViewEnvironment
6662
) -> Unit
6763
): ComposeWorkflow<PropsT, OutputT> = object : ComposeWorkflow<PropsT, OutputT>() {
6864
@Composable override fun RenderingContent(
6965
props: PropsT,
7066
outputSink: Sink<OutputT>,
71-
viewEnvironment: ViewEnvironment
7267
) {
73-
render(props, outputSink, viewEnvironment)
68+
render(props, outputSink)
7469
}
7570
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import com.squareup.workflow1.Snapshot
1010
import com.squareup.workflow1.StatefulWorkflow
1111
import com.squareup.workflow1.action
1212
import com.squareup.workflow1.contraMap
13-
import com.squareup.workflow1.ui.ViewEnvironment
1413
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
1514
import com.squareup.workflow1.ui.compose.ComposeScreen
1615

@@ -39,12 +38,12 @@ internal class ComposeWorkflowImpl<PropsT, OutputT : Any>(
3938
propsHolder,
4039
sinkHolder,
4140
object : ComposeScreen {
42-
@Composable override fun Content(viewEnvironment: ViewEnvironment) {
41+
@Composable override fun Content() {
4342
// The sink will get set on the first render pass, which must happen before this is first
4443
// composed, so it should never be null.
4544
val sink = sinkHolder.sink!!
4645
// Important: Use the props from the MutableState, _not_ the one passed into render.
47-
workflow.RenderingContent(propsHolder.value, sink, viewEnvironment)
46+
workflow.RenderingContent(propsHolder.value, sink)
4847
}
4948
}
5049
)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import com.squareup.sample.compose.hellocomposeworkflow.HelloComposeWorkflow.Tog
1616
import com.squareup.workflow1.Sink
1717
import com.squareup.workflow1.WorkflowExperimentalRuntime
1818
import com.squareup.workflow1.config.AndroidRuntimeConfigTools
19-
import com.squareup.workflow1.ui.ViewEnvironment
2019
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2120
import com.squareup.workflow1.ui.compose.WorkflowRendering
2221
import com.squareup.workflow1.ui.compose.renderAsState
@@ -34,7 +33,6 @@ object HelloComposeWorkflow : ComposeWorkflow<String, Toggle>() {
3433
@Composable override fun RenderingContent(
3534
props: String,
3635
outputSink: Sink<Toggle>,
37-
viewEnvironment: ViewEnvironment
3836
) {
3937
MaterialTheme {
4038
Text(
@@ -57,5 +55,5 @@ fun HelloComposeWorkflowPreview() {
5755
onOutput = {},
5856
runtimeConfig = AndroidRuntimeConfigTools.getAppWorkflowRuntimeConfig()
5957
)
60-
WorkflowRendering(rendering, ViewEnvironment.EMPTY)
58+
WorkflowRendering(rendering)
6159
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.squareup.workflow1.WorkflowExperimentalRuntime
2222
import com.squareup.workflow1.config.AndroidRuntimeConfigTools
2323
import com.squareup.workflow1.parse
2424
import com.squareup.workflow1.ui.Screen
25-
import com.squareup.workflow1.ui.ViewEnvironment
2625
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2726
import com.squareup.workflow1.ui.compose.ComposeScreen
2827
import com.squareup.workflow1.ui.compose.WorkflowRendering
@@ -60,7 +59,7 @@ fun InlineRenderingWorkflowRendering() {
6059
onOutput = {},
6160
runtimeConfig = AndroidRuntimeConfigTools.getAppWorkflowRuntimeConfig()
6261
)
63-
WorkflowRendering(rendering, ViewEnvironment.EMPTY)
62+
WorkflowRendering(rendering)
6463
}
6564

6665
@Preview(showBackground = true)

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,22 @@ import com.squareup.workflow1.ui.ViewRegistry
1919
import com.squareup.workflow1.ui.WorkflowLayout
2020
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2121
import com.squareup.workflow1.ui.compose.withComposeInteropSupport
22-
import com.squareup.workflow1.ui.compose.withCompositionRoot
2322
import com.squareup.workflow1.ui.plus
2423
import com.squareup.workflow1.ui.renderWorkflowIn
2524
import com.squareup.workflow1.ui.withEnvironment
2625
import kotlinx.coroutines.flow.StateFlow
2726

2827
@OptIn(WorkflowUiExperimentalApi::class)
29-
private val viewRegistry = ViewRegistry(RecursiveViewFactory)
28+
private val viewRegistry = ViewRegistry(RecursiveComposableFactory)
3029

3130
@OptIn(WorkflowUiExperimentalApi::class)
3231
private val viewEnvironment =
3332
(ViewEnvironment.EMPTY + viewRegistry)
34-
.withCompositionRoot { content ->
33+
.withComposeInteropSupport { content ->
3534
CompositionLocalProvider(LocalBackgroundColor provides Color.Green) {
3635
content()
3736
}
3837
}
39-
.withComposeInteropSupport()
4038

4139
@WorkflowUiExperimentalApi
4240
class NestedRenderingsActivity : AppCompatActivity() {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,21 @@ import androidx.compose.ui.tooling.preview.Preview
2424
import com.squareup.sample.compose.R
2525
import com.squareup.sample.compose.nestedrenderings.RecursiveWorkflow.Rendering
2626
import com.squareup.workflow1.ui.Screen
27-
import com.squareup.workflow1.ui.ViewEnvironment
2827
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2928
import com.squareup.workflow1.ui.compose.ScreenComposableFactory
3029
import com.squareup.workflow1.ui.compose.WorkflowRendering
3130
import com.squareup.workflow1.ui.compose.tooling.Preview
3231

3332
/**
34-
* Composition local of [Color] to use as the background color for a [RecursiveViewFactory].
33+
* Composition local of [Color] to use as the background color for a [RecursiveComposableFactory].
3534
*/
3635
val LocalBackgroundColor = compositionLocalOf<Color> { error("No background color specified") }
3736

3837
/**
3938
* A `ViewFactory` that renders [RecursiveWorkflow.Rendering]s.
4039
*/
4140
@OptIn(WorkflowUiExperimentalApi::class)
42-
val RecursiveViewFactory = ScreenComposableFactory<Rendering> { rendering, viewEnvironment ->
41+
val RecursiveComposableFactory = ScreenComposableFactory<Rendering> { rendering ->
4342
// Every child should be drawn with a slightly-darker background color.
4443
val color = LocalBackgroundColor.current
4544
val childColor = remember(color) {
@@ -57,7 +56,6 @@ val RecursiveViewFactory = ScreenComposableFactory<Rendering> { rendering, viewE
5756
CompositionLocalProvider(LocalBackgroundColor provides childColor) {
5857
Children(
5958
rendering.children,
60-
viewEnvironment,
6159
// Pass a weight so that the column fills all the space not occupied by the buttons.
6260
modifier = Modifier.weight(1f, fill = true)
6361
)
@@ -75,7 +73,7 @@ val RecursiveViewFactory = ScreenComposableFactory<Rendering> { rendering, viewE
7573
@Composable
7674
fun RecursiveViewFactoryPreview() {
7775
CompositionLocalProvider(LocalBackgroundColor provides Color.Green) {
78-
RecursiveViewFactory.Preview(
76+
RecursiveComposableFactory.Preview(
7977
Rendering(
8078
children = listOf(
8179
StringRendering("foo"),
@@ -97,7 +95,6 @@ fun RecursiveViewFactoryPreview() {
9795
@Composable
9896
private fun Children(
9997
children: List<Screen>,
100-
viewEnvironment: ViewEnvironment,
10198
modifier: Modifier
10299
) {
103100
Column(
@@ -110,7 +107,6 @@ private fun Children(
110107
childRendering,
111108
// Pass a weight so all children are partitioned evenly within the total column space.
112109
// Without the weight, each child is the full size of the parent.
113-
viewEnvironment,
114110
modifier = Modifier
115111
.weight(1f, fill = true)
116112
.fillMaxWidth()

0 commit comments

Comments
 (0)