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

Commit de5171b

Browse files
Merge branch 'zachklipp/better-root' into weekend-features
* zachklipp/better-root: Make ViewFactory.showRendering function responsible for applying the ComposeViewFactoryRoot. Tidy up the package structure.
2 parents 893f716 + 8e70be8 commit de5171b

File tree

24 files changed

+520
-102
lines changed

24 files changed

+520
-102
lines changed

compose-tooling/api/compose-tooling.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public final class com/squareup/workflow/ui/compose/tooling/BuildConfig {
66
}
77

88
public final class com/squareup/workflow/ui/compose/tooling/ComposeWorkflowsKt {
9-
public static final fun preview (Lcom/squareup/workflow/compose/ComposeWorkflow;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/ui/core/Modifier;Lkotlin/jvm/functions/Function1;Landroidx/compose/Composer;)V
10-
public static synthetic fun preview$default (Lcom/squareup/workflow/compose/ComposeWorkflow;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/ui/core/Modifier;Lkotlin/jvm/functions/Function1;Landroidx/compose/Composer;ILjava/lang/Object;)V
9+
public static final fun preview (Lcom/squareup/workflow/ui/compose/ComposeWorkflow;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/ui/core/Modifier;Lkotlin/jvm/functions/Function1;Landroidx/compose/Composer;)V
10+
public static synthetic fun preview$default (Lcom/squareup/workflow/ui/compose/ComposeWorkflow;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/ui/core/Modifier;Lkotlin/jvm/functions/Function1;Landroidx/compose/Composer;ILjava/lang/Object;)V
1111
}
1212

1313
public final class com/squareup/workflow/ui/compose/tooling/ViewFactoriesKt {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ 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.compose.composed
34+
import com.squareup.workflow.ui.compose.composed
3535
import com.squareup.workflow.ui.ViewEnvironmentKey
3636
import com.squareup.workflow.ui.compose.showRendering
3737
import org.junit.Rule

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import androidx.compose.Composable
2121
import androidx.ui.core.Modifier
2222
import androidx.ui.foundation.Box
2323
import com.squareup.workflow.Sink
24-
import com.squareup.workflow.compose.ComposeWorkflow
24+
import com.squareup.workflow.ui.compose.ComposeWorkflow
2525
import com.squareup.workflow.ui.ViewEnvironment
2626
import com.squareup.workflow.ui.ViewFactory
2727
import com.squareup.workflow.ui.ViewRegistry

core-compose/api/core-compose.api

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1-
public final class com/squareup/workflow/compose/ComposeRendering {
2-
public static final field Companion Lcom/squareup/workflow/compose/ComposeRendering$Companion;
1+
public final class com/squareup/workflow/ui/compose/ComposeRendering {
2+
public static final field Companion Lcom/squareup/workflow/ui/compose/ComposeRendering$Companion;
33
public static final fun <clinit> ()V
44
public fun <init> (Lkotlin/jvm/functions/Function2;)V
55
}
66

7-
public final class com/squareup/workflow/compose/ComposeRendering$Companion {
7+
public final class com/squareup/workflow/ui/compose/ComposeRendering$Companion {
88
public final fun getFactory ()Lcom/squareup/workflow/ui/ViewFactory;
9-
public final fun getNoopRendering ()Lcom/squareup/workflow/compose/ComposeRendering;
10-
}
11-
12-
public abstract class com/squareup/workflow/compose/ComposeWorkflow : com/squareup/workflow/Workflow {
13-
public fun <init> ()V
14-
public fun asStatefulWorkflow ()Lcom/squareup/workflow/StatefulWorkflow;
15-
public abstract fun render (Ljava/lang/Object;Lcom/squareup/workflow/Sink;Lcom/squareup/workflow/ui/ViewEnvironment;Landroidx/compose/Composer;)V
16-
}
17-
18-
public final class com/squareup/workflow/compose/ComposeWorkflowKt {
19-
public static final fun composed (Lcom/squareup/workflow/Workflow$Companion;Lkotlin/jvm/functions/Function4;)Lcom/squareup/workflow/compose/ComposeWorkflow;
20-
}
21-
22-
public final class com/squareup/workflow/ui/compose/ComposeSupportKt {
23-
public static final fun <clinit> ()V
9+
public final fun getNoopRendering ()Lcom/squareup/workflow/ui/compose/ComposeRendering;
2410
}
2511

2612
public final class com/squareup/workflow/ui/compose/ComposeViewFactory : com/squareup/workflow/ui/ViewFactory {
@@ -42,10 +28,21 @@ public final class com/squareup/workflow/ui/compose/ComposeViewFactoryRoot$Compa
4228
}
4329

4430
public final class com/squareup/workflow/ui/compose/ComposeViewFactoryRootKt {
31+
public static final fun <clinit> ()V
4532
public static final fun ComposeViewFactoryRoot (Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow/ui/compose/ComposeViewFactoryRoot;
4633
public static final fun withComposeViewFactoryRoot (Lcom/squareup/workflow/ui/ViewEnvironment;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow/ui/ViewEnvironment;
4734
}
4835

36+
public abstract class com/squareup/workflow/ui/compose/ComposeWorkflow : com/squareup/workflow/Workflow {
37+
public fun <init> ()V
38+
public fun asStatefulWorkflow ()Lcom/squareup/workflow/StatefulWorkflow;
39+
public abstract fun render (Ljava/lang/Object;Lcom/squareup/workflow/Sink;Lcom/squareup/workflow/ui/ViewEnvironment;Landroidx/compose/Composer;)V
40+
}
41+
42+
public final class com/squareup/workflow/ui/compose/ComposeWorkflowKt {
43+
public static final fun composed (Lcom/squareup/workflow/Workflow$Companion;Lkotlin/jvm/functions/Function4;)Lcom/squareup/workflow/ui/compose/ComposeWorkflow;
44+
}
45+
4946
public final class com/squareup/workflow/ui/compose/ViewEnvironmentsKt {
5047
public static final fun showRendering (Lcom/squareup/workflow/ui/ViewEnvironment;Ljava/lang/Object;Landroidx/ui/core/Modifier;Landroidx/compose/Composer;)V
5148
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
@@ -70,6 +67,10 @@ public final class com/squareup/workflow/ui/compose/WorkflowContainerKt {
7067
public static synthetic fun WorkflowContainer$default (Lcom/squareup/workflow/Workflow;Lkotlin/jvm/functions/Function1;Landroidx/ui/core/Modifier;Lcom/squareup/workflow/diagnostic/WorkflowDiagnosticListener;Lkotlin/jvm/functions/Function2;Landroidx/compose/Composer;ILjava/lang/Object;)V
7168
}
7269

70+
public final class com/squareup/workflow/ui/compose/internal/ComposeSupportKt {
71+
public static final fun <clinit> ()V
72+
}
73+
7374
public final class com/squareup/workflow/ui/core/compose/BuildConfig {
7475
public static final field BUILD_TYPE Ljava/lang/String;
7576
public static final field DEBUG Z
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2020 Square Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.workflow.ui.compose
17+
18+
import android.content.Context
19+
import android.widget.FrameLayout
20+
import androidx.compose.FrameManager
21+
import androidx.compose.mutableStateOf
22+
import androidx.test.ext.junit.runners.AndroidJUnit4
23+
import androidx.ui.foundation.Text
24+
import androidx.ui.layout.Column
25+
import androidx.ui.test.assertIsDisplayed
26+
import androidx.ui.test.createComposeRule
27+
import androidx.ui.test.findByText
28+
import com.squareup.workflow.ui.ViewEnvironment
29+
import com.squareup.workflow.ui.ViewRegistry
30+
import com.squareup.workflow.ui.WorkflowViewStub
31+
import org.junit.Rule
32+
import org.junit.Test
33+
import org.junit.runner.RunWith
34+
35+
@RunWith(AndroidJUnit4::class)
36+
class ComposeViewFactoryTest {
37+
38+
@Rule @JvmField val composeRule = createComposeRule()
39+
40+
@Test fun wrapsFactoryWithRoot() {
41+
val wrapperText = mutableStateOf("one")
42+
val viewEnvironment = ViewEnvironment(ViewRegistry(TestFactory))
43+
.withComposeViewFactoryRoot { content ->
44+
Column {
45+
Text(wrapperText.value)
46+
content()
47+
}
48+
}
49+
50+
composeRule.setContent {
51+
// This is valid Compose code, but the IDE doesn't know that yet so it will show an
52+
// unsuppressable error.
53+
RootView(viewEnvironment = viewEnvironment)
54+
}
55+
56+
findByText("one\ntwo").assertIsDisplayed()
57+
FrameManager.framed {
58+
wrapperText.value = "ENO"
59+
}
60+
findByText("ENO\ntwo").assertIsDisplayed()
61+
}
62+
63+
private class RootView(context: Context) : FrameLayout(context) {
64+
private val stub = WorkflowViewStub(context).also(::addView)
65+
66+
fun setViewEnvironment(viewEnvironment: ViewEnvironment) {
67+
stub.update(TestRendering("two"), viewEnvironment)
68+
}
69+
}
70+
71+
private data class TestRendering(val text: String)
72+
73+
private companion object {
74+
val TestFactory = bindCompose<TestRendering> { rendering, _ ->
75+
Text(rendering.text)
76+
}
77+
}
78+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2020 Square Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
@file:Suppress("RemoveEmptyParenthesesFromAnnotationEntry")
17+
18+
package com.squareup.workflow.ui.compose.internal
19+
20+
import android.content.Context
21+
import android.widget.FrameLayout
22+
import androidx.compose.Composable
23+
import androidx.compose.CompositionReference
24+
import androidx.compose.FrameManager
25+
import androidx.compose.Providers
26+
import androidx.compose.Recomposer
27+
import androidx.compose.ambientOf
28+
import androidx.compose.compositionReference
29+
import androidx.compose.currentComposer
30+
import androidx.compose.mutableStateOf
31+
import androidx.test.ext.junit.runners.AndroidJUnit4
32+
import androidx.ui.foundation.Text
33+
import androidx.ui.test.assertIsDisplayed
34+
import androidx.ui.test.createComposeRule
35+
import androidx.ui.test.findByText
36+
import org.junit.Rule
37+
import org.junit.Test
38+
import org.junit.runner.RunWith
39+
40+
@RunWith(AndroidJUnit4::class)
41+
class ComposeSupportTest {
42+
43+
@Rule @JvmField val composeRule = createComposeRule()
44+
45+
@Test fun ambientsPassThroughSubcomposition() {
46+
composeRule.setContent {
47+
TestComposable("foo")
48+
}
49+
50+
findByText("foo").assertIsDisplayed()
51+
}
52+
53+
@Test fun ambientChangesPassThroughSubcomposition() {
54+
val ambientValue = mutableStateOf("foo")
55+
composeRule.setContent {
56+
TestComposable(ambientValue.value)
57+
}
58+
59+
findByText("foo").assertIsDisplayed()
60+
FrameManager.framed {
61+
ambientValue.value = "bar"
62+
}
63+
findByText("bar").assertIsDisplayed()
64+
}
65+
66+
@Composable private fun TestComposable(ambientValue: String) {
67+
Providers(TestAmbient provides ambientValue) {
68+
LegacyHostComposable {
69+
Text(TestAmbient.current)
70+
}
71+
}
72+
}
73+
74+
@Composable private fun LegacyHostComposable(leafContent: @Composable() () -> Unit) {
75+
val wormhole = Wormhole(currentComposer.recomposer, compositionReference(), leafContent)
76+
// This is valid Compose code, but the IDE doesn't know that yet so it will show an
77+
// unsuppressable error.
78+
WormholeView(wormhole = wormhole)
79+
}
80+
81+
private class Wormhole(
82+
val recomposer: Recomposer,
83+
val parentReference: CompositionReference,
84+
val childContent: @Composable() () -> Unit
85+
)
86+
87+
private class WormholeView(context: Context) : FrameLayout(context) {
88+
fun setWormhole(wormhole: Wormhole) {
89+
setContent(wormhole.recomposer, wormhole.parentReference, wormhole.childContent)
90+
}
91+
}
92+
93+
private companion object {
94+
val TestAmbient = ambientOf<String> { error("Ambient not provided") }
95+
}
96+
}

0 commit comments

Comments
 (0)