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:
object MyViewFactory : ScreenViewFactory<*>>
by ScreenViewFactory(
buildView = { environment, context, _ ->
val view = MyBackStackContainer(context)
ScreenViewHolder(environment, view) { rendering, environment ->
view.update(rendering, environment)
}
}
)
object MyFinder : ScreenViewFactoryFinder {
override fun getViewFactoryForRendering(
environment: ViewEnvironment,
rendering: ScreenT
): ScreenViewFactory {
@Suppress("UNCHECKED_CAST")
if (rendering is BackStackScreen<*>) return MyViewFactory as ScreenViewFactory
return super.getViewFactoryForRendering(environment, rendering)
}
}
class MyViewModel(savedState: SavedStateHandle) : ViewModel() {
val renderings: StateFlow by lazy {
val env = ViewEnvironment.EMPTY + (ScreenViewFactoryFinder to MyFinder)
renderWorkflowIn(
workflow = MyRootWorkflow.mapRenderings { it.withEnvironment(env) },
scope = viewModelScope,
savedStateHandle = savedState
)
}
}
Content copied to clipboard
Functions
Link copied to clipboard
open fun <ScreenT : Screen> getViewFactoryForRendering(environment: ViewEnvironment, rendering: ScreenT): ScreenViewFactory<ScreenT>?
Link copied to clipboard
fun <ScreenT : Screen> ScreenViewFactoryFinder.requireViewFactoryForRendering(environment: ViewEnvironment, rendering: ScreenT): ScreenViewFactory<ScreenT>