diff --git a/buildSrc/src/main/java/Dependencies.kt b/buildSrc/src/main/java/Dependencies.kt index d15a07bc..9ac3f8f2 100644 --- a/buildSrc/src/main/java/Dependencies.kt +++ b/buildSrc/src/main/java/Dependencies.kt @@ -19,7 +19,7 @@ import java.util.Locale.US import kotlin.reflect.full.declaredMembers object Versions { - const val compose = "0.1.0-dev10" + const val compose = "0.1.0-dev11" const val kotlin = "1.3.71" const val targetSdk = 29 const val workflow = "0.28.0" diff --git a/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/PlaceholderViewFactory.kt b/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/PlaceholderViewFactory.kt index 75056be8..fae90a78 100644 --- a/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/PlaceholderViewFactory.kt +++ b/compose-tooling/src/main/java/com/squareup/workflow/ui/compose/tooling/PlaceholderViewFactory.kt @@ -29,7 +29,9 @@ import androidx.ui.geometry.Offset import androidx.ui.graphics.Color import androidx.ui.graphics.Paint import androidx.ui.graphics.Shadow -import androidx.ui.graphics.withSave +import androidx.ui.graphics.painter.Stroke +import androidx.ui.graphics.painter.drawCanvas +import androidx.ui.graphics.painter.rotate import androidx.ui.graphics.withSaveLayer import androidx.ui.layout.fillMaxSize import androidx.ui.text.TextStyle @@ -49,17 +51,19 @@ import com.squareup.workflow.ui.compose.bindCompose internal fun placeholderViewFactory(modifier: Modifier): ViewFactory = bindCompose { rendering, _ -> Text( - modifier = modifier/*.fillMaxSize()*/ + modifier = modifier .clipToBounds() .drawBehind { - withSaveLayer(size.toRect(), Paint().apply { alpha = .2f }) { - drawRect(size.toRect(), Paint().apply { color = Color.Gray }) - drawCrossHatch( - color = Color.Red, - strokeWidth = 2.dp, - spaceWidth = 5.dp, - angle = 45f - ) + drawCanvas { canvas, size -> + canvas.withSaveLayer(size.toRect(), Paint().apply { alpha = .2f }) { + canvas.drawRect(size.toRect(), Paint().apply { color = Color.Gray }) + drawCrossHatch( + color = Color.Red, + strokeWidth = 2.dp, + spaceWidth = 5.dp, + angle = 45f + ) + } } }, text = rendering.toString(), @@ -74,25 +78,25 @@ internal fun placeholderViewFactory(modifier: Modifier): ViewFactory = @Preview(widthDp = 200, heightDp = 200) @Composable private fun PreviewStubViewBindingOnWhite() { Box(backgroundColor = Color.White) { - placeholderViewFactory(Modifier).preview( - rendering = "preview", - modifier = Modifier.fillMaxSize() - .drawBorder(size = 1.dp, color = Color.Red) - ) + PreviewStubBindingPreviewTemplate() } } @Preview(widthDp = 200, heightDp = 200) @Composable private fun PreviewStubViewBindingOnBlack() { Box(backgroundColor = Color.Black) { - placeholderViewFactory(Modifier).preview( - rendering = "preview", - modifier = Modifier.fillMaxSize() - .drawBorder(size = 1.dp, color = Color.Red) - ) + PreviewStubBindingPreviewTemplate() } } +@Composable private fun PreviewStubBindingPreviewTemplate() { + placeholderViewFactory(Modifier).preview( + rendering = "preview", + placeholderModifier = Modifier.fillMaxSize() + .drawBorder(size = 1.dp, color = Color.Red) + ) +} + private fun DrawScope.drawCrossHatch( color: Color, strokeWidth: Dp, @@ -109,34 +113,27 @@ private fun DrawScope.drawHatch( spaceWidth: Dp, angle: Float ) { - val strokeWidthPx = strokeWidth.toPx() - .value - val paint = Paint().also { - it.color = color.scaleColors(.5f) - it.strokeWidth = strokeWidthPx - } - - withSave { - val halfWidth = size.width.value / 2 - val halfHeight = size.height.value / 2 - translate(halfWidth, halfHeight) - rotate(angle) - translate(-halfWidth, -halfHeight) + val strokeWidthPx = strokeWidth.toPx().value + val spaceWidthPx = spaceWidth.toPx().value + val strokeColor = color.scaleColors(.5f) + val stroke = Stroke(width = strokeWidthPx) + rotate(angle) { // Draw outside our bounds to fill the space even when rotated. - val left = -size.width.value - val right = size.width.value * 2 - val top = -size.height.value - val bottom = size.height.value * 2 + val left = -size.width + val right = size.width * 2 + val top = -size.height + val bottom = size.height * 2 var y = top + strokeWidthPx * 2f while (y < bottom) { drawLine( + strokeColor, Offset(left, y), Offset(right, y), - paint + stroke = stroke ) - y += spaceWidth.toPx().value * 2 + y += spaceWidthPx * 2 } } } diff --git a/samples/nested-renderings/src/androidTest/java/com/squareup/sample/nestedrenderings/NestedRenderingsTest.kt b/samples/nested-renderings/src/androidTest/java/com/squareup/sample/nestedrenderings/NestedRenderingsTest.kt index bb8514c4..c2014b74 100644 --- a/samples/nested-renderings/src/androidTest/java/com/squareup/sample/nestedrenderings/NestedRenderingsTest.kt +++ b/samples/nested-renderings/src/androidTest/java/com/squareup/sample/nestedrenderings/NestedRenderingsTest.kt @@ -16,12 +16,15 @@ package com.squareup.sample.nestedrenderings import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.ui.test.SemanticsNodeInteraction +import androidx.ui.test.SemanticsNodeInteractionCollection import androidx.ui.test.android.AndroidComposeTestRule +import androidx.ui.test.assertCountEquals import androidx.ui.test.assertIsDisplayed import androidx.ui.test.doClick import androidx.ui.test.findAllByText import androidx.ui.test.findByText -import com.google.common.truth.Truth.assertThat +import androidx.ui.test.last import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -35,24 +38,26 @@ class NestedRenderingsTest { @Rule @JvmField val composeRule = AndroidComposeTestRule() @Test fun childrenAreAddedAndRemoved() { - val resetButton = findByText("Reset") - findByText(ADD_BUTTON_TEXT) .assertIsDisplayed() .doClick() findAllByText(ADD_BUTTON_TEXT) - .also { addButtons -> - assertThat(addButtons).hasSize(2) - addButtons.forEach { it.doClick() } - } + .assertCountEquals(2) + .forEach { it.doClick() } findAllByText(ADD_BUTTON_TEXT) - .also { addButtons -> - assertThat(addButtons).hasSize(4) - } + .assertCountEquals(4) + + findAllByText("Reset").last() + .doClick() + findAllByText(ADD_BUTTON_TEXT).assertCountEquals(1) + } - resetButton.doClick() - assertThat(findAllByText(ADD_BUTTON_TEXT)).hasSize(1) + private fun SemanticsNodeInteractionCollection.forEach( + block: (SemanticsNodeInteraction) -> Unit + ) { + val count = fetchSemanticsNodes().size + for (i in 0 until count) block(get(i)) } } diff --git a/samples/nested-renderings/src/main/res/values/java/com/squareup/workflow/ui/compose/tooling/ComposeWorkflows.kt b/samples/nested-renderings/src/main/res/values/java/com/squareup/workflow/ui/compose/tooling/ComposeWorkflows.kt deleted file mode 100644 index 85d10445..00000000 --- a/samples/nested-renderings/src/main/res/values/java/com/squareup/workflow/ui/compose/tooling/ComposeWorkflows.kt +++ /dev/null @@ -1,43 +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.tooling - -import androidx.compose.Composable -import androidx.compose.Immutable -import com.squareup.workflow.Sink -import com.squareup.workflow.compose.ComposeWorkflow -import com.squareup.workflow.ui.ViewBinding - -/** - * Draws this [ComposeWorkflow] using a special preview `ViewRegistry`. - * - * The sink passed to [ComposeWorkflow.render] will be a no-op implementation, since previews can't - * process input. - * - * Use inside `@Preview` Composable functions. - */ -//@Composable fun ComposeWorkflow.preview( -// props: PropsT, -// stubBinding: ViewBinding = PreviewStubViewBinding -//) { -// val containerHints = PreviewContainerHints(stubBinding) -// render(props, NoopSink, containerHints) -//} - -//@Immutable -//private object NoopSink : Sink { -// override fun send(value: Any?) = Unit -//}