Hey everyone welcome back.
Johnny B here with slopes dot com.
In the last lesson we work on getting our log and flow set up and being able to select channels and
updating our chat B.C info a little bit and so we're going to continue on in that direction.
And in this lesson we're actually going to write our functions to be able to fetch the messages stored
on our API.
And I know right now that you guys are going to have any messages on your servers yet because we haven't
gotten to that point yet but that's OK.
I'll show you what the returning G song and everything will look like.
So the first thing that we're going to need to do is know what our model needs to look like.
So we open up postman and if you find the find all messages for Channel web request we can take a look
at it it is a get by request so we're just retrieving information and the header is a bear.
So we we need to be logged in with an authorization token to access it and the new URL is a message
slash by channel and then the channel ID.
All right.
So if you check out your Mangu database here it's going to open up right here on the our m labs.
So I'm in the I'm here in the collections are the channels collection and this shows our channels that
we have.
So you can see here we have the name of the channel just general the description and the oad right here
that's the channel ID that we're going to be using in our app to has a unique identifier.
All right.
So and then that unique identifier is what we pass into the API request to retrieve the messages.
And if I press send then this is what is returned.
It's an array of J song objects in each chase object object or presents a chat message.
And so the things that we need to account for in our model is the ID message body user ID channel ID
user name user Avatar and Avatar color and the time stamp.
Right.
So we need the the user id and user name and all that information to be able to you know make a clear
indication of who it is that is sending the sending the message and youll see here this time stamp will
get to this later.
The server returns it in a format that will have to convert later on.
But anyways Yes so these are the properties of our model that we need to account for.
So let's go ahead and create that model.
We're going to come up here to model and say Right click on model group and say new file and this is
going to be a swift file and we're going to call it message.
I'm going to create it.
All right and just like our channel this is going to be a struct structure and we call it message and
we're just going to type out all of our properties of our structure that we need as a public private
set ups not capitalize our message.
And that's a strong hand.
And I'm going to just copy this one to see
and see how many are there.
I'm an open post man and we need 1 2 3 4 5.
One 8 1 2 3 4 6 7.
We need one more.
All right and those are user name
channel ID user Avatar
user have tar Culloden
ID and time stamp.
See that is one.
One two three four five six seven I guess it was only seven.
Looks good.
Now we have our model that will be able to use to save objects.
All right so let's go ahead and now create the Alamo of request web request that emulates the postman
that we just saw and work on that to do that we are going to need a u r l constant end point.
So go ahead and open up our utilities and go to Constans and here under.
You are always just going to copy this guy here paste it and this is going to be you are ill get messages
find the base you are l and then it is message slash by channel.
I say that then let's go into our message service start swift and we were going to create a new function
to get all messages.
All right.
That's why I say find a funk find all messages for Channel.
OK.
And we're going to pass into that a channel ID because that's what we need to be able to make the request
and say channel Heidi.
And that's going to be a string and then we're going to have her completion handler at completion at
scaping
and completion handler cause the bottom pane.
Give us some room here.
All right.
Let's do this.
Rinse the Alamo fire request and that says the request we want you are l is the one that we just created
you are hell underscored get messages the method.
Remember this one is a get.
The parameters are.
No we don't have a body to pass.
The encoding is just on encoding that default and the headers we do need this to be a better header.
There you go.
And we want the response to come back in the form of a.
Then offer the closure I'm going to press to return and change its name to response and encode and get
rid of that.
All right.
Nice.
So now we're going to see if very response start result.
That error is equal to.
You know we're going to do something else.
We are going to take completion is false.
And then we are going to want to put the de-bug above that so we're going to debug.
Print the errors when we say response that result.
Dot error as any then we're going to see completion to false.
But if we are successful then we are going to look through the object the jakes the objects in our response
and parse out the properties of a message that we need.
All of these then we're going to create a new message and then we're going to append it to an array
of messages that we're going to create right here.
So messages equal to an array of message and initialize.
OK so we're only going to be storing the messages for one channel out of time.
So once we select one channel we're going to load up the messages for that channel and then after.
And then if we select a different channel we're going to erase the messages the current messages and
then replace that with the new channels messages to actually going to need a clear messages function
right here today.
We're going to see phunk clear messages that's going to be messages that remove all that we go.
And so right here we can actually clear the messages here.
We're going to say sulf that clear messages.
There you go.
And then we'll then world.
Now we're going to go through the parsing parts when I say so remember the first step is to get the
data and then turn that data into adjacent object using Swiftie Jaison.
So when they say God hold it to equal the response that they tell else return and then we're going to
say if let chase on equal to Jaison and we use the Swifty juice on initialiser right here and the data
is the data that we just created and that is going to be of type array.
OK.
Now we have an array of objects so now we're going to loop through we're going to see for item in chase
on.
So just like we saw in post man so this would be the case on object that we just created.
Now we're going to go through.
We're going to see for each of these in it go through and extract the properties for each one.
All right.
So let's go ahead and do that.
We're going to say let message body equal item and the key is called message body and that's going to
be of type string.
About you.
All right.
And since there is quite a few of these I'm going to just kind of zip through.
I'm going to copy and paste to the seven so one two three four five six seven and this is going to be
channel.
Heidi this one's going to be ID.
This is going to be user name.
This one's going to be user but Tiger is going to be user have a tar color.
And this is going to be time stamp.
Right now we got to go through and switch up all of the keys.
So this key is channel ID and this key is underscore ID the username key is user name the user Avatar
key is user Avatar the user Avatar color key is user of our color and the time stamp key is time stamp
right there we go.
So now we have all of the properties in variables that we need so we can create a new message I'm going
to stay message equal message with the struct initialiser and the message is message body.
The username is a user name channel ID is channel I the user Avatar is used for Avatar use the Avatar
color is user have a tar color.
The ID is ID and the timestamp is time stamp Whoy.
All right.
So we are parsing through each individual item in J song.
We are grabbing out the information and creating a new message object with that information and then
we can append that new one that we just created two messages array and say self that message is that
append and the new element is that message that we just created.
All right.
And then we can see completion is true.
All right.
I had to leave these spaces and Im going to save that and I think that that pretty much covers it that
looks pretty good.
So to test this lets go ahead and actually print the print this data out and say self that you are going
to say print self-taught messages.
OK.
And I say this and then we're going to jump over to our chat in D.C. actually because once we log in
the first thing we do is we go and we find all the channels right.
So if we go and we get all of the channels there are either going to be no channels if is that like
the very first time you have logged into the app and no one has created channels before.
So there could be no channels or there could be some channels if there are no channels then we don't
want to try and go and get messages because there's there's no channel there's no messages.
All right so what we're going to do is we're going to do a check for that.
We're going to see if message service dot
instance that channels dot count is greater than zero.
So we've logged in we've gotten all of our channels and if there are more than zero if there is at least
one we're going to do is we're going to set the message service instance dot selected channel equal
to the first one the message service in that instance that channels the zeroth item.
So this is like if you have been logged in before and then you reenter the app and this is just going
to by default set the first channel as the selected one.
All right so we're going to say that and then we're going to say self-taught update with channel if
there are no channels.
We're going to go ahead and change our self channel name label that text to say no channels.
OK.
So we log in we find all of the channels if there's at least one channel then we set our selected channel
equal to it and then we're going to update the channel then we're going to call this update with channel.
So here an update with channel.
What do we want to do.
Well we're going to create a new function called get messages.
And so this is where we call the function that we just created.
So when I say phunk get messages
it here we're going to say we're going to call the function we need a channel ID.
Right.
So we're going to first unwrap that channel IDs for an inside guard that channel ID equal message service
instance does selected channel dot channel ID
or Slik challenge that ID that we go.
Else we we're going to return.
OK so now we have the channel ID the selected channel ID to create some space for us here.
And now we can call the get the get messages and the message service dot instance dot find all messages
for Channel and the channel that we want to retrieve messages for our channel ID that we just unwrapped
completion.
I'm going to change to success.
And right now we're not doing anything with that information.
But let's put our get messages function right here and update with channel so get messages.
And that should that should work for us.
So I'm going to run this.
And while it's coming up I'm going to walk you through the flow once again.
All right so we logon we do our find all channel function which returns an array of functions.
If there's at least one function then we are going to set the selected channel equal to the zero channel
then we're going to update with channel.
What that does is we set our channel name and we try to and we attempt to get messages when we attempt
to get messages we are getting the channel ID from our selected channel and then we're passing that
in to find a message for a channel where we look through if there are any if there are any messages
then we loop through and extract the information that we need and then create new message objects and
add them to our messages rate and message service.
And so we said to print it out so let's see if we got anything here.
You know what.
I accidentally put the completion in the wrong place.
So I have it inside of the for loop so I'm going to cut these two lines and these should actually be
outside of the area.
All right.
Let's try that again.
OK.
So that wasn't the only mistake I made.
I made a rather large one right here.
You're all that we need to ping is not just is not just this right.
Your will should also have appended at the end of it.
The channel ID so going to do some string interpolation and say Your will get messages and then we also
need to tack onto it the channel ID string that we have passed into it.
So I want to say channel Heidi.
There we go.
Save that and run.
All right.
So third time's the charm.
Finally we are seeing the message data that is being printed out.
So that is that is good because see here we have the Avatar color user names Emma the message hey Johnny
what's going on.
So yeah it's working out well.
So just to recap what we did in this lesson we created a model for the messages.
We created a function in our message service that's swift to go and retrieve all of the messages for
a given channel.
Then in our chat Visi we did a little bit of work and created a few more functions so that when we log
in we do a check for any check for more than one channel.
We select a channel and then we update the view based on that based on which channel is selected and
that is where we created our new function from getting messages which goes and fetches the messages
for a selected channel.
So things are looking good.
Probably you guys are having fun and in the next Listen we're going to work on adding messages so that
you guys can actually see something be returned with that function.
And yes so we're we're making great progress.
And I'm going to finish up here by adding and committing our changes and then I'll see you guys in the
next one.