1
00:00:04,300 --> 00:00:08,660
Welcome back. It's slightly annoying having to leave the report screen

2
00:00:08,660 --> 00:00:12,260
to change the settings, especially as one of the settings

3
00:00:12,260 --> 00:00:14,060
is used to configure the report.

4
00:00:14,940 --> 00:00:18,140
Adding the option to the reports menu is trivial,

5
00:00:18,140 --> 00:00:19,740
so let's get that out of the way.

6
00:00:20,730 --> 00:00:25,230
Edit the menu report menu file in res/menu,

7
00:00:33,480 --> 00:00:36,480
drag a new menu item to the end of the menu.

8
00:00:41,980 --> 00:00:45,580
The ID should be rm_settings,

9
00:00:50,580 --> 00:00:53,780
and the title should be the string resource

10
00:00:53,780 --> 00:00:56,440
menutitle_settings.

11
00:01:06,320 --> 00:01:09,520
For the show as action, choose never.

12
00:01:16,320 --> 00:01:17,920
That's the menu changed.

13
00:01:18,170 --> 00:01:20,770
Next, we respond to it being tapped.

14
00:01:21,130 --> 00:01:25,130
We do that in the OnOptionsItemSelected function

15
00:01:25,430 --> 00:01:27,430
in the DurationsReport class.

16
00:02:13,430 --> 00:02:15,530
I won't spend time in the video

17
00:02:15,530 --> 00:02:18,530
testing the settings option appears on the reports menu.

18
00:02:18,830 --> 00:02:20,530
I suggest you do test it

19
00:02:20,530 --> 00:02:23,230
to make sure you haven't introduced a bug into the app.

20
00:02:24,730 --> 00:02:28,830
What you'll find is that changing the FirstDayOfTheWeek in the settings

21
00:02:28,830 --> 00:02:30,330
doesn't update the report.

22
00:02:31,130 --> 00:02:33,430
To make the changes take effect straight away,

23
00:02:33,430 --> 00:02:36,990
we need to observe the change to the app's shared preferences

24
00:02:36,990 --> 00:02:38,590
and respond when they change.

25
00:02:39,690 --> 00:02:43,790
The code to do that is very similar to how we're observing changes

26
00:02:43,790 --> 00:02:45,450
in the ContentObserver

27
00:02:45,950 --> 00:02:48,550
and also similar to our BroadcastReceiver.

28
00:02:49,540 --> 00:02:52,840
We create an implementation of the shared preferences

29
00:02:52,840 --> 00:02:56,940
on shared preference change listener interface and register it.

30
00:02:57,640 --> 00:03:00,840
We'll also unregister it in the uncleared function.

31
00:03:01,940 --> 00:03:06,300
We had a good demonstration of what can happen if you don't unregister things,

32
00:03:06,800 --> 00:03:09,160
so I don't think we need to say very much more about that.

33
00:03:09,660 --> 00:03:13,060
I'll create the listener after our BroadcastReceiver

34
00:03:13,060 --> 00:03:15,060
in the DurationsViewModel.

35
00:03:55,610 --> 00:03:59,210
It's important to check that we're responding to a change in the

36
00:03:59,210 --> 00:04:01,610
settings FirstDayOfTheWeek preference.

37
00:04:02,160 --> 00:04:04,060
We re-query the database

38
00:04:04,060 --> 00:04:07,660
and don't want to do that if an unrelated setting is changed.

39
00:04:08,540 --> 00:04:11,740
Our listener will be called when any of the settings are changed,

40
00:04:11,990 --> 00:04:14,590
so we ignore anything that won't affect the report.

41
00:04:15,470 --> 00:04:18,829
If the first day of the week setting is the one we were called about,

42
00:04:19,230 --> 00:04:22,930
we'll update the _FirstDayOfTheWeek variable,

43
00:04:23,430 --> 00:04:26,730
then set the calendar to the new first day.

44
00:04:27,730 --> 00:04:32,530
We used very similar code in the BroadcastReceiversOnReceive function.

45
00:04:33,630 --> 00:04:38,230
All right. Next, we have to register the listener in the init block.

46
00:04:51,830 --> 00:04:55,230
And of course, we need to unregister it in OnCleared.

47
00:05:10,230 --> 00:05:14,830
Time to test it. I'll run the app and go into the reports.

48
00:05:22,030 --> 00:05:26,530
It's easy to check that everything's working if I sort the report by date.

49
00:05:28,030 --> 00:05:32,030
At the moment, I've got rows from Tuesday to the following Monday.

50
00:05:32,530 --> 00:05:34,520
My week currently starts on a Tuesday.

51
00:05:35,170 --> 00:05:39,270
Call up the calendar to check if you've been experimenting with the settings.

52
00:05:41,970 --> 00:05:46,270
I'll go into the settings and change the first day to Friday.

53
00:05:54,770 --> 00:05:57,770
That's a day that is used in some places around the world.

54
00:05:58,570 --> 00:06:01,770
Tap okay, and the report updates straightaway.

55
00:06:04,070 --> 00:06:06,570
Listening to settings changes like this

56
00:06:06,570 --> 00:06:09,570
makes your app more responsive to your user.

57
00:06:10,230 --> 00:06:12,030
If they change one of the settings,

58
00:06:12,030 --> 00:06:15,030
they naturally expect the app to use the new values

59
00:06:15,530 --> 00:06:17,030
and this is how you can do that.

60
00:06:17,930 --> 00:06:20,530
There's only one thing left to implement in our app

61
00:06:21,030 --> 00:06:25,330
and that's the setting that lets the user choose to ignore short timings.

62
00:06:25,930 --> 00:06:29,230
We've provided the setting but we're not using it yet.

63
00:06:32,230 --> 00:06:35,830
So here's the challenge. Can you implement that feature?

64
00:06:36,430 --> 00:06:39,930
All the changes will be in the TaskTimerviewModel class,

65
00:06:40,430 --> 00:06:44,090
you'll need to listen to the change and update a variable

66
00:06:44,090 --> 00:06:48,090
when the settings_ignore_less_than preference changes.

67
00:06:48,990 --> 00:06:52,090
The variable will be used in the saveTiming function

68
00:06:52,450 --> 00:06:56,250
to only save timings that have a duration greater than it.

69
00:06:56,910 --> 00:07:01,270
Be careful with that part because it's not quite as straightforward as it seems.

70
00:07:02,070 --> 00:07:05,570
Make sure you understand how we're saving the timings

71
00:07:05,770 --> 00:07:08,370
before changing the saveTiming function.

72
00:07:09,360 --> 00:07:12,360
We've covered everything you'll need for the challenge.

73
00:07:12,360 --> 00:07:14,660
Have a go and see how you get on.

74
00:07:15,160 --> 00:07:17,960
I'll go over my solution in the next video.

75
00:07:17,960 --> 00:07:19,560
Good luck, and see you there.

