1
00:00:03,920 --> 00:00:09,470
G'day everyone, welcome back. As SettingsDialog
isn't doing much at the moment, one thing

2
00:00:09,470 --> 00:00:14,360
it should certainly do, is save the
values of the user changes. We'll do that

3
00:00:14,360 --> 00:00:19,400
using the default shared preferences, and
we've seen that before, in the flickr

4
00:00:19,400 --> 00:00:25,339
browser app. We've got two values to save;
the day that a week starts on, and the

5
00:00:25,339 --> 00:00:30,380
number of seconds for timings that we
won't bother saving. Because the shared

6
00:00:30,380 --> 00:00:34,910
preferences uses key value pairs to
store the values, just like a bundle,

7
00:00:34,910 --> 00:00:40,190
I'll declare some constants for the two
keys. I'll also use the constants for the

8
00:00:40,190 --> 00:00:44,539
default values - the values that will be
used if the user hasn't chosen a

9
00:00:44,539 --> 00:00:47,050
different value.

10
00:01:00,620 --> 00:01:05,030
We can use those default constants to
initialize the fields or store the

11
00:01:05,030 --> 00:01:11,020
values. We also need a default day that
the week starts on.

12
00:01:26,560 --> 00:01:32,409
When we create a new gregorian calendar
instance and provide it with a locale,

13
00:01:32,409 --> 00:01:37,900
it'll automatically start the week on the
usual day for that locale. Well, it'll do

14
00:01:37,900 --> 00:01:41,760
that most of the time. I'll talk more
about that later.

15
00:01:41,760 --> 00:01:46,060
Alright, now we can write the functions
to save the settings, and read them back

16
00:01:46,060 --> 00:01:51,670
in again. I'll start with, say, values,
which I'll put after the onViewCreated

17
00:01:51,670 --> 00:01:54,119
function.

18
00:02:33,670 --> 00:02:38,920
There's nothing too complicated in there.
The spinner widget has a selected item

19
00:02:38,920 --> 00:02:43,840
position property, that returns a
zero-based value, corresponding to the

20
00:02:43,840 --> 00:02:50,420
position of the item in the list. Sunday
will be 0, Monday will be 1, and so forth.

21
00:02:50,420 --> 00:02:55,800
I've added 1 to the value to save time
later. When we come to work with dates,

22
00:02:55,800 --> 00:03:02,340
you'll see that the calendar class
uses 1 for Sunday, 2 for Monday, etc.

23
00:03:02,350 --> 00:03:07,239
The important thing is, that we won't end up
adding an incorrect value. The seek bar

24
00:03:07,239 --> 00:03:11,620
widget returns its value in the get
Progress method, but because we're using

25
00:03:11,620 --> 00:03:17,380
Kotlin, we can access it as a property.
We'll get back a value from 0, to the

26
00:03:17,380 --> 00:03:22,930
maximum value that we set, and we set
that at 24 in the layout. It's still not

27
00:03:22,930 --> 00:03:28,840
clear why I chose the value 24, but don't
worry, we'll be coming to that. Before

28
00:03:28,840 --> 00:03:33,459
saving the preferences, we check that
they have changed. That saves the user's

29
00:03:33,459 --> 00:03:38,739
battery, because we're not writing values
unnecessarily. There's another important

30
00:03:38,739 --> 00:03:44,140
reason for doing that. Our app will be
listening to the preferences, and will do

31
00:03:44,140 --> 00:03:49,720
things like re-querying the data, if the
starting date changes. That's quite

32
00:03:49,720 --> 00:03:55,380
costly, and we really don't want to do
that if the setting hasn't been changed.

33
00:03:55,380 --> 00:04:00,300
Alright, our SettingsDialog will need
to read in the current settings, so the

34
00:04:00,310 --> 00:04:04,720
widgets correctly reflect the stored
values. We'll need a readValues function

35
00:04:04,720 --> 00:04:09,959
to do that, and I'll put it before the
saveValues.

36
00:04:25,889 --> 00:04:31,600
That's just the reverse of saveValues.
We specify a default value for each call

37
00:04:31,600 --> 00:04:35,950
to getInt, so that we've got the correct
defaults, if no settings have been saved

38
00:04:35,950 --> 00:04:41,830
yet. Alright, the last step is to make
sure the two widgets reflect the values

39
00:04:41,830 --> 00:04:47,169
that we've just read in. The place to do
that is in the onViewStateRestored

40
00:04:47,169 --> 00:04:52,060
function. I mentioned that back in the
videos about the fragment life cycle.

41
00:04:52,060 --> 00:04:57,370
If we try to set the widgets in onView
Created, they'll be reset when onRestore

42
00:04:57,370 --> 00:05:03,160
InstanceState gets called. onViewState
Restored is called after Android's

43
00:05:03,160 --> 00:05:07,180
restored the state of all the widgets in
the view, and that's where I need to set

44
00:05:07,180 --> 00:05:12,580
them. I used the example of a checkbox in
the life cycle videos, but it applies to

45
00:05:12,580 --> 00:05:17,320
any widget that Android will save and
restore for us. I'll get Android studio

46
00:05:17,320 --> 00:05:22,660
to generate the stub after onViewCreated,

47
00:05:28,040 --> 00:05:30,940
then add the code.

48
00:05:49,540 --> 00:05:53,960
We can test the settings are working
after uncommenting that call to save

49
00:05:53,960 --> 00:06:03,960
Values, in the okButton's onClick
handler, onViewCreated.

50
00:06:03,960 --> 00:06:09,240
Okay, run the app and let's see how the settings behave.

51
00:06:13,320 --> 00:06:22,380
I'll change the start date to Monday, and
drag the slider to about halfway.

52
00:06:22,380 --> 00:06:29,660
When I tap OK, the settings should be saved. That
means they'll show Monday, and a seek

53
00:06:29,660 --> 00:06:38,220
bar value of about halfway, when I
go back in. And that's looking good.

54
00:06:38,220 --> 00:06:49,660
I'll choose Thursday and drag the slider all
the way to the right, then tap OK and go

55
00:06:49,670 --> 00:06:55,760
back into settings. That's working. For
the final test,

56
00:06:55,760 --> 00:07:06,230
I'll choose Friday and drag the slider
back to halfway. This time, I'll tap

57
00:07:06,230 --> 00:07:14,780
cancel. When I go back into settings, it
still shows Thursday with the slider

58
00:07:14,780 --> 00:07:19,690
over on the right. Cancelling doesn't save
the settings, so that's working as well.

59
00:07:19,690 --> 00:07:25,010
The text above the slider doesn't look
very good. In the next video, we'll see

60
00:07:25,010 --> 00:07:31,120
how we can replace that with the seek
bar's value. See you then.

