Skip to content

//workflow/com.squareup.workflow1.ui/ViewRegistry

ViewRegistry

[androidJvm] @WorkflowUiExperimentalApi()

interface ViewRegistry

The ViewEnvironment service that can be used to display the stream of renderings from a workflow tree as View instances. This is the engine behind AndroidViewRendering, WorkflowViewStub and ViewFactory. Most apps can ignore ViewRegistry as an implementation detail, by using AndroidViewRendering to tie their rendering classes to view code.

To avoid that coupling between workflow code and the Android runtime, registries can be loaded with ViewFactory instances at runtime, and provided as an optional parameter to WorkflowLayout.start.

For example:

 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
val AuthViewFactories = ViewRegistry(
  AuthorizingLayoutRunner, LoginLayoutRunner, SecondFactorLayoutRunner
)

val TicTacToeViewFactories = ViewRegistry(
  NewGameLayoutRunner, GamePlayLayoutRunner, GameOverLayoutRunner
)

val ApplicationViewFactories = ViewRegistry(ApplicationLayoutRunner) +
  AuthViewFactories + TicTacToeViewFactories

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

  val model: MyViewModel by viewModels()
  setContentView(
    WorkflowLayout(this).apply { start(model.renderings, ApplicationViewFactories) }
  )
}

/** As always, use an androidx ViewModel for state that survives config change. */
class MyViewModel(savedState: SavedStateHandle) : ViewModel() {
  val renderings: StateFlow<Any> by lazy {
    renderWorkflowIn(
      workflow = rootWorkflow,
      scope = viewModelScope,
      savedStateHandle = savedState
    )
  }
}

In the above example, it is assumed that the companion objects of the various decoupled LayoutRunner classes honor a convention of implementing ViewFactory, in aid of this kind of assembly.

1
2
3
4
5
6
7
8
class GamePlayLayoutRunner(view: View) : LayoutRunner<GameRendering> {

  // ...

  companion object : ViewFactory<GameRendering> by LayoutRunner.bind(
    R.layout.game_layout, ::GameLayoutRunner
  )
}

Types

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


Functions

Name Summary
getFactoryFor [androidJvm]
Content
abstract fun <RenderingT : Any> getFactoryFor(renderingType: KClassRenderingT>): ViewFactory<RenderingT>?
More info
This method is not for general use, use WorkflowViewStub instead.


Properties

Name Summary
keys [androidJvm] abstract val keys: Set<KClass<*>>The set of unique keys which this registry can derive from the renderings passed to buildView and for which it knows how to create views.

Extensions

Name Summary
buildView [androidJvm]
Content
@WorkflowUiExperimentalApi()

fun <RenderingT : Any> ViewRegistry.buildView(initialRendering: RenderingT, initialViewEnvironment: ViewEnvironment, contextForNewView: Context, container: ViewGroup? = null, initializeView: View.() -> Unit = { showFirstRendering() }): View
More info
It is usually more convenient to use WorkflowViewStub or DecorativeViewFactory than to call this method directly.


getFactoryForRendering [androidJvm]
Content
@WorkflowUiExperimentalApi()

fun <RenderingT : Any> ViewRegistry.getFactoryForRendering(rendering: RenderingT): ViewFactory<RenderingT>
More info
It is usually more convenient to use WorkflowViewStub or DecorativeViewFactory than to call this method directly.


plus [androidJvm]
Content
@WorkflowUiExperimentalApi()

operator fun ViewRegistry.plus(binding: ViewFactory<*>): ViewRegistry
@WorkflowUiExperimentalApi()

operator fun ViewRegistry.plus(other: ViewRegistry): ViewRegistry


withCompositionRoot [androidJvm]
Content
@WorkflowUiExperimentalApi()

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