From adfea020739129d5a2873f93c48e7a680fcfe56f Mon Sep 17 00:00:00 2001 From: Ray Ryan Date: Thu, 18 May 2023 15:33:47 -0700 Subject: [PATCH 1/2] Introduces `fun Screen.deepMap(Screen): Screen` Introduces and uses `Screen`-specific subtypes of the `Container` and `Wrapper` interfaces. This allows the inroduction of `Screen.deepMap()`, which allows us to apply a transformation to the "real" `Screen`s collected in a `Container`, no matter how deeply wrapped they are. --- .../sample/container/panel/ScrimScreen.kt | 3 +- .../nestedoverlays/TopAndBottomBarsScreen.kt | 4 +-- .../workflow1/ui/ScreenContainerTest.kt | 23 ++++++++++++++ workflow-ui/core-android/api/core-android.api | 18 ++++++++++- .../com/squareup/workflow1/ui/UiFactory.kt | 24 ++++++++++++++ .../com/squareup/workflow1/ui/VisualHolder.kt | 24 ++++++++++++++ .../ui/container/BackButtonScreen.kt | 5 ++- .../workflow1/ui/ScreenViewFactoryTest.kt | 2 +- workflow-ui/core-common/api/core-common.api | 28 +++++++++++++++-- .../com/squareup/workflow1/ui/NamedScreen.kt | 2 +- .../squareup/workflow1/ui/ScreenContainer.kt | 31 +++++++++++++++++++ .../workflow1/ui/container/BackStackScreen.kt | 4 +-- .../ui/container/EnvironmentScreen.kt | 5 +-- 13 files changed, 157 insertions(+), 16 deletions(-) create mode 100644 workflow-ui/container-android/src/test/java/com/squareup/workflow1/ui/ScreenContainerTest.kt create mode 100644 workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/UiFactory.kt create mode 100644 workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/VisualHolder.kt create mode 100644 workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ScreenContainer.kt diff --git a/samples/containers/common/src/main/java/com/squareup/sample/container/panel/ScrimScreen.kt b/samples/containers/common/src/main/java/com/squareup/sample/container/panel/ScrimScreen.kt index daef4cd52..13fa600c4 100644 --- a/samples/containers/common/src/main/java/com/squareup/sample/container/panel/ScrimScreen.kt +++ b/samples/containers/common/src/main/java/com/squareup/sample/container/panel/ScrimScreen.kt @@ -1,6 +1,7 @@ package com.squareup.sample.container.panel import com.squareup.workflow1.ui.Screen +import com.squareup.workflow1.ui.ScreenWrapper import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.Wrapper @@ -12,6 +13,6 @@ import com.squareup.workflow1.ui.Wrapper class ScrimScreen( override val content: C, val dimmed: Boolean -) : Wrapper, Screen { +) : ScreenWrapper, Screen { override fun map(transform: (C) -> D) = ScrimScreen(transform(content), dimmed) } diff --git a/samples/nested-overlays/src/main/java/com/squareup/sample/nestedoverlays/TopAndBottomBarsScreen.kt b/samples/nested-overlays/src/main/java/com/squareup/sample/nestedoverlays/TopAndBottomBarsScreen.kt index 9314cd33b..80c6edb02 100644 --- a/samples/nested-overlays/src/main/java/com/squareup/sample/nestedoverlays/TopAndBottomBarsScreen.kt +++ b/samples/nested-overlays/src/main/java/com/squareup/sample/nestedoverlays/TopAndBottomBarsScreen.kt @@ -7,15 +7,15 @@ import com.squareup.workflow1.ui.AndroidScreen import com.squareup.workflow1.ui.Screen import com.squareup.workflow1.ui.ScreenViewFactory import com.squareup.workflow1.ui.ScreenViewFactory.Companion.fromViewBinding +import com.squareup.workflow1.ui.ScreenWrapper import com.squareup.workflow1.ui.WorkflowUiExperimentalApi -import com.squareup.workflow1.ui.Wrapper @OptIn(WorkflowUiExperimentalApi::class) data class TopAndBottomBarsScreen( override val content: T, val topBar: ButtonBar? = null, val bottomBar: ButtonBar? = null -) : AndroidScreen>, Wrapper { +) : AndroidScreen>, ScreenWrapper { override fun map(transform: (T) -> ContentU) = TopAndBottomBarsScreen(transform(content), topBar, bottomBar) diff --git a/workflow-ui/container-android/src/test/java/com/squareup/workflow1/ui/ScreenContainerTest.kt b/workflow-ui/container-android/src/test/java/com/squareup/workflow1/ui/ScreenContainerTest.kt new file mode 100644 index 000000000..679ae3caf --- /dev/null +++ b/workflow-ui/container-android/src/test/java/com/squareup/workflow1/ui/ScreenContainerTest.kt @@ -0,0 +1,23 @@ +@file:OptIn(WorkflowUiExperimentalApi::class) + +package com.squareup.workflow1.ui + +import com.google.common.truth.Truth.assertThat +import com.squareup.workflow1.ui.container.BackStackScreen +import com.squareup.workflow1.ui.container.EnvironmentScreen +import com.squareup.workflow1.ui.container.withEnvironment +import org.junit.Test + +internal class ScreenContainerTest { + object MyScreen : Screen + + @Test + fun deepMapRecurses() { + val backStack = BackStackScreen(NamedScreen(MyScreen, "name")) + @Suppress("UNCHECKED_CAST") + val mappedBackStack = backStack + .deepMap { it.withEnvironment() } as BackStackScreen>> + + assertThat(mappedBackStack.top.content.content).isSameInstanceAs(MyScreen) + } +} diff --git a/workflow-ui/core-android/api/core-android.api b/workflow-ui/core-android/api/core-android.api index 007cdd5c2..4b2639d32 100644 --- a/workflow-ui/core-android/api/core-android.api +++ b/workflow-ui/core-android/api/core-android.api @@ -205,6 +205,20 @@ public abstract interface class com/squareup/workflow1/ui/ViewStarter { public abstract fun startView (Landroid/view/View;Lkotlin/jvm/functions/Function0;)V } +public abstract interface class com/squareup/workflow1/ui/VisualFactory { + public abstract fun createOrNull (Ljava/lang/Object;Ljava/lang/Object;Lcom/squareup/workflow1/ui/ViewEnvironment;Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/VisualHolder; +} + +public abstract interface class com/squareup/workflow1/ui/VisualHolder { + public static final field Companion Lcom/squareup/workflow1/ui/VisualHolder$Companion; + public abstract fun getVisual ()Ljava/lang/Object; + public abstract fun update (Ljava/lang/Object;)Z +} + +public final class com/squareup/workflow1/ui/VisualHolder$Companion { + public final fun invoke (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/VisualHolder; +} + public final class com/squareup/workflow1/ui/WorkflowLayout : android/widget/FrameLayout { public fun (Landroid/content/Context;Landroid/util/AttributeSet;)V public synthetic fun (Landroid/content/Context;Landroid/util/AttributeSet;ILkotlin/jvm/internal/DefaultConstructorMarker;)V @@ -339,7 +353,7 @@ public final class com/squareup/workflow1/ui/container/AsDialogHolderWithContent public static final fun fixBackgroundAndDimming (Landroid/app/Dialog;)V } -public final class com/squareup/workflow1/ui/container/BackButtonScreen : com/squareup/workflow1/ui/AndroidScreen, com/squareup/workflow1/ui/Wrapper { +public final class com/squareup/workflow1/ui/container/BackButtonScreen : com/squareup/workflow1/ui/AndroidScreen, com/squareup/workflow1/ui/ScreenWrapper { public fun (Lcom/squareup/workflow1/ui/Screen;ZLkotlin/jvm/functions/Function0;)V public synthetic fun (Lcom/squareup/workflow1/ui/Screen;ZLkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun asSequence ()Lkotlin/sequences/Sequence; @@ -351,6 +365,8 @@ public final class com/squareup/workflow1/ui/container/BackButtonScreen : com/sq public fun getViewFactory ()Lcom/squareup/workflow1/ui/ScreenViewFactory; public final fun getWrapped ()Lcom/squareup/workflow1/ui/Screen; public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/Container; + public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/ScreenContainer; + public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/ScreenWrapper; public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/Wrapper; public fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/container/BackButtonScreen; } diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/UiFactory.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/UiFactory.kt new file mode 100644 index 000000000..43ff9d09d --- /dev/null +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/UiFactory.kt @@ -0,0 +1,24 @@ +package com.squareup.workflow1.ui + +@WorkflowUiExperimentalApi +public fun interface VisualFactory { + /** + * Given a ui model ([rendering]), creates a [VisualHolder] which pairs: + * + * - a native view system object of type [VisualT] -- a [visual][VisualHolder.visual] + * - an [update function][VisualHolder.update] to apply [RenderingT] instances to + * the new [VisualT] instance. + * + * This method must not call [VisualHolder.update], to ensure that callers have + * complete control over the lifecycle of the new [VisualT]. + * + * @param getFactory can be used to make recursive calls to build VisualT + * instances for sub-parts of [rendering] + */ + public fun createOrNull( + rendering: RenderingT, + context: ContextT, + environment: ViewEnvironment, + getFactory: (ViewEnvironment) -> VisualFactory + ): VisualHolder? +} diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/VisualHolder.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/VisualHolder.kt new file mode 100644 index 000000000..3c38e97be --- /dev/null +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/VisualHolder.kt @@ -0,0 +1,24 @@ +package com.squareup.workflow1.ui + +@WorkflowUiExperimentalApi +public interface VisualHolder { + public val visual: VisualT + + public fun update(rendering: RenderingT): Boolean + + public companion object { + public operator fun invoke( + visual: VisualT, + onUpdate: (RenderingT) -> Unit + ): VisualHolder { + return object : VisualHolder { + override val visual = visual + + override fun update(rendering: RenderingT): Boolean { + onUpdate(rendering) + return true + } + } + } + } +} diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/BackButtonScreen.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/BackButtonScreen.kt index 308ec4bbb..3ca22b86d 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/BackButtonScreen.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/BackButtonScreen.kt @@ -4,8 +4,8 @@ import com.squareup.workflow1.ui.AndroidScreen import com.squareup.workflow1.ui.Compatible.Companion.keyFor import com.squareup.workflow1.ui.Screen import com.squareup.workflow1.ui.ScreenViewFactory +import com.squareup.workflow1.ui.ScreenWrapper import com.squareup.workflow1.ui.WorkflowUiExperimentalApi -import com.squareup.workflow1.ui.Wrapper import com.squareup.workflow1.ui.setBackHandler /** @@ -28,10 +28,9 @@ public class BackButtonScreen( public override val content: C, public val shadow: Boolean = false, public val onBackPressed: (() -> Unit)? = null -) : Wrapper, AndroidScreen> { +) : ScreenWrapper, AndroidScreen> { // If they change the shadow value, we need to build a new view to reorder the handlers. override val compatibilityKey: String = keyFor(content, "BackButtonScreen+shadow:$shadow") - override fun map(transform: (C) -> D): BackButtonScreen = BackButtonScreen(transform(content), shadow, onBackPressed) diff --git a/workflow-ui/core-android/src/test/java/com/squareup/workflow1/ui/ScreenViewFactoryTest.kt b/workflow-ui/core-android/src/test/java/com/squareup/workflow1/ui/ScreenViewFactoryTest.kt index 4a888559b..123ced75e 100644 --- a/workflow-ui/core-android/src/test/java/com/squareup/workflow1/ui/ScreenViewFactoryTest.kt +++ b/workflow-ui/core-android/src/test/java/com/squareup/workflow1/ui/ScreenViewFactoryTest.kt @@ -72,7 +72,7 @@ internal class ScreenViewFactoryTest { @OptIn(WorkflowUiExperimentalApi::class) private class MyWrapper( override val content: C - ) : Wrapper, AndroidScreen> { + ) : ScreenWrapper, AndroidScreen> { override fun map(transform: (C) -> D) = MyWrapper(transform(content)) override val viewFactory = forWrapper, C>() } diff --git a/workflow-ui/core-common/api/core-common.api b/workflow-ui/core-common/api/core-common.api index 17b3865a2..42d73c21e 100644 --- a/workflow-ui/core-common/api/core-common.api +++ b/workflow-ui/core-common/api/core-common.api @@ -46,7 +46,7 @@ public final class com/squareup/workflow1/ui/Named : com/squareup/workflow1/ui/C public fun toString ()Ljava/lang/String; } -public final class com/squareup/workflow1/ui/NamedScreen : com/squareup/workflow1/ui/Screen, com/squareup/workflow1/ui/Wrapper { +public final class com/squareup/workflow1/ui/NamedScreen : com/squareup/workflow1/ui/ScreenWrapper { public fun (Lcom/squareup/workflow1/ui/Screen;Ljava/lang/String;)V public fun asSequence ()Lkotlin/sequences/Sequence; public final fun component1 ()Lcom/squareup/workflow1/ui/Screen; @@ -62,6 +62,8 @@ public final class com/squareup/workflow1/ui/NamedScreen : com/squareup/workflow public fun hashCode ()I public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/Container; public fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/NamedScreen; + public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/ScreenContainer; + public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/ScreenWrapper; public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/Wrapper; public fun toString ()Ljava/lang/String; } @@ -69,6 +71,23 @@ public final class com/squareup/workflow1/ui/NamedScreen : com/squareup/workflow public abstract interface class com/squareup/workflow1/ui/Screen { } +public abstract interface class com/squareup/workflow1/ui/ScreenContainer : com/squareup/workflow1/ui/Container, com/squareup/workflow1/ui/Screen { + public abstract fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/ScreenContainer; +} + +public final class com/squareup/workflow1/ui/ScreenContainerKt { + public static final fun deepMap (Lcom/squareup/workflow1/ui/Screen;Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/Screen; +} + +public abstract interface class com/squareup/workflow1/ui/ScreenWrapper : com/squareup/workflow1/ui/Screen, com/squareup/workflow1/ui/ScreenContainer, com/squareup/workflow1/ui/Wrapper { + public abstract fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/ScreenWrapper; +} + +public final class com/squareup/workflow1/ui/ScreenWrapper$DefaultImpls { + public static fun asSequence (Lcom/squareup/workflow1/ui/ScreenWrapper;)Lkotlin/sequences/Sequence; + public static fun getCompatibilityKey (Lcom/squareup/workflow1/ui/ScreenWrapper;)Ljava/lang/String; +} + public abstract interface class com/squareup/workflow1/ui/TextController { public abstract fun getOnTextChanged ()Lkotlinx/coroutines/flow/Flow; public abstract fun getTextValue ()Ljava/lang/String; @@ -214,7 +233,7 @@ public final class com/squareup/workflow1/ui/container/BackStackConfigKt { public static final fun plus (Lcom/squareup/workflow1/ui/ViewEnvironment;Lcom/squareup/workflow1/ui/container/BackStackConfig;)Lcom/squareup/workflow1/ui/ViewEnvironment; } -public final class com/squareup/workflow1/ui/container/BackStackScreen : com/squareup/workflow1/ui/Container, com/squareup/workflow1/ui/Screen { +public final class com/squareup/workflow1/ui/container/BackStackScreen : com/squareup/workflow1/ui/ScreenContainer { public static final field Companion Lcom/squareup/workflow1/ui/container/BackStackScreen$Companion; public fun (Lcom/squareup/workflow1/ui/Screen;Ljava/util/List;)V public fun (Lcom/squareup/workflow1/ui/Screen;[Lcom/squareup/workflow1/ui/Screen;)V @@ -226,6 +245,7 @@ public final class com/squareup/workflow1/ui/container/BackStackScreen : com/squ public final fun getTop ()Lcom/squareup/workflow1/ui/Screen; public fun hashCode ()I public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/Container; + public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/ScreenContainer; public fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/container/BackStackScreen; public final fun mapIndexed (Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow1/ui/container/BackStackScreen; public fun toString ()Ljava/lang/String; @@ -255,7 +275,7 @@ public final class com/squareup/workflow1/ui/container/BodyAndOverlaysScreen : c public final fun mapOverlays (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/container/BodyAndOverlaysScreen; } -public final class com/squareup/workflow1/ui/container/EnvironmentScreen : com/squareup/workflow1/ui/Screen, com/squareup/workflow1/ui/Wrapper { +public final class com/squareup/workflow1/ui/container/EnvironmentScreen : com/squareup/workflow1/ui/ScreenWrapper { public fun (Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;)V public synthetic fun (Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun asSequence ()Lkotlin/sequences/Sequence; @@ -265,6 +285,8 @@ public final class com/squareup/workflow1/ui/container/EnvironmentScreen : com/s public final fun getEnvironment ()Lcom/squareup/workflow1/ui/ViewEnvironment; public final fun getWrapped ()Lcom/squareup/workflow1/ui/Screen; public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/Container; + public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/ScreenContainer; + public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/ScreenWrapper; public synthetic fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/Wrapper; public fun map (Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/ui/container/EnvironmentScreen; } diff --git a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/NamedScreen.kt b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/NamedScreen.kt index a20196b95..204c8647b 100644 --- a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/NamedScreen.kt +++ b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/NamedScreen.kt @@ -11,7 +11,7 @@ package com.squareup.workflow1.ui public data class NamedScreen( override val content: C, val name: String -) : Screen, Wrapper { +) : ScreenWrapper { init { require(name.isNotBlank()) { "name must not be blank." } } diff --git a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ScreenContainer.kt b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ScreenContainer.kt new file mode 100644 index 000000000..8c7f0f600 --- /dev/null +++ b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ScreenContainer.kt @@ -0,0 +1,31 @@ +package com.squareup.workflow1.ui + +@WorkflowUiExperimentalApi +public interface ScreenContainer : Container, Screen { + public override fun map(transform: (C) -> D): ScreenContainer +} + +@WorkflowUiExperimentalApi +public interface ScreenWrapper : ScreenContainer, Wrapper, Screen { + public override fun map(transform: (C) -> D): ScreenWrapper +} + +/** + * Applies [transform] to the receiver unless it is a [ScreenContainer]. In that case, + * makes a recursive call to [ScreenContainer.map] and applies [deepMap] to its + * contents. + * + * For example, consider this snippet: + * + * val backStack = BackStackScreen(SomeWrapper(theRealScreen)) + * val loggingBackStack = backStack.deepMap { WithLogging(it) } + * + * `loggingBackStack` will have a structure like so: + * + * BackStackScreen(SomeWrapper(WithLogging(theRealScreen))) + */ +@WorkflowUiExperimentalApi +public fun Screen.deepMap(transform: (Screen) -> Screen): Screen { + return if (this is ScreenContainer<*>) map { it.deepMap(transform) } + else transform(this) +} diff --git a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/BackStackScreen.kt b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/BackStackScreen.kt index 7bd13ad70..71351d526 100644 --- a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/BackStackScreen.kt +++ b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/BackStackScreen.kt @@ -1,7 +1,7 @@ package com.squareup.workflow1.ui.container -import com.squareup.workflow1.ui.Container import com.squareup.workflow1.ui.Screen +import com.squareup.workflow1.ui.ScreenContainer import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.container.BackStackScreen.Companion import com.squareup.workflow1.ui.container.BackStackScreen.Companion.fromList @@ -22,7 +22,7 @@ import com.squareup.workflow1.ui.container.BackStackScreen.Companion.fromListOrN @WorkflowUiExperimentalApi public class BackStackScreen internal constructor( public val frames: List -) : Screen, Container { +) : ScreenContainer { /** * Creates a screen with elements listed from the [bottom] to the top. */ diff --git a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/EnvironmentScreen.kt b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/EnvironmentScreen.kt index c82219d06..67a549121 100644 --- a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/EnvironmentScreen.kt +++ b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/container/EnvironmentScreen.kt @@ -1,10 +1,10 @@ package com.squareup.workflow1.ui.container import com.squareup.workflow1.ui.Screen +import com.squareup.workflow1.ui.ScreenWrapper import com.squareup.workflow1.ui.ViewEnvironment import com.squareup.workflow1.ui.ViewRegistry import com.squareup.workflow1.ui.WorkflowUiExperimentalApi -import com.squareup.workflow1.ui.Wrapper import com.squareup.workflow1.ui.plus /** @@ -19,7 +19,7 @@ import com.squareup.workflow1.ui.plus public class EnvironmentScreen( public override val content: C, public val environment: ViewEnvironment = ViewEnvironment.EMPTY -) : Wrapper, Screen { +) : ScreenWrapper { override fun map(transform: (C) -> D): EnvironmentScreen = EnvironmentScreen(transform(content), environment) @@ -58,6 +58,7 @@ public fun Screen.withEnvironment( EnvironmentScreen(content, this.environment + environment) } } + else -> EnvironmentScreen(this, environment) } } From 30be037f4fe32978ec4cdd3de21d3c8bcf2c0d74 Mon Sep 17 00:00:00 2001 From: rjrjr Date: Wed, 15 Nov 2023 21:01:15 +0000 Subject: [PATCH 2/2] Apply changes from ktLintFormat Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../com/squareup/sample/container/panel/ScrimScreen.kt | 1 - .../java/com/squareup/workflow1/ui/ScreenContainerTest.kt | 1 + .../main/java/com/squareup/workflow1/ui/ScreenContainer.kt | 7 +++++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/samples/containers/common/src/main/java/com/squareup/sample/container/panel/ScrimScreen.kt b/samples/containers/common/src/main/java/com/squareup/sample/container/panel/ScrimScreen.kt index 13fa600c4..09ed59fd5 100644 --- a/samples/containers/common/src/main/java/com/squareup/sample/container/panel/ScrimScreen.kt +++ b/samples/containers/common/src/main/java/com/squareup/sample/container/panel/ScrimScreen.kt @@ -3,7 +3,6 @@ package com.squareup.sample.container.panel import com.squareup.workflow1.ui.Screen import com.squareup.workflow1.ui.ScreenWrapper import com.squareup.workflow1.ui.WorkflowUiExperimentalApi -import com.squareup.workflow1.ui.Wrapper /** * Show a scrim over some [content], which is invisible if [dimmed] is false, diff --git a/workflow-ui/container-android/src/test/java/com/squareup/workflow1/ui/ScreenContainerTest.kt b/workflow-ui/container-android/src/test/java/com/squareup/workflow1/ui/ScreenContainerTest.kt index 679ae3caf..0254467a1 100644 --- a/workflow-ui/container-android/src/test/java/com/squareup/workflow1/ui/ScreenContainerTest.kt +++ b/workflow-ui/container-android/src/test/java/com/squareup/workflow1/ui/ScreenContainerTest.kt @@ -14,6 +14,7 @@ internal class ScreenContainerTest { @Test fun deepMapRecurses() { val backStack = BackStackScreen(NamedScreen(MyScreen, "name")) + @Suppress("UNCHECKED_CAST") val mappedBackStack = backStack .deepMap { it.withEnvironment() } as BackStackScreen>> diff --git a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ScreenContainer.kt b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ScreenContainer.kt index 8c7f0f600..fa487ee7e 100644 --- a/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ScreenContainer.kt +++ b/workflow-ui/core-common/src/main/java/com/squareup/workflow1/ui/ScreenContainer.kt @@ -26,6 +26,9 @@ public interface ScreenWrapper : ScreenContainer, Wrapper Screen): Screen { - return if (this is ScreenContainer<*>) map { it.deepMap(transform) } - else transform(this) + return if (this is ScreenContainer<*>) { + map { it.deepMap(transform) } + } else { + transform(this) + } }