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

Commit ea57ae0

Browse files
Renames ComposeViewFactoryRoot to CompositionRoot and decouples the implementation.
The root is now applied via a `ViewRegistry` wrapper that wraps individual factories to apply the root, instead of having this logic hard-coded inside `ComposeViewFactory`. This approach is more flexible in general (could be used to do other tricks), and decouples the rooting feature from the rest of the code.
1 parent 3f460f8 commit ea57ae0

File tree

13 files changed

+273
-343
lines changed

13 files changed

+273
-343
lines changed

core-compose/api/core-compose.api

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,6 @@ public final class com/squareup/workflow/ui/compose/ComposeViewFactory : com/squ
1515
public fun getType ()Lkotlin/reflect/KClass;
1616
}
1717

18-
public abstract interface class com/squareup/workflow/ui/compose/ComposeViewFactoryRoot {
19-
public static final field Companion Lcom/squareup/workflow/ui/compose/ComposeViewFactoryRoot$Companion;
20-
public static fun <clinit> ()V
21-
public abstract fun wrap (Lkotlin/jvm/functions/Function1;Landroidx/compose/Composer;)V
22-
}
23-
24-
public final class com/squareup/workflow/ui/compose/ComposeViewFactoryRoot$Companion : com/squareup/workflow/ui/ViewEnvironmentKey {
25-
public static final fun <clinit> ()V
26-
public fun getDefault ()Lcom/squareup/workflow/ui/compose/ComposeViewFactoryRoot;
27-
public synthetic fun getDefault ()Ljava/lang/Object;
28-
}
29-
30-
public final class com/squareup/workflow/ui/compose/ComposeViewFactoryRootKt {
31-
public static final fun <clinit> ()V
32-
public static final fun ComposeViewFactoryRoot (Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow/ui/compose/ComposeViewFactoryRoot;
33-
public static final fun withComposeViewFactoryRoot (Lcom/squareup/workflow/ui/ViewEnvironment;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow/ui/ViewEnvironment;
34-
}
35-
3618
public abstract class com/squareup/workflow/ui/compose/ComposeWorkflow : com/squareup/workflow/Workflow {
3719
public fun <init> ()V
3820
public fun asStatefulWorkflow ()Lcom/squareup/workflow/StatefulWorkflow;
@@ -43,6 +25,12 @@ public final class com/squareup/workflow/ui/compose/ComposeWorkflowKt {
4325
public static final fun composed (Lcom/squareup/workflow/Workflow$Companion;Lkotlin/jvm/functions/Function4;)Lcom/squareup/workflow/ui/compose/ComposeWorkflow;
4426
}
4527

28+
public final class com/squareup/workflow/ui/compose/CompositionRootKt {
29+
public static final fun <clinit> ()V
30+
public static final fun withCompositionRoot (Lcom/squareup/workflow/ui/ViewEnvironment;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow/ui/ViewEnvironment;
31+
public static final fun withCompositionRoot (Lcom/squareup/workflow/ui/ViewRegistry;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow/ui/ViewRegistry;
32+
}
33+
4634
public final class com/squareup/workflow/ui/compose/ViewEnvironmentsKt {
4735
public static final fun WorkflowRendering (Ljava/lang/Object;Lcom/squareup/workflow/ui/ViewEnvironment;Landroidx/ui/core/Modifier;Landroidx/compose/Composer;)V
4836
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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ComposeViewFactoryTest {
4040
@Test fun wrapsFactoryWithRoot() {
4141
val wrapperText = mutableStateOf("one")
4242
val viewEnvironment = ViewEnvironment(ViewRegistry(TestFactory))
43-
.withComposeViewFactoryRoot { content ->
43+
.withCompositionRoot { content ->
4444
Column {
4545
Text(wrapperText.value)
4646
content()
Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
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

18+
import androidx.compose.FrameManager
19+
import androidx.compose.mutableStateOf
1820
import androidx.test.ext.junit.runners.AndroidJUnit4
1921
import androidx.ui.foundation.Text
2022
import androidx.ui.layout.Column
@@ -23,28 +25,114 @@ import androidx.ui.test.assertIsDisplayed
2325
import androidx.ui.test.createComposeRule
2426
import androidx.ui.test.findByText
2527
import com.google.common.truth.Truth.assertThat
26-
import com.squareup.workflow.ui.compose.ComposeViewFactoryRoot
2728
import org.junit.Rule
2829
import org.junit.Test
2930
import org.junit.runner.RunWith
3031
import kotlin.test.assertFailsWith
3132

3233
@RunWith(AndroidJUnit4::class)
33-
class SafeComposeViewFactoryRootTest {
34+
class CompositionRootTest {
3435

3536
@Rule @JvmField val composeRule = createComposeRule()
3637

38+
@Test fun wrapWithRootIfNecessary_wrapsWhenNecessary() {
39+
val root: CompositionRoot = { content ->
40+
Column {
41+
Text("one")
42+
content()
43+
}
44+
}
45+
46+
composeRule.setContent {
47+
wrapWithRootIfNecessary(root) {
48+
Text("two")
49+
}
50+
}
51+
52+
findByText("one\ntwo").assertIsDisplayed()
53+
}
54+
55+
@Test fun wrapWithRootIfNecessary_onlyWrapsOnce() {
56+
val root: CompositionRoot = { content ->
57+
Column {
58+
Text("one")
59+
content()
60+
}
61+
}
62+
63+
composeRule.setContent {
64+
wrapWithRootIfNecessary(root) {
65+
Text("two")
66+
wrapWithRootIfNecessary(root) {
67+
Text("three")
68+
}
69+
}
70+
}
71+
72+
findByText("one\ntwo\nthree").assertIsDisplayed()
73+
}
74+
75+
@Test fun wrapWithRootIfNecessary_seesUpdatesFromRootWrapper() {
76+
val wrapperText = mutableStateOf("one")
77+
val root: CompositionRoot = { content ->
78+
Column {
79+
Text(wrapperText.value)
80+
content()
81+
}
82+
}
83+
84+
composeRule.setContent {
85+
wrapWithRootIfNecessary(root) {
86+
Text("two")
87+
}
88+
}
89+
90+
findByText("one\ntwo").assertIsDisplayed()
91+
FrameManager.framed {
92+
wrapperText.value = "ENO"
93+
}
94+
findByText("ENO\ntwo").assertIsDisplayed()
95+
}
96+
97+
@Test fun wrapWithRootIfNecessary_rewrapsWhenDifferentRoot() {
98+
val root1: CompositionRoot = { content ->
99+
Column {
100+
Text("one")
101+
content()
102+
}
103+
}
104+
val root2: CompositionRoot = { content ->
105+
Column {
106+
Text("ENO")
107+
content()
108+
}
109+
}
110+
val viewEnvironment = mutableStateOf(root1)
111+
112+
composeRule.setContent {
113+
wrapWithRootIfNecessary(viewEnvironment.value) {
114+
Text("two")
115+
}
116+
}
117+
118+
findByText("one\ntwo").assertIsDisplayed()
119+
FrameManager.framed {
120+
viewEnvironment.value = root2
121+
}
122+
findByText("ENO\ntwo").assertIsDisplayed()
123+
}
124+
37125
@Test fun safeComposeViewFactoryRoot_wraps_content() {
38-
val wrapped = ComposeViewFactoryRoot { content ->
126+
val wrapped: CompositionRoot = { content ->
39127
Column {
40128
Text("Parent")
41129
content()
42130
}
43131
}
44-
val safeRoot = SafeComposeViewFactoryRoot(wrapped)
132+
val safeRoot = safeCompositionRoot(wrapped)
45133

46134
composeRule.setContent {
47-
safeRoot.wrap {
135+
safeRoot {
48136
// Need an explicit semantics container, otherwise both Texts will be merged into a single
49137
// Semantics object with the text "Parent\nChild".
50138
Semantics(container = true) {
@@ -58,12 +146,12 @@ class SafeComposeViewFactoryRootTest {
58146
}
59147

60148
@Test fun safeComposeViewFactoryRoot_throws_whenChildrenNotInvoked() {
61-
val wrapped = ComposeViewFactoryRoot { }
62-
val safeRoot = SafeComposeViewFactoryRoot(wrapped)
149+
val wrapped: CompositionRoot = { }
150+
val safeRoot = safeCompositionRoot(wrapped)
63151

64152
val error = assertFailsWith<IllegalStateException> {
65153
composeRule.setContent {
66-
safeRoot.wrap {}
154+
safeRoot {}
67155
}
68156
}
69157

@@ -74,15 +162,15 @@ class SafeComposeViewFactoryRootTest {
74162
}
75163

76164
@Test fun safeComposeViewFactoryRoot_throws_whenChildrenInvokedMultipleTimes() {
77-
val wrapped = ComposeViewFactoryRoot { children ->
165+
val wrapped: CompositionRoot = { children ->
78166
children()
79167
children()
80168
}
81-
val safeRoot = SafeComposeViewFactoryRoot(wrapped)
169+
val safeRoot = safeCompositionRoot(wrapped)
82170

83171
val error = assertFailsWith<IllegalStateException> {
84172
composeRule.setContent {
85-
safeRoot.wrap {
173+
safeRoot {
86174
Text("Hello")
87175
}
88176
}

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

Lines changed: 0 additions & 142 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import com.squareup.workflow.ui.ViewEnvironment
2525
import com.squareup.workflow.ui.ViewRegistry
2626
import com.squareup.workflow.ui.compose.composedViewFactory
2727
import com.squareup.workflow.ui.compose.WorkflowRendering
28-
import com.squareup.workflow.ui.compose.withComposeViewFactoryRoot
28+
import com.squareup.workflow.ui.compose.withCompositionRoot
2929
import org.junit.Rule
3030
import org.junit.Test
3131
import org.junit.runner.RunWith
@@ -37,7 +37,7 @@ class ViewFactoriesTest {
3737

3838
@Test fun WorkflowRendering_wrapsFactoryWithRoot_whenAlreadyInComposition() {
3939
val viewEnvironment = ViewEnvironment(ViewRegistry(TestFactory))
40-
.withComposeViewFactoryRoot { content ->
40+
.withCompositionRoot { content ->
4141
Column {
4242
Text("one")
4343
content()

0 commit comments

Comments
 (0)