Created
August 31, 2021 18:30
-
-
Save rjrjr/9d8e314df212c4cfc0eeaafcef67c5b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(WorkflowUiExperimentalApi::class) | |
class HelloWorkflowActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// This ViewModel will survive configuration changes. It's instantiated | |
// by the first call to viewModels(), and that original instance is returned by | |
// succeeding calls. | |
val model: HelloViewModel by viewModels() | |
model.init(this) | |
setContentView( | |
WorkflowLayout(this).apply { start(model.renderings) } | |
) | |
} | |
} | |
class HelloViewModel(savedState: SavedStateHandle) : ViewModel() { | |
private lateinit var activities: MutableStateFlow<Activity> | |
fun init(activity: Activity) { | |
if (!this::activities.isInitialized) { | |
activities = MutableStateFlow(activity) | |
} else { | |
activities.value = activity | |
} | |
} | |
@OptIn(WorkflowUiExperimentalApi::class) | |
val renderings: StateFlow<HelloRendering> by lazy { | |
renderWorkflowIn( | |
workflow = HelloWorkflow, | |
scope = viewModelScope, | |
savedStateHandle = savedState, | |
props = activities | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment