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

Rename showRendering to WorkflowRendering and make it not an extension function. #36

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -98,7 +98,7 @@ class PreviewComposeWorkflowTest {
Column {
Text(props.first)
Semantics(container = true, mergeAllDescendants = true) {
environment.showRendering(rendering = props.second)
WorkflowRendering(props.second, environment)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this. The noun v verb discussion resonated.

}
}
}
Expand All @@ -111,11 +111,11 @@ class PreviewComposeWorkflowTest {
Workflow.composed<Triple<String, String, String>, 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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,14 +99,15 @@ class PreviewViewFactoryTest {
findByText("foo").assertIsDisplayed()
}

private val ParentWithOneChild = composedViewFactory<Pair<String, String>> { rendering, environment ->
Column {
Text(rendering.first)
Semantics(container = true, mergeAllDescendants = true) {
environment.showRendering(rendering = rendering.second)
private val ParentWithOneChild =
composedViewFactory<Pair<String, String>> { 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"))
Expand All @@ -116,11 +117,11 @@ class PreviewViewFactoryTest {
composedViewFactory<Triple<String, String, String>> { 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)
}
}
}
Expand All @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RenderingT : Any>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions core-compose/api/core-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> { rendering, _ ->
@Test fun workflowRendering_recomposes_whenFactoryChanged() {
val registry1 = ViewRegistry(composedViewFactory<String> { rendering, _ ->
Text(rendering)
})
val registry2 = ViewRegistry(bindCompose<String> { rendering, _ ->
val registry2 = ViewRegistry(composedViewFactory<String> { 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -45,7 +45,7 @@ class ViewFactoriesTest {
}

composeRule.setContent {
viewEnvironment.showRendering(TestRendering("two"))
WorkflowRendering(TestRendering("two"), viewEnvironment)
}

findByText("one\ntwo").assertIsDisplayed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +40,7 @@ import com.squareup.workflow.ui.compose.internal.showRendering
*
* val FramedContainerViewFactory = composedViewFactory<FramedRendering> { rendering, environment ->
* Surface(border = Border(rendering.borderColor, 8.dp)) {
* environment.showRendering(rendering.child)
* WorkflowRendering(rendering.child, environment)
* }
* }
* ```
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RenderingT>
@Composable internal fun <RenderingT : Any> ViewFactory<Any>.showRendering(
@Composable internal fun <RenderingT : Any> WorkflowRendering(
rendering: RenderingT,
viewFactory: ViewFactory<RenderingT>,
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.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -109,10 +109,11 @@ val RecursiveViewFactory = composedViewFactory<Rendering> { 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))
)
Expand Down