Welcome back and this video, we are going to at the feature that we stored the weather data and share
preferences so that we can then load the data, even if we, for example, don't have an Internet connection
and we open up the app so that it doesn't look empty or doesn't display nothing pretty much OK.
So at least it will display something, even though it will not be the correct data for the current
moment, but it will, of course, try to reload.
OK, so we're just going to see how to store data with shared preferences, which is a way to store
very simple data, so to speak.
Usually you would only store strings in there or integers or doubles, but you can also store whole
objects if you make those classes, as we have done, serializable.
All right.
So we made everything serializable.
So this allows us to make, so to speak, string's out of it so we can make an object to become a string,
which we then store and share preferences.
Because if we look at our Lochhead, we could already see that we get the whole response pretty much.
So this weather response is in the end, really just a string, OK?
So all of this information is stored as a string and jasa so we can just bring it in the right format
and store it.
So let's go ahead and do that.
Therefore, we need to go to our main activity and first of all, start with creating a new variable,
which will be our shared preferences so private, late in it, variable and shared preferences will
be of time shared.
Preferences.
All right, next, we need to initialize those shared preferences and I think a good spot would be uncreate.
So just here, let's go ahead and use Hamshire preferences to initialize it with get shared preferences.
Now, here we need to enter a name and then a mote.
So the name that I want to use is a constant.
So let's go ahead to our project and go to our Java folder constants.
So here I'm just going to create new variables.
Right?
So first of all, the preference name, so conceivable preference name and then weather response data.
So these two variables as to which I want to have.
OK, so the preference name will be the weather preference, which will be our shared preferences that
I want to use.
So going back to our main activity here, I will use constant thought, shared or preference may.
And then I need to give it a moat, so let me do that real quick here.
So context Dot and here are multiple modes available and I'm going to use the most private, which in
the end.
Is the file creation mode where the created file can only be accessed by the calling application or
all application sharing the same user ID, so it pretty much means that this shirt preference information
that we store on the phone should only be visible for this application and no other application.
You can, of course, store your data so that it will be accessible from other applications, but in
our case, we're not going to do that.
All right, now inside of our and method, and that is when we get the weather details here in on cue,
once we get the response, once we have the weather list, at this point, we can go ahead and store
the weather list and share preferences.
So instead of.
Passing that to the CWI or whether list what we will do is we will say, OK, Sarabi doesn't need that,
it will get that from the shared preferences because storing something in your preferences and getting
it back from ship preferences is a super quick process.
It doesn't take too much time.
OK, so what are we going to do is we're going to store the weather list inside of our shared preferences.
So I'm going to create a new variable, which is going to be weather response.
Jason.
String, where I will use Jason to Jason with our weather list.
So basically, I'm bringing it into this format, so let me imported Jason.
And now I'm bringing it into a string type of format.
OK, to Jason will return a string.
OK.
So now I have this string and as you know, share preferences can store strength, so we need to prepare
the ed our shared preferences.
Ed so this is the default behavior or default code that you use for this.
So you start the headed process at this point and then you put information into this, Ed, and you
can see you can put a boolean float in long string and string set.
So you are really limited to basic data types.
So it cannot pass in the whole object, even though we do that with a little trick, as you can see,
so we can go ahead and store that under weather response data, dot or comma and what we store in there
while our weather response Drayson String.
OK, so instead we could have of course, just stored something like hello here and then it would be
stored in the key weather response data, which is this weather response data string that we paired
in our constants.
OK, so basically it's always a string to whatever type you had afterwards.
Pair.
OK, so here string two in this case, string pair.
So we're putting something at a position.
So this is the position where we put it, so to speak, a.
Folder in which we put it, and this is going to be the file that we put into that folder.
All right.
So in our case, it's of course not going to be hello, but it's going to be the strength we just prepared
based on the weather list, which is, of course, if we look at it.
The response result, which is this whole weather response, pretty much, but it's not just one, it's
all of them.
But we, of course, only have one that we actually use.
So we have this, Ed, and then we need to apply the changes.
So we prepared it.
We put the string in there, we applied it.
And now at this point we can call set up by now.
We, of course, need to change that up UI because here we don't need a weather response anymore, a
weather list.
But instead what we do is we get that from our shared preferences.
So let's go ahead and get it from there.
Voll Weather response.
Jason String will be Hamshire preferences dot get string.
So now we'll get the string back from our constants dot weather response data.
So now at this point, we get this whether.
Response to Jason Strain, and we can, of course, go through it by checking if it's not now or if
it's not empty, so we can check both weather response.
Jason String is not null or empty.
So here, by the way, I'm using this exclamation mark for this not statement that I just said, OK.
And then we can make a Jason or Jason from it back, so pretty much we can convert it back.
How do we do that?
Well, create a new variable called weather list.
Which will be our Jason D'Arte from Jason, and we're going to pass the weather responses and string.
And of course, this whole Falu.
All the way up down here can be put inside of this if statement.
OK, so here.
We are pretty much converting it from Jason, and by the way, Jason also needs to know how to interpret
this string because it's just getting a string.
But we saw from Jason before and also needs to have the type for the class.
So actually the class like this here.
OK, so we are passing the class that we want to use for this.
And this is our weather response class.
That.
And of course, we need to write it correctly.
OK, weather response, class Java.
So as it seems, I still have a little error in their.
We have to be super careful here.
So when we get the string.
We need to pass on the default value.
That's something that I missed.
So that's the thing if you.
Have way too little space on the screen.
OK, so what I changed is in our shared preferences, that's something that you have to take care of.
The weather response, Jason, strength.
We get the string from our weather response data, but we also need to assign a default value.
So what if there is nothing in there?
So I'm going to say, OK, if there's nothing in there, just use an empty string as a default value
for weather response and string.
So then what we do is we check is that string empty or not?
So in this case, if it's the default value, which will be the case at the very first time that we
start the application now, then it will be empty and it will do the else block of this if statement,
which is nothing.
But if it's not empty, which means the second time that we run it, what it will do is it will.
Go inside of this if statement and then it will get the weather list from our Jason strength.
And it's going to be transferred into a weather object, which is going to be, of course, a weather
response object which contains all of this information.
So we'll coordinate the list with the weather, the string and so forth.
So that may visibility and so forth.
OK, so it's going to give us all of that and then we can use the follow up as we did before.
So nothing has changed pretty much, at least for this for loop.
All right, so at this point, we need to make sure that up, which is also called in our uncreate method,
so let's go in and create.
And I think it makes sense to set up the UI directly.
Under our shared preferences, so as soon as we got in the ship preferences.
So now let's test it and see what's going to happen.
So the thing is it well, first of all, have empty data and then it will load the data that will populate
the data.
And at the same time, it will store the data and share preferences.
So the next time we open up the application.
You can see the next time we open the application, it will not be empty as it was before.
So let me run it again.
Just to show you.
So you can see it had data there already, which was the old data, but it had data there already before
it allowed it to data from the show preference.
So that's really the idea behind it, even though it's not going to display the current up to date data,
but at least it will display something for the user before Dunnill, of course, loads the data from
the server.
All right, so that's how you can use your preferences to use Jason objects basically, and store them
on your phone without using scroll light or anything complicated, really keeping it super duper simple.
All right.
So I said let's check out the next video.