Step #6: Saving the Report
Now, we can wrap all this up, saving the report to the desired location.
First, add a new constructor parameter to RosterMotor
, so we can get access to a RosterReport
instance:
class RosterMotor(
private val repo: ToDoRepository,
private val report: RosterReport
) : ViewModel() {
This will require a change to its corresponding line in ToDoApp
to get()
the second parameter:
viewModel { RosterMotor(get(), get()) }
Then, add this function to RosterMotor
:
fun saveReport(doc: Uri) {
viewModelScope.launch {
report.generate(_states.value.items, doc)
}
}
Here, we launch()
our generate()
coroutine, supplying that list of items from the current RosterViewState
, along with the Uri
identifying where we want the report to be written.
Next, in RosterListFragment
, modify the createDoc
property to call saveReport()
on our RosterMotor
:
private val createDoc =
registerForActivityResult(ActivityResultContracts.CreateDocument()) {
motor.saveReport(it)
}
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.