-
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
242 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
71 changes: 71 additions & 0 deletions
71
...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,71 @@ | ||
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 | ||
|
||
/** | ||
* Extensible base implementation of [OverlayDialogFactory] for [ScreenOverlay] | ||
* types. Also serves as the default factory for [FullScreenModal]. | ||
* (Use a custom [OverlayDialogFactoryFinder] to customize the presentation | ||
* of [FullScreenModal].) | ||
* | ||
* Dialogs built by this class are compatible with | ||
* [View.backPressedHandler][com.squareup.workflow1.ui.backPressedHandler], | ||
* 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) | ||
} | ||
|
||
// Note that we set onBackPressed to null, so that the implementation built | ||
// into ComponentDialog will be used. Our default implementation basically | ||
// duplicates that one. | ||
return OverlayDialogHolder( | ||
environment, | ||
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
19 changes: 19 additions & 0 deletions
19
.../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,19 @@ | ||
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 | ||
|
||
@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.