Welcome back.
Now that we have created the functionality to upload an image.
Let's go ahead and pretty much update our user info as well, because just uploading the image is not
going to be enough.
We also need to make sure that the image is stored in the user database as well.
All right.
So let's go ahead.
And first of all, create a little function here, which I'm going to call a profile update success,
which will be called each time that our profile update is successful.
And for now, we're just going to do the following.
We're going to highlight the progress dialog and we're going to finish the current screen.
Or activity, so we're closing the current activity with the finished statement here.
So basically, when the user has updated, what we want to do is we want to close this activity and
send the user back so he doesn't have to do it manually.
Next, what I want to do is and that will be the coat that will be done in our fire store functionality.
So let's go over to our forest or class.
So here we will have another function which will take care of updating the user profile data so you
can see this class is responsible for all of the database stuff, the other functions, they just do
the code that has to be done inside of the class itself and which is not directly linked to the fire
store or firebase functionality.
So this is very useful when you want to make your app unmaintainable.
OK, so fun update user profile data should be responsible of updating the user profile.
OK, and here what I'm going to require is an activity of type, my profile activity.
And a hash map, so let's look at this.
I'm going to call this one user hash map and what we have to have hash map and now we need to define
what variable types it will have.
So as the key, it will have a string and as a value it will have any.
OK, so this is what I was talking about in the last video.
So basically the idea is that we will have the functionality that allows us to pass in the user as a
hash map and I will show you why that is required in the second.
So let's just start this fire store collection stuff, as we have done before.
So we get to constant users, then we get to document for the current user ID and then now we don't
want to get the data, but we want to update the data.
Now, the thing is, if you want to update.
So here, let's have a look at this update.
It will offer you multiple options, so data field and field property or whatever, and the one that
I want to use is the one that requires a user or a Hashmat.
OK, so it requires a type of a Hashmat.
If you look at this update function, you will see none nullable or non null map with a string, an
object.
So object is the class name and Java.
And in Scotland it's called any and that's exactly the type that we have here.
So instead of object, you have to use any in.
Cotton and Kinzie object itself is not accepted.
We need to use any here, it's the cotton in class, the base class, everything is built upon.
So that's why we need to pass in a Hashmat instead of a user object, because it would have been easier,
as we have done before.
Right.
Where we get the whole thing is user object, and then we make changes to the user object and then we
just send it over and update it.
But the thing is, it requires something of type, Hashmat.
So we need to transform our user object into this specific Hashmat type.
So whenever you call this update user profile data, you don't need to pass on activity and user Hashmat
OK, and that will then update the collection.
So then the next thing we want to do is we want to add an unsuccess listener.
So we want to know if things went right.
So what do we want to do there?
Well, first of all, let's write a little luck entry here where we just say profile that update that
successfully.
Very basic log entry.
Then we can make a little toast so that the user also knows that things have been updated, profile,
updated successfully, and then we want to call activity that profile.
Update success, so basically dysfunction that we just prepared before profile, update success.
So what will happen if there is a failure, so add on failure.
Listener.
Well, what I'm going to do is I'm going to use that exception and.
It was activity that I progress dialogue.
Have a little log entry again, so error while creating a board and yeah, that's pretty much.
We can also, of course, add a little toast here saying.
Profile update's.
Error or error when updating the profile?
All right, and we can go back to our my profile activity and we will need two new variables here.
Actually, we have one already, which is a powerful image, but we need another one which will be private
late in it, variable and user details of type user.
And we will initialize it.
In said user data.
UI updates function, where is it, said user data in UI.
So that's what we're going to set up the user because that's where we get the user, right?
So we get the user from the other class, from our Firestar class, and now we can set it so user details
will be our user object at this pass to us.
So what we're going to change is the image, the mobile and the name, and it makes sense to have these
names, so to speak, as Constance.
Because they are constant names inside of our database and we want to change values behind those key,
so keys should always be used as constants, at least in this context.
Right.
So let's go ahead and go to our Constance again.
So here you tale's Constance, so far, we only have to use this table as a constant.
Actually, it's not the table, it's the whole document.
Right.
But what we want to have is we want to also have the image, the name and the mobile as Constance.
All right.
So these three variables here.
All right, and now we can actually update the user profile data, so we have this function called update
user profile data in our Firestar class.
Now, let's create a function with the same name and our my profile activity.
OK, so.
It's also going to be called update user profile.
Data and this time it doesn't need any specific data because the profile data is inside of this class
already.
So what we will do here is we'll create a user hash map.
All right, you've probably already guessed that we will need it one at one point, right?
So user hash map will be of type hash map.
With a string and any as the.
Key value pair.
OK, now we can add values to that, Hashmat.
So how do we add a value to a Hashmat and what are the values that we want to add to the Hashmat?
Well, it's the image URL.
It's the name of the user and it's the mobile number.
It's the same three values that we have here as constants, right?
Image, name and mobile.
OK, so let's do that, let's add the image, first of all, we can check if my profile image, your
URL is not empty.
Because if it's empty, we shouldn't use it and image profile your URL.
Is not the same as the image that we already use as the user details image.
So, um, user details about image.
So this is the one that is stored in the database, right, if it's exactly the same your URL, then
we don't need to make any changes.
But if it's not the same, then we want to make changes.
So then we will just say user Hashmat at the position.
So at the key.
Constance, that image.
He's going to be equal to him, profile image, Earl.
So he was just saying he was a Hashmat at the position.
That's the same as if we had used a hash map and here we pass an image, OK, because if you look at
this, Constance is just image.
So it's just a key, and if you want to get the value from something or from a Hashmat, you can just
use the key like this.
So this will then return whatever is behind image.
In our case, it will be this jury will hear this disvalue.
OK, so this upload Wikimedia work and so forth.
And if you want to assign something to it, you just used to equal some.
OK, so this is how we can update a Hashmat, how we can add values to the Hashmat.
Now, the next thing we want to see if the user has changed the username.
So if et on the screen name.
To the text to string is going to be not equal the same as Muser.
Details that name.
Because if it's the same, there's no use in updating, right?
So the idea is that we first check if the user has made any updates before you press the update button,
because maybe he didn't make any changes and he still presses the update button.
But if that's the case, we don't want to make another call to the database.
It's not necessary.
We don't want to make any changes in the database if there are no changes to be made.
And that's why we check first if there are any changes, because we already know the data from the database
and we can compare it to the data that the user has entered.
And if there's no change, then don't do anything.
OK, so here we're just changing the same thing for the name.
So we're just saying hash map constants name is going to be.
Equal to eat underscore name.
Text to string.
And then finally, we do the same thing with the mobile number, so Etty Mobile takes to strength,
is it the same as the mobile value?
Then we check that mobile.
And here, mobile to string, of course.
And then we say E.T. mobile text to string to.
Long.
Because they used the Hashmat expects a long value here.
Because, of course, our phone number here is not a string, it's a long value.
OK, and at that point, we have prepared everything and we can just say fire or.
Class.
Update user.
Profile and here.
It's this Firestar class, like this first class update user profile data with this activity and the
user, Hashmat, has the hash map that we need to pass.
Now, of course, as I said, if there are no changes, we would need to call this function so you could
create a new variable here.
So var any changes made is false.
And then if that is the case, you could say any changes made equals true.
And here, the same, any changes made equals true.
And then finally here as well.
Any changes made equals true and then check if any changes made equals true, no, not equals.
Well, actually, just like this, if it's true, then just run the opposite profile data.
OK, so this is how you could check it, if the user presses to button, either we want to update anything
or we don't.
But the thing is in our update, user profile.
Do we do anything about hiding the progress dialogue where we are hiding the progress dialogue here?
So in that case, what you would then of course, also have to do is to highlight the progress dialogue
accordingly.
All right, so I'm just going to get rid of this, if any changes made and so forth, you can use it
now, you know how to do it.
But I'm just going to keep it simple for now.
OK, so we have the same name, right, we use the same name for both functions, update user profile
data, one is inside of my profile activity and the other one is in our Firestar class.
But the fire still needs the activity in which it should perform it and it needs the user hash map as
well.
All right, and at that point, we can modify our on update button click listener here, button updates
on Click Listener, so now we can make changes to it because otherwise what we want to do is we want
to.
Showed a progress dialogue.
I'm not the image chooser, but the progress dialogue with please wait.
And then what we want to do is we want to update our profile data.
So update user profile data now, this user profile data we have to do still, we added a little to
do somewhere in our code.
And actually, it's easier to find it like this.
So here.
We have this to do, hi, progress dialogue, so now instead of doing this, we can't call this optic
user profile data function and that is inside of our upload user image.
So the thing is, when we upload the user image now, we will also do is we will call the optic user
profile data function, which will then.
Trigger making changes.
All right, now we can, of course, make this function private, and at this point we can test our
application because we never use up the user profile data somewhere else.
So it makes sense to.
Make it private.
All right, so let's test it.
Let's see if any changes are going to be made.
So let's go to my profile.
Let's select the Holmer image here.
OK, now it's uploaded, we can see Holmer well, let's change it to Homer Simpson.
And the mobile phone number will have seven, eight, nine as well.
OK, at that point, let's update just to see if anything's going to happen.
OK, so the dialogue opens profile update successfully and we're sent back to our main activity.
And now let's look at the database, let's see.
OK, so the link has been changed, the link now, is this your URL here?
Which is this almost instant image, perfect, and other than that, what else was changed?
Well, the mobile number has been changed at the name also has been changed.
OK, so now it works perfectly.
You can see how you can update data in the database, not just create data, but also update data.
Of course, also how to load data, because if you want to update something, you need to know what
you want to update.
So now you pretty much know everything you need to know to build the whole application, I'd say.
Of course, there are functionalities where we need third party libraries still and where we need to
look into it a little more.
But overall, I think we're at a very good spot now.
OK, so let's go over to the next video.