Skip to content

//workflow/com.squareup.workflow1.ui/ScreenViewFactoryFinder

ScreenViewFactoryFinder

[androidJvm] @WorkflowUiExperimentalApi()

interface ScreenViewFactoryFinder

ViewEnvironment service object used by Screen.toViewFactory to find the right ScreenViewFactory to build and manage a View to display Screens of the type of the receiver. The default implementation makes AndroidScreen work, and provides default bindings for NamedScreen, EnvironmentScreen, BackStackScreen, etc.

Here is how this hook could be used to provide a custom view to handle BackStackScreen:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
object MyViewFactory : ScreenViewFactory<BackStackScreen<*>>
by ScreenViewFactory(
  buildView = { environment, context, _ ->
    val view = MyBackStackContainer(context)
      .apply { layoutParams = (LayoutParams(MATCH_PARENT, MATCH_PARENT)) }
    ScreenViewHolder(environment, view) { rendering, environment ->
      view.update(rendering, environment)
    }
  }
)

object MyFinder : ScreenViewFactoryFinder {
  override fun <ScreenT : Screen> getViewFactoryForRendering(
    environment: ViewEnvironment,
    rendering: ScreenT
  ): ScreenViewFactory<ScreenT> {
    @Suppress("UNCHECKED_CAST")
    if (rendering is BackStackScreen<*>) return MyViewFactory as ScreenViewFactory<ScreenT>
    return super.getViewFactoryForRendering(environment, rendering)
  }
}

class MyViewModel(savedState: SavedStateHandle) : ViewModel() {
  val renderings: StateFlow<MyRootRendering> by lazy {
    val env = ViewEnvironment.EMPTY + (ScreenViewFactoryFinder to MyFinder)
    renderWorkflowIn(
      workflow = MyRootWorkflow.mapRenderings { it.withEnvironment(env) },
      scope = viewModelScope,
      savedStateHandle = savedState
    )
  }
}

Types

Name Summary
Companion [androidJvm]
Content
object Companion : ViewEnvironmentKey<ScreenViewFactoryFinder>


Functions

Name Summary
getViewFactoryForRendering [androidJvm]
Content
open fun <ScreenT : Screen> getViewFactoryForRendering(environment: ViewEnvironment, rendering: ScreenT): ScreenViewFactory<ScreenT>


Extensions

Name Summary
withCompositionRoot [androidJvm]
Content
@WorkflowUiExperimentalApi()

fun ScreenViewFactoryFinder.withCompositionRoot(root: CompositionRoot): ScreenViewFactoryFinder
More info
Returns a ScreenViewFactoryFinder that ensures that any composeScreenViewFactory factories registered in this registry will be wrapped exactly once with a CompositionRoot wrapper.