@@ -12,18 +12,17 @@ import android.view.ViewGroup
1212import android.view.ViewGroup.LayoutParams.MATCH_PARENT
1313import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
1414import androidx.annotation.IdRes
15- import com.squareup.workflow1.ui.asScreen
1615import com.squareup.workflow1.ui.BuilderViewFactory
1716import com.squareup.workflow1.ui.ViewEnvironment
1817import com.squareup.workflow1.ui.ViewRegistry
1918import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
19+ import com.squareup.workflow1.ui.asScreen
2020import com.squareup.workflow1.ui.backPressedHandler
2121import com.squareup.workflow1.ui.bindShowRendering
2222import com.squareup.workflow1.ui.buildView
2323import com.squareup.workflow1.ui.modal.ModalViewContainer.Companion.binding
2424import com.squareup.workflow1.ui.onBackPressedDispatcherOwnerOrNull
2525import com.squareup.workflow1.ui.showRendering
26- import com.squareup.workflow1.ui.start
2726import kotlin.reflect.KClass
2827
2928/* *
@@ -65,45 +64,47 @@ public open class ModalViewContainer @JvmOverloads constructor(
6564 initialModalRendering : Any ,
6665 initialViewEnvironment : ViewEnvironment
6766 ): DialogRef <Any > {
68- val view = asScreen(initialModalRendering).buildView(
69- viewEnvironment = initialViewEnvironment,
70- contextForNewView = this .context,
71- container = this
72- )
73- .apply {
74- start()
75- // If the modal's root view has no backPressedHandler, add a no-op one to
76- // ensure that the `onBackPressed` call below will not leak up to handlers
77- // that should be blocked by this modal session.
78- if (backPressedHandler == null ) backPressedHandler = { }
79- }
67+ val view = asScreen(initialModalRendering)
68+ .buildView(
69+ viewEnvironment = initialViewEnvironment,
70+ contextForNewView = this .context,
71+ container = this
72+ )
73+ .let { holder ->
74+ holder.start()
75+ holder.view
76+ }
77+ // If the modal's root view has no backPressedHandler, add a no-op one to
78+ // ensure that the `onBackPressed` call below will not leak up to handlers
79+ // that should be blocked by this modal session.
80+ if (view.backPressedHandler == null ) view.backPressedHandler = { }
8081
8182 return buildDialogForView(view)
82- .apply {
83- // Dialogs are modal windows and so they block events, including back button presses
84- // -- that's their job! But we *want* the Activity's onBackPressedDispatcher to fire
85- // when back is pressed, so long as it doesn't look past this modal window for handlers.
86- //
87- // Here, we handle the ACTION_UP portion of a KEYCODE_BACK key event, and below
88- // we make sure that the root view has a backPressedHandler that will consume the
89- // onBackPressed call if no child of the root modal view does.
83+ .apply {
84+ // Dialogs are modal windows and so they block events, including back button presses
85+ // -- that's their job! But we *want* the Activity's onBackPressedDispatcher to fire
86+ // when back is pressed, so long as it doesn't look past this modal window for handlers.
87+ //
88+ // Here, we handle the ACTION_UP portion of a KEYCODE_BACK key event, and below
89+ // we make sure that the root view has a backPressedHandler that will consume the
90+ // onBackPressed call if no child of the root modal view does.
9091
91- setOnKeyListener { _, keyCode, keyEvent ->
92- if (keyCode == KeyEvent .KEYCODE_BACK && keyEvent.action == ACTION_UP ) {
93- view.context.onBackPressedDispatcherOwnerOrNull()
94- ?.onBackPressedDispatcher
95- ?.let {
96- if (it.hasEnabledCallbacks()) it.onBackPressed()
97- }
98- true
99- } else {
100- false
101- }
92+ setOnKeyListener { _, keyCode, keyEvent ->
93+ if (keyCode == KeyEvent .KEYCODE_BACK && keyEvent.action == ACTION_UP ) {
94+ view.context.onBackPressedDispatcherOwnerOrNull()
95+ ?.onBackPressedDispatcher
96+ ?.let {
97+ if (it.hasEnabledCallbacks()) it.onBackPressed()
98+ }
99+ true
100+ } else {
101+ false
102102 }
103103 }
104- .run {
105- DialogRef (initialModalRendering, initialViewEnvironment, this , view)
106- }
104+ }
105+ .run {
106+ DialogRef (initialModalRendering, initialViewEnvironment, this , view)
107+ }
107108 }
108109
109110 override fun updateDialog (dialogRef : DialogRef <Any >) {
@@ -118,14 +119,14 @@ public open class ModalViewContainer @JvmOverloads constructor(
118119 type : KClass <H >
119120 ) : com.squareup.workflow1.ui.ViewFactory<H>
120121 by BuilderViewFactory (
121- type = type,
122- viewConstructor = { initialRendering, initialEnv, context, _ ->
123- ModalViewContainer (context).apply {
124- this .id = id
125- layoutParams = ViewGroup .LayoutParams (MATCH_PARENT , MATCH_PARENT )
126- bindShowRendering(initialRendering, initialEnv, ::update)
127- }
122+ type = type,
123+ viewConstructor = { initialRendering, initialEnv, context, _ ->
124+ ModalViewContainer (context).apply {
125+ this .id = id
126+ layoutParams = ViewGroup .LayoutParams (MATCH_PARENT , MATCH_PARENT )
127+ bindShowRendering(initialRendering, initialEnv, ::update)
128128 }
129+ }
129130 )
130131
131132 public companion object {
0 commit comments