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

Commit 49228bd

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

File tree

14 files changed

+56
-94
lines changed

14 files changed

+56
-94
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: 11 additions & 10 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.composedViewFactory
35-
import com.squareup.workflow.ui.compose.showRendering
3636
import org.junit.Rule
3737
import org.junit.Test
3838
import org.junit.runner.RunWith
@@ -99,14 +99,15 @@ class PreviewViewFactoryTest {
9999
findByText("foo").assertIsDisplayed()
100100
}
101101

102-
private val ParentWithOneChild = composedViewFactory<Pair<String, String>> { rendering, environment ->
103-
Column {
104-
Text(rendering.first)
105-
Semantics(container = true, mergeAllDescendants = true) {
106-
environment.showRendering(rendering = rendering.second)
102+
private val ParentWithOneChild =
103+
composedViewFactory<Pair<String, String>> { rendering, environment ->
104+
Column {
105+
Text(rendering.first)
106+
Semantics(container = true, mergeAllDescendants = true) {
107+
WorkflowRendering(rendering.second, environment)
108+
}
107109
}
108110
}
109-
}
110111

111112
@Preview @Composable private fun ParentWithOneChildPreview() {
112113
ParentWithOneChild.preview(Pair("one", "two"))
@@ -116,11 +117,11 @@ class PreviewViewFactoryTest {
116117
composedViewFactory<Triple<String, String, String>> { rendering, environment ->
117118
Column {
118119
Semantics(container = true) {
119-
environment.showRendering(rendering = rendering.first)
120+
WorkflowRendering(rendering.first, environment)
120121
}
121122
Text(rendering.second)
122123
Semantics(container = true) {
123-
environment.showRendering(rendering = rendering.third)
124+
WorkflowRendering(rendering.third, environment)
124125
}
125126
}
126127
}
@@ -139,7 +140,7 @@ class PreviewViewFactoryTest {
139140
Text(rendering.text)
140141
rendering.child?.let { child ->
141142
Semantics(container = true) {
142-
environment.showRendering(rendering = child)
143+
WorkflowRendering(rendering = child, viewEnvironment = environment)
143144
}
144145
}
145146
}

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 {
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.squareup.workflow.ui.compose.internal
16+
package com.squareup.workflow.ui.compose
1717

1818
import androidx.compose.FrameManager
1919
import androidx.compose.mutableStateOf
@@ -24,27 +24,26 @@ import androidx.ui.test.createComposeRule
2424
import androidx.ui.test.findByText
2525
import com.squareup.workflow.ui.ViewEnvironment
2626
import com.squareup.workflow.ui.ViewRegistry
27-
import com.squareup.workflow.ui.compose.bindCompose
2827
import org.junit.Rule
2928
import org.junit.Test
3029
import org.junit.runner.RunWith
3130

3231
@RunWith(AndroidJUnit4::class)
33-
class ViewRegistriesTest {
32+
class ViewEnvironmentsTest {
3433

3534
@Rule @JvmField val composeRule = createComposeRule()
3635

37-
@Test fun showRendering_recomposes_whenFactoryChanged() {
38-
val registry1 = ViewRegistry(bindCompose<String> { rendering, _ ->
36+
@Test fun workflowRendering_recomposes_whenFactoryChanged() {
37+
val registry1 = ViewRegistry(composedViewFactory<String> { rendering, _ ->
3938
Text(rendering)
4039
})
41-
val registry2 = ViewRegistry(bindCompose<String> { rendering, _ ->
40+
val registry2 = ViewRegistry(composedViewFactory<String> { rendering, _ ->
4241
Text(rendering.reversed())
4342
})
4443
val registry = mutableStateOf(registry1)
4544

4645
composeRule.setContent {
47-
registry.value.showRendering("hello", ViewEnvironment(registry.value))
46+
WorkflowRendering("hello", ViewEnvironment(registry.value))
4847
}
4948

5049
findByText("hello").assertIsDisplayed()

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.composedViewFactory
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 = composedViewFactory<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
*/

0 commit comments

Comments
 (0)