|
| 1 | +package com.squareup.workflow1.ui |
| 2 | + |
| 3 | +import com.squareup.workflow1.ui.container.BackStackScreen |
| 4 | +import com.squareup.workflow1.ui.container.BackStackScreenViewFactory |
| 5 | +import com.squareup.workflow1.ui.container.BodyAndModalsContainer |
| 6 | +import com.squareup.workflow1.ui.container.BodyAndModalsScreen |
| 7 | +import com.squareup.workflow1.ui.container.EnvironmentScreen |
| 8 | +import com.squareup.workflow1.ui.container.EnvironmentScreenViewFactory |
| 9 | + |
| 10 | +/** |
| 11 | + * [ViewEnvironment] service object used by [Screen.buildView] to find the right |
| 12 | + * [ScreenViewFactory]. The default implementation makes [AndroidScreen] work |
| 13 | + * and provides default bindings for [NamedScreen], [EnvironmentScreen], [BackStackScreen], |
| 14 | + * etc. |
| 15 | + * |
| 16 | + * Here is how this hook could be used to provide a custom view to handle [BackStackScreen]: |
| 17 | + * |
| 18 | + * object MyViewFactory : ScreenViewFactory<BackStackScreen<*>> |
| 19 | + * by ManualScreenViewFactory( |
| 20 | + * type = BackStackScreen::class, |
| 21 | + * viewConstructor = { initialRendering, initialEnv, context, _ -> |
| 22 | + * MyBackStackContainer(context) |
| 23 | + * .apply { |
| 24 | + * layoutParams = (LayoutParams(MATCH_PARENT, MATCH_PARENT)) |
| 25 | + * bindShowRendering(initialRendering, initialEnv, ::update) |
| 26 | + * } |
| 27 | + * } |
| 28 | + * ) |
| 29 | + * |
| 30 | + * object MyFinder : ScreenViewFactoryFinder { |
| 31 | + * @Suppress("UNCHECKED_CAST") |
| 32 | + * if (rendering is BackStackScreen<*>) |
| 33 | + * return MyViewFactory as ScreenViewFactory<ScreenT> |
| 34 | + * return super.getViewFactoryForRendering(environment, rendering) |
| 35 | + * } |
| 36 | + * |
| 37 | + * class MyViewModel(savedState: SavedStateHandle) : ViewModel() { |
| 38 | + * val renderings: StateFlow<MyRootRendering> by lazy { |
| 39 | + * val customized = ViewEnvironment() + (ScreenViewFactoryFinder to MyFinder) |
| 40 | + * renderWorkflowIn( |
| 41 | + * workflow = MyRootWorkflow.withEnvironment(customized), |
| 42 | + * scope = viewModelScope, |
| 43 | + * savedStateHandle = savedState |
| 44 | + * ) |
| 45 | + * } |
| 46 | + * } |
| 47 | + */ |
| 48 | + |
| 49 | +@WorkflowUiExperimentalApi |
| 50 | +public interface ScreenViewFactoryFinder { |
| 51 | + public fun <ScreenT : Screen> getViewFactoryForRendering( |
| 52 | + environment: ViewEnvironment, |
| 53 | + rendering: ScreenT |
| 54 | + ): ScreenViewFactory<ScreenT> { |
| 55 | + val entry = environment[ViewRegistry].getEntryFor(rendering::class) |
| 56 | + |
| 57 | + @Suppress("UNCHECKED_CAST", "DEPRECATION") |
| 58 | + return (entry as? ScreenViewFactory<ScreenT>) |
| 59 | + ?: (rendering as? AndroidScreen<*>)?.viewFactory as? ScreenViewFactory<ScreenT> |
| 60 | + ?: (rendering as? AsScreen<*>)?.let { AsScreenViewFactory as ScreenViewFactory<ScreenT> } |
| 61 | + ?: (rendering as? BackStackScreen<*>)?.let { |
| 62 | + BackStackScreenViewFactory as ScreenViewFactory<ScreenT> |
| 63 | + } |
| 64 | + ?: (rendering as? BodyAndModalsScreen<*, *>)?.let { |
| 65 | + BodyAndModalsContainer as ScreenViewFactory<ScreenT> |
| 66 | + } |
| 67 | + ?: (rendering as? NamedScreen<*>)?.let { |
| 68 | + NamedScreenViewFactory as ScreenViewFactory<ScreenT> |
| 69 | + } |
| 70 | + ?: (rendering as? EnvironmentScreen<*>)?.let { |
| 71 | + EnvironmentScreenViewFactory as ScreenViewFactory<ScreenT> |
| 72 | + } |
| 73 | + ?: throw IllegalArgumentException( |
| 74 | + "A ScreenViewFactory should have been registered to display $rendering, " + |
| 75 | + "or that class should implement AndroidScreen. Instead found $entry." |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + public companion object : ViewEnvironmentKey<ScreenViewFactoryFinder>( |
| 80 | + ScreenViewFactoryFinder::class |
| 81 | + ) { |
| 82 | + override val default: ScreenViewFactoryFinder |
| 83 | + get() = object : ScreenViewFactoryFinder {} |
| 84 | + } |
| 85 | +} |
0 commit comments