1
00:00:04,500 --> 00:00:06,000
Welcome back. All right.

2
00:00:06,000 --> 00:00:09,360
Did you spot what we did in TaskTimerViewModel

3
00:00:09,360 --> 00:00:12,260
that we didn't do in DurationsViewModel.

4
00:00:12,260 --> 00:00:15,060
We're not observing the changes to the database

5
00:00:15,060 --> 00:00:18,060
and don't update the adapter when the data changes.

6
00:00:18,720 --> 00:00:23,120
The code is almost identical to the code we used in TaskTimerViewModel.

7
00:00:23,620 --> 00:00:26,120
So I'll copy and paste it from there.

8
00:00:45,420 --> 00:00:48,020
Accept the imports then change the function name.

9
00:00:48,420 --> 00:00:52,020
We called it LoadTasks in TaskTimerViewModel,

10
00:00:52,020 --> 00:00:54,120
but it's called LoadData in this class,

11
00:00:54,480 --> 00:00:58,480
that's declared our ContentObserver. Now we have to register it.

12
00:00:58,880 --> 00:01:00,580
We do that in the init block.

13
00:01:28,780 --> 00:01:33,380
Once again, that's very similar to the code in the TaskTimerViewModel class.

14
00:01:34,130 --> 00:01:36,790
We're observing changes to the timings table

15
00:01:36,790 --> 00:01:38,790
rather than the tasks table

16
00:01:39,290 --> 00:01:42,290
so we use the timings contract content URI.

17
00:01:42,840 --> 00:01:46,200
I've also obtained the application reference a bit differently.

18
00:01:46,750 --> 00:01:48,410
Inside the init block,

19
00:01:48,410 --> 00:01:52,010
we still have access to any arguments passed to the constructor

20
00:01:52,560 --> 00:01:55,260
that means we don't have to call get application,

21
00:01:55,650 --> 00:01:57,310
we can use application directly.

22
00:01:57,860 --> 00:01:59,260
I wanted to point that out

23
00:02:00,060 --> 00:02:03,060
but I'd probably use get application throughout the class

24
00:02:03,360 --> 00:02:05,660
as I did in the TaskTimerViewModel.

25
00:02:06,260 --> 00:02:10,250
It's less confusing if all the references to the application object

26
00:02:10,250 --> 00:02:11,950
are obtained in the same way.

27
00:02:12,610 --> 00:02:16,110
But if you see other android view model classes that access

28
00:02:16,110 --> 00:02:18,610
application directly in their init block,

29
00:02:18,970 --> 00:02:21,470
you now know that it's exactly the same object

30
00:02:21,470 --> 00:02:23,340
the get application returns.

31
00:02:24,640 --> 00:02:27,840
The last thing we need to do is stop observing

32
00:02:27,840 --> 00:02:29,940
when our ViewModel is being destroyed.

33
00:02:30,600 --> 00:02:35,590
That's important, failing to do it can result in your app leaking resources.

34
00:02:36,490 --> 00:02:39,590
I'll copy the entire uncleared function

35
00:02:39,590 --> 00:02:43,590
from TaskTimerViewModel and paste it at the end of this class.

36
00:03:01,790 --> 00:03:05,390
That's all the changes we need to make, let's see if it works.

37
00:03:06,390 --> 00:03:07,750
I'll run the app again

38
00:03:10,250 --> 00:03:13,750
and delete all the records before the 15th of May

39
00:03:13,750 --> 00:03:14,950
2019.

40
00:03:32,720 --> 00:03:36,620
This time the display updates as soon as I confirm the change.

41
00:03:37,500 --> 00:03:41,840
Creating observers is a flexible way for your classes to react to changes

42
00:03:41,840 --> 00:03:43,340
in other parts of the system.

43
00:03:44,140 --> 00:03:47,640
Our activities are observing the live data objects

44
00:03:47,640 --> 00:03:49,140
in the ViewModel classes,

45
00:03:49,580 --> 00:03:52,380
and our ViewModels are observing the URIs

46
00:03:52,380 --> 00:03:55,080
that they pass to the on ContentResolver.

47
00:03:56,680 --> 00:03:59,040
We haven't had to create interfaces

48
00:03:59,040 --> 00:04:01,140
to implement CallBack functions

49
00:04:01,140 --> 00:04:03,940
to let the ViewModels know the data has changed.

50
00:04:04,840 --> 00:04:09,140
As long as you remember to unregister your observers to avoid memory leaks,

51
00:04:09,500 --> 00:04:11,500
you'll find observers are very useful.

52
00:04:12,700 --> 00:04:16,899
We'll be using another observer later when we get the reports to update

53
00:04:16,899 --> 00:04:18,899
if the user changes the settings.

54
00:04:19,700 --> 00:04:20,899
But before that,

55
00:04:21,399 --> 00:04:23,400
we've got a small bug in our code,

56
00:04:23,400 --> 00:04:26,700
I'll demonstrate it in the next video. See you there.

