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

Commit 6daf2e7

Browse files
Merge pull request #26 from square/zachklipp/dev11
Upgrade Compose to dev11.
2 parents 059db5e + 84fa590 commit 6daf2e7

File tree

4 files changed

+54
-95
lines changed

4 files changed

+54
-95
lines changed

buildSrc/src/main/java/Dependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import java.util.Locale.US
1919
import kotlin.reflect.full.declaredMembers
2020

2121
object Versions {
22-
const val compose = "0.1.0-dev10"
22+
const val compose = "0.1.0-dev11"
2323
const val kotlin = "1.3.71"
2424
const val targetSdk = 29
2525
const val workflow = "0.28.0"

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

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import androidx.ui.geometry.Offset
2929
import androidx.ui.graphics.Color
3030
import androidx.ui.graphics.Paint
3131
import androidx.ui.graphics.Shadow
32-
import androidx.ui.graphics.withSave
32+
import androidx.ui.graphics.painter.Stroke
33+
import androidx.ui.graphics.painter.drawCanvas
34+
import androidx.ui.graphics.painter.rotate
3335
import androidx.ui.graphics.withSaveLayer
3436
import androidx.ui.layout.fillMaxSize
3537
import androidx.ui.text.TextStyle
@@ -49,17 +51,19 @@ import com.squareup.workflow.ui.compose.bindCompose
4951
internal fun placeholderViewFactory(modifier: Modifier): ViewFactory<Any> =
5052
bindCompose { rendering, _ ->
5153
Text(
52-
modifier = modifier/*.fillMaxSize()*/
54+
modifier = modifier
5355
.clipToBounds()
5456
.drawBehind {
55-
withSaveLayer(size.toRect(), Paint().apply { alpha = .2f }) {
56-
drawRect(size.toRect(), Paint().apply { color = Color.Gray })
57-
drawCrossHatch(
58-
color = Color.Red,
59-
strokeWidth = 2.dp,
60-
spaceWidth = 5.dp,
61-
angle = 45f
62-
)
57+
drawCanvas { canvas, size ->
58+
canvas.withSaveLayer(size.toRect(), Paint().apply { alpha = .2f }) {
59+
canvas.drawRect(size.toRect(), Paint().apply { color = Color.Gray })
60+
drawCrossHatch(
61+
color = Color.Red,
62+
strokeWidth = 2.dp,
63+
spaceWidth = 5.dp,
64+
angle = 45f
65+
)
66+
}
6367
}
6468
},
6569
text = rendering.toString(),
@@ -74,25 +78,25 @@ internal fun placeholderViewFactory(modifier: Modifier): ViewFactory<Any> =
7478
@Preview(widthDp = 200, heightDp = 200)
7579
@Composable private fun PreviewStubViewBindingOnWhite() {
7680
Box(backgroundColor = Color.White) {
77-
placeholderViewFactory(Modifier).preview(
78-
rendering = "preview",
79-
modifier = Modifier.fillMaxSize()
80-
.drawBorder(size = 1.dp, color = Color.Red)
81-
)
81+
PreviewStubBindingPreviewTemplate()
8282
}
8383
}
8484

8585
@Preview(widthDp = 200, heightDp = 200)
8686
@Composable private fun PreviewStubViewBindingOnBlack() {
8787
Box(backgroundColor = Color.Black) {
88-
placeholderViewFactory(Modifier).preview(
89-
rendering = "preview",
90-
modifier = Modifier.fillMaxSize()
91-
.drawBorder(size = 1.dp, color = Color.Red)
92-
)
88+
PreviewStubBindingPreviewTemplate()
9389
}
9490
}
9591

92+
@Composable private fun PreviewStubBindingPreviewTemplate() {
93+
placeholderViewFactory(Modifier).preview(
94+
rendering = "preview",
95+
placeholderModifier = Modifier.fillMaxSize()
96+
.drawBorder(size = 1.dp, color = Color.Red)
97+
)
98+
}
99+
96100
private fun DrawScope.drawCrossHatch(
97101
color: Color,
98102
strokeWidth: Dp,
@@ -109,34 +113,27 @@ private fun DrawScope.drawHatch(
109113
spaceWidth: Dp,
110114
angle: Float
111115
) {
112-
val strokeWidthPx = strokeWidth.toPx()
113-
.value
114-
val paint = Paint().also {
115-
it.color = color.scaleColors(.5f)
116-
it.strokeWidth = strokeWidthPx
117-
}
118-
119-
withSave {
120-
val halfWidth = size.width.value / 2
121-
val halfHeight = size.height.value / 2
122-
translate(halfWidth, halfHeight)
123-
rotate(angle)
124-
translate(-halfWidth, -halfHeight)
116+
val strokeWidthPx = strokeWidth.toPx().value
117+
val spaceWidthPx = spaceWidth.toPx().value
118+
val strokeColor = color.scaleColors(.5f)
119+
val stroke = Stroke(width = strokeWidthPx)
125120

121+
rotate(angle) {
126122
// Draw outside our bounds to fill the space even when rotated.
127-
val left = -size.width.value
128-
val right = size.width.value * 2
129-
val top = -size.height.value
130-
val bottom = size.height.value * 2
123+
val left = -size.width
124+
val right = size.width * 2
125+
val top = -size.height
126+
val bottom = size.height * 2
131127

132128
var y = top + strokeWidthPx * 2f
133129
while (y < bottom) {
134130
drawLine(
131+
strokeColor,
135132
Offset(left, y),
136133
Offset(right, y),
137-
paint
134+
stroke = stroke
138135
)
139-
y += spaceWidth.toPx().value * 2
136+
y += spaceWidthPx * 2
140137
}
141138
}
142139
}

samples/nested-renderings/src/androidTest/java/com/squareup/sample/nestedrenderings/NestedRenderingsTest.kt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
package com.squareup.sample.nestedrenderings
1717

1818
import androidx.test.ext.junit.runners.AndroidJUnit4
19+
import androidx.ui.test.SemanticsNodeInteraction
20+
import androidx.ui.test.SemanticsNodeInteractionCollection
1921
import androidx.ui.test.android.AndroidComposeTestRule
22+
import androidx.ui.test.assertCountEquals
2023
import androidx.ui.test.assertIsDisplayed
2124
import androidx.ui.test.doClick
2225
import androidx.ui.test.findAllByText
2326
import androidx.ui.test.findByText
24-
import com.google.common.truth.Truth.assertThat
27+
import androidx.ui.test.last
2528
import org.junit.Rule
2629
import org.junit.Test
2730
import org.junit.runner.RunWith
@@ -35,24 +38,26 @@ class NestedRenderingsTest {
3538
@Rule @JvmField val composeRule = AndroidComposeTestRule<NestedRenderingsActivity>()
3639

3740
@Test fun childrenAreAddedAndRemoved() {
38-
val resetButton = findByText("Reset")
39-
4041
findByText(ADD_BUTTON_TEXT)
4142
.assertIsDisplayed()
4243
.doClick()
4344

4445
findAllByText(ADD_BUTTON_TEXT)
45-
.also { addButtons ->
46-
assertThat(addButtons).hasSize(2)
47-
addButtons.forEach { it.doClick() }
48-
}
46+
.assertCountEquals(2)
47+
.forEach { it.doClick() }
4948

5049
findAllByText(ADD_BUTTON_TEXT)
51-
.also { addButtons ->
52-
assertThat(addButtons).hasSize(4)
53-
}
50+
.assertCountEquals(4)
51+
52+
findAllByText("Reset").last()
53+
.doClick()
54+
findAllByText(ADD_BUTTON_TEXT).assertCountEquals(1)
55+
}
5456

55-
resetButton.doClick()
56-
assertThat(findAllByText(ADD_BUTTON_TEXT)).hasSize(1)
57+
private fun SemanticsNodeInteractionCollection.forEach(
58+
block: (SemanticsNodeInteraction) -> Unit
59+
) {
60+
val count = fetchSemanticsNodes().size
61+
for (i in 0 until count) block(get(i))
5762
}
5863
}

samples/nested-renderings/src/main/res/values/java/com/squareup/workflow/ui/compose/tooling/ComposeWorkflows.kt

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

0 commit comments

Comments
 (0)