-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fun ComponentDialog.setContent()
replaces ScreenOverlayDialogFactory
With AndroidX 1.6.0, `ComponentDialog` serves as its own `LifecycleOwner` and `OnBackPressedDispatcherOwner`. To take advantage of this we introduce a new `ComponentDialog.setContent` extension function, deprecate `ScreenOverlayDialogFactory`, and deprecate our hooks for customizing `Dialog` back press handling. Related kdoc is improved, and a factory function is bound to `OverlayDialogFactory.Companion`.
- Loading branch information
Showing
15 changed files
with
244 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...core-android/src/main/java/com/squareup/workflow1/ui/container/ContentDialogSetContent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.squareup.workflow1.ui.container | ||
|
||
import android.graphics.drawable.ColorDrawable | ||
import android.util.TypedValue | ||
import android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND | ||
import androidx.activity.ComponentDialog | ||
import androidx.activity.setViewTreeOnBackPressedDispatcherOwner | ||
import com.squareup.workflow1.ui.Screen | ||
import com.squareup.workflow1.ui.ViewEnvironment | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
import com.squareup.workflow1.ui.androidx.WorkflowLifecycleOwner | ||
import com.squareup.workflow1.ui.show | ||
import com.squareup.workflow1.ui.startShowing | ||
import com.squareup.workflow1.ui.toViewFactory | ||
|
||
/** | ||
* Given a [ComponentDialog], wrap it in an [OverlayDialogHolder] that can drive | ||
* the Dialog's content via instances of a particular type of [ScreenOverlay]. | ||
* | ||
* Dialogs managed this way are compatible with | ||
* [View.setBackHandler][com.squareup.workflow1.ui.setBackHandler], | ||
* and honor the [OverlayArea] and [CoveredByModal] values placed in | ||
* the [ViewEnvironment] by the standard [BodyAndOverlaysScreen] container. | ||
*/ | ||
@WorkflowUiExperimentalApi | ||
public fun <C : Screen, O : ScreenOverlay<C>> ComponentDialog.setContent( | ||
overlay: O, | ||
environment: ViewEnvironment | ||
): OverlayDialogHolder<O> { | ||
val contentHolder = overlay.content.toViewFactory(environment) | ||
.startShowing(overlay.content, environment, context) { view, doStart -> | ||
view.setViewTreeOnBackPressedDispatcherOwner(this@setContent) | ||
WorkflowLifecycleOwner.installOn(view) { | ||
this@setContent.lifecycle | ||
} | ||
doStart() | ||
} | ||
|
||
setCancelable(false) | ||
setContentView(contentHolder.view) | ||
|
||
// Welcome to Android. Nothing workflow-related here, this is just how one | ||
// finds the window background color for the theme. I sure hope it's better in Compose. | ||
val maybeWindowColor = TypedValue() | ||
context.theme.resolveAttribute(android.R.attr.windowBackground, maybeWindowColor, true) | ||
|
||
val background = | ||
if (maybeWindowColor.type in TypedValue.TYPE_FIRST_COLOR_INT..TypedValue.TYPE_LAST_COLOR_INT) { | ||
ColorDrawable(maybeWindowColor.data) | ||
} else { | ||
// If we don't at least set it to null, the window cannot go full bleed. | ||
null | ||
} | ||
with(window!!) { | ||
setBackgroundDrawable(background) | ||
clearFlags(FLAG_DIM_BEHIND) | ||
} | ||
|
||
return OverlayDialogHolder( | ||
initialEnvironment = environment, | ||
dialog = this, | ||
) { newOverlay, newEnvironment -> | ||
contentHolder.show(newOverlay.content, newEnvironment) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../core-android/src/main/java/com/squareup/workflow1/ui/container/FullScreenModalFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.squareup.workflow1.ui.container | ||
|
||
import android.content.Context | ||
import androidx.activity.ComponentDialog | ||
import com.squareup.workflow1.ui.Screen | ||
import com.squareup.workflow1.ui.ViewEnvironment | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
|
||
/** | ||
* Default [OverlayDialogFactory] for the standard [FullScreenModal] rendering class. | ||
* Nothing more than a direct call to [ComponentDialog.setContent]. | ||
* | ||
* To provide a custom binding for [FullScreenModal], see [OverlayDialogFactoryFinder]. | ||
*/ | ||
@WorkflowUiExperimentalApi | ||
internal class FullScreenModalFactory<C : Screen>() : OverlayDialogFactory<FullScreenModal<C>> { | ||
override val type = FullScreenModal::class | ||
|
||
override fun buildDialog( | ||
initialRendering: FullScreenModal<C>, | ||
initialEnvironment: ViewEnvironment, | ||
context: Context | ||
): OverlayDialogHolder<FullScreenModal<C>> = | ||
ComponentDialog(context).setContent(initialRendering, initialEnvironment) | ||
} |
Oops, something went wrong.