|
| 1 | +package com.squareup.sample.runtimelibrary.app |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import androidx.activity.viewModels |
| 5 | +import androidx.appcompat.app.AppCompatActivity |
| 6 | +import androidx.lifecycle.SavedStateHandle |
| 7 | +import androidx.lifecycle.ViewModel |
| 8 | +import androidx.lifecycle.viewModelScope |
| 9 | +import com.squareup.sample.runtimelibrary.lib.checkJvmLinkage |
| 10 | +import com.squareup.workflow1.RuntimeConfig |
| 11 | +import com.squareup.workflow1.Workflow |
| 12 | +import com.squareup.workflow1.WorkflowExperimentalRuntime |
| 13 | +import com.squareup.workflow1.android.renderWorkflowIn |
| 14 | +import com.squareup.workflow1.config.AndroidRuntimeConfigTools |
| 15 | +import com.squareup.workflow1.internal.withKey |
| 16 | +import com.squareup.workflow1.ui.ViewEnvironment |
| 17 | +import com.squareup.workflow1.ui.compose.withComposeInteropSupport |
| 18 | +import com.squareup.workflow1.ui.withEnvironment |
| 19 | +import com.squareup.workflow1.ui.workflowContentView |
| 20 | +import kotlinx.coroutines.CoroutineScope |
| 21 | +import kotlinx.coroutines.flow.StateFlow |
| 22 | +import kotlinx.coroutines.flow.map |
| 23 | + |
| 24 | +private val viewEnvironment = ViewEnvironment.EMPTY.withComposeInteropSupport() |
| 25 | + |
| 26 | +class AppActivity : AppCompatActivity() { |
| 27 | + |
| 28 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 29 | + super.onCreate(savedInstanceState) |
| 30 | + |
| 31 | + // This method is defined by both Android and JVM targets. |
| 32 | + RuntimeException().withKey("foo") |
| 33 | + |
| 34 | + // This makes the same call as above, but is linked with the JVM artifact. If Gradle is |
| 35 | + // misconfigured, it'll try to load a different class with the same FQN. |
| 36 | + checkJvmLinkage() |
| 37 | + |
| 38 | + // This ViewModel will survive configuration changes. It's instantiated |
| 39 | + // by the first call to viewModels(), and that original instance is returned by |
| 40 | + // succeeding calls. |
| 41 | + val model: AppViewModel by viewModels() |
| 42 | + workflowContentView.take( |
| 43 | + lifecycle = lifecycle, |
| 44 | + renderings = model.renderings |
| 45 | + .map { it.withEnvironment(viewEnvironment) } |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + @OptIn(WorkflowExperimentalRuntime::class) |
| 50 | + class AppViewModel(savedState: SavedStateHandle) : ViewModel() { |
| 51 | + val renderings: StateFlow<AppRendering> by lazy { |
| 52 | + renderWorkflowIn( |
| 53 | + workflow = AppWorkflow, |
| 54 | + scope = viewModelScope, |
| 55 | + savedStateHandle = savedState, |
| 56 | + runtimeConfig = AndroidRuntimeConfigTools.getAppWorkflowRuntimeConfig(), |
| 57 | + ) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fun <O, R> renderWorkflowInWrapper( |
| 63 | + workflow: Workflow<Unit, O, R>, |
| 64 | + scope: CoroutineScope, |
| 65 | + runtimeConfig: RuntimeConfig, |
| 66 | +): StateFlow<R> = renderWorkflowIn( |
| 67 | + workflow = workflow, |
| 68 | + scope = scope, |
| 69 | + runtimeConfig = runtimeConfig, |
| 70 | +) |
0 commit comments