Welcome back and this video we are going to use to storage and we're also going to update the user,
and before we do that, I would like to talk about two things.
One of them is the concept of a hash map.
The concept of a hash map is also something that is called dictionary and other programming languages
such as Swift.
And the idea is that you have a pair of a key and a value, but this whole thing is stored in a hash
map instead of in an object.
OK, so what we have done is we stored stuff in an object, so we created an extra user class for the
user information.
But in some cases you don't need to pass on the whole object.
But it makes sense to pass in a hash map and it is even required in some cases.
So we need to look into hash maps.
So the idea is that you, for example, have a string as the key and then a value of the type any as
the value itself.
So in our case, we will have any as the type that is stored within the hash map and that will be the
value type, so to speak.
OK, so we have the key email and the value then it's a that come then we have the key FCMB token.
The value is an empty string, then the ID image and then here the mobile is a different again.
So we have to string as the key type and then the value type is the loan value here that we can see
here, which is a number.
We will see later on when that makes sense, and I think I will show you a little more then then it
will be a lot easier to understand.
I think the other thing that we're going to use is the storage.
All right.
So far, we have used authentication database and now it's time to use storage and you can simply get
started.
So here, store and retrieve user generated files like images, audio and video without server side
code.
So this super powerful, very helpful.
We can just go ahead and store the images that we have and the storage.
All right.
So let's get started with it.
You can see you have to have the security rules accordingly.
And at one point, so after you define your data structure, you will need to write rules to secure
your data.
You should definitely check this out.
But for now, we can just press next and we can now use it.
And here the cloud storage location is going to be the same as we have selected before.
So now I'm going to press done and it's creating a default bucket for us.
So now we can go ahead and start storing stuff in our firebase storage.
You can upload files from here directly if you want to.
You can see that there are certain rules that you can define.
So if you want to know more about the rules of you, the documents and check those out and a lot more
depth.
But what is interesting for us is going to be to upload files.
By the way, we also have here usage where we can see how much bytes are stored, how many objects there
are, the bandwidth that was used so far and everything.
All right.
So let's go ahead and upload files, but of course, not by using it manually, but via code.
Therefore, we need to go to our gradle and add an implementation here, so.
Let me add this implementation, which is come that Google, that firebase Colen fire based on historic
cold in 1931, that one.
All right.
So once you have added that, let's think it.
And once it sink, we can use it.
So now let's go back over to our my profile activity in here.
I will create a new function which will help me to understand the file extension of a file that we have
downloaded because a file could have multiple different extension types.
Right.
And it's interesting for us, because if it's an image type, then we can use it as an image.
If it's another type, then we cannot use it as an image, for example, not as a profile image.
So we're just going to go ahead and create a function which will get the file extension based on the
URAI.
So we get a your eye, which is a link.
Right.
And we can see is it an image or whatever type is it.
All right.
So we can return a string or we can return nothing.
Right.
It really depends.
Now, you see, we, of course, need to return something.
And what we will use is we will use the mime type class.
So return mime type map, get Singleton.
Thought, Oh, here, get.
Extension and by the way, we need to import MYM type map as to that real quick and then get extension
from my type.
And here we can pass into your right, so content resolver, get type with the URL that we pass in and
it's our you or I or it's all.
Let's look over what's happening here.
So MYM type map.
The two way map that maps modern times to file extensions and vice versa, so it's basically a clause
which allows us to find out the type of a you're right, then we use get Singleton in order to get an
instance of this mind type map class so that we can use its functions.
Then we use to get extension from a mine type.
So we basically find out extension for the given mine type.
So we just go ahead and say, OK, use this.
I get to type of the you or I used the content resource over here from the context.
So basically we just say, OK, use this URAI here and then return the.
Extension, so the type of it or find it out based on the extension.
So if we have a file, which is let's just use the Homer Simpson example again.
So he it's stored right here, let's use this value.
So you can see it has to start P and G, so this is the extension is the ending of the file.
It starts with a dot so we know, OK, it's of type and G.
So this will be an image.
If it's of type three, then it will be an audio file if it's of type MyFord and it will be a video
file.
All right.
So basically, we're passing in the whole link, this GPS and so forth, that PNG and we will get a
return type, which will be of type string, and it will basically give us the extension type.
OK.
All right.
Now it's time to upload something to our storage.
So private phone upload user images.
I'm going to call this function.
And what it should do is it should, first of all, show the progress, dialogue, social progress,
dialogue with our please wait text.
So please wait.
Actually, we have to get it from the resources, so here we sources get string.
Heartstring thought, and here was something like, please wait.
Yeah, there it is.
So we have this string, right, which is called please wait, this one.
So of course, we want to make our application easily extendable and adjustable.
So that's why we used this approach.
OK, so we showed a progress dial up and then.
If the selected image file eye is not empty, so if I am selected image file your eye, that's the variable
that were created in the last video.
If that is not now, then do something with it.
So basically, you want to then store it in our storage, OK?
And in order to do that, we need to have a storage reference, first of all.
All right.
So we use VUL SRF, which stands for storage.
Referents will be our firebase and is, by the way, to type.
It's not equal sign here.
Colon storage reference.
We need to import that.
It's also from Firebase.
Right.
So this is a class which is given by Firebase and we can go ahead and use Firebase Storage now to get
the instance of it.
And here in Port Firebase Storage as well.
Or write it correctly like this, OK, then you don't need to.
Well, actually, we need to import it.
Let's do that.
Then you enter reference.
Thought child.
So this is basically the structure that you will need every time if you want to store something, so
you need a storage reference first, then you get the child of that firebase storage instance.
And here you can pass in the name of the image that you want to then use.
OK, so in our case, what I want to do is I want to call this one user image.
Plus system, that current time in milliseconds, plus a dot and then the file extension, so get a
file extension based on our M and how do we call it M selected image urai.
OK, so let's look at it in one line so it's a little more readable.
So basically what we want to have is an image which has a very unique name.
So it's going to be called user image.
Then it's going to take the current time in milliseconds and then it will add the file extension.
OK, so this will generate a rather unique value for us so that each image has a unique name.
So it's going to be whatever disk will return.
So it's going to return that time in milliseconds.
Right.
So it's going to be a very long, long value.
OK, plus, I don't intend to file extension that we get from our selected image file.
You're right that we have stored on our device.
So that's what we use to get file extension function for so that we get the extension, that we can
add that to the location where we want to put it.
So this is the storage reference where we want to put our image and now let's put the image there.
So we can go ahead and say as ref dot put file and which file we want to put there?
Well, our M selected image file you're right now thinks this is a nullable.
So what we do is we force it here because we check that it's not known.
So we only do the code if it's not all right.
And then when we put a file onto our storage, we can check if it was successful or not.
So year on or add on.
Listener Adam Complete listener at Unsuccess Listener.
So all of those are available again.
And I want to use the add on success listener.
And there I'm going to get a snapshot.
And as a result, I want to then use this test snapshot to log it for now.
So what I'm going to do is I'm just going to take the snapshot, get the metadata from it, get the
reference from it, get to download your URL from it and make a string from it.
OK, so basically with this line here, I make a string out of where the file is stored.
OK, and I'm just going to log that as an arrow into my log saying Firebase immaturely could of course
use info here as well instead.
So we're going to get an info saying Firebase Image URL and then the imagery where it is stored.
And we can later on check this value out in our Lockette.
All right, so what is happening here is that once we put the file, we get an unsuccess, listen.
All right.
And when once it was successful, we get a snapshot of that task and that task will give us to download
Jurrell.
And we need to download the URL in order to store it in the user data as well, so in order to store
it here, right.
If you want to store it here, we need to get the link, the link of the file in the storage.
All right.
So in the storage, it gets a unique link.
And now we want to also storage in the database.
So let's prepare that.
Therefore, we take this task snapshot, we get its metadata and so forth.
So we pretty much do the same thing as here.
Except for this two string part.
And we add another Unsuccess listener here.
So here this time, we gather your eye.
Which is the actual link.
OK, now we can go ahead and store that, you or I, somewhere.
So what we can do is we can have another log entry.
Which is going to be the you are I as a string, so downloadable imagery, and that will now be to your
eye to string.
And now what we can do is we can go ahead and create a variable in which we store that value.
So let's go out and create a new variable here.
So we have to select that image file URAI, which is on the device.
But now we need to have the downloadable urai.
So private var and profile.
Image, you're going to call it simple string, because that's going to be fine as well, an empty string
for now.
So the profile image, you, Earl, and now we can go ahead and set that.
So in the upload user image function.
Here we can go ahead and set it.
So let's say profile image, your URL is going to be a you or I to string so that you write that we
get as a string will be stored in a profile image URL.
Now, at this point, what we want to do is we want to update the user profile data with this profile
image.
OK, so here to do update user profile data, so we will need to create a function which will update
the user profile data, which includes the name and all of the changes that the user could have made.
It could have changed to email.
Well, not that, but the phone number, his name, and then whatever settings you want to add later
on as well.
OK, but of course, when something can go right, there's also something that can go wrong.
So let's add.
For this part, here for is putting file something into our database or into our storage and on failure
listener.
So what will happen if we get an error here?
So then we will get an exception.
Right.
And what do we want to do with this exception?
Well, what we're going to do is just write a little toast with the message.
OK, so we're just going to take this exception and then display the exception message.
And of course, what we should also do is hide our progress dialogue, because otherwise it will be
constantly displaying if we had the error and would not disappear.
All right, so at this point, let's just test it real quick, and in order to be able to test it,
we need to give our update button a functionality.
So let's go ahead and use the button update.
Said on Click Listener.
And here we're just going to say, if I'm selected, image URL is not empty, so it's not null, then
run our update or upload user image.
Of course, it's not done yet.
We still need to do a lot more here.
So what if the image you URL is empty?
What do you want to do then?
Well, then we just want to update the other information.
For example, with the user, changed the name or changed the phone number.
And we still want to update those.
Right.
All right.
Now, now test tested with what we got.
OK, so let's go over to our profile.
Now, let's select the image of Homer.
And let's see if we can upload anything.
OK, so let's press update, please wait and of course, we don't have the functionality that will close
this because currently if we look at our code.
When we upload it, upload image URL, we start a progress dialogue and we only close it in the unfilial
listener, so here it is, hard progress dialogue.
Of course, we will later on do that in the update user profile data.
So this high progress dialogue, but for now, we can do it in here as well.
But now let's just seen the Lockette to check out if we get something.
And what we should get is a downloadable.
And Maduro, so let's search for downloadable and we can see downloadable images, URL as Firebase Dorridge,
Google APIs, dot com slash V and so forth.
So the name of our project here, app spot dot com slash user image and then this.
Value and then tarping.
So it's one five eight two five five search for these values here in our storage.
And let's see if we got it there, we can see one five eight two five five.
All right.
So the images here and if we check out this image, we can see it's this Holmer image.
It has roughly 75 kilobytes.
But overall, you can see it works.
So now you know how you can store something on your firebase storage, but of course what we want to
do is also store it in our database here as well.
So where we just update the image here, what the New York URL, and that's what we're going to look
at in the next video.
So I'll see you there.