Skip to content
This repository was archived by the owner on Feb 5, 2021. It is now read-only.

Commit e39b9ff

Browse files
Rename showRendering to WorkflowRendering and make it not an extension function.
Fixes #21, see that issue for rationale.
1 parent dc194c3 commit e39b9ff

File tree

13 files changed

+44
-82
lines changed

13 files changed

+44
-82
lines changed

compose-tooling/src/androidTest/java/com/squareup/workflow/ui/compose/tooling/PreviewComposeWorkflowTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ import androidx.ui.test.findByText
3131
import androidx.ui.tooling.preview.Preview
3232
import androidx.ui.unit.dp
3333
import com.squareup.workflow.Workflow
34-
import com.squareup.workflow.ui.compose.composed
3534
import com.squareup.workflow.ui.ViewEnvironmentKey
36-
import com.squareup.workflow.ui.compose.showRendering
35+
import com.squareup.workflow.ui.compose.WorkflowRendering
36+
import com.squareup.workflow.ui.compose.composed
3737
import org.junit.Rule
3838
import org.junit.Test
3939
import org.junit.runner.RunWith
@@ -98,7 +98,7 @@ class PreviewComposeWorkflowTest {
9898
Column {
9999
Text(props.first)
100100
Semantics(container = true, mergeAllDescendants = true) {
101-
environment.showRendering(rendering = props.second)
101+
WorkflowRendering(props.second, environment)
102102
}
103103
}
104104
}
@@ -111,11 +111,11 @@ class PreviewComposeWorkflowTest {
111111
Workflow.composed<Triple<String, String, String>, Nothing> { props, _, environment ->
112112
Column {
113113
Semantics(container = true) {
114-
environment.showRendering(rendering = props.first)
114+
WorkflowRendering(rendering = props.first, viewEnvironment = environment)
115115
}
116116
Text(props.second)
117117
Semantics(container = true) {
118-
environment.showRendering(rendering = props.third)
118+
WorkflowRendering(rendering = props.third, viewEnvironment = environment)
119119
}
120120
}
121121
}

compose-tooling/src/androidTest/java/com/squareup/workflow/ui/compose/tooling/PreviewViewFactoryTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import androidx.ui.test.findByText
3131
import androidx.ui.tooling.preview.Preview
3232
import androidx.ui.unit.dp
3333
import com.squareup.workflow.ui.ViewEnvironmentKey
34+
import com.squareup.workflow.ui.compose.WorkflowRendering
3435
import com.squareup.workflow.ui.compose.bindCompose
35-
import com.squareup.workflow.ui.compose.showRendering
3636
import org.junit.Rule
3737
import org.junit.Test
3838
import org.junit.runner.RunWith
@@ -103,7 +103,7 @@ class PreviewViewFactoryTest {
103103
Column {
104104
Text(rendering.first)
105105
Semantics(container = true, mergeAllDescendants = true) {
106-
environment.showRendering(rendering = rendering.second)
106+
WorkflowRendering(rendering.second, environment)
107107
}
108108
}
109109
}
@@ -116,11 +116,11 @@ class PreviewViewFactoryTest {
116116
bindCompose<Triple<String, String, String>> { rendering, environment ->
117117
Column {
118118
Semantics(container = true) {
119-
environment.showRendering(rendering = rendering.first)
119+
WorkflowRendering(rendering.first, environment)
120120
}
121121
Text(rendering.second)
122122
Semantics(container = true) {
123-
environment.showRendering(rendering = rendering.third)
123+
WorkflowRendering(rendering.third, environment)
124124
}
125125
}
126126
}
@@ -139,7 +139,7 @@ class PreviewViewFactoryTest {
139139
Text(rendering.text)
140140
rendering.child?.let { child ->
141141
Semantics(container = true) {
142-
environment.showRendering(rendering = child)
142+
WorkflowRendering(rendering = child, viewEnvironment = environment)
143143
}
144144
}
145145
}

compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/PreviewViewEnvironment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import kotlin.reflect.KClass
5050

5151
/**
5252
* A [ViewRegistry] that uses [mainFactory] for rendering [RenderingT]s, and [placeholderFactory]
53-
* for all other [showRendering][com.squareup.workflow.ui.compose.showRendering] calls.
53+
* for all other [WorkflowRendering][com.squareup.workflow.ui.compose.WorkflowRendering] calls.
5454
*/
5555
@Immutable
5656
private class PreviewViewRegistry<RenderingT : Any>(

compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/ViewFactories.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import androidx.ui.core.Modifier
2222
import com.squareup.workflow.ui.ViewEnvironment
2323
import com.squareup.workflow.ui.ViewFactory
2424
import com.squareup.workflow.ui.ViewRegistry
25-
import com.squareup.workflow.ui.compose.showRendering
25+
import com.squareup.workflow.ui.compose.WorkflowRendering
2626

2727
/**
2828
* Draws this [ViewFactory] using a special preview [ViewRegistry].
@@ -48,5 +48,5 @@ import com.squareup.workflow.ui.compose.showRendering
4848
) {
4949
val previewEnvironment =
5050
previewViewEnvironment(placeholderModifier, viewEnvironmentUpdater, mainFactory = this)
51-
previewEnvironment.showRendering(rendering, modifier)
51+
WorkflowRendering(rendering, previewEnvironment, modifier)
5252
}

core-compose/api/core-compose.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public final class com/squareup/workflow/ui/compose/ComposeWorkflowKt {
4444
}
4545

4646
public final class com/squareup/workflow/ui/compose/ViewEnvironmentsKt {
47-
public static final fun showRendering (Lcom/squareup/workflow/ui/ViewEnvironment;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/compose/Composer;)V
48-
public static synthetic fun showRendering$default (Lcom/squareup/workflow/ui/ViewEnvironment;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/compose/Composer;ILjava/lang/Object;)V
47+
public static final fun WorkflowRendering (Ljava/lang/Object;Lcom/squareup/workflow/ui/ViewEnvironment;Landroidx/ui/core/Modifier;Landroidx/compose/Composer;)V
48+
public static synthetic fun WorkflowRendering$default (Ljava/lang/Object;Lcom/squareup/workflow/ui/ViewEnvironment;Landroidx/ui/core/Modifier;Landroidx/compose/Composer;ILjava/lang/Object;)V
4949
}
5050

5151
public final class com/squareup/workflow/ui/compose/WorkflowContainerKt {

core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/internal/ViewFactoriesTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import androidx.ui.test.findByText
2424
import com.squareup.workflow.ui.ViewEnvironment
2525
import com.squareup.workflow.ui.ViewRegistry
2626
import com.squareup.workflow.ui.compose.bindCompose
27-
import com.squareup.workflow.ui.compose.showRendering
27+
import com.squareup.workflow.ui.compose.WorkflowRendering
2828
import com.squareup.workflow.ui.compose.withComposeViewFactoryRoot
2929
import org.junit.Rule
3030
import org.junit.Test
@@ -35,7 +35,7 @@ class ViewFactoriesTest {
3535

3636
@Rule @JvmField val composeRule = createComposeRule()
3737

38-
@Test fun showRendering_wrapsFactoryWithRoot_whenAlreadyInComposition() {
38+
@Test fun WorkflowRendering_wrapsFactoryWithRoot_whenAlreadyInComposition() {
3939
val viewEnvironment = ViewEnvironment(ViewRegistry(TestFactory))
4040
.withComposeViewFactoryRoot { content ->
4141
Column {
@@ -45,7 +45,7 @@ class ViewFactoriesTest {
4545
}
4646

4747
composeRule.setContent {
48-
viewEnvironment.showRendering(TestRendering("two"))
48+
WorkflowRendering(TestRendering("two"), viewEnvironment)
4949
}
5050

5151
findByText("one\ntwo").assertIsDisplayed()

core-compose/src/main/java/com/squareup/workflow/ui/compose/ComposeViewFactory.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ import kotlin.reflect.KClass
6464
* renderings using the [ViewRegistry][com.squareup.workflow.ui.ViewRegistry].
6565
*
6666
* View factories defined using this function may also show nested renderings. Doing so is as simple
67-
* as calling [ViewEnvironment.showRendering] and passing in the nested rendering. See the kdoc on
68-
* that function for an example.
67+
* as calling [WorkflowRendering] and passing in the nested rendering. See the kdoc on that function
68+
* for an example.
6969
*
7070
* Nested renderings will have access to any ambients defined in outer composable, even if there are
7171
* legacy views in between them, as long as the [ViewEnvironment] is propagated continuously between

core-compose/src/main/java/com/squareup/workflow/ui/compose/ViewEnvironments.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import androidx.compose.remember
2020
import androidx.ui.core.Modifier
2121
import com.squareup.workflow.ui.ViewEnvironment
2222
import com.squareup.workflow.ui.ViewRegistry
23-
import com.squareup.workflow.ui.compose.internal.showRendering
23+
import com.squareup.workflow.ui.compose.internal.WorkflowRendering
2424

2525
/**
2626
* Renders [rendering] into the composition using this [ViewEnvironment]'s
@@ -40,7 +40,7 @@ import com.squareup.workflow.ui.compose.internal.showRendering
4040
*
4141
* val FramedContainerViewFactory = bindCompose<FramedRendering> { rendering, environment ->
4242
* Surface(border = Border(rendering.borderColor, 8.dp)) {
43-
* environment.showRendering(rendering.child)
43+
* WorkflowRendering(rendering.child, environment)
4444
* }
4545
* }
4646
* ```
@@ -52,10 +52,15 @@ import com.squareup.workflow.ui.compose.internal.showRendering
5252
*
5353
* @throws IllegalArgumentException if no factory can be found for [rendering]'s type.
5454
*/
55-
@Composable fun ViewEnvironment.showRendering(
55+
@Composable fun WorkflowRendering(
5656
rendering: Any,
57+
viewEnvironment: ViewEnvironment,
5758
modifier: Modifier = Modifier
5859
) {
59-
val viewRegistry = remember(this) { this[ViewRegistry] }
60-
viewRegistry.showRendering(rendering, this, modifier)
60+
val viewRegistry = remember(viewEnvironment) { viewEnvironment[ViewRegistry] }
61+
val renderingType = rendering::class
62+
val viewFactory = remember(viewRegistry, renderingType) {
63+
viewRegistry.getFactoryFor(renderingType)
64+
}
65+
WorkflowRendering(rendering, viewFactory, viewEnvironment, modifier)
6166
}

core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ParentComposition.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import com.squareup.workflow.ui.ViewEnvironmentKey
3030
* Holds a [CompositionReference] and that can be passed to [setOrSubcomposeContent] to create a
3131
* composition that is a child of another composition. Subcompositions get ambients and other
3232
* compose context from their parent, and propagate invalidations, which allows ambients provided
33-
* around a [showRendering] call to be read by nested Compose-based view factories.
33+
* around a [WorkflowRendering] call to be read by nested Compose-based view factories.
3434
*
35-
* When [showRendering] is called, it will store an instance of this class in the [ViewEnvironment].
35+
* When [WorkflowRendering] is called, it will store an instance of this class in the [ViewEnvironment].
3636
* [ComposeViewFactory] pulls the reference out of the environment and uses it to link its
3737
* composition to the outer one.
3838
*/

core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ViewFactories.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,23 @@ import com.squareup.workflow.ui.compose.ComposeViewFactory
3535
import com.squareup.workflow.ui.showRendering
3636

3737
/**
38-
* Renders [rendering] into the composition using the `ViewRegistry` from the [ViewEnvironment] to
39-
* determine how to draw it.
38+
* Renders [rendering] into the composition using [viewFactory].
4039
*
41-
* To display a nested rendering from a [Composable view binding][bindCompose], use
42-
* [ViewEnvironment.showRendering].
40+
* To display a nested rendering from a [Composable view binding][bindCompose], use the overload
41+
* without a [ViewFactory] parameter.
4342
*
4443
* *Note: [rendering] must be the same type as this [ViewFactory], even though the type system does
4544
* not enforce this constraint. This is due to a Compose compiler bug tracked
4645
* [here](https://issuetracker.google.com/issues/156527332).
4746
*
48-
* @see ViewEnvironment.showRendering
49-
* @see com.squareup.workflow.ui.ViewRegistry.showRendering
47+
* @see com.squareup.workflow.ui.compose.WorkflowRendering
5048
*/
51-
// TODO(https://issuetracker.google.com/issues/156527332) Should be ViewFactory<RenderingT>
52-
@Composable internal fun <RenderingT : Any> ViewFactory<Any>.showRendering(
49+
@Composable internal fun <RenderingT : Any> WorkflowRendering(
5350
rendering: RenderingT,
51+
viewFactory: ViewFactory<RenderingT>,
5452
viewEnvironment: ViewEnvironment,
5553
modifier: Modifier = Modifier
5654
) {
57-
val viewFactory = this
5855
Box(modifier = modifier) {
5956
// "Fast" path: If the child binding is also a Composable, we don't need to go through the
6057
// legacy view system and can just invoke the binding's composable function directly.

0 commit comments

Comments
 (0)