Everyone welcome back.
Johnny B here with slopes dot com and the time is now we are finally going to actually do something
chat related.
We are going to pull down the chat channels in this case.
All right.
Or at least we're going to create the function that does that.
I said let's go ahead and get started.
Well we're going to do is we're going to create a new service called our message service and this is
the service that's going to be responsible for storing our messages and channels and also the functions
that will retrieve messages and channels as well as some other utilities that we'll be needing for it.
All right.
So let's get started.
I'm going to come over here to services right click and say new file is going to be a swift file and
say next we're on a call this message service.
I'm going to say create that and then just like our other services this is going to be a class called
Message Service like our other services this is also going to be a singleton.
So when I say static that instance full equal and Message Service has taught you that.
And so what do we do we're going to put in here.
Well we're going to have a whole bunch of channels stored in here as well as the messages for whichever
channel is selected.
So first we're going to need a couple of models right.
So let's go ahead and create those.
I was going to say a new file on this is a swift file.
And I say next and this is going to be a model called the channel.
I'm going to create it.
And let's go ahead and take a peek at postman and see what kind of stuff is returned from a channel.
All right so this is the function or the API point find all channels is to be one slash channel and
that just returns all of the all of the channels that we get.
It's a get request.
And so we can see here we have an array of objects of Giese objects.
So we have one channel here and another channel here.
The first one is called general and it doesn't have a description.
The second channel is called description or inside the second the channel is called random and its description
is This is the random channel.
So anyways so these are the properties of a channel that were going to be working with were going to
want to have the ID name and description.
All right.
So thats create a model on that.
And this is going to be a struct Superstruct.
Its going to be called Channel.
And like I said the properties that we want can be public private set VAR channel title and thats going
to be of type string.
Are going to have public private clubs these are called Channel description and that's also going to
be a string unless they are just going to copy this.
So don't write that out copy and paste.
And this is going to be ID.
All right.
So a very simple model for us.
So that'll be nice and to say that and then we can jump back into our message service and say our channels
is equal to an array of channel objects to instantiate that right there.
And so now we have a place to store our channels.
Now we need a function to actually retrieve those.
All right and so this is the postman API call and we're.
So this is what we get when we return it.
What else do we need.
We need an authorization header and we need our you or else so it's just the base you are L plus slash
channel All right.
So let's go ahead and create Are you or L for that I'm going to come over to utility's here.
Go to Constans.
Let's create our New You are going to say let you l score get more channels equal to the string interpolation
of the base you are l and then just a channel and that'll do it.
I just want to say that.
Copy the euro constant and jump back into our message service.
All right so now we are ready to create our function that is going to have our web request and go hit
our API and bring back all of our channels for us.
All right.
It's going to say phunk find all channel can and this is just a get request so we don't have to actually
pass anything in the body or anything so we don't need anything to Pessin.
So all we need is our completion handler would say at this gate being completion handler.
OK so now we can create our Alamo file request and say Alamo fire dot request and we need to actually
import those of course from the same port Alamo fire and import Swiftie Jaison.
There you go.
So no we got it.
Alamo fire request.
And there we go.
And the you are all that we want to hit is your no underscore get channels.
The method is to get the parameters aren't nil the encoding is of type.
Jaison do what.
Is it just on encoding that default and the headers is we do need a barer headers.
Right.
Barer header and the response that we want we want it to be in the form of G songs so response Dusan
and press return and then return again and then renaming the data response to response.
And then just deleting that placeholder text.
All right so we we pinged the API.
We've sent it the information that we want and now we're going to receive it back.
And so just like we've done in the past we're going to see if response that resolve that error is equal
to you know then we're going to do something with it.
Else we're going to say completion is false and we're going to debug the print we're going to debug
print.
The problem is we're going to say responsa that resolved that error as in the case that I'll just print
out the response if we were successful then what are we going to do.
Well we're going to do like we've done in the past we're going to parse out the Jason.
All right so we're going to create some object out of the data.
So first off let's get the data and should guard that data.
Equal response that result that our untyped just response that data.
There you go.
Else return.
So now we're going to turn that data into some sort of Jason.
But coming back here to postpone it we see that this is actually an array.
So we want to turn our Jason into an array object.
So using Swiftie juice that's super easy we just say if let Jason is equal to Jason and then we're going
to say data are going to pass in the data.
And then we're going to say Dr. Ray.
OK.
And so that gives us an array of objects.
And now we can say for item in chase on sort of we're going to loop through each item in the sun array
and we're going to say no we're going to parse out the actual individual properties of the channel.
Games are going to say let name equal and that's going to be item and drill into it.
Look for the key of name and cast that as a string value.
Then we're going to say left channel description equal item and we're going to search for the key of
description and that's going to be cast to a string value.
Then I say let the equal item and we're going to drill into it and search for the key of underscore
ID string value.
And if you're curious where I'm getting these.
Again just from right here.
So this is the Giese on response.
And so these are the keys that we need to be looking for.
All right.
And then with those with these three variables we can now instantiate and create a new channel object.
OK so sorry can say let channel equal and then using the default struct initializer we can just say
channel title is equal to name channel description is equal to channel description and the ID is equal
to the ID.
OK so each time we loop through one of the items in the juice array we're going to extract the properties
that we need and then we're going to initialize a new channel object from the struct initializer and
then we're going to add this new channel to our channels array that we that we store inside of our message
service to say self-taught channels that append and the new element is this new channel here.
So that's how we look through those Jason objects and add them to our channels array right here and
that's it.
We can say completion is true right here.
Okay.
So that's that's how we've been doing some parsing up till now.
Right.
And I like doing it this way but in swift for there's a new way and it's really nice in some ways.
So I definitely want to show you that we're probably going to continue doing it this way you can make
the choice yourself going forward which way you want to do it.
But the new way is they have new decoded and protocols and it's super nice.
So I'm going to show you how to do that.
So what we're going to do is I'm going to go ahead and just comment out here
and to make it work well we have to do is we have to make our channel conform to the decoded bull protocol.
OK.
And then the other thing that we have to do and this is the reason that I kind of don't like it is we
have to make our vegans find a way to make this visible while we still see.
We have to make our model mirror exactly what we see in the JSL response.
OK so I just can put this over here on my other window.
So these names have to be the same so.
And in the same order.
So this needs to be underscore ID.
This needs to be name.
This needs to be the scription
right description.
And then the thing the other thing that it really like is that you have to have everything.
So before were just ignoring this underscore underscore V I'm not really sure what it is some sort of
Mangu database identifier or something but because it's in the Jason we also have to have it here in
our model.
So I'm going to go ahead and add that the C control copy and paste.
And so then what you can do is this should be underscore underscore.
Be all right.
So then what we can do is we can come back into message service and we can actually do the parsing and
practically one line.
And so that's what's really nice about it.
So we're going to we're going to do is say I do.
And the reason we have to have a do and catch block is because we're going to be using the sun decoder
which can throw an error.
So we're going to say self thought channels is equal to try.
And so when you have a do you need to try chase on decoder dot de-code and so one we want and the type
is.
So we're going to say the type is channel.
So we made it an array because we know that it's an array so channel that self.
And then you just pass the data right here and that is all of the juice on parsing that you need it
kind of just takes your channel model.
And it just crams everything that comes from the juice on it decodes it and stuffs it right into whatever
variables you have in the message service.
Or in your in your channel model as long as it matches correctly and then like I said because this is
a try it has the potential to throw an error so we need to handle that.
So when we say here catch that error and then we're just going to debug.
Print that error OK.
Has any.
And.
Yes.
That is that's it.
That's all you got to do.
So let's go ahead and test this.
So here are under it I'm going to say print and then I'm going to say self channels I'm going to save
it and let's run this Frum's.
Let's just put it in the chat.
VC view did load.
OK so when I say Message Service instance you know actually we're going to need to.
I think we have a logged in user.
So I'm going to be super careful here.
Message Service instance dot find all channel and say success and we're not really doing anything with
it.
So I'm just going to run it oh we got an error here says expect to decode a string but found a number
instead.
And that's my bad.
Here in the channel model if we look at post-bin this property here is actually in a number so let's
instead cast that to an int.
And if you make it optional with this decoded will if it's not there then it just puts Nyland there.
So I don't know if that property is always going to be this or I'm going to make that an end or I mean
a.
Optional.
All right.
And here we go.
So we can see here.
Yeah we got we can see some of the properties that we would expect in the channel name general description.
Welcome to the water cooler.
Yes so it is it is printing out the channel information so yeah that parsing is not parsing is working.
And so it's kind of just up to you how you want to do it.
So like I said the drawbacks for me are that you have to have everything in the model and you have to
have the naming convention the same as what the Jaison on returns.
But yeah I mean this is super super nice being able to just do that one line of that one line of code
instead of having to parse through lots of different properties and stuff but still in the end I actually
prefer the old way just because I can ignore some things in the in the model.
So I'm just going to command Z here in the channel all the way back to how we had it with our with our
naming convention and also being able to ignore properties in the sun that we just don't really care
about.
So I want to save that and then come back to our message service and here I am going to go ahead and
command Z all the way back.
Well yeah like I said I just wanted to show you that because you know this is a course on swift for
it is new.
So you guys definitely deserve to learn about it.
Personally I still prefer doing it this way and I feel like it just gives it gives me more control over
what my models look like.
And also you know not being forced to use everything that they use on spits back at us.
But yeah so up to you.
But once again here let's go ahead and let's make sure it's working with this way as well so I'm going
to print out the let's say self that channels and then I'll pick the whole axis.
The zeroth item and say that
channel channel title do that right and we're loaded and sure enough there we go.
General is the name of the first channel all right.
So how are we doing on time.
Looks like we're out of time here but there you go.
We have created the function to hit our API and bring back to us all of the channels that are that are
currently on the API.
All right so in the next lessons we're actually going to do something with this data.
We're going to work it in our table view inside of the channel AVC and we're going to start make it
so that we can actually visualize the channel data there.
All right.
So that's about it for now.
I'm going to go ahead and finish off by adding the progress that we have made and committing it.
And I will see you in the next one.