Skip to content

//workflow/com.squareup.workflow1.ui/renderWorkflowIn

renderWorkflowIn

[androidJvm]
Content
@WorkflowUiExperimentalApi()

fun <OutputT, RenderingT> renderWorkflowIn(workflow: Workflow<Unit, OutputT, RenderingT>, scope: CoroutineScope, savedStateHandle: SavedStateHandle? = null, interceptors: List = emptyList(), onOutput: suspend (OutputT) -> Unit = {}): StateFlow<RenderingT>
More info

An Android ViewModel-friendly wrapper for com.squareup.workflow1.renderWorkflowIn, for use with a workflow that takes no input (that is, has PropsT set to Unit).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
@OptIn(WorkflowUiExperimentalApi::class)
class HelloWorkflowActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

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

class HelloViewModel(savedState: SavedStateHandle) : ViewModel() {
  @OptIn(WorkflowUiExperimentalApi::class)
  val renderings: StateFlow<HelloRendering> = renderWorkflowIn(
    workflow = HelloWorkflow,
    scope = this.viewModelScope,
    savedStateHandle = savedState
  )
}

Return

A StateFlow of RenderingTs that will emit any time the root workflow creates a new rendering.

Parameters

androidJvm

workflow

The root workflow to render.

scope

The CoroutineScope in which to launch the workflow runtime, typically from the androidx ViewModel.viewModelScope extension. Any exceptions thrown in any workflows, after the initial render pass, will be handled by this scope, and cancelling this scope will cancel the workflow runtime and any running workers. Note that any dispatcher in this scope will not be used to execute the very first render pass.

savedStateHandle

Used to restore workflow state in a new process. Typically this is the savedState: SavedStateHandle constructor parameter of an androidx ViewModel.

interceptors

An optional list of WorkflowInterceptors that will wrap every workflow rendered by the runtime. Interceptors will be invoked in 0-to-length order: the interceptor at index 0 will process the workflow first, then the interceptor at index 1, etc.

onOutput

A function that will be called whenever the root workflow emits an OutputT. This is a suspend function, and is invoked synchronously within the runtime: if it suspends, the workflow runtime will effectively be paused until it returns. This means that it will propagate backpressure if used to forward outputs to a Flow or Channel, for example.

[androidJvm]
Content
@WorkflowUiExperimentalApi()

fun <PropsT, OutputT, RenderingT> renderWorkflowIn(workflow: Workflow<PropsT, OutputT, RenderingT>, scope: CoroutineScope, prop: PropsT, savedStateHandle: SavedStateHandle? = null, interceptors: List = emptyList(), onOutput: suspend (OutputT) -> Unit = {}): StateFlow<RenderingT>
More info

An Android ViewModel-friendly wrapper for com.squareup.workflow1.renderWorkflowIn, for use with a workflow that requires one input value (prop) to run.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@OptIn(WorkflowUiExperimentalApi::class)
class HelloNameWorkflowActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

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

class HelloNameViewModel(savedState: SavedStateHandle) : ViewModel() {
  @OptIn(WorkflowUiExperimentalApi::class)
  val renderings: StateFlow<HelloRendering> = renderWorkflowIn(
    workflow = HelloNameWorkflow,
    scope = this.viewModelScope,
    savedStateHandle = savedState,
    prop = "Your name here!"
  )
}

Return

A StateFlow of RenderingTs that will emit any time the root workflow creates a new rendering.

Parameters

androidJvm

workflow

The root workflow to render.

scope

The CoroutineScope in which to launch the workflow runtime, typically from the androidx ViewModel.viewModelScope extension. Any exceptions thrown in any workflows, after the initial render pass, will be handled by this scope, and cancelling this scope will cancel the workflow runtime and any running workers. Note that any dispatcher in this scope will not be used to execute the very first render pass.

prop

Specifies the sole PropsT value to use to render the root workflow. To allow updates, use the renderWorkflowIn overload with a props: StateFlow argument instead of this one.

savedStateHandle

Used to restore workflow state in a new process. Typically this is the savedState: SavedStateHandle constructor parameter of an androidx ViewModel.

interceptors

An optional list of WorkflowInterceptors that will wrap every workflow rendered by the runtime. Interceptors will be invoked in 0-to-length order: the interceptor at index 0 will process the workflow first, then the interceptor at index 1, etc.

onOutput

A function that will be called whenever the root workflow emits an OutputT. This is a suspend function, and is invoked synchronously within the runtime: if it suspends, the workflow runtime will effectively be paused until it returns. This means that it will propagate backpressure if used to forward outputs to a Flow or Channel, for example.

[androidJvm]
Content
@WorkflowUiExperimentalApi()

fun <PropsT, OutputT, RenderingT> renderWorkflowIn(workflow: Workflow<PropsT, OutputT, RenderingT>, scope: CoroutineScope, props: StateFlow<PropsT>, savedStateHandle: SavedStateHandle? = null, interceptors: List = emptyList(), onOutput: suspend (OutputT) -> Unit = {}): StateFlow<RenderingT>
More info

An Android ViewModel-friendly wrapper for com.squareup.workflow1.renderWorkflowIn, for use with a workflow that requires input (props) to run.

For example, for a workflow that uses android.content.Intent as its PropsT type, you could do something like this:

 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
33
@OptIn(WorkflowUiExperimentalApi::class)
class HelloIntentsWorkflowActivity : AppCompatActivity() {

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

    val model: HelloIntentsViewModel by viewModels()
    model.intents.value = intent

    setContentView(
      WorkflowLayout(this).apply { start(model.renderings) }
    )
  }

  override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)

    val model: HelloIntentsViewModel by viewModels()
    model.intents.value = intent
  }
}

class HelloIntentsViewModel(savedState: SavedStateHandle) : ViewModel() {
  val intents = MutableStateFlow(Intent())

  @OptIn(WorkflowUiExperimentalApi::class)
  val renderings: StateFlow<HelloRendering> = renderWorkflowIn(
    workflow = HelloWorkflow,
    scope = this.viewModelScope,
    savedStateHandle = savedState,
    props = intents
  )
}

Return

A StateFlow of RenderingTs that will emit any time the root workflow creates a new rendering.

Parameters

androidJvm

workflow

The root workflow to render.

scope

The CoroutineScope in which to launch the workflow runtime, typically from the androidx ViewModel.viewModelScope extension. Any exceptions thrown in any workflows, after the initial render pass, will be handled by this scope, and cancelling this scope will cancel the workflow runtime and any running workers. Note that any dispatcher in this scope will not be used to execute the very first render pass.

props

Specifies the initial PropsT to use to render the root workflow, and will cause a re-render when new props are emitted. If this flow completes after emitting at least one value, the runtime will not fail or stop, it will continue running with the last-emitted input. To only pass a single props value, simply create a MutableStateFlow with the value.

savedStateHandle

Used to restore workflow state in a new process. Typically this is the savedState: SavedStateHandle constructor parameter of an androidx ViewModel.

interceptors

An optional list of WorkflowInterceptors that will wrap every workflow rendered by the runtime. Interceptors will be invoked in 0-to-length order: the interceptor at index 0 will process the workflow first, then the interceptor at index 1, etc.

onOutput

A function that will be called whenever the root workflow emits an OutputT. This is a suspend function, and is invoked synchronously within the runtime: if it suspends, the workflow runtime will effectively be paused until it returns. This means that it will propagate backpressure if used to forward outputs to a Flow or Channel, for example.