1
00:00:04,400 --> 00:00:05,600
Welcome back everyone.

2
00:00:06,800 --> 00:00:09,160
We've created a very basic activity

3
00:00:09,160 --> 00:00:11,660
to test our adapter and our layouts.

4
00:00:12,260 --> 00:00:14,260
There's still work to do in the activity.

5
00:00:14,760 --> 00:00:18,060
We're going to add a menu to allow the reports to be filtered by date.

6
00:00:18,720 --> 00:00:22,080
And we'll also let our users sort the report on different columns.

7
00:00:22,980 --> 00:00:27,440
First though, we're going to do things properly and use a ViewModel

8
00:00:27,440 --> 00:00:30,440
to handle fetching the data from the database.

9
00:00:32,240 --> 00:00:33,840
Create a new Kotlin class,

10
00:00:34,240 --> 00:00:37,240
which I'll call DurationsViewModel.

11
00:00:53,540 --> 00:00:56,640
The basics will be very similar to our existing view model.

12
00:00:57,190 --> 00:01:01,390
I'll start with the basics and we'll add the code to work with dates later.

13
00:01:02,050 --> 00:01:05,050
It's going to be useful to put a lot of logging in this class.

14
00:01:12,050 --> 00:01:14,850
Just like the TaskTimerViewModel class,

15
00:01:14,850 --> 00:01:17,550
we're going to subclass AndroidViewModel.

16
00:01:18,100 --> 00:01:22,500
That's because we need an application instance for accessing the content resolver.

17
00:01:37,700 --> 00:01:41,060
At the moment, our activity is fetching the data from the database.

18
00:01:41,920 --> 00:01:45,470
We don't want the activity to deal with anything that isn't concerned

19
00:01:45,470 --> 00:01:46,770
with the user interface.

20
00:01:47,370 --> 00:01:51,770
Anything else will be the responsibility of our DurationsViewModel class.

21
00:01:52,370 --> 00:01:54,370
I'll cut the database cursor,

22
00:01:54,370 --> 00:01:59,360
sort order and selection variables from the DurationsReport activity.

23
00:02:18,960 --> 00:02:23,260
The cursor shouldn't be a field because the activity is going to observe it.

24
00:02:23,660 --> 00:02:26,660
Just like we did in the TaskTimerViewModel class,

25
00:02:27,360 --> 00:02:30,660
it needs to be a live data object. In fact,

26
00:02:30,960 --> 00:02:34,260
I can copy the declaration from TaskTimerViewModel

27
00:02:34,510 --> 00:02:35,810
and paste it in here.

28
00:02:55,810 --> 00:02:57,810
We've now got a live data object

29
00:02:57,810 --> 00:03:00,810
that the DurationsReport activity can observe

30
00:03:00,810 --> 00:03:03,010
to get the cursor it needs for its adapter.

31
00:03:03,810 --> 00:03:07,410
We also want the activity to be able to change the sort order.

32
00:03:08,010 --> 00:03:12,410
When it does, the data will need to be re-queried to get the data in the correct order.

33
00:03:13,290 --> 00:03:17,390
I'll add a setter to the set order field to take care of that.

34
00:03:32,190 --> 00:03:34,690
Because we're re-querying the database,

35
00:03:35,090 --> 00:03:37,690
we check to make sure that the value has changed.

36
00:03:38,240 --> 00:03:40,040
There's no point accessing the database

37
00:03:40,040 --> 00:03:42,940
if sort order is set to the value it already had.

38
00:03:43,440 --> 00:03:47,430
We've got an error because we haven't written the load data function yet.

39
00:03:48,310 --> 00:03:51,810
Well, that's not true we have written it it's just in the wrong class.

40
00:03:52,360 --> 00:03:55,130
I'll cut it out of the DurationsReport activity,

41
00:03:55,530 --> 00:03:56,630
and paste it in here.

42
00:04:20,130 --> 00:04:22,330
That's almost fine as it is.

43
00:04:22,930 --> 00:04:25,700
The report adapter is owned by the activity.

44
00:04:25,700 --> 00:04:29,800
Instead of swapping the cursor, we just update the live data object

45
00:04:29,800 --> 00:04:31,500
that the activity is observing.

46
00:04:47,160 --> 00:04:48,520
We've still got an error.

47
00:04:49,220 --> 00:04:52,580
Remember that we can't use property access syntax

48
00:04:52,580 --> 00:04:55,880
to refer to the application in a ViewModel class.

49
00:04:56,430 --> 00:05:00,830
We have to call the getApplication getter function instead.

50
00:05:11,090 --> 00:05:11,990
All right.

51
00:05:11,990 --> 00:05:14,790
That's looking good we've got no errors at least.

52
00:05:15,450 --> 00:05:19,250
One thing we're not doing though is calling the LoadData function.

53
00:05:20,050 --> 00:05:22,410
It gets called when the sort order changes

54
00:05:22,960 --> 00:05:27,460
but we don't want to present the user with a blank screen until they sort the report.

55
00:05:28,660 --> 00:05:31,860
I'll add an init block to call LoadData.

56
00:05:32,630 --> 00:05:36,830
I'll put it after the field declarations, just before the LoadData function.

57
00:05:48,430 --> 00:05:51,430
I'll finish the video with one more change.

58
00:05:51,980 --> 00:05:55,280
Our sort columns enum really belongs in here

59
00:05:55,280 --> 00:05:57,080
rather than in the activity.

60
00:06:16,880 --> 00:06:18,780
This ViewModel should work,

61
00:06:19,330 --> 00:06:21,930
but we've made a real mess of durations report.

62
00:06:22,830 --> 00:06:27,030
We'll fix that and test everything still works in the next video.

