1
00:00:04,400 --> 00:00:06,100
Welcome back. So how did you get on.

2
00:00:06,700 --> 00:00:10,800
Most of the code is straightforward. There's just one bit that needs care.

3
00:00:11,300 --> 00:00:14,800
You may have done things differently to me but here's my solution.

4
00:00:15,460 --> 00:00:19,260
All the changes will be in the TaskTimerViewModel class.

5
00:00:29,760 --> 00:00:33,960
I'll start by getting a reference to the DefaultSharedPreferences

6
00:00:34,560 --> 00:00:38,160
and creating a variable to store the value that we should ignore.

7
00:00:39,260 --> 00:00:42,460
It's very similar to the code in DurationsViewModel.

8
00:01:38,960 --> 00:01:43,160
Next, we implement the OnSharedPreferenceChangeListener

9
00:01:43,560 --> 00:01:47,160
to update Ignore_less_than when the preference changes.

10
00:01:47,860 --> 00:01:49,660
I'll also log the new value.

11
00:02:12,020 --> 00:02:15,680
We need to register the listener in the init block

12
00:02:16,010 --> 00:02:18,310
and of course unregister it in OnCleared.

13
00:02:19,110 --> 00:02:22,310
I'll include some logging to let us check that things are working.

14
00:02:34,860 --> 00:02:38,960
Whenever we register a listener, we need to unregister again.

15
00:02:38,960 --> 00:02:41,320
And of course that will do in OnCleared. All

16
00:02:53,920 --> 00:02:56,420
that's pretty much the same as we did

17
00:02:56,420 --> 00:02:58,720
in the DurationsViewModel class.

18
00:02:59,420 --> 00:03:00,920
Where it gets interesting

19
00:03:01,220 --> 00:03:04,020
is when we use the ignore LessThan value

20
00:03:04,320 --> 00:03:06,020
in the SaveTiming function.

21
00:03:07,320 --> 00:03:09,720
When a user starts timing a task,

22
00:03:09,720 --> 00:03:11,620
we add a new row to the database.

23
00:03:12,280 --> 00:03:15,940
When they stop timing, we update that row with the duration.

24
00:03:16,930 --> 00:03:19,430
If we're going to ignore short durations,

25
00:03:19,790 --> 00:03:22,450
we need to delete the original row that was added.

26
00:03:23,250 --> 00:03:25,750
Our content provider makes that easy.

27
00:03:26,150 --> 00:03:29,450
First, I'll wrap the update code in a test.

28
00:04:46,450 --> 00:04:47,350
All right.

29
00:04:48,230 --> 00:04:50,230
We're now only updating the row

30
00:04:50,230 --> 00:04:52,930
if the duration is greater than our ignore value.

31
00:04:53,930 --> 00:04:57,430
If we don't update, we have to delete the row instead.

32
00:06:24,430 --> 00:06:27,030
It's not going to be easy to check what's going on,

33
00:06:27,530 --> 00:06:29,890
which is why I've added logging in each case.

34
00:06:30,490 --> 00:06:34,290
The logcat will let us know whether timings are being saved or not.

35
00:06:35,280 --> 00:06:39,180
Okay. It's time to test it. Run the app,

36
00:06:39,540 --> 00:06:41,340
and make sure the log cat's visible.

37
00:06:44,700 --> 00:06:47,700
I'll start by making sure everything still works

38
00:06:47,700 --> 00:06:49,200
before setting the option.

39
00:06:50,100 --> 00:06:52,100
When I time a task,

40
00:06:54,000 --> 00:06:56,000
then tap it again a second time,

41
00:06:57,900 --> 00:07:00,200
we can see it being saved in the logcat.

42
00:07:00,860 --> 00:07:04,850
Our update function returns 1, so it seems to be working.

43
00:07:06,550 --> 00:07:11,150
In the settings, I'll change the value to ignore to 5 seconds.

44
00:07:23,650 --> 00:07:25,350
I don't want to go too high

45
00:07:25,650 --> 00:07:29,010
because we'll be sitting in silence waiting for the time to elapse.

46
00:07:29,510 --> 00:07:33,180
Obviously, you should repeat these tests with a range of values.

47
00:07:34,060 --> 00:07:36,560
Okay. Let's see if timings

48
00:07:36,760 --> 00:07:38,960
less than five seconds are ignored.

49
00:07:39,760 --> 00:07:43,060
The logcat has logged the entry from our settings listener

50
00:07:43,060 --> 00:07:45,260
in the TaskTimerViewModel class.

51
00:07:45,760 --> 00:07:48,160
So we know the new setting's being detected.

52
00:07:48,820 --> 00:07:52,920
When I start timing a task and stop within 5 seconds,

53
00:07:52,920 --> 00:07:54,920
we can see the row being deleted.

54
00:08:00,720 --> 00:08:03,320
The delete function returns one

55
00:08:03,320 --> 00:08:05,920
indicating that one row was deleted.

56
00:08:06,580 --> 00:08:07,940
That's all looking good.

57
00:08:08,930 --> 00:08:13,330
The last test I'll do is to start timing and leave it for more than 5 seconds.

58
00:08:16,830 --> 00:08:21,030
If all goes well, we should see the update function returning 1

59
00:08:21,030 --> 00:08:23,030
to indicate a successful update.

60
00:08:23,830 --> 00:08:27,430
We'll also get a log entry from the SaveTiming function.

61
00:08:28,330 --> 00:08:31,990
I'm just killing time now. 5 seconds is a long time on video.

62
00:08:34,390 --> 00:08:38,390
Okay. That should do it. I'll stop the timing and check the logcat,

63
00:08:40,789 --> 00:08:42,090
and that looks good.

64
00:08:42,440 --> 00:08:45,940
We've got SaveTiming reporting that it saved the timing

65
00:08:45,940 --> 00:08:47,740
with the duration of 23,

66
00:08:48,340 --> 00:08:50,340
the update function returned 1,

67
00:08:50,700 --> 00:08:52,900
indicating that one row was updated.

68
00:08:53,800 --> 00:08:56,680
Obviously, you now want to perform thorough testing

69
00:08:56,680 --> 00:08:58,880
to make sure everything works as it should

70
00:08:59,380 --> 00:09:01,260
and the figures in the report are correct.

71
00:09:02,250 --> 00:09:03,850
That's the end of this section

72
00:09:04,350 --> 00:09:07,150
and all the functionality of our apps now complete.

73
00:09:08,140 --> 00:09:10,800
In this section we added a new activity to the app.

74
00:09:11,350 --> 00:09:14,750
That means main activity and its fragments

75
00:09:14,750 --> 00:09:18,850
will be stopped when we switch to the new durations report activity.

76
00:09:19,400 --> 00:09:23,060
That makes it easy to spot resources that we may be leaking.

77
00:09:23,940 --> 00:09:27,040
In the next section, we'll do some final tidying up.

78
00:09:27,840 --> 00:09:31,830
This is an essential step before you release your app to the play store

79
00:09:32,190 --> 00:09:33,390
and it should become a habit.

80
00:09:34,290 --> 00:09:35,890
I'll see you in the next section.

