Skip to content

Instantly share code, notes, and snippets.

@rjrjr
Created August 31, 2021 18:30
Show Gist options
  • Save rjrjr/9d8e314df212c4cfc0eeaafcef67c5b2 to your computer and use it in GitHub Desktop.
Save rjrjr/9d8e314df212c4cfc0eeaafcef67c5b2 to your computer and use it in GitHub Desktop.
@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