Hey guys this is Caleb with Dev slopes and in this video we're going to write a function that's going
to convert our unique ID into a username or an email for our user.
We're also going to configure the cells so that they appropriately show the username the way they're
supposed to.
And we're also going to change our messages array so that the feeder shows in reverse.
So the most recent post cell will show up at the very top and it's super easy to do that just to show
you what I mean in the feed.
The last post that I made is at the bottom we're going to set it up so that it actually goes to the
top.
And it's really easy.
So let's actually start by doing that first because that's where we left off last time.
It's literally like six letters or less or more like seven letters.
Anyway what we need to do is the array that is returned from get all feed messages.
We're going to actually call a function on this array.
I actually had no idea that this was a thing and I was planning on having to write some big fancy function
to reverse the order of the array.
But all you all you need to do is literally type dot reversed.
That's it.
This is going to take the array and it's going to flip everything.
And that way we can show the most recent post at the very top.
I'm not kidding you it is that easy.
It returns a reverse duplicate of this array.
Let's build and run and I will show you what I mean.
Check it out guys it's really really cool.
It's going to give me an error because of my simulator.
But all you need to do is quit the sim and rerun it.
It will reopen the simulator and as soon as it's ready we will be able to see how easy it is to reverse
all of our messages.
By the way while this is loading I'm going to open the data service because we're going to be adding
a function in there in just a moment but now it's ready.
So it downloads all the messages and you see that they're in reverse order.
Now watch what happens when I post.
This is the newest post.
And you know it just for fun let's add an emoji.
Nice thing.
OK thing and send and it shows up at the very top that is how easy it is to reverse all of our posts.
So awesome.
That's easy.
Now we're going to write a function to convert our you ID into a username so to do that we're going
to go into the data service and we're going to write a function called phunk get username for you ID.
OK and I'm using an internal parameter there.
And the thing is as we're going to be using some firebase closures and so to get values out we need
to use a completion handler.
So go ahead and type handler at escaping like we have been.
You should be an expert at this by now.
And in the function we're going to go ahead and pass a value called username of type string.
All right.
We're going to use that at the very end once we have our user name.
Now we need to do is we need to observe the users reference and we need to cycle through all the users
and find one that matches the ID we pass in.
So to do that go ahead and call ref users observe single event.
The reason we're doing that is because we only need to look through at one time.
We don't need it to persistently observe.
We're going to go ahead and observe value meaning all values in the user's reference.
And we're going to get a snapshot back.
And this is going to be called user snapshot.
That's just what I'm naming it.
Now what we're going to do is we're going to create a constant that holds this snapshot then we're going
to write a for loop to cycle through all of them.
So go ahead and type guard let users snapshot equals the user snapshot that we got returned snapshot
dot children meaning all the children inside of the user snapshot.
And we're going to return all of the objects we need to force cast this just like before as a an array
of WIPs data snapshot.
Now the only thing is if we get nothing back we should do something.
And that is to call.
Else what else.
And then inside of here we can just return and get out of this function.
So that's what we're going to do if there is nothing return we'll just get out so that we don't have
a crash now that we have a user snapshot assuming we get some users downloaded.
We're going to go ahead and call for user and user snapshot and we're going to basically check to see
if a certain user dot key because remember if we look at firebase Let's pull it up every user.
Oh.
Oh that's not good.
So it looks like we've authenticated some users but the user was deleted but it's still remaining inside
of firebase.
OK let's finish this function up and then we'll go fix it so that we can actually sign out and then
sign in as a new user because right now we have no users in our Snapshot.
If user Dom key is equal to the ID we pass in K..
We can think about it like the feed has users and the users have a unique ID.
If the the key here matches the ID we pass in then we can call handler dot or wups handler and we're
going to pass a string.
But in order to get that we can go into the user account and we can call child snapshot for path.
If you remember when we create a user Let's go to log in Visi we pass in the where is it we pass in
an e-mail.
You know what it might be and register user Let's see.
Yes.
We pass in a child called e-mail and so let's go ahead and let's go for the child called e-mail.
Now of course we want the value not the key.
And we want to cast it as a string because we need to pass back a string to our handler.
Now when we pass any you I.D. it will cycle through here.
It will find the user that matches that you I.D. and then we will get the e-mail back for them.
So in order to actually utilize this we need to go to the feed visi.
And what we're going to do is we're actually going to use the Sender ID but we're going to pass it into
our get username function first.
So let's go ahead and call data service instance get username for you I.D..
We have that right here.
Right.
Message that Sender ID so let's pass that in to our function and the handlers where we get our user
name back.
So let's call returned username and we can call configure cell in here now.
So we're going to configure the cell with the proper username as returned username.
OK.
And that'll give us our e-mail value to show up in our cell.
Super duper cool but we're going to run into an issue.
Let's build and run the app and we'll see that we'll get nothing back for the username.
We may even have a crash because we currently have no users in our account.
We're just going to get our default message back.
We have the right number but we can't configure the cell because we're not actually getting a value
back and that is a problem.
So we need to actually go ahead and set up MVH see to sign us out properly.
So let's go ahead and let's do that now by going to the story board and we're going to open up the assistant
editor as soon as it's done loading.
There we go.
And let's go ahead and let's pull open MVH see which is right here and we're going to update our action
to actually contain the proper sign out code and what we're going to do is we're going to set up what's
called a UI alert controller.
But we're going to modify it for logout.
It's very cool.
So let's go ahead and let's create an instance of you Isler controller let's call it let maybe log out
pop up and it's going to be equal to UI alerts controller.
Now of course this is going to have a title and a message.
The title is just going to be log out of the question mark.
The message will be.
Are you sure you want to log out and the preferred style.
This is where we're going to get the log out style pop up as we're going to call action sheet.
OK.
That's going to pop up from the bottom similar to like when you share stuff.
If you wanted to share something and it pops up that list of options of where you can share it that's
an action sheet.
Next we're going to have to create a log out action for when we tap on the button.
What's it going to do.
So let's do that now by typing.
Let pop or let's do let log out log out action equal UI alert action and it kind of has a similar similar
style here.
We're going to give it a title of log out a style of destructive and that means if you look it says
well it doesn't tell you here but it basically means that it's used for things that are destructive
like we're destroying the user's account where we're removing it completely from our app.
So what's going to happen is when the button is tapped we're going to try to sign out and we can use
some firebase stuff for that.
I'm actually going to pull open MVH see in full screen here so you can see it really good.
There we go.
And so inside of this completion handler after the button is tapped we're going to go ahead and we're
going to call off ah with whoops we have not imported firebase import firebase.
There we go.
And we're going to call off Auth..
Sign out.
And do you see right there it says sign out and it says that it throws.
That means it throws errors.
And because of that we need to use do try.
OK.
That's a safe way for us to call things that could have critical errors.
So to do this we're going to do.
And then at the end we're going to call catch and that's where errors are handled.
So cut this and move it into the do block and now we need to call try because that tells us that we're
going to try to sign out and assuming that works we should instantiate our log in Visi once we log out
it should pop up the log and visi again so that we can see it.
So go ahead and type let log in VC equal storyboard instantiate view controller with identifier and
we're going to call our identifier which is off the seat.
OK.
Now in order for this to actually show up as an author Visi we need to cast it as Visi just like that.
And now we can actually present it on top of whatever view controller we're on which happens to be MVH
seat.
So go ahead and type self up present and we're going to present log and visi just like that animated
of course and we don't care about what happens at the end because it doesn't really matter.
We've signed out and that's great.
OK.
Since the storyboard here is optional we need to force on RRAP log in VC but it's cool.
Oh you know what I just realized it's off.
VC not log in B C we should use the right name.
Sorry about that.
So change this to Auth. VC and there we go.
Now we are successfully presenting a view controller but if we can't then we're just going to print
the error and you're probably wondering where did the error come from.
It comes from Sign out.
But since we're using a do catch block it automatically captures an a variable called error and sign
out passes any values to this.
If there's a problem we'll know next what we need to do now that our action is done is we need to add
the action to our log out pop up.
So we need to go ahead and blow this.
Now that our log out action is finished call log out pop up add action and give it the log out action.
OK that's simple.
Now when the pop up goes up it knows what it can do and we push the log out button.
And finally when we push the sign out button we need to present this whole alert.
So now we can call present and we're going to call log out pop up.
That's the alert controller.
Of course we want it to be animated.
But we do not care whether it completes or not because we handle that in here with our.
Do catch block.
Let's go ahead and let's try this let's try to remove our account and then we're going to go ahead and
move on to where we were before OK here we go and we get billed failed.
Interesting Let's see what the problem is.
Oh implicit use of self enclosure.
We need to use self to access the storyboard that makes sense.
Can you go ahead and build and run and let's pull open the simulator here so we can see how that works
when we log out.
If successful the log out or the authentication VC should pop up automatically which would be awesome.
So let's go give it a shot.
Let's try to log out.
There's our pop up and there's our message let's go ahead and tap log out and our log in Visi shows
up.
That's amazing.
OK perfect.
Let's log back and I'm going actually create a new account here.
How about Harvey at dent dot com.
Hey password of course is Batman.
Let's sign in.
All right there we go.
Oh and you know what.
Look at that.
We get stuck on our off the sea we need to go fix that.
And I know exactly how to do it.
AVC when the view appears we should set it to if we are logged in we should set the view to dismiss.
So let's go ahead and call.
You did appear cause that's called every time the view appears not just when it loads for the first
time.
Call superdog viewed it appear and pass in a boolean of animated so that it's nice and animated and
now we need to do is we need to check using firebase first import firebase and we're going to basically
check if off off.
Current User is not nil wups is not nil meaning if there's a user go ahead and dismiss the view controller
because think about it if you're logged out and this pops up there's no user once you're logged in it
should automatically dismiss.
OK.
So let's go try that again.
We're going to build and run pop this thing up and we'll go see if it worked.
OK.
So we're going to log out log out.
It pops up.
Let's log into my account here Harvey Dent dot com.
That man is my password.
And let's sign in and let's see if it disappears Oh you know I guess I got my password wrong.
Bat Man.
There we go.
Sign in.
And it dismisses the way we would hope.
That's beautiful.
OK.
Awesome.
We're still having the issue of our fields here not showing up and let's go figure out why that is.
OK.
Let's see.
We have a user he has an e-mail and the provider is firebase.
So let's go look at Pheed VC and let's figure out what the problem is.
Now get user name passes in the sender ID and we get a user name return.
But if you think about it all of these messages were not sent by a user in our database.
So we actually should get rid of all these messages because they're never going to show up from a user.
Now what we should do and you see they disappear which is cool because it's real time.
Let's go ahead and let's add a user or out a message from Harvey Dent.
Hey Batman.
You stink.
I heard Robin laid an egg.
Lol.
OK.
Let's send it.
And it shows up as Harvey at dent dot com.
That's exactly what it's supposed to do.
It's supposed to get all the usernames and it's supposed to post them at the top of the cell.
So so cool.
That's working.
I did notice something while we were doing this and this is just part of building apps you notice things
here in there when we pull open post post a new or create post-PC user at breakpoint is still showing
up.
Not the current users not the current users name.
Let's fix that.
All we need to do is to go into create post-PC and we need to create an IP outlet for the users e-mail
label and we've already done it.
It's already set up it's ready to go but we have just not yet set it up to pass in the e-mail for the
user.
So what we're going to do to do that is basically to call view will appear because that's going to get
called before the view loads and it'll update the view changes so that it doesn't show the wrong email
first and then quickly flashed a change to show the right one.
It'll just show the right one from the get go.
So-called super You will appear animated.
And what we're going to do since we're already logged in we can just pull out the information from firebase
and just get the email straight from the current user.
So call self e-mail label dot text and set it to off off off.
Current User and it has an email property we can just pull out the user's e-mail address straight from
firebase.
So go ahead and BuildOn run.
And let's go see if this change is reflected appropriately.
And the reason I'm doing this is because you can't even get to this screen unless you're logged in.
So go ahead and open it and look at that Harvey at dent dot com.
There's one other place we should do this.
It's here in visi.
So I'm actually going to just copy this and go to TVC and I'm going to paste it because we said the
same name for the e-mail label.
Let's build and run again and let's make sure that it works OK.
OK let's check it out.
All right.
Very good.
But what happens if we log out let's try logging out and log into a new account let me log in to my
old account.
Well I log into a new one here maybe Batman at Gotham dot net and the password Joker sucks.
Seinen.
And here's the problem it's still holding the old user's name.
Now a way that we can fix this is if we put it in view will appear because every time the view appears
it's going to change the current user.
So let's call it you will appear super dog.
You will appear animated and we will update this as soon as the view appears that way.
We don't have that same issue.
So go ahead and build and run it let it happen and as soon as it goes up we will check to see if it
worked.
Here we go.
OK.
So Batman at Gotham dot net.
Let's actually make a post.
Hey there it is up at the top.
Very nice.
Now when we log out and log back in let's log in is Harvey Dent dotcom what was my password.
Batman I think.
Let's sign in and it should update the user and show the new user name as soon as the view appears very
very awesome.
So let's go ahead let's move on to the next video we did just a couple of little tweaks here just for
fun.
In the middle of it.
But now our feed cells are showing usernames our message Gerais is reversed so the most current post
is on top.
And guys this is looking really good so far in the next video we're going to create one of my favorite
controllers in this whole app it's called create group Visi and it's going to allow us to search through
all the users that have created accounts in our apps and add them specifically it's going to be really
really cool.
So let's move on to the next video and let's start adding some groups into our app.