From db313690e72683c7bac0b83bc4f7ebfeac3d13cf Mon Sep 17 00:00:00 2001 From: Ray Ryan Date: Tue, 21 Mar 2023 10:49:31 -0700 Subject: [PATCH] Extracts `ScreenViewFactory.map` from `ScreenViewFactory.forWrapper` Coupling `Wrapper` to `ScreenViewFactory.forWrapper` was a mistake, made simple transformations that weren't in the strict `Wrapper` shape more difficult. Fixes #973 --- .../poetry/views/MayBeLoadingScreen.kt | 8 - .../sample/container/panel/ScrimScreen.kt | 11 +- .../hellobackbutton/AreYouSureWorkflow.kt | 21 ++- workflow-ui/core-android/api/core-android.api | 14 +- .../workflow1/ui/ScreenViewFactory.kt | 144 +++++++++++------- .../workflow1/ui/ScreenViewFactoryFinder.kt | 9 +- .../squareup/workflow1/ui/ScreenViewHolder.kt | 21 +-- .../ui/container/BackButtonScreen.kt | 28 ++-- .../ui/container/OverlayDialogHolder.kt | 41 ++--- 9 files changed, 173 insertions(+), 124 deletions(-) diff --git a/benchmarks/performance-poetry/complex-poetry/src/main/java/com/squareup/benchmarks/performance/complex/poetry/views/MayBeLoadingScreen.kt b/benchmarks/performance-poetry/complex-poetry/src/main/java/com/squareup/benchmarks/performance/complex/poetry/views/MayBeLoadingScreen.kt index d16777d99b..bb894a163a 100644 --- a/benchmarks/performance-poetry/complex-poetry/src/main/java/com/squareup/benchmarks/performance/complex/poetry/views/MayBeLoadingScreen.kt +++ b/benchmarks/performance-poetry/complex-poetry/src/main/java/com/squareup/benchmarks/performance/complex/poetry/views/MayBeLoadingScreen.kt @@ -20,11 +20,3 @@ fun MayBeLoadingScreen( loaders.map { FullScreenOverlay(it) } ) } - -@OptIn(WorkflowUiExperimentalApi::class) -val MayBeLoadingScreen.baseScreen: OverviewDetailScreen - get() = body.content - -@OptIn(WorkflowUiExperimentalApi::class) -val MayBeLoadingScreen.loaders: List - get() = overlays.map { it.content } 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 c0303a87c9..daef4cd52f 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,18 +1,17 @@ package com.squareup.sample.container.panel -import com.squareup.workflow1.ui.Compatible -import com.squareup.workflow1.ui.Compatible.Companion.keyFor import com.squareup.workflow1.ui.Screen 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, * visible if it is true. */ @OptIn(WorkflowUiExperimentalApi::class) -class ScrimScreen( - val content: T, +class ScrimScreen( + override val content: C, val dimmed: Boolean -) : Screen, Compatible { - override val compatibilityKey = keyFor(content, "ScrimScreen") +) : Wrapper, Screen { + override fun map(transform: (C) -> D) = ScrimScreen(transform(content), dimmed) } diff --git a/samples/containers/hello-back-button/src/main/java/com/squareup/sample/hellobackbutton/AreYouSureWorkflow.kt b/samples/containers/hello-back-button/src/main/java/com/squareup/sample/hellobackbutton/AreYouSureWorkflow.kt index 0bf418de74..f120a5580d 100644 --- a/samples/containers/hello-back-button/src/main/java/com/squareup/sample/hellobackbutton/AreYouSureWorkflow.kt +++ b/samples/containers/hello-back-button/src/main/java/com/squareup/sample/hellobackbutton/AreYouSureWorkflow.kt @@ -2,6 +2,7 @@ package com.squareup.sample.hellobackbutton import android.os.Parcelable import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.Finished +import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.Rendering import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.State import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.State.Quitting import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.State.Running @@ -9,6 +10,9 @@ import com.squareup.workflow1.Snapshot import com.squareup.workflow1.StatefulWorkflow import com.squareup.workflow1.WorkflowAction.Companion.noAction import com.squareup.workflow1.action +import com.squareup.workflow1.ui.AndroidScreen +import com.squareup.workflow1.ui.Screen +import com.squareup.workflow1.ui.ScreenViewFactory import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.container.AlertOverlay import com.squareup.workflow1.ui.container.AlertOverlay.Button.NEGATIVE @@ -27,12 +31,21 @@ import kotlinx.parcelize.Parcelize */ @OptIn(WorkflowUiExperimentalApi::class) object AreYouSureWorkflow : - StatefulWorkflow>() { + StatefulWorkflow() { override fun initialState( props: Unit, snapshot: Snapshot? ): State = snapshot?.toParcelable() ?: Running + data class Rendering( + val base: Screen, + val alert: AlertOverlay? = null + ) : AndroidScreen { + override val viewFactory = ScreenViewFactory.map { + BodyAndOverlaysScreen(base, listOfNotNull(alert)) + } + } + @Parcelize enum class State : Parcelable { Running, @@ -45,12 +58,12 @@ object AreYouSureWorkflow : renderProps: Unit, renderState: State, context: RenderContext - ): BodyAndOverlaysScreen<*, AlertOverlay> { + ): Rendering { val ableBakerCharlie = context.renderChild(HelloBackButtonWorkflow, Unit) { noAction() } return when (renderState) { Running -> { - BodyAndOverlaysScreen( + Rendering( BackButtonScreen(ableBakerCharlie) { // While we always provide a back button handler, by default the view code // associated with BackButtonScreen ignores ours if the view created for the @@ -80,7 +93,7 @@ object AreYouSureWorkflow : } ) - BodyAndOverlaysScreen(ableBakerCharlie, alert) + Rendering(ableBakerCharlie, alert) } } } diff --git a/workflow-ui/core-android/api/core-android.api b/workflow-ui/core-android/api/core-android.api index 508fe71a21..2b69173851 100644 --- a/workflow-ui/core-android/api/core-android.api +++ b/workflow-ui/core-android/api/core-android.api @@ -129,13 +129,17 @@ public final class com/squareup/workflow1/ui/ScreenViewFactoryKt { } public abstract interface class com/squareup/workflow1/ui/ScreenViewHolder { + public static final field Companion Lcom/squareup/workflow1/ui/ScreenViewHolder$Companion; public abstract fun getEnvironment ()Lcom/squareup/workflow1/ui/ViewEnvironment; public abstract fun getRunner ()Lcom/squareup/workflow1/ui/ScreenViewRunner; public abstract fun getView ()Landroid/view/View; } +public final class com/squareup/workflow1/ui/ScreenViewHolder$Companion { + public final fun invoke (Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/view/View;Lcom/squareup/workflow1/ui/ScreenViewRunner;)Lcom/squareup/workflow1/ui/ScreenViewHolder; +} + public final class com/squareup/workflow1/ui/ScreenViewHolderKt { - public static final fun ScreenViewHolder (Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/view/View;Lcom/squareup/workflow1/ui/ScreenViewRunner;)Lcom/squareup/workflow1/ui/ScreenViewHolder; public static final fun canShow (Lcom/squareup/workflow1/ui/ScreenViewHolder;Lcom/squareup/workflow1/ui/Screen;)Z public static final fun getShowing (Lcom/squareup/workflow1/ui/ScreenViewHolder;)Lcom/squareup/workflow1/ui/Screen; public static final fun show (Lcom/squareup/workflow1/ui/ScreenViewHolder;Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;)V @@ -429,6 +433,7 @@ public final class com/squareup/workflow1/ui/container/OverlayDialogFactoryKt { } public abstract interface class com/squareup/workflow1/ui/container/OverlayDialogHolder { + public static final field Companion Lcom/squareup/workflow1/ui/container/OverlayDialogHolder$Companion; public abstract fun getDialog ()Landroid/app/Dialog; public abstract fun getEnvironment ()Lcom/squareup/workflow1/ui/ViewEnvironment; public abstract fun getOnBackPressed ()Lkotlin/jvm/functions/Function0; @@ -436,9 +441,12 @@ public abstract interface class com/squareup/workflow1/ui/container/OverlayDialo public abstract fun getRunner ()Lkotlin/jvm/functions/Function2; } +public final class com/squareup/workflow1/ui/container/OverlayDialogHolder$Companion { + public final fun invoke (Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/app/Dialog;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow1/ui/container/OverlayDialogHolder; + public static synthetic fun invoke$default (Lcom/squareup/workflow1/ui/container/OverlayDialogHolder$Companion;Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/app/Dialog;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lcom/squareup/workflow1/ui/container/OverlayDialogHolder; +} + public final class com/squareup/workflow1/ui/container/OverlayDialogHolderKt { - public static final fun OverlayDialogHolder (Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/app/Dialog;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;)Lcom/squareup/workflow1/ui/container/OverlayDialogHolder; - public static synthetic fun OverlayDialogHolder$default (Lcom/squareup/workflow1/ui/ViewEnvironment;Landroid/app/Dialog;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lcom/squareup/workflow1/ui/container/OverlayDialogHolder; public static final fun canShow (Lcom/squareup/workflow1/ui/container/OverlayDialogHolder;Lcom/squareup/workflow1/ui/container/Overlay;)Z public static final fun getShowing (Lcom/squareup/workflow1/ui/container/OverlayDialogHolder;)Lcom/squareup/workflow1/ui/container/Overlay; public static final fun show (Lcom/squareup/workflow1/ui/container/OverlayDialogHolder;Lcom/squareup/workflow1/ui/container/Overlay;Lcom/squareup/workflow1/ui/ViewEnvironment;)V diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactory.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactory.kt index 9fa05f2e63..9c0371b501 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactory.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactory.kt @@ -139,24 +139,10 @@ public interface ScreenViewFactory : ViewRegistry.Entry( - * override val content: W - * ) : AndroidScreen>, Wrapper { - * override val viewFactory = forWrapper, W>() - * - * override fun map(transform: (W) -> U) = - * MyWrapper(transform(content)) - * } - * - * To make a wrapper that customizes [View] initialization: + * For example, to make a [Wrapper] that customizes [View] initialization: * * class WithTutorialTips( * override val content: W @@ -168,46 +154,92 @@ public interface ScreenViewFactory : ViewRegistry.Entry map(transform: (W) -> U) = * WithTutorialTips(transform(content)) * } + */ + @WorkflowUiExperimentalApi + public inline fun forWrapper( + crossinline prepEnvironment: (environment: ViewEnvironment) -> ViewEnvironment = { it }, + crossinline prepContext: ( + environment: ViewEnvironment, + context: Context + ) -> Context = { _, c -> c }, + crossinline beforeShowing: (viewHolder: ScreenViewHolder) -> Unit = {}, + crossinline showWrapper: ( + view: View, + wrapper: WrapperT, + environment: ViewEnvironment, + showContent: (ContentT, ViewEnvironment) -> Unit + ) -> Unit = { _, src, e, show -> show(src.content, e) }, + ): ScreenViewFactory + where WrapperT : Screen, WrapperT : Wrapper = map( + prepEnvironment, + prepContext, + beforeShowing, + { view, source, _, environment, showTransformed -> + showWrapper(view, source, environment, showTransformed) + } + ) { it.content } + + /** + * Creates a [ScreenViewFactory] for renderings of type [SourceT], which finds and + * delegates to the factory for [TransformedT]. Allows [SourceT] to add information + * or behavior, without requiring wasteful parallel wrapping in the view system. + * + * See [forWrapper] if [SourceT] implements [Wrapper]. + * + * For example, to display a [Screen] with a strictly locked down layering policy + * via the built-in type + * [BodyAndOverlaysScreen][com.squareup.workflow1.ui.container.BodyAndOverlaysScreen]: + * + * class Layers( + * val base: Screen, + * val wizard: BackStackScreen<*>? = null, + * val alert: AlertOverlay? = null + * ) : AndroidScreen { + * override val viewFactory = ScreenViewFactory.map { + * BodyAndOverlaysScreen(it.base, listOfNotNull(it.wizard, it.alert)) + * } + * } * - * @param prepEnvironment a function to process the initial [ViewEnvironment] + * @param prepEnvironment optional function to process the initial [ViewEnvironment] * before the [ScreenViewFactory] is fetched. Note that this function is not - * applied on updates. Add a [showWrapperScreen] function if you need that. + * applied on updates. Add a [showSource] function if you need that. * - * @param prepContext a function to process the [Context] used to create each [View]. + * @param prepContext optional function to process the [Context] used to create each [View]. * it is passed the product of [prepEnvironment] * - * @param unwrap a function to extract [WrappedT] instances from [WrapperT]s. + * @param beforeShowing optional function to be invoked immediately after a new [View] is built. * - * @param beforeShowing a function to be invoked immediately after a new [View] is built. + * @param showSource function to be invoked when an instance of [SourceT] needs + * to be shown in a [View] built to display instances of [TransformedT]. Allows pre- + * and post-processing of the [View]. Default implementation simply applies [transform]. * - * @param showWrapperScreen a function to be invoked when an instance of [WrapperT] needs - * to be shown in a [View] built to display instances of [WrappedT]. Allows pre- - * and post-processing of the [View]. + * @param transform a function to extract [TransformedT] instances from [SourceT]s. */ @WorkflowUiExperimentalApi - public inline fun forWrapper( + public inline fun map( crossinline prepEnvironment: (environment: ViewEnvironment) -> ViewEnvironment = { it }, crossinline prepContext: ( environment: ViewEnvironment, context: Context ) -> Context = { _, c -> c }, - crossinline unwrap: (wrapperScreen: WrapperT) -> WrappedT = { it.content }, - crossinline beforeShowing: (viewHolder: ScreenViewHolder) -> Unit = {}, - crossinline showWrapperScreen: ( + crossinline beforeShowing: (viewHolder: ScreenViewHolder) -> Unit = {}, + crossinline showSource: ( view: View, - wrapperScreen: WrapperT, + source: SourceT, + transformer: (source: SourceT) -> TransformedT, environment: ViewEnvironment, - showUnwrappedScreen: (WrappedT, ViewEnvironment) -> Unit - ) -> Unit = { _, wrapper, e, showWrapper -> showWrapper(wrapper.content, e) }, - ): ScreenViewFactory where WrapperT : Screen, WrapperT : Wrapper { + showTransformed: (TransformedT, ViewEnvironment) -> Unit + ) -> Unit = { _, src, xform, e, show -> show(xform(src), e) }, + noinline transform: (source: SourceT) -> TransformedT, + ): ScreenViewFactory { return fromCode { initialRendering, initialEnvironment, context, container -> val preppedEnvironment = prepEnvironment(initialEnvironment) - val wrappedFactory = unwrap(initialRendering).toViewFactory(preppedEnvironment) - val wrapperFactory = wrappedFactory.toUnwrappingViewFactory( + val wrappedFactory = transform(initialRendering).toViewFactory(preppedEnvironment) + val wrapperFactory = wrappedFactory.map( + transform, prepEnvironment, prepContext, - unwrap, - showWrapperScreen + showSource ) // Note that we give the factory the original initialEnvironment. @@ -324,50 +356,50 @@ public fun interface ViewStarter { } /** - * Transforms a [ScreenViewFactory] of [WrappedT] into one that can handle instances of [WrapperT]. + * Transforms a [ScreenViewFactory] of [TransformedT] into one that can handle + * instances of [SourceT]. * - * @see [ScreenViewFactory.forWrapper]. + * @see [ScreenViewFactory.map]. */ @WorkflowUiExperimentalApi -public inline fun ScreenViewFactory.toUnwrappingViewFactory( +public inline +fun ScreenViewFactory.map( + noinline transform: (wrapperScreen: SourceT) -> TransformedT, crossinline prepEnvironment: (environment: ViewEnvironment) -> ViewEnvironment = { e -> e }, crossinline prepContext: ( environment: ViewEnvironment, context: Context ) -> Context = { _, c -> c }, - crossinline unwrap: (wrapperScreen: WrapperT) -> WrappedT = { it.content }, - crossinline showWrapperScreen: ( + crossinline showSource: ( view: View, - wrapperScreen: WrapperT, + source: SourceT, + transform: (wrapperScreen: SourceT) -> TransformedT, environment: ViewEnvironment, - showUnwrappedScreen: (WrappedT, ViewEnvironment) -> Unit - ) -> Unit = { _, wrapperScreen, environment, showUnwrappedScreen -> - showUnwrappedScreen(wrapperScreen.content, environment) - } -): ScreenViewFactory - where WrapperT : Screen, WrapperT : Wrapper, WrappedT : Screen { + showTransformed: (TransformedT, ViewEnvironment) -> Unit + ) -> Unit = { _, src, xform, e, showTransformed -> showTransformed(xform(src), e) } +): ScreenViewFactory { val wrappedFactory = this - return object : ScreenViewFactory by fromCode( + return object : ScreenViewFactory by fromCode( buildView = { initialRendering, initialEnvironment, context, container -> val preppedInitialEnvironment = prepEnvironment(initialEnvironment) val preppedContext = prepContext(preppedInitialEnvironment, context) val wrappedHolder = wrappedFactory.buildView( - unwrap(initialRendering), + transform(initialRendering), preppedInitialEnvironment, preppedContext, container ) - object : ScreenViewHolder { + object : ScreenViewHolder { override val view = wrappedHolder.view override val environment: ViewEnvironment get() = wrappedHolder.environment - override val runner: ScreenViewRunner = - ScreenViewRunner { wrapperScreen, newEnvironment -> - showWrapperScreen(view, wrapperScreen, newEnvironment) { unwrappedScreen, env -> - wrappedHolder.runner.showRendering(unwrappedScreen, env) + override val runner: ScreenViewRunner = + ScreenViewRunner { newSource, newEnvironment -> + showSource(view, newSource, transform, newEnvironment) { transformed, env -> + wrappedHolder.runner.showRendering(transformed, env) } } } diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactoryFinder.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactoryFinder.kt index 2e16a92b78..4f75a9db51 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactoryFinder.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactoryFinder.kt @@ -74,11 +74,10 @@ public interface ScreenViewFactoryFinder { } ?: (rendering as? EnvironmentScreen<*>)?.let { forWrapper, ScreenT>( - prepEnvironment = { e -> e + rendering.environment }, - showWrapperScreen = { _, envScreen, environment, showUnwrapped -> - showUnwrapped(envScreen.content, environment + envScreen.environment) - } - ) as ScreenViewFactory + prepEnvironment = { e -> e + rendering.environment } + ) { _, envScreen, environment, showUnwrapped -> + showUnwrapped(envScreen.content, environment + envScreen.environment) + } as ScreenViewFactory } ?: throw IllegalArgumentException( "A ScreenViewFactory should have been registered to display $rendering, " + diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewHolder.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewHolder.kt index 3d9b0709af..54b5e6c714 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewHolder.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewHolder.kt @@ -26,6 +26,16 @@ public interface ScreenViewHolder { * maintained correctly, and [showing] keeps working. */ public val runner: ScreenViewRunner + + public companion object { + public operator fun invoke( + initialEnvironment: ViewEnvironment, + view: View, + viewRunner: ScreenViewRunner + ): ScreenViewHolder { + return RealScreenViewHolder(initialEnvironment, view, viewRunner) + } + } } /** @@ -80,17 +90,8 @@ public fun ScreenViewHolder.show( * * Note that the exact type of the returned [Screen] is likely not to match that of * the receiver's `ScreenT` type parameter, e.g. if a - * [wrapping view factory][ScreenViewFactory.forWrapper] is in use. + * [mapping view factory][ScreenViewFactory.map] is in use. */ @WorkflowUiExperimentalApi public val ScreenViewHolder<*>.showing: Screen get() = view.screen - -@WorkflowUiExperimentalApi -public fun ScreenViewHolder( - initialEnvironment: ViewEnvironment, - view: View, - viewRunner: ScreenViewRunner -): ScreenViewHolder { - return RealScreenViewHolder(initialEnvironment, view, viewRunner) -} 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 808bf2dd0a..9c9fee6a3a 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 @@ -32,24 +32,22 @@ public class BackButtonScreen( BackButtonScreen(transform(content), shadow, onBackPressed) override val viewFactory: ScreenViewFactory> = - ScreenViewFactory.forWrapper( - showWrapperScreen = { view, backButtonScreen, env, showUnwrapped -> - if (!backButtonScreen.shadow) { - // Place our handler before invoking innerShowRendering, so that - // its later calls to view.backPressedHandler will take precedence - // over ours. - view.backPressedHandler = backButtonScreen.onBackPressed - } + ScreenViewFactory.forWrapper { view, backButtonScreen, env, showUnwrapped -> + if (!backButtonScreen.shadow) { + // Place our handler before invoking innerShowRendering, so that + // its later calls to view.backPressedHandler will take precedence + // over ours. + view.backPressedHandler = backButtonScreen.onBackPressed + } - // Show the content Screen. - showUnwrapped(backButtonScreen.content, env) + // Show the content Screen. + showUnwrapped(backButtonScreen.content, env) - if (backButtonScreen.shadow) { - // Place our handler after invoking innerShowRendering, so that ours wins. - view.backPressedHandler = backButtonScreen.onBackPressed - } + if (backButtonScreen.shadow) { + // Place our handler after invoking innerShowRendering, so that ours wins. + view.backPressedHandler = backButtonScreen.onBackPressed } - ) + } @Deprecated("Use content", ReplaceWith("content")) public val wrapped: C = content diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayDialogHolder.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayDialogHolder.kt index b169c5cd1a..853c2b012f 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayDialogHolder.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/OverlayDialogHolder.kt @@ -55,6 +55,30 @@ public interface OverlayDialogHolder { * method. */ public val onBackPressed: (() -> Unit)? + + public companion object { + public operator fun invoke( + initialEnvironment: ViewEnvironment, + dialog: Dialog, + onUpdateBounds: ((Rect) -> Unit)? = { dialog.setBounds(it) }, + onBackPressed: (() -> Unit)? = { + dialog.context.onBackPressedDispatcherOwnerOrNull() + ?.onBackPressedDispatcher + ?.let { + if (it.hasEnabledCallbacks()) it.onBackPressed() + } + }, + runner: (rendering: OverlayT, environment: ViewEnvironment) -> Unit + ): OverlayDialogHolder { + return RealOverlayDialogHolder( + initialEnvironment, + dialog, + onUpdateBounds, + onBackPressed, + runner + ) + } + } } /** @@ -101,20 +125,3 @@ public fun OverlayDialogHolder.show( @WorkflowUiExperimentalApi public val OverlayDialogHolder<*>.showing: Overlay get() = dialog.overlay - -@WorkflowUiExperimentalApi -public fun OverlayDialogHolder( - initialEnvironment: ViewEnvironment, - dialog: Dialog, - onUpdateBounds: ((Rect) -> Unit)? = { dialog.setBounds(it) }, - onBackPressed: (() -> Unit)? = { - dialog.context.onBackPressedDispatcherOwnerOrNull() - ?.onBackPressedDispatcher - ?.let { - if (it.hasEnabledCallbacks()) it.onBackPressed() - } - }, - runner: (rendering: OverlayT, environment: ViewEnvironment) -> Unit -): OverlayDialogHolder { - return RealOverlayDialogHolder(initialEnvironment, dialog, onUpdateBounds, onBackPressed, runner) -}