diff --git a/compose-tooling/src/androidTest/java/com/squareup/workflow/ui/compose/tooling/PreviewComposeWorkflowTest.kt b/compose-tooling/src/androidTest/java/com/squareup/workflow/ui/compose/tooling/PreviewComposeWorkflowTest.kt index 8b9b542e..1c09b994 100644 --- a/compose-tooling/src/androidTest/java/com/squareup/workflow/ui/compose/tooling/PreviewComposeWorkflowTest.kt +++ b/compose-tooling/src/androidTest/java/com/squareup/workflow/ui/compose/tooling/PreviewComposeWorkflowTest.kt @@ -31,9 +31,9 @@ import androidx.ui.test.findByText import androidx.ui.tooling.preview.Preview import androidx.ui.unit.dp import com.squareup.workflow.Workflow -import com.squareup.workflow.ui.compose.composed import com.squareup.workflow.ui.ViewEnvironmentKey -import com.squareup.workflow.ui.compose.showRendering +import com.squareup.workflow.ui.compose.WorkflowRendering +import com.squareup.workflow.ui.compose.composed import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -98,7 +98,7 @@ class PreviewComposeWorkflowTest { Column { Text(props.first) Semantics(container = true, mergeAllDescendants = true) { - environment.showRendering(rendering = props.second) + WorkflowRendering(props.second, environment) } } } @@ -111,11 +111,11 @@ class PreviewComposeWorkflowTest { Workflow.composed, Nothing> { props, _, environment -> Column { Semantics(container = true) { - environment.showRendering(rendering = props.first) + WorkflowRendering(rendering = props.first, viewEnvironment = environment) } Text(props.second) Semantics(container = true) { - environment.showRendering(rendering = props.third) + WorkflowRendering(rendering = props.third, viewEnvironment = environment) } } } diff --git a/compose-tooling/src/androidTest/java/com/squareup/workflow/ui/compose/tooling/PreviewViewFactoryTest.kt b/compose-tooling/src/androidTest/java/com/squareup/workflow/ui/compose/tooling/PreviewViewFactoryTest.kt index b09ff3bd..1d4b44f3 100644 --- a/compose-tooling/src/androidTest/java/com/squareup/workflow/ui/compose/tooling/PreviewViewFactoryTest.kt +++ b/compose-tooling/src/androidTest/java/com/squareup/workflow/ui/compose/tooling/PreviewViewFactoryTest.kt @@ -31,8 +31,8 @@ import androidx.ui.test.findByText import androidx.ui.tooling.preview.Preview import androidx.ui.unit.dp import com.squareup.workflow.ui.ViewEnvironmentKey +import com.squareup.workflow.ui.compose.WorkflowRendering import com.squareup.workflow.ui.compose.composedViewFactory -import com.squareup.workflow.ui.compose.showRendering import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -99,14 +99,15 @@ class PreviewViewFactoryTest { findByText("foo").assertIsDisplayed() } - private val ParentWithOneChild = composedViewFactory> { rendering, environment -> - Column { - Text(rendering.first) - Semantics(container = true, mergeAllDescendants = true) { - environment.showRendering(rendering = rendering.second) + private val ParentWithOneChild = + composedViewFactory> { rendering, environment -> + Column { + Text(rendering.first) + Semantics(container = true, mergeAllDescendants = true) { + WorkflowRendering(rendering.second, environment) + } } } - } @Preview @Composable private fun ParentWithOneChildPreview() { ParentWithOneChild.preview(Pair("one", "two")) @@ -116,11 +117,11 @@ class PreviewViewFactoryTest { composedViewFactory> { rendering, environment -> Column { Semantics(container = true) { - environment.showRendering(rendering = rendering.first) + WorkflowRendering(rendering.first, environment) } Text(rendering.second) Semantics(container = true) { - environment.showRendering(rendering = rendering.third) + WorkflowRendering(rendering.third, environment) } } } @@ -139,7 +140,7 @@ class PreviewViewFactoryTest { Text(rendering.text) rendering.child?.let { child -> Semantics(container = true) { - environment.showRendering(rendering = child) + WorkflowRendering(rendering = child, viewEnvironment = environment) } } } diff --git a/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/PreviewViewEnvironment.kt b/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/PreviewViewEnvironment.kt index 05a86cf8..7693e8b5 100644 --- a/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/PreviewViewEnvironment.kt +++ b/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/PreviewViewEnvironment.kt @@ -50,7 +50,7 @@ import kotlin.reflect.KClass /** * A [ViewRegistry] that uses [mainFactory] for rendering [RenderingT]s, and [placeholderFactory] - * for all other [showRendering][com.squareup.workflow.ui.compose.showRendering] calls. + * for all other [WorkflowRendering][com.squareup.workflow.ui.compose.WorkflowRendering] calls. */ @Immutable private class PreviewViewRegistry( diff --git a/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/ViewFactories.kt b/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/ViewFactories.kt index 4314486f..fe9f55a2 100644 --- a/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/ViewFactories.kt +++ b/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/ViewFactories.kt @@ -22,7 +22,7 @@ import androidx.ui.core.Modifier import com.squareup.workflow.ui.ViewEnvironment import com.squareup.workflow.ui.ViewFactory import com.squareup.workflow.ui.ViewRegistry -import com.squareup.workflow.ui.compose.showRendering +import com.squareup.workflow.ui.compose.WorkflowRendering /** * Draws this [ViewFactory] using a special preview [ViewRegistry]. @@ -48,5 +48,5 @@ import com.squareup.workflow.ui.compose.showRendering ) { val previewEnvironment = previewViewEnvironment(placeholderModifier, viewEnvironmentUpdater, mainFactory = this) - previewEnvironment.showRendering(rendering, modifier) + WorkflowRendering(rendering, previewEnvironment, modifier) } diff --git a/core-compose/api/core-compose.api b/core-compose/api/core-compose.api index 9e593426..a2a16ed7 100644 --- a/core-compose/api/core-compose.api +++ b/core-compose/api/core-compose.api @@ -44,8 +44,8 @@ public final class com/squareup/workflow/ui/compose/ComposeWorkflowKt { } public final class com/squareup/workflow/ui/compose/ViewEnvironmentsKt { - public static final fun showRendering (Lcom/squareup/workflow/ui/ViewEnvironment;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/compose/Composer;)V - 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 + public static final fun WorkflowRendering (Ljava/lang/Object;Lcom/squareup/workflow/ui/ViewEnvironment;Landroidx/ui/core/Modifier;Landroidx/compose/Composer;)V + 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 } public final class com/squareup/workflow/ui/compose/WorkflowContainerKt { diff --git a/core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/internal/ViewRegistriesTest.kt b/core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/ViewEnvironmentsTest.kt similarity index 78% rename from core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/internal/ViewRegistriesTest.kt rename to core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/ViewEnvironmentsTest.kt index f6302b12..364e3679 100644 --- a/core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/internal/ViewRegistriesTest.kt +++ b/core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/ViewEnvironmentsTest.kt @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.squareup.workflow.ui.compose.internal +package com.squareup.workflow.ui.compose import androidx.compose.FrameManager import androidx.compose.mutableStateOf @@ -24,27 +24,26 @@ import androidx.ui.test.createComposeRule import androidx.ui.test.findByText import com.squareup.workflow.ui.ViewEnvironment import com.squareup.workflow.ui.ViewRegistry -import com.squareup.workflow.ui.compose.bindCompose import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) -class ViewRegistriesTest { +class ViewEnvironmentsTest { @Rule @JvmField val composeRule = createComposeRule() - @Test fun showRendering_recomposes_whenFactoryChanged() { - val registry1 = ViewRegistry(bindCompose { rendering, _ -> + @Test fun workflowRendering_recomposes_whenFactoryChanged() { + val registry1 = ViewRegistry(composedViewFactory { rendering, _ -> Text(rendering) }) - val registry2 = ViewRegistry(bindCompose { rendering, _ -> + val registry2 = ViewRegistry(composedViewFactory { rendering, _ -> Text(rendering.reversed()) }) val registry = mutableStateOf(registry1) composeRule.setContent { - registry.value.showRendering("hello", ViewEnvironment(registry.value)) + WorkflowRendering("hello", ViewEnvironment(registry.value)) } findByText("hello").assertIsDisplayed() diff --git a/core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/internal/ViewFactoriesTest.kt b/core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/internal/ViewFactoriesTest.kt index 110a76b5..998e6d75 100644 --- a/core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/internal/ViewFactoriesTest.kt +++ b/core-compose/src/androidTest/java/com/squareup/workflow/ui/compose/internal/ViewFactoriesTest.kt @@ -24,7 +24,7 @@ import androidx.ui.test.findByText import com.squareup.workflow.ui.ViewEnvironment import com.squareup.workflow.ui.ViewRegistry import com.squareup.workflow.ui.compose.composedViewFactory -import com.squareup.workflow.ui.compose.showRendering +import com.squareup.workflow.ui.compose.WorkflowRendering import com.squareup.workflow.ui.compose.withComposeViewFactoryRoot import org.junit.Rule import org.junit.Test @@ -35,7 +35,7 @@ class ViewFactoriesTest { @Rule @JvmField val composeRule = createComposeRule() - @Test fun showRendering_wrapsFactoryWithRoot_whenAlreadyInComposition() { + @Test fun WorkflowRendering_wrapsFactoryWithRoot_whenAlreadyInComposition() { val viewEnvironment = ViewEnvironment(ViewRegistry(TestFactory)) .withComposeViewFactoryRoot { content -> Column { @@ -45,7 +45,7 @@ class ViewFactoriesTest { } composeRule.setContent { - viewEnvironment.showRendering(TestRendering("two")) + WorkflowRendering(TestRendering("two"), viewEnvironment) } findByText("one\ntwo").assertIsDisplayed() diff --git a/core-compose/src/main/java/com/squareup/workflow/ui/compose/ComposeViewFactory.kt b/core-compose/src/main/java/com/squareup/workflow/ui/compose/ComposeViewFactory.kt index a67cf1c9..bc8fc805 100644 --- a/core-compose/src/main/java/com/squareup/workflow/ui/compose/ComposeViewFactory.kt +++ b/core-compose/src/main/java/com/squareup/workflow/ui/compose/ComposeViewFactory.kt @@ -64,8 +64,8 @@ import kotlin.reflect.KClass * renderings using the [ViewRegistry][com.squareup.workflow.ui.ViewRegistry]. * * View factories defined using this function may also show nested renderings. Doing so is as simple - * as calling [ViewEnvironment.showRendering] and passing in the nested rendering. See the kdoc on - * that function for an example. + * as calling [WorkflowRendering] and passing in the nested rendering. See the kdoc on that function + * for an example. * * Nested renderings will have access to any ambients defined in outer composable, even if there are * legacy views in between them, as long as the [ViewEnvironment] is propagated continuously between diff --git a/core-compose/src/main/java/com/squareup/workflow/ui/compose/ViewEnvironments.kt b/core-compose/src/main/java/com/squareup/workflow/ui/compose/ViewEnvironments.kt index aabd9c4f..d9bb433c 100644 --- a/core-compose/src/main/java/com/squareup/workflow/ui/compose/ViewEnvironments.kt +++ b/core-compose/src/main/java/com/squareup/workflow/ui/compose/ViewEnvironments.kt @@ -20,7 +20,7 @@ import androidx.compose.remember import androidx.ui.core.Modifier import com.squareup.workflow.ui.ViewEnvironment import com.squareup.workflow.ui.ViewRegistry -import com.squareup.workflow.ui.compose.internal.showRendering +import com.squareup.workflow.ui.compose.internal.WorkflowRendering /** * Renders [rendering] into the composition using this [ViewEnvironment]'s @@ -40,7 +40,7 @@ import com.squareup.workflow.ui.compose.internal.showRendering * * val FramedContainerViewFactory = composedViewFactory { rendering, environment -> * Surface(border = Border(rendering.borderColor, 8.dp)) { - * environment.showRendering(rendering.child) + * WorkflowRendering(rendering.child, environment) * } * } * ``` @@ -52,10 +52,15 @@ import com.squareup.workflow.ui.compose.internal.showRendering * * @throws IllegalArgumentException if no factory can be found for [rendering]'s type. */ -@Composable fun ViewEnvironment.showRendering( +@Composable fun WorkflowRendering( rendering: Any, + viewEnvironment: ViewEnvironment, modifier: Modifier = Modifier ) { - val viewRegistry = remember(this) { this[ViewRegistry] } - viewRegistry.showRendering(rendering, this, modifier) + val viewRegistry = remember(viewEnvironment) { viewEnvironment[ViewRegistry] } + val renderingType = rendering::class + val viewFactory = remember(viewRegistry, renderingType) { + viewRegistry.getFactoryFor(renderingType) + } + WorkflowRendering(rendering, viewFactory, viewEnvironment, modifier) } diff --git a/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ParentComposition.kt b/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ParentComposition.kt index 3b205fbc..454365de 100644 --- a/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ParentComposition.kt +++ b/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ParentComposition.kt @@ -30,9 +30,9 @@ import com.squareup.workflow.ui.ViewEnvironmentKey * Holds a [CompositionReference] and that can be passed to [setOrSubcomposeContent] to create a * composition that is a child of another composition. Subcompositions get ambients and other * compose context from their parent, and propagate invalidations, which allows ambients provided - * around a [showRendering] call to be read by nested Compose-based view factories. + * around a [WorkflowRendering] call to be read by nested Compose-based view factories. * - * When [showRendering] is called, it will store an instance of this class in the [ViewEnvironment]. + * When [WorkflowRendering] is called, it will store an instance of this class in the [ViewEnvironment]. * [ComposeViewFactory] pulls the reference out of the environment and uses it to link its * composition to the outer one. */ diff --git a/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ViewFactories.kt b/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ViewFactories.kt index db33eb55..dab4d885 100644 --- a/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ViewFactories.kt +++ b/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ViewFactories.kt @@ -35,27 +35,24 @@ import com.squareup.workflow.ui.compose.ComposeViewFactory import com.squareup.workflow.ui.showRendering /** - * Renders [rendering] into the composition using the `ViewRegistry` from the [ViewEnvironment] to - * determine how to draw it. + * Renders [rendering] into the composition using [viewFactory]. * * To display a nested rendering from a - * [Composable view binding][com.squareup.workflow.ui.compose.composedViewFactory], use - * [ViewEnvironment.showRendering]. + * [Composable view binding][com.squareup.workflow.ui.compose.composedViewFactory], use the overload + * without a [ViewFactory] parameter. * * *Note: [rendering] must be the same type as this [ViewFactory], even though the type system does * not enforce this constraint. This is due to a Compose compiler bug tracked * [here](https://issuetracker.google.com/issues/156527332). * - * @see ViewEnvironment.showRendering - * @see com.squareup.workflow.ui.ViewRegistry.showRendering + * @see com.squareup.workflow.ui.compose.WorkflowRendering */ -// TODO(https://issuetracker.google.com/issues/156527332) Should be ViewFactory -@Composable internal fun ViewFactory.showRendering( +@Composable internal fun WorkflowRendering( rendering: RenderingT, + viewFactory: ViewFactory, viewEnvironment: ViewEnvironment, modifier: Modifier = Modifier ) { - val viewFactory = this Box(modifier = modifier) { // "Fast" path: If the child binding is also a Composable, we don't need to go through the // legacy view system and can just invoke the binding's composable function directly. diff --git a/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ViewRegistries.kt b/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ViewRegistries.kt deleted file mode 100644 index 4fbd747d..00000000 --- a/core-compose/src/main/java/com/squareup/workflow/ui/compose/internal/ViewRegistries.kt +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2020 Square Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.squareup.workflow.ui.compose.internal - -import androidx.compose.Composable -import androidx.compose.remember -import androidx.ui.core.Modifier -import com.squareup.workflow.ui.ViewEnvironment -import com.squareup.workflow.ui.ViewFactory -import com.squareup.workflow.ui.ViewRegistry - -/** - * Renders [rendering] into the composition using this [ViewRegistry] to determine how to draw it. - * - * To display a nested rendering from a [Composable view binding][composedViewFactory], use - * [ViewEnvironment.showRendering]. - * - * @see ViewEnvironment.showRendering - * @see ViewFactory.showRendering - */ -@Composable internal fun ViewRegistry.showRendering( - rendering: Any, - hints: ViewEnvironment, - modifier: Modifier = Modifier -) { - val renderingType = rendering::class - val viewFactory = remember(this, renderingType) { getFactoryFor(renderingType) } - viewFactory.showRendering(rendering, hints, modifier) -} diff --git a/samples/hello-compose/src/main/java/com/squareup/sample/hellocompose/App.kt b/samples/hello-compose/src/main/java/com/squareup/sample/hellocompose/App.kt index c6676a24..f778326e 100644 --- a/samples/hello-compose/src/main/java/com/squareup/sample/hellocompose/App.kt +++ b/samples/hello-compose/src/main/java/com/squareup/sample/hellocompose/App.kt @@ -27,7 +27,7 @@ import com.squareup.workflow.diagnostic.SimpleLoggingDiagnosticListener import com.squareup.workflow.ui.ViewEnvironment import com.squareup.workflow.ui.ViewRegistry import com.squareup.workflow.ui.compose.WorkflowContainer -import com.squareup.workflow.ui.compose.showRendering +import com.squareup.workflow.ui.compose.WorkflowRendering private val viewRegistry = ViewRegistry(HelloBinding) private val viewEnvironment = ViewEnvironment(viewRegistry) @@ -38,8 +38,9 @@ private val viewEnvironment = ViewEnvironment(viewRegistry) diagnosticListener = SimpleLoggingDiagnosticListener() ) { rendering -> MaterialTheme { - viewEnvironment.showRendering( + WorkflowRendering( rendering, + viewEnvironment, modifier = Modifier.drawBorder( shape = RoundedCornerShape(10.dp), size = 10.dp, diff --git a/samples/nested-renderings/src/main/java/com/squareup/sample/nestedrenderings/RecursiveViewFactory.kt b/samples/nested-renderings/src/main/java/com/squareup/sample/nestedrenderings/RecursiveViewFactory.kt index 54b078a2..5bbbd7d6 100644 --- a/samples/nested-renderings/src/main/java/com/squareup/sample/nestedrenderings/RecursiveViewFactory.kt +++ b/samples/nested-renderings/src/main/java/com/squareup/sample/nestedrenderings/RecursiveViewFactory.kt @@ -40,7 +40,7 @@ import androidx.ui.tooling.preview.Preview import com.squareup.sample.nestedrenderings.RecursiveWorkflow.Rendering import com.squareup.workflow.ui.ViewEnvironment import com.squareup.workflow.ui.compose.composedViewFactory -import com.squareup.workflow.ui.compose.showRendering +import com.squareup.workflow.ui.compose.WorkflowRendering import com.squareup.workflow.ui.compose.tooling.preview /** @@ -109,10 +109,11 @@ val RecursiveViewFactory = composedViewFactory { rendering, viewEnvir horizontalGravity = CenterHorizontally ) { children.forEach { childRendering -> - viewEnvironment.showRendering( + WorkflowRendering( childRendering, // Pass a weight so all children are partitioned evenly within the total column space. // Without the weight, each child is the full size of the parent. + viewEnvironment, modifier = Modifier.weight(1f, true) .padding(dimensionResource(R.dimen.recursive_padding)) )