Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: Compose overhaul makes ViewEnvironment less in your face. #1207

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ private val viewEnvironment = ViewEnvironment.EMPTY.withComposeInteropSupport()
)
WorkflowRendering(
rendering,
viewEnvironment,
Modifier.border(
shape = RoundedCornerShape(10.dp),
width = 10.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.ComposeScreen
import com.squareup.workflow1.ui.compose.tooling.Preview
Expand All @@ -18,7 +17,7 @@ data class HelloComposeScreen(
val message: String,
val onClick: () -> Unit
) : ComposeScreen {
@Composable override fun Content(viewEnvironment: ViewEnvironment) {
@Composable override fun Content() {
Text(
message,
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.squareup.workflow1.ui.compose.ScreenComposableFactory
import com.squareup.workflow1.ui.compose.tooling.Preview

@OptIn(WorkflowUiExperimentalApi::class)
val HelloBinding = ScreenComposableFactory<Rendering> { rendering, _ ->
val HelloBinding = ScreenComposableFactory<Rendering> { rendering ->
Text(
rendering.message,
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.squareup.workflow1.ui.ViewRegistry
import com.squareup.workflow1.ui.WorkflowLayout
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.withComposeInteropSupport
import com.squareup.workflow1.ui.compose.withCompositionRoot
import com.squareup.workflow1.ui.plus
import com.squareup.workflow1.ui.renderWorkflowIn
import com.squareup.workflow1.ui.withEnvironment
Expand All @@ -27,10 +26,9 @@ import kotlinx.coroutines.flow.StateFlow
@OptIn(WorkflowUiExperimentalApi::class)
private val viewEnvironment =
(ViewEnvironment.EMPTY + ViewRegistry(HelloBinding))
.withCompositionRoot { content ->
.withComposeInteropSupport { content ->
MaterialTheme(content = content)
}
.withComposeInteropSupport()

/**
* Demonstrates how to create and display a view factory with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.squareup.workflow1.Sink
import com.squareup.workflow1.StatefulWorkflow
import com.squareup.workflow1.Workflow
import com.squareup.workflow1.WorkflowIdentifier
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.ComposeScreen

Expand Down Expand Up @@ -35,17 +34,15 @@ abstract class ComposeWorkflow<in PropsT, out OutputT : Any> :

/**
* Renders [props] by emitting Compose UI. This function will be called to update the UI whenever
* the [props] or [viewEnvironment] change.
* the [props] change.
*
* @param props The data to render.
* @param outputSink A [Sink] that can be used from UI event handlers to send outputs to this
* workflow's parent.
* @param viewEnvironment The [ViewEnvironment] passed down through the `ViewBinding` pipeline.
*/
@Composable abstract fun RenderingContent(
props: PropsT,
outputSink: Sink<OutputT>,
viewEnvironment: ViewEnvironment
)

override fun asStatefulWorkflow(): StatefulWorkflow<PropsT, *, OutputT, ComposeScreen> =
Expand All @@ -62,14 +59,12 @@ inline fun <PropsT, OutputT : Any> Workflow.Companion.composed(
crossinline render: @Composable (
props: PropsT,
outputSink: Sink<OutputT>,
environment: ViewEnvironment
) -> Unit
): ComposeWorkflow<PropsT, OutputT> = object : ComposeWorkflow<PropsT, OutputT>() {
@Composable override fun RenderingContent(
props: PropsT,
outputSink: Sink<OutputT>,
viewEnvironment: ViewEnvironment
) {
render(props, outputSink, viewEnvironment)
render(props, outputSink)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.squareup.workflow1.Snapshot
import com.squareup.workflow1.StatefulWorkflow
import com.squareup.workflow1.action
import com.squareup.workflow1.contraMap
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.ComposeScreen

Expand Down Expand Up @@ -39,12 +38,12 @@ internal class ComposeWorkflowImpl<PropsT, OutputT : Any>(
propsHolder,
sinkHolder,
object : ComposeScreen {
@Composable override fun Content(viewEnvironment: ViewEnvironment) {
@Composable override fun Content() {
// The sink will get set on the first render pass, which must happen before this is first
// composed, so it should never be null.
val sink = sinkHolder.sink!!
// Important: Use the props from the MutableState, _not_ the one passed into render.
workflow.RenderingContent(propsHolder.value, sink, viewEnvironment)
workflow.RenderingContent(propsHolder.value, sink)
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.squareup.sample.compose.hellocomposeworkflow.HelloComposeWorkflow.Tog
import com.squareup.workflow1.Sink
import com.squareup.workflow1.WorkflowExperimentalRuntime
import com.squareup.workflow1.config.AndroidRuntimeConfigTools
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.WorkflowRendering
import com.squareup.workflow1.ui.compose.renderAsState
Expand All @@ -34,7 +33,6 @@ object HelloComposeWorkflow : ComposeWorkflow<String, Toggle>() {
@Composable override fun RenderingContent(
props: String,
outputSink: Sink<Toggle>,
viewEnvironment: ViewEnvironment
) {
MaterialTheme {
Text(
Expand All @@ -57,5 +55,5 @@ fun HelloComposeWorkflowPreview() {
onOutput = {},
runtimeConfig = AndroidRuntimeConfigTools.getAppWorkflowRuntimeConfig()
)
WorkflowRendering(rendering, ViewEnvironment.EMPTY)
WorkflowRendering(rendering)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.squareup.workflow1.WorkflowExperimentalRuntime
import com.squareup.workflow1.config.AndroidRuntimeConfigTools
import com.squareup.workflow1.parse
import com.squareup.workflow1.ui.Screen
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.ComposeScreen
import com.squareup.workflow1.ui.compose.WorkflowRendering
Expand Down Expand Up @@ -60,7 +59,7 @@ fun InlineRenderingWorkflowRendering() {
onOutput = {},
runtimeConfig = AndroidRuntimeConfigTools.getAppWorkflowRuntimeConfig()
)
WorkflowRendering(rendering, ViewEnvironment.EMPTY)
WorkflowRendering(rendering)
}

@Preview(showBackground = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,22 @@ import com.squareup.workflow1.ui.ViewRegistry
import com.squareup.workflow1.ui.WorkflowLayout
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.withComposeInteropSupport
import com.squareup.workflow1.ui.compose.withCompositionRoot
import com.squareup.workflow1.ui.plus
import com.squareup.workflow1.ui.renderWorkflowIn
import com.squareup.workflow1.ui.withEnvironment
import kotlinx.coroutines.flow.StateFlow

@OptIn(WorkflowUiExperimentalApi::class)
private val viewRegistry = ViewRegistry(RecursiveViewFactory)
private val viewRegistry = ViewRegistry(RecursiveComposableFactory)

@OptIn(WorkflowUiExperimentalApi::class)
private val viewEnvironment =
(ViewEnvironment.EMPTY + viewRegistry)
.withCompositionRoot { content ->
.withComposeInteropSupport { content ->
CompositionLocalProvider(LocalBackgroundColor provides Color.Green) {
content()
}
}
.withComposeInteropSupport()

@WorkflowUiExperimentalApi
class NestedRenderingsActivity : AppCompatActivity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,21 @@ import androidx.compose.ui.tooling.preview.Preview
import com.squareup.sample.compose.R
import com.squareup.sample.compose.nestedrenderings.RecursiveWorkflow.Rendering
import com.squareup.workflow1.ui.Screen
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.ScreenComposableFactory
import com.squareup.workflow1.ui.compose.WorkflowRendering
import com.squareup.workflow1.ui.compose.tooling.Preview

/**
* Composition local of [Color] to use as the background color for a [RecursiveViewFactory].
* Composition local of [Color] to use as the background color for a [RecursiveComposableFactory].
*/
val LocalBackgroundColor = compositionLocalOf<Color> { error("No background color specified") }

/**
* A `ViewFactory` that renders [RecursiveWorkflow.Rendering]s.
*/
@OptIn(WorkflowUiExperimentalApi::class)
val RecursiveViewFactory = ScreenComposableFactory<Rendering> { rendering, viewEnvironment ->
val RecursiveComposableFactory = ScreenComposableFactory<Rendering> { rendering ->
// Every child should be drawn with a slightly-darker background color.
val color = LocalBackgroundColor.current
val childColor = remember(color) {
Expand All @@ -57,7 +56,6 @@ val RecursiveViewFactory = ScreenComposableFactory<Rendering> { rendering, viewE
CompositionLocalProvider(LocalBackgroundColor provides childColor) {
Children(
rendering.children,
viewEnvironment,
// Pass a weight so that the column fills all the space not occupied by the buttons.
modifier = Modifier.weight(1f, fill = true)
)
Expand All @@ -75,7 +73,7 @@ val RecursiveViewFactory = ScreenComposableFactory<Rendering> { rendering, viewE
@Composable
fun RecursiveViewFactoryPreview() {
CompositionLocalProvider(LocalBackgroundColor provides Color.Green) {
RecursiveViewFactory.Preview(
RecursiveComposableFactory.Preview(
Rendering(
children = listOf(
StringRendering("foo"),
Expand All @@ -97,7 +95,6 @@ fun RecursiveViewFactoryPreview() {
@Composable
private fun Children(
children: List<Screen>,
viewEnvironment: ViewEnvironment,
modifier: Modifier
) {
Column(
Expand All @@ -110,7 +107,6 @@ private fun Children(
childRendering,
// Pass a weight so all children are partitioned evenly within the total column space.
// Without the weight, each child is the full size of the parent.
viewEnvironment,
modifier = Modifier
.weight(1f, fill = true)
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.squareup.workflow1.ui.Screen
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.ComposeScreen
import com.squareup.workflow1.ui.compose.WorkflowRendering
Expand Down Expand Up @@ -59,8 +58,8 @@ data class ContactRendering(
val name: String,
val details: ContactDetailsRendering
) : ComposeScreen {
@Composable override fun Content(viewEnvironment: ViewEnvironment) {
ContactDetails(this, viewEnvironment)
@Composable override fun Content() {
ContactDetails(this)
}
}

Expand All @@ -70,10 +69,7 @@ data class ContactDetailsRendering(
) : Screen

@Composable
private fun ContactDetails(
rendering: ContactRendering,
environment: ViewEnvironment
) {
private fun ContactDetails(rendering: ContactRendering) {
Card(
modifier = Modifier
.padding(8.dp)
Expand All @@ -86,7 +82,6 @@ private fun ContactDetails(
Text(rendering.name, style = MaterialTheme.typography.body1)
WorkflowRendering(
rendering = rendering.details,
viewEnvironment = environment,
modifier = Modifier
.aspectRatio(1f)
.border(0.dp, Color.LightGray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import com.squareup.workflow1.config.AndroidRuntimeConfigTools
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.ViewRegistry
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.WorkflowRendering
import com.squareup.workflow1.ui.compose.RootScreen
import com.squareup.workflow1.ui.compose.renderAsState
import com.squareup.workflow1.ui.plus

private val viewEnvironment = ViewEnvironment.EMPTY + ViewRegistry(TextInputViewFactory)
private val viewEnvironment = ViewEnvironment.EMPTY + ViewRegistry(TextInputComposableFactory)

@Composable fun TextInputApp() {
MaterialTheme {
Expand All @@ -24,7 +24,7 @@ private val viewEnvironment = ViewEnvironment.EMPTY + ViewRegistry(TextInputView
onOutput = {},
runtimeConfig = AndroidRuntimeConfigTools.getAppWorkflowRuntimeConfig()
)
WorkflowRendering(rendering, viewEnvironment)
viewEnvironment.RootScreen(rendering)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.squareup.workflow1.ui.compose.asMutableState
import com.squareup.workflow1.ui.compose.tooling.Preview

@OptIn(WorkflowUiExperimentalApi::class)
val TextInputViewFactory = ScreenComposableFactory<Rendering> { rendering, _ ->
val TextInputComposableFactory = ScreenComposableFactory<Rendering> { rendering ->
Column(
modifier = Modifier
.fillMaxSize()
Expand Down Expand Up @@ -52,7 +52,7 @@ val TextInputViewFactory = ScreenComposableFactory<Rendering> { rendering, _ ->
@Preview(showBackground = true)
@Composable
private fun TextInputViewFactoryPreview() {
TextInputViewFactory.Preview(
TextInputComposableFactory.Preview(
Rendering(
textController = TextController("Hello world"),
onSwapText = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.Screen
import com.squareup.workflow1.ui.ViewEnvironmentKey
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.compose.LocalWorkflowEnvironment
import com.squareup.workflow1.ui.compose.ScreenComposableFactory
import com.squareup.workflow1.ui.compose.WorkflowRendering
import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule
Expand Down Expand Up @@ -96,10 +97,10 @@ internal class PreviewViewFactoryTest {
}

private val ParentWithOneChild =
ScreenComposableFactory<TwoStrings> { rendering, environment ->
ScreenComposableFactory<TwoStrings> { rendering ->
Column {
BasicText(rendering.first.text)
WorkflowRendering(rendering.second, environment)
WorkflowRendering(rendering.second)
}
}

Expand All @@ -109,11 +110,11 @@ internal class PreviewViewFactoryTest {
}

private val ParentWithTwoChildren =
ScreenComposableFactory<ThreeStrings> { rendering, environment ->
ScreenComposableFactory<ThreeStrings> { rendering ->
Column {
WorkflowRendering(rendering.first, environment)
WorkflowRendering(rendering.first)
BasicText(rendering.second.text)
WorkflowRendering(rendering.third, environment)
WorkflowRendering(rendering.third)
}
}

Expand Down Expand Up @@ -156,11 +157,11 @@ internal class PreviewViewFactoryTest {
) : Screen

private val ParentRecursive =
ScreenComposableFactory<RecursiveRendering> { rendering, environment ->
ScreenComposableFactory<RecursiveRendering> { rendering ->
Column {
BasicText(rendering.text)
rendering.child?.let { child ->
WorkflowRendering(rendering = child, viewEnvironment = environment)
WorkflowRendering(rendering = child)
}
}
}
Expand Down Expand Up @@ -198,8 +199,8 @@ internal class PreviewViewFactoryTest {
override val default: String get() = error("Not specified")
}

private val ParentConsumesCustomKey = ScreenComposableFactory<TwoStrings> { _, environment ->
BasicText(environment[TestEnvironmentKey])
private val ParentConsumesCustomKey = ScreenComposableFactory<TwoStrings> { _ ->
BasicText(LocalWorkflowEnvironment.current[TestEnvironmentKey])
}

@Preview @Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import com.squareup.workflow1.ui.compose.ScreenComposableFactory
internal fun placeholderScreenComposableFactory(
modifier: Modifier
): ScreenComposableFactory<Screen> =
ScreenComposableFactory { rendering, _ ->
ScreenComposableFactory { rendering ->
BoxWithConstraints {
BasicText(
modifier = modifier
Expand Down
Loading
Loading