G'day everyone, welcome back. As SettingsDialog isn't doing much at the moment, one thing
it should certainly do, is save the values of the user changes. We'll do that
using the default shared preferences, and we've seen that before, in the flickr
browser app. We've got two values to save; the day that a week starts on, and the
number of seconds for timings that we won't bother saving. Because the shared
preferences uses key value pairs to store the values, just like a bundle,
I'll declare some constants for the two keys. I'll also use the constants for the
default values - the values that will be used if the user hasn't chosen a
different value.
We can use those default constants to initialize the fields or store the
values. We also need a default day that the week starts on.
When we create a new gregorian calendar instance and provide it with a locale,
it'll automatically start the week on the usual day for that locale. Well, it'll do
that most of the time. I'll talk more about that later.
Alright, now we can write the functions to save the settings, and read them back
in again. I'll start with, say, values, which I'll put after the onViewCreated
function.
There's nothing too complicated in there. The spinner widget has a selected item
position property, that returns a zero-based value, corresponding to the
position of the item in the list. Sunday will be 0, Monday will be 1, and so forth.
I've added 1 to the value to save time later. When we come to work with dates,
you'll see that the calendar class uses 1 for Sunday, 2 for Monday, etc.
The important thing is, that we won't end up adding an incorrect value. The seek bar
widget returns its value in the get Progress method, but because we're using
Kotlin, we can access it as a property. We'll get back a value from 0, to the
maximum value that we set, and we set that at 24 in the layout. It's still not
clear why I chose the value 24, but don't worry, we'll be coming to that. Before
saving the preferences, we check that they have changed. That saves the user's
battery, because we're not writing values unnecessarily. There's another important
reason for doing that. Our app will be listening to the preferences, and will do
things like re-querying the data, if the starting date changes. That's quite
costly, and we really don't want to do that if the setting hasn't been changed.
Alright, our SettingsDialog will need to read in the current settings, so the
widgets correctly reflect the stored values. We'll need a readValues function
to do that, and I'll put it before the saveValues.
That's just the reverse of saveValues. We specify a default value for each call
to getInt, so that we've got the correct defaults, if no settings have been saved
yet. Alright, the last step is to make sure the two widgets reflect the values
that we've just read in. The place to do that is in the onViewStateRestored
function. I mentioned that back in the videos about the fragment life cycle.
If we try to set the widgets in onView Created, they'll be reset when onRestore
InstanceState gets called. onViewState Restored is called after Android's
restored the state of all the widgets in the view, and that's where I need to set
them. I used the example of a checkbox in the life cycle videos, but it applies to
any widget that Android will save and restore for us. I'll get Android studio
to generate the stub after onViewCreated,
then add the code.
We can test the settings are working after uncommenting that call to save
Values, in the okButton's onClick handler, onViewCreated.
Okay, run the app and let's see how the settings behave.
I'll change the start date to Monday, and drag the slider to about halfway.
When I tap OK, the settings should be saved. That means they'll show Monday, and a seek
bar value of about halfway, when I go back in. And that's looking good.
I'll choose Thursday and drag the slider all the way to the right, then tap OK and go
back into settings. That's working. For the final test,
I'll choose Friday and drag the slider back to halfway. This time, I'll tap
cancel. When I go back into settings, it still shows Thursday with the slider
over on the right. Cancelling doesn't save the settings, so that's working as well.
The text above the slider doesn't look very good. In the next video, we'll see
how we can replace that with the seek bar's value. See you then.