1
00:00:05,150 --> 00:00:06,250
Welcome back, everyone.

2
00:00:06,650 --> 00:00:08,650
Another change we wanted to make

3
00:00:08,650 --> 00:00:11,770
is to update the display when we delete a task

4
00:00:11,770 --> 00:00:13,270
that's currently being timed.

5
00:00:14,470 --> 00:00:18,270
Let's see the problem you're trying to solve then you can have a go at fixing it.

6
00:00:19,150 --> 00:00:21,510
Run the app, and make sure we're not timing anything.

7
00:00:22,610 --> 00:00:26,710
At the top of the screen, we've got the message "No task being timed".

8
00:00:27,590 --> 00:00:29,890
Long tap a task to start timing it.

9
00:00:32,890 --> 00:00:36,880
The display updates and now we're currently timing task 1.

10
00:00:37,280 --> 00:00:41,280
The problem we want to fix is what happens when I swipe the task left

11
00:00:41,640 --> 00:00:42,540
to delete it.

12
00:00:46,340 --> 00:00:49,640
When I do that and tap the delete button to confirm,

13
00:00:51,520 --> 00:00:53,020
the display doesn't change.

14
00:00:53,570 --> 00:00:56,470
We're still currently timing task 1.

15
00:00:56,870 --> 00:00:59,070
Of course, we're not,we just deleted it.

16
00:01:00,870 --> 00:01:01,870
The fix is easy,

17
00:01:02,230 --> 00:01:06,590
and the only change you need to make is in our TaskTimerViewModel.

18
00:01:07,140 --> 00:01:10,440
As long as you've taken the time to understand how all this works

19
00:01:10,800 --> 00:01:14,800
and how MainActivity is observing changes in the ViewModel,

20
00:01:15,050 --> 00:01:17,050
this is an easy change to make.

21
00:01:17,950 --> 00:01:21,950
So your challenge is to fix the code in TaskTimerViewModel

22
00:01:22,250 --> 00:01:26,050
to display the correct message when we delete the task that we're timing.

23
00:01:26,750 --> 00:01:31,110
Pause the video now, and I'll go over my solution when you come back.

24
00:01:36,360 --> 00:01:38,260
Welcome back. How did you get on?

25
00:01:39,060 --> 00:01:42,560
You should have been able to fix the display with just two lines of code

26
00:01:42,560 --> 00:01:44,060
inside a condition.

27
00:01:44,260 --> 00:01:47,460
Let's review how all this works before writing any code. 

28
00:01:47,460 --> 00:01:48,760
Open MainActivity.

29
00:02:06,160 --> 00:02:08,759
The display is updated in MainActivity

30
00:02:08,759 --> 00:02:12,760
by observing the timing LiveData object in the ViewModel.

31
00:02:13,120 --> 00:02:15,220
When the ViewModel deletes a task,

32
00:02:15,220 --> 00:02:17,820
it should check to see if the task is being timed.

33
00:02:18,520 --> 00:02:22,720
It can then update its LiveData object to allow MainActivity

34
00:02:22,720 --> 00:02:24,220
to update the display.

35
00:02:25,020 --> 00:02:29,620
We add the code in the TaskTimerViewModel's DeleteTask function.

36
00:03:35,980 --> 00:03:39,340
We're storing the task that's being timed in current timing,

37
00:03:40,140 --> 00:03:43,640
that may be null so we need a safe call operator.

38
00:03:44,440 --> 00:03:46,340
That's on line 135.

39
00:03:47,140 --> 00:03:51,140
If current timing's task ID is the same as the ID of the task

40
00:03:51,140 --> 00:03:52,140
being deleted,

41
00:03:52,640 --> 00:03:55,520
we update the TaskTimingLiveData object.

42
00:03:56,070 --> 00:04:00,370
Setting it to null will let MainActivity know that nothing's being timed.

43
00:04:01,250 --> 00:04:03,550
We also set current timing to null.

44
00:04:03,910 --> 00:04:05,510
If we've just deleted the task

45
00:04:05,910 --> 00:04:08,110
then, we're obviously not still timing it.

46
00:04:08,770 --> 00:04:13,230
I'll go into code, reformat the code, and then run the app again to test it.

47
00:04:25,670 --> 00:04:28,770
StartTiming a task then swipe to delete.

48
00:04:31,770 --> 00:04:35,670
The first test I'll perform is to cancel the deletion.

49
00:04:37,670 --> 00:04:40,870
This should restore the task and we should still be timing it,

50
00:04:41,370 --> 00:04:45,570
tap cancel and the task reappears and it's still being timed,

51
00:04:45,930 --> 00:04:46,830
so that works.

52
00:04:47,820 --> 00:04:52,420
Repeat the operation but this time tap the dialog's delete button.

53
00:04:56,320 --> 00:04:59,320
Task is deleted, and the display updates.

54
00:05:00,120 --> 00:05:02,120
There are other tests you should perform.

55
00:05:02,120 --> 00:05:06,620
Make sure that deleting a task that isn't being timed, also still works.

56
00:05:07,320 --> 00:05:09,820
You'll want to perform that test twice too,

57
00:05:09,820 --> 00:05:12,820
make sure that both the cancel and the delete work

58
00:05:12,820 --> 00:05:14,820
when the confirmation dialog appears.

59
00:05:15,920 --> 00:05:19,120
In the next video, we'll have a look at how we can implement

60
00:05:19,120 --> 00:05:23,580
parameterized queries in SQLite. I'll see you there.

