Yo-Yo Ma code homeys I'm not really that cool.
Hey guys this is Caleb with Dev slopes and in this video we're going to set up our group feed Visi to
present when selected from groups AVC.
We're going to initialize it with data and we're going to go ahead and get user names for the group
so that we can pass them straight in.
It will be really cool.
Let's go ahead and let's initialize that data.
So we're going to actually go ahead and go into groups Visi And just so you remember let me pull open
the storyboard here and show you what is what.
Let's take a look see.
All right.
So currently we have groups Visi and we're going to set it so that when a table cell is tapped it's
going to go ahead and present this view controller but we're going to load it up with the group's title
The group member names et cetera et cetera.
And so what we're actually needing to do first is we need to give this a storyboard IDs that we can
present it when we select a table view.
So let's go ahead and just copy group feed Visi its identity name and paste that in as its storyboard
ID.
And now we are good to go to present it.
So let's head back to groups AVC and we're going to go ahead and conform to another protocol method
of UI table if you delegate.
And it is did select row at index path.
So for whatever cell we select it's going to allow us to basically present the group that we want which
is really cool.
So let's go ahead and let's decide how we're going to do this.
I think first we should create a group Visi or group feed VC rather than we should initialize some data
for it and then we should present it to the view controller.
So before we actually initialize any data let's just see if we can present the view controller as it
is let's use guard let just because we may not return a value in this would keep our app from crashing
if we forgot to use the identifier or something like that.
So let's go ahead and type.
Guard let a group feed VC equals storyboard instantiate view controller with or with identifier.
And of course we just set it as group feed visi.
Now of course to get it to actually instantiate as the right view controller we need to optionally cast
it as a group feed Visi else.
If that doesn't work we're going to just return and get out of this function.
So that's cool.
Now we have a group feed Visi and all we need to do is just call present and pass in group VBC animated
is true and the completion block we don't care.
So let's go ahead let's just build and run this and let's see if it presents the view controller properly
like we are hoping that it does and then we're going to go ahead and set it up so that we can actually
pass in data will initialize some data and pass it in so that we can give it a group title and a group
description and all that good stuff will pass it over.
So here we go.
If I select a group it presents our group that's amazing.
Although Look we're having some trouble here.
It would appear that our top bar there is kind of out of place.
That's not good.
Let's go fix that.
So go in the main story board and let's see what the issue is.
It looks like we may have lost some constraints here.
Looks like we have leading space trailing space top space height constraint and bottom space that looks
good.
Perhaps we have missed something here we align the center which is here the button is here.
Maybe the button lost its top constraint.
Ah yes it would appear that it has.
So let's give it a top constraint and I believe for the buttons.
No you know what that looks good.
Let's give it a top constraint to lock it in place there and once it is there that should move everything
down.
But the main point guys is that this is presenting.
It looks great.
If I open send message let's make sure that it actually slides up over the table view there.
All right looks good.
Awesome.
And let's toggle it again let's just make sure that it follows.
Beautiful.
OK.
That's great.
And it's passing in the default users that I set up here.
But now let's go write a function that will allow us to initialize the proper group when we select the
table view to do that we're going to start in-groups AVC because this is where we download our groups
after all.
And we have our groups array which you can see here we can pull out a group at the index path and that's
actually the same way that we're going to pass a group to group feed visi.
Now before we can call it here in groups Visi we need to go to group feed Visi which you can do that
by clicking on group VBC or control command.
Clicking on any instance will take you straight to the definition of that which is kind of cool anyway
so we're going to create a little function up at the top here and we're going to call it phunk in knit
group data and we're going to make it basically take a group we're going to say for wups for group.
Maybe we'll just call it in data for group that's a better definition.
And it's going to be of type group.
OK.
So that looks awesome and all we're going to need to do is to create a variable in here that's going
to hold our group so just call it var group of type group and we're going to optionally we're going
to cast it optional here because at the initialization of the view controller we're not sure if we have
a value yet but we do set it in our initializers So just to be safe we're going to call self-talk group
and we're going to set it to be equal to the group we pass in from our data function.
Now the cool thing is now that we have our group if you remember a group has a title description key
member count and an array of members.
So when we initialize a group and pass it over we're going to have access to all of that good stuff.
So what we're going to do is we're going to call view will appear super view will appear.
Course it's animated and we're going to set our title label and our members label to include all of
the information that we want.
So what we're going to do is we're first going to go ahead and set group title label dot text.
This text is equal to group.
The one that we have in our view controller no group dot group title when we select it.
It'll pass it in and set the label.
Next we're going to go ahead and we're going to set members label text to be equal to group members.
Remember that's an array of strings and we can use dot joined and we can use a separator to basically
turn those into a really nice string with commas separating each element.
Very very cool.
So that'll set up all of our members properly.
That'll save us a lot of time and resources and we don't need to go to firebase to download all of the
information and convert IDs to usernames et cetera et cetera.
We can just take the one from the group that we've already downloaded and we can just join them with
a comma.
Super super cool.
Very good.
OK so now let's think what do we need to do.
We can pop up our view controller.
OK let's go in and just see how that works.
You can pop it up.
We have a back button which needs to actually dismiss the view controller.
Oh it appears that it does but it's hiding.
Ok good it works.
When we rebuild it remember we fixed the constraints so it should show.
Now I think all we need to do is to go initialize this from groups AVC when we pass it with.
Did select wrote index path.
So let's do that now.
Beneath our constant group feed Visi call group feed VC data for group.
And just like up here we're going to pull out the groups array.
Sorry the item from the group's array at the index path that we select.
So I'm going to copy this and actually just pass that in because that's the group we need.
Once it's initialized we're going to present the view controller.
So let's build and run this.
And let's see if we properly pass the data that we want and of course it's going to give me a simulator
error lets build and run it again after quitting.
Here we go.
Looks like it's loading up and showing up.
Whoops I made it jumbo sized down there.
OK.
Awesome.
Here we go building running.
And let's check it out.
So if I click nerd herd the title pops in as nerd herd.
Very cool.
I'll take a look.
We get a user ID.
I totally forgot that we only download the user id so in fact we do need a function to convert these
into the proper emails.
But I'm glad that the title is passing in properly and we can dismiss it with our back button.
So let's go into our data service and let's create a function that can convert IDs into emails.
Let's do it.
So let's go ahead and beneath get IDs for usernames.
Let's create a function called phunk get usernames or have a let's call it get e-mails for.
Well no it's call for group and we'll pass in the group because we have every group has a an array of
IDs.
Now of course we'll need a handler to return this back.
It's going to be escaping and we're going to use a function here and we're going to call back an empty
function.
But what do we need to pass back.
We need to pass back an array of strings right.
An array of user names.
So let's actually just actually call them e-mails instead.
And we're going to go ahead and return an array of strings that contains all of our e-mails.
Let's call this e-mail Ira.
That's a little more specific.
So just like before we create an empty array we create a snapshot we loop through it and add the proper
items to the array.
Then we pass it back with our handler it's going to be the same thing here.
So let's call this var e-mail array equals an array of string.
And we're going to initialize it like so with parentheses then we're going to go ahead and observe all
users.
OK.
Because we're going to pass in the IDs from the group and we're going to return their e-mails.
So go ahead and monitor refusers observe observe single event because when we do this we only really
care that it goes through once and returns all the proper emails.
So then it's going to be of type value because we're looking at all the values in our firebase snapshot
and this will just be called email snapshot.
No it's more of a user snapshot because it comes from our user child in firebase.
So we're going to use guard let create a snapshot instance like so guard let users snapshot and moving
quickly because we've already done this like five times so I'm going to just go ahead and move on you
know what I'm doing here.
Kate guard let users snapshot and if we don't have that we're just going to return.
Very cool.
But assuming we do we have a user snapshot we're going to go ahead.
Go ahead and a for loop through them by typing for a user and user snapshot.
And now what we're going to do is we're going to check our group.
OK.
We're going to check our group and we're going to go into the member's array of our group.
We're going to check to see if that group contains the same key in the users.
And if it does we're going to pull down the e-mail and then append it to our e-mail.
So if our group members that's an array of string if you remember properly contains If it contains the
user key because remember the members like we just saw in our simulator.
It just shows the key.
So if the members contains the key from the user snapshot then what we're going to do is we're going
to go ahead and we're going to append the e-mail of that user.
So go ahead and type e-mail or write append.
And you know what.
Let's actually create a constant to hold that user's e-mail by typing let e-mail equals a user child
snapshot.
Remember every user has an e-mail child.
We want to pull out the value and cast that as a string.
So now we have their e-mail.
We're going to append it and that's it.
It'll cycle through.
It'll append all the proper e-mails and at the very end we call our handler we give it email because
it's now full of the e-mails we need.
Now we're going to go ahead and head back to group feed Visi and let's check out where we set up our
members title K members label dot text.
It only showed the IDs right.
We need the user names.
So in order to actually get those We're going to call data service instance and we're going to do get
e-mails get e-mails for group will pass in the group and check it out guys we get an array back of returned
e-mails.
Now what we can do is we can actually use this same concept.
We'll just go to and get rid of that and we'll cut this and paste it.
Members label dot text and all we need to do is to give it returned e-mails and we're going to join
them all.
Oh what's the problem returned e-mails comes back as the right thing right.
Members label dot text.
Oh you know what it might need to be self-taught members label text they were passing in a group.
You know what the group needs to be force rapped because it's optional.
So force in rap the group now we can do returned e-mails and we're going to use joint.
We've already done this before.
Whoops.
We weren't joined with a separator of string and we're going to separate each element with a comma and
a space.
So now when this load just going to convert all of the e-mails and it's going to go ahead and convert
all the IDs and turn them into emails then we're going to set members label to include all of them and
they're joined and separated with a comma and a space.
Now of course it's telling me that I need to call self because that is a part of this.
Now it's giving me another error.
And oh it's just asking us to force on it which we already did let's build and run this let's see if
that fixes our problem with emails and let's go into the simulator.
Let's take a look.
Here we go.
Select a group and look at that.
There's all the e-mails.
That's awesome.
They all show up.
Cool cool.
So they're converted right away.
They show up right away.
And guys this is awesome.
We have our users showing up in the actual group that we added them to including ourselves remember
Batman at Gotham dot net.
So so cool.
Now that's done we have downloaded all the users we've passed in the data.
We know what group we're in now.
We're going to do is we're going to set it up so that we can send messages into our group.
Using this textfield here and having them show up in real time only in our group it's going to be really
really cool.
Let's head on over guys.
We're almost done.
We are so close to being finished.
Let's keep going.