Welcome back everyone.
We've created a very basic activity
to test our adapter and our layouts.
There's still work to do in the activity.
We're going to add a menu to allow the reports to be filtered by date.
And we'll also let our users sort the report on different columns.
First though, we're going to do things properly and use a ViewModel
to handle fetching the data from the database.
Create a new Kotlin class,
which I'll call DurationsViewModel.
The basics will be very similar to our existing view model.
I'll start with the basics and we'll add the code to work with dates later.
It's going to be useful to put a lot of logging in this class.
Just like the TaskTimerViewModel class,
we're going to subclass AndroidViewModel.
That's because we need an application instance for accessing the content resolver.
At the moment, our activity is fetching the data from the database.
We don't want the activity to deal with anything that isn't concerned
with the user interface.
Anything else will be the responsibility of our DurationsViewModel class.
I'll cut the database cursor,
sort order and selection variables from the DurationsReport activity.
The cursor shouldn't be a field because the activity is going to observe it.
Just like we did in the TaskTimerViewModel class,
it needs to be a live data object. In fact,
I can copy the declaration from TaskTimerViewModel
and paste it in here.
We've now got a live data object
that the DurationsReport activity can observe
to get the cursor it needs for its adapter.
We also want the activity to be able to change the sort order.
When it does, the data will need to be re-queried to get the data in the correct order.
I'll add a setter to the set order field to take care of that.
Because we're re-querying the database,
we check to make sure that the value has changed.
There's no point accessing the database
if sort order is set to the value it already had.
We've got an error because we haven't written the load data function yet.
Well, that's not true we have written it it's just in the wrong class.
I'll cut it out of the DurationsReport activity,
and paste it in here.
That's almost fine as it is.
The report adapter is owned by the activity.
Instead of swapping the cursor, we just update the live data object
that the activity is observing.
We've still got an error.
Remember that we can't use property access syntax
to refer to the application in a ViewModel class.
We have to call the getApplication getter function instead.
All right.
That's looking good we've got no errors at least.
One thing we're not doing though is calling the LoadData function.
It gets called when the sort order changes
but we don't want to present the user with a blank screen until they sort the report.
I'll add an init block to call LoadData.
I'll put it after the field declarations, just before the LoadData function.
I'll finish the video with one more change.
Our sort columns enum really belongs in here
rather than in the activity.
This ViewModel should work,
but we've made a real mess of durations report.
We'll fix that and test everything still works in the next video.