Hey everyone welcome back Johnny be here with slopes dot com.
In the last lesson we worked on getting our log and flow set up and being able to select channels and
updating our chat B.C.E. 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 a 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 better header so we have we need to be logged in with an authorization token to access it and the
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 our 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 oh Id 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.
So we need the the user id and use 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 you'll 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 but anyways.
Yes so these are the properties of our model that we need to account for.
So 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 a public private set
ups not capitalize our message.
That's a strong hand.
And then 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 one two three four five six seven eight.
One two three four five six seven I think we need one more.
All right and those are user name
channel ID user Avatar
user have a tar color
I.D. and time stamp you see that I mean one.
One two three four five six then I guess it's only seven.
All right so 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 fire request web request that emulates the postman
that we just saw and worked on that to do that we are going to need a u r l constant end point.
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 or I'll get messages
on 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 right.
That's when I say find her 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 API
requests 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.
That's and completion handler cause the bottom pane
give us some room here.
All right let's do this.
Baranski Elmo fire request and this is the request we want the U R L is the one that we just created
you are a hell underscore getting messages.
The method.
Remember this one is a gift.
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 we go and we want the response to come back in the form of a song.
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 thanks.
So now we're going to see if very spawn start result.
That error is equal to nil.
Never going to do something else.
We are going to say completion is false and then we are going to want to put the debug above that so
we're going to debug.
Print the errors when I say response stop.
Result.
Error as any then we're going to set completion to false.
But if we are successful then we are going to look through the object the joke's on the objects in our
response and parse out the properties of a message that we need.
All of these were meant to create a new message and now we're going to append it to an array of messages
that were 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 at a 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.
So right here we can actually clear the messages here.
We're going to say sulf that clear messages area 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 into adjacent object using Swiftie Jaison to miss it.
Guard hold it.
Data equal or response date time.
Else return.
And then we're going to say if let chase on equal to Jaison and we use the 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 Jasen objects so now we're going to loop through ruinously for item in chase
on.
So just like we saw in post man.
So this would be the J song 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 have a tire.
This 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.
This key is under score 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 all 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 put message equal message with the struct initializers and the message is message body.
The username is a user name channel ID is channel I d the user Avatar is used for Avatar use the Avatar
color is user have a tar color.
The ID is ID and the time stamp is time stamp duty.
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 just append
and the new element is that message that we just created.
All right.
And then we can see completion is true.
All right go ahead.
Delete these spaces and we're going to save that.
And I think that that pretty much covers it that looks pretty good.
So to test this let's go ahead and actually print the print this data and say self that going to say
print self-taught messages.
OK.
And I say this and then we're going to jump over to our chat VC 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 dobbed
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 that instance does 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.
Yes.
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 funk 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.
We're going to say guard let channel ID equal message service instance does selected channel dot channel
ID
or Slik child ID that we go.
Else 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 functions from and say Message Service that 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 zeroth 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 so 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.
The Tauriel should also have appended at the end of it the channel ID.
So we're going to do some string interpolation and say your l 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 to see here we have your 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 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 the view based on that based on which channel is selected
and that is where we created our new function.
We get messages which goes and fetches the messages for a selected channel right.
So things are looking good.
Probably guys are having fun and in the next lesson 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.