Skip to content

Commit

Permalink
wip: 🔥 Only three @deprecated remain, all worker related
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrjr committed Oct 13, 2023
1 parent fab75bb commit 6f8879b
Show file tree
Hide file tree
Showing 84 changed files with 53 additions and 6,082 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.ViewRegistry
import com.squareup.workflow1.ui.WorkflowLayout
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.composeViewFactory
import com.squareup.workflow1.ui.compose.withCompositionRoot
import com.squareup.workflow1.ui.container.withEnvironment
import com.squareup.workflow1.ui.plus
Expand All @@ -31,7 +30,8 @@ private val viewEnvironment =
}

/**
* Demonstrates how to create and display a view factory with [composeViewFactory].
* Demonstrates how to create and display a view factory with
* [composeScreenViewFactory][com.squareup.workflow1.ui.compose.composeScreenViewFactory].
*/
class HelloBindingActivity : AppCompatActivity() {
@OptIn(WorkflowUiExperimentalApi::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import com.squareup.sample.timemachine.shakeable.ShakeableTimeMachineScreen
import com.squareup.sample.timemachine.shakeable.ShakeableTimeMachineWorkflow
import com.squareup.sample.timemachine.shakeable.ShakeableTimeMachineWorkflow.PropsFactory
import com.squareup.workflow1.StatelessWorkflow
import com.squareup.workflow1.mapRendering
import com.squareup.workflow1.renderChild
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.asScreen
import kotlin.time.ExperimentalTime
import kotlin.time.TimeSource

Expand All @@ -25,10 +23,9 @@ class TimeMachineAppWorkflow(
context: Context
) : StatelessWorkflow<BoardPath, Nothing, ShakeableTimeMachineScreen>() {

@Suppress("DEPRECATION")
private val timeMachineWorkflow =
ShakeableTimeMachineWorkflow(
TimeMachineWorkflow(appWorkflow.mapRendering { asScreen(it) }, clock),
TimeMachineWorkflow(appWorkflow, clock),
context
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ internal class WorkerCompositionIntegrationTest {
state += 1
}

val workflow = Workflow.stateful<Int, Int, () -> Unit>(
val workflow = Workflow.stateful(
initialState = 0,
render = { _ ->
runningWorker(triggerOutput) { action { setOutput(state) } }
Expand Down Expand Up @@ -221,7 +221,7 @@ internal class WorkerCompositionIntegrationTest {

@Test fun `runningWorker throws when output emitted`() {
@Suppress("UNCHECKED_CAST")
val worker = Worker.from { Unit } as Worker<Nothing>
val worker = Worker.from { } as Worker<Nothing>
val workflow = Workflow.stateless<Unit, Unit, Unit> {
// Suppress runningWorker usage because we want to make sure the deprecated
// method still throws an exception as expected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ internal class RealRenderTesterTest {

@Test fun `verifyAction verifies workflow output`() {
val child = Workflow.stateless<Unit, String, Unit> {}
val workflow = Workflow.stateless<Unit, Nothing, Unit> {
val workflow = Workflow.stateless {
renderChild(child) { TestAction(it) }
}
val testResult = workflow.testRender(Unit)
Expand All @@ -999,7 +999,7 @@ internal class RealRenderTesterTest {

@Test fun `verifyAction verifies worker output`() {
val worker = Worker.finished<String>()
val workflow = Workflow.stateless<Unit, Nothing, Unit> {
val workflow = Workflow.stateless {
runningWorker(worker) { TestAction(it) }
}
val testResult = workflow.testRender(Unit)
Expand Down
11 changes: 0 additions & 11 deletions workflow-ui/compose-tooling/api/compose-tooling.api
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
public final class com/squareup/workflow1/ui/compose/tooling/ComposableSingletons$LegacyViewFactoriesKt {
public static final field INSTANCE Lcom/squareup/workflow1/ui/compose/tooling/ComposableSingletons$LegacyViewFactoriesKt;
public static field lambda-1 Lkotlin/jvm/functions/Function4;
public fun <init> ()V
public final fun getLambda-1$wf1_compose_tooling ()Lkotlin/jvm/functions/Function4;
}

public final class com/squareup/workflow1/ui/compose/tooling/ComposableSingletons$ViewFactoriesKt {
public static final field INSTANCE Lcom/squareup/workflow1/ui/compose/tooling/ComposableSingletons$ViewFactoriesKt;
public static field lambda-1 Lkotlin/jvm/functions/Function4;
public fun <init> ()V
public final fun getLambda-1$wf1_compose_tooling ()Lkotlin/jvm/functions/Function4;
}

public final class com/squareup/workflow1/ui/compose/tooling/LegacyViewFactoriesKt {
public static final fun Preview (Lcom/squareup/workflow1/ui/ViewFactory;Ljava/lang/Object;Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)V
}

public final class com/squareup/workflow1/ui/compose/tooling/ViewFactoriesKt {
public static final fun Preview (Lcom/squareup/workflow1/ui/ScreenViewFactory;Lcom/squareup/workflow1/ui/Screen;Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)V
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:Suppress("TestFunctionName", "PrivatePropertyName", "DEPRECATION")
@file:Suppress("TestFunctionName", "PrivatePropertyName")

package com.squareup.workflow1.ui.compose.tooling

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
@file:Suppress("SameParameterValue", "DEPRECATION")
@file:Suppress("SameParameterValue")
@file:OptIn(WorkflowUiExperimentalApi::class)

package com.squareup.workflow1.ui.compose.tooling

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.draw.drawBehind
Expand All @@ -25,49 +20,12 @@ import androidx.compose.ui.graphics.drawscope.scale
import androidx.compose.ui.graphics.withSaveLayer
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.squareup.workflow1.ui.AsScreen
import com.squareup.workflow1.ui.Screen
import com.squareup.workflow1.ui.ScreenViewFactory
import com.squareup.workflow1.ui.ViewFactory
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.composeScreenViewFactory
import com.squareup.workflow1.ui.compose.composeViewFactory

/**
* A [ViewFactory] that will be used any time a [PreviewViewRegistry] is asked to show a rendering.
* It displays a placeholder graphic and the rendering's `toString()` result.
*/
internal fun placeholderViewFactory(modifier: Modifier): ViewFactory<Any> =
composeViewFactory { rendering, _ ->
BoxWithConstraints {
BasicText(
modifier = modifier
.clipToBounds()
.drawBehind {
drawIntoCanvas { canvas ->
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 = 8.dp,
)
}
}
}
.padding(8.dp),
text = rendering.toString(),
style = TextStyle(
textAlign = TextAlign.Center,
color = Color.White,
shadow = Shadow(blurRadius = 5f, color = Color.Black)
)
)
}
}

/**
* A [ScreenViewFactory] that will be used any time a [PreviewScreenViewFactoryFinder]
Expand All @@ -93,7 +51,7 @@ internal fun placeholderScreenViewFactory(modifier: Modifier): ScreenViewFactory
}
}
.padding(8.dp),
text = (rendering as? AsScreen<*>)?.rendering?.toString() ?: rendering.toString(),
text = rendering.toString(),
style = TextStyle(
textAlign = TextAlign.Center,
color = Color.White,
Expand All @@ -103,61 +61,6 @@ internal fun placeholderScreenViewFactory(modifier: Modifier): ScreenViewFactory
}
}

@Preview(widthDp = 200, heightDp = 200)
@Composable
private fun PreviewStubViewBindingOnWhite() {
Box(Modifier.background(Color.White)) {
PreviewStubBindingPreviewTemplate()
}
}

@Preview(widthDp = 200, heightDp = 200)
@Composable
private fun PreviewStubViewBindingOnBlack() {
Box(Modifier.background(Color.Black)) {
PreviewStubBindingPreviewTemplate()
}
}

@Preview(widthDp = 50, showBackground = true)
@Composable
private fun PreviewStubViewBindingTall() {
Box {
PreviewStubBindingPreviewTemplate("very long text to test cross-hatch rendering edge cases")
}
}

@Preview(widthDp = 200, showBackground = true)
@Composable
private fun PreviewStubViewBindingWide() {
Box {
PreviewStubBindingPreviewTemplate("very long text to test cross-hatch rendering edge cases")
}
}

@Composable private fun PreviewStubBindingPreviewTemplate(previewRendering: String = "preview") {
placeholderViewFactory(Modifier).Preview(
rendering = PlaceholderRendering(previewRendering),
placeholderModifier = Modifier
.fillMaxSize()
.border(width = 1.dp, color = Color.Red)
)
}

private class PlaceholderRendering(val text: String = "preview") : Screen {
override fun equals(other: Any?): Boolean {
return (other as? PlaceholderRendering)?.text == text
}

override fun hashCode(): Int {
return text.hashCode()
}

override fun toString(): String {
return text
}
}

private fun DrawScope.drawCrossHatch(
color: Color,
strokeWidth: Dp,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@file:Suppress("DEPRECATION")
@file:OptIn(WorkflowUiExperimentalApi::class)

package com.squareup.workflow1.ui.compose.tooling
Expand All @@ -11,36 +10,14 @@ import com.squareup.workflow1.ui.Screen
import com.squareup.workflow1.ui.ScreenViewFactory
import com.squareup.workflow1.ui.ScreenViewFactoryFinder
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.ViewFactory
import com.squareup.workflow1.ui.ViewRegistry
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.plus
import kotlin.reflect.KClass

@Deprecated("Use overload with a ScreenViewFactory parameter.")
@Composable
internal fun rememberPreviewViewEnvironment(
placeholderModifier: Modifier,
viewEnvironmentUpdater: ((ViewEnvironment) -> ViewEnvironment)? = null,
mainFactory: ViewFactory<*>? = null
): ViewEnvironment {
val viewRegistry = remember(mainFactory, placeholderModifier) {
PreviewViewRegistry(mainFactory, placeholderViewFactory(placeholderModifier))
}
return remember(viewRegistry, viewEnvironmentUpdater) {
(ViewEnvironment.EMPTY + viewRegistry).let { environment ->
// Give the preview a chance to add its own elements to the ViewEnvironment.
viewEnvironmentUpdater?.let { it(environment) } ?: environment
}
}
}

/**
* Creates and [remember]s a [ViewEnvironment] that has a special [ScreenViewFactoryFinder]
* and any additional elements as configured by [viewEnvironmentUpdater].
*
* The [ScreenViewFactoryFinder] will contain [mainFactory] if specified, as well as a
* [placeholderViewFactory] that will be used to show any renderings that don't match
* [placeholderScreenViewFactory] that will be used to show any renderings that don't match
* [mainFactory]'s type. All placeholders will have [placeholderModifier] applied.
*/
@Composable internal fun rememberPreviewViewEnvironment(
Expand All @@ -59,26 +36,6 @@ internal fun rememberPreviewViewEnvironment(
}
}

/**
* A [ViewRegistry] that uses [mainFactory] for rendering [RenderingT]s, and [placeholderFactory]
* for all other [WorkflowRendering][com.squareup.workflow1.ui.compose.WorkflowRendering] calls.
*/
@Immutable
private class PreviewViewRegistry<RenderingT : Any>(
private val mainFactory: ViewFactory<RenderingT>? = null,
private val placeholderFactory: ViewFactory<Any>
) : ViewRegistry {
override val keys: Set<KClass<*>> get() = mainFactory?.let { setOf(it.type) } ?: emptySet()

@Suppress("UNCHECKED_CAST")
override fun <RenderingT : Any> getEntryFor(
renderingType: KClass<out RenderingT>
): ViewFactory<RenderingT> = when (renderingType) {
mainFactory?.type -> mainFactory
else -> placeholderFactory
} as ViewFactory<RenderingT>
}

/**
* A [ScreenViewFactoryFinder] that uses [mainFactory] for rendering [RenderingT]s,
* and [placeholderFactory] for all other
Expand Down
Loading

0 comments on commit 6f8879b

Please sign in to comment.