|
| 1 | +@file:OptIn(WorkflowExperimentalRuntime::class) |
| 2 | + |
| 3 | +package com.squareup.sample.nestedoverlays |
| 4 | + |
| 5 | +import android.os.Bundle |
| 6 | +import androidx.activity.viewModels |
| 7 | +import androidx.appcompat.app.AppCompatActivity |
| 8 | +import androidx.lifecycle.SavedStateHandle |
| 9 | +import androidx.lifecycle.ViewModel |
| 10 | +import androidx.lifecycle.viewModelScope |
| 11 | +import com.squareup.workflow1.WorkflowExperimentalRuntime |
| 12 | +import com.squareup.workflow1.config.AndroidRuntimeConfigTools |
| 13 | +import com.squareup.workflow1.ui.WorkflowLayout |
| 14 | +import com.squareup.workflow1.ui.WorkflowUiExperimentalApi |
| 15 | +import com.squareup.workflow1.ui.renderWorkflowIn |
| 16 | +import kotlinx.coroutines.flow.StateFlow |
| 17 | + |
| 18 | +@OptIn(WorkflowUiExperimentalApi::class) |
| 19 | +class NestedOverlaysActivity : AppCompatActivity() { |
| 20 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 21 | + super.onCreate(savedInstanceState) |
| 22 | + |
| 23 | + // This ViewModel will survive configuration changes. It's instantiated |
| 24 | + // by the first call to viewModels(), and that original instance is returned by |
| 25 | + // succeeding calls. |
| 26 | + val model: HelloViewModel by viewModels() |
| 27 | + setContentView( |
| 28 | + WorkflowLayout(this).apply { take(lifecycle, model.renderings) } |
| 29 | + ) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class HelloViewModel(savedState: SavedStateHandle) : ViewModel() { |
| 34 | + @OptIn(WorkflowUiExperimentalApi::class) |
| 35 | + val renderings: StateFlow<HelloRendering> by lazy { |
| 36 | + renderWorkflowIn( |
| 37 | + workflow = NestedOverlaysWorkflow, |
| 38 | + scope = viewModelScope, |
| 39 | + savedStateHandle = savedState, |
| 40 | + runtimeConfig = AndroidRuntimeConfigTools.getAppWorkflowRuntimeConfig() |
| 41 | + ) |
| 42 | + } |
| 43 | +} |
0 commit comments