1
00:00:07,620 --> 00:00:08,730
Everyone welcome back.

2
00:00:08,730 --> 00:00:15,930
Johnny B here with slopes dot com and the time is now we are finally going to actually do something

3
00:00:16,200 --> 00:00:17,190
chat related.

4
00:00:17,190 --> 00:00:21,840
We are going to pull down the chat channels in this case.

5
00:00:21,890 --> 00:00:22,440
All right.

6
00:00:22,440 --> 00:00:25,370
Or at least we're going to create the function that does that.

7
00:00:25,380 --> 00:00:27,020
I said let's go ahead and get started.

8
00:00:27,240 --> 00:00:32,910
Well we're going to do is we're going to create a new service called our message service and this is

9
00:00:32,910 --> 00:00:41,220
the service that's going to be responsible for storing our messages and channels and also the functions

10
00:00:41,220 --> 00:00:48,330
that will retrieve messages and channels as well as some other utilities that we'll be needing for it.

11
00:00:48,330 --> 00:00:48,790
All right.

12
00:00:48,960 --> 00:00:49,800
So let's get started.

13
00:00:49,800 --> 00:00:56,160
I'm going to come over here to services right click and say new file is going to be a swift file and

14
00:00:56,160 --> 00:01:01,050
say next we're on a call this message service.

15
00:01:01,290 --> 00:01:07,650
I'm going to say create that and then just like our other services this is going to be a class called

16
00:01:07,710 --> 00:01:14,630
Message Service like our other services this is also going to be a singleton.

17
00:01:14,630 --> 00:01:24,580
So when I say static that instance full equal and Message Service has taught you that.

18
00:01:25,070 --> 00:01:27,550
And so what do we do we're going to put in here.

19
00:01:27,570 --> 00:01:32,900
Well we're going to have a whole bunch of channels stored in here as well as the messages for whichever

20
00:01:32,900 --> 00:01:34,460
channel is selected.

21
00:01:34,680 --> 00:01:36,880
So first we're going to need a couple of models right.

22
00:01:37,040 --> 00:01:39,450
So let's go ahead and create those.

23
00:01:39,470 --> 00:01:43,380
I was going to say a new file on this is a swift file.

24
00:01:43,440 --> 00:01:49,220
And I say next and this is going to be a model called the channel.

25
00:01:49,740 --> 00:01:50,900
I'm going to create it.

26
00:01:52,290 --> 00:02:00,430
And let's go ahead and take a peek at postman and see what kind of stuff is returned from a channel.

27
00:02:00,430 --> 00:02:07,450
All right so this is the function or the API point find all channels is to be one slash channel and

28
00:02:07,450 --> 00:02:11,610
that just returns all of the all of the channels that we get.

29
00:02:11,610 --> 00:02:12,900
It's a get request.

30
00:02:13,150 --> 00:02:18,040
And so we can see here we have an array of objects of Giese objects.

31
00:02:18,160 --> 00:02:21,050
So we have one channel here and another channel here.

32
00:02:21,100 --> 00:02:24,830
The first one is called general and it doesn't have a description.

33
00:02:24,850 --> 00:02:32,260
The second channel is called description or inside the second the channel is called random and its description

34
00:02:32,260 --> 00:02:34,050
is This is the random channel.

35
00:02:34,390 --> 00:02:40,330
So anyways so these are the properties of a channel that were going to be working with were going to

36
00:02:40,330 --> 00:02:43,740
want to have the ID name and description.

37
00:02:43,960 --> 00:02:44,200
All right.

38
00:02:44,230 --> 00:02:46,210
So thats create a model on that.

39
00:02:46,360 --> 00:02:50,500
And this is going to be a struct Superstruct.

40
00:02:50,900 --> 00:02:53,500
Its going to be called Channel.

41
00:02:54,160 --> 00:03:06,230
And like I said the properties that we want can be public private set VAR channel title and thats going

42
00:03:06,230 --> 00:03:09,020
to be of type string.

43
00:03:09,110 --> 00:03:22,590
Are going to have public private clubs these are called Channel description and that's also going to

44
00:03:22,590 --> 00:03:25,730
be a string unless they are just going to copy this.

45
00:03:25,750 --> 00:03:30,180
So don't write that out copy and paste.

46
00:03:30,180 --> 00:03:33,390
And this is going to be ID.

47
00:03:33,430 --> 00:03:34,180
All right.

48
00:03:34,360 --> 00:03:37,750
So a very simple model for us.

49
00:03:37,810 --> 00:03:47,110
So that'll be nice and to say that and then we can jump back into our message service and say our channels

50
00:03:47,200 --> 00:03:56,800
is equal to an array of channel objects to instantiate that right there.

51
00:03:57,100 --> 00:04:01,650
And so now we have a place to store our channels.

52
00:04:01,690 --> 00:04:05,690
Now we need a function to actually retrieve those.

53
00:04:05,710 --> 00:04:10,500
All right and so this is the postman API call and we're.

54
00:04:10,510 --> 00:04:13,500
So this is what we get when we return it.

55
00:04:13,510 --> 00:04:14,140
What else do we need.

56
00:04:14,140 --> 00:04:21,940
We need an authorization header and we need our you or else so it's just the base you are L plus slash

57
00:04:21,940 --> 00:04:22,630
channel All right.

58
00:04:22,630 --> 00:04:27,540
So let's go ahead and create Are you or L for that I'm going to come over to utility's here.

59
00:04:27,550 --> 00:04:28,640
Go to Constans.

60
00:04:28,930 --> 00:04:41,890
Let's create our New You are going to say let you l score get more channels equal to the string interpolation

61
00:04:41,890 --> 00:04:49,800
of the base you are l and then just a channel and that'll do it.

62
00:04:49,840 --> 00:04:51,200
I just want to say that.

63
00:04:51,370 --> 00:04:58,000
Copy the euro constant and jump back into our message service.

64
00:04:58,000 --> 00:05:06,040
All right so now we are ready to create our function that is going to have our web request and go hit

65
00:05:06,040 --> 00:05:09,500
our API and bring back all of our channels for us.

66
00:05:09,490 --> 00:05:09,830
All right.

67
00:05:09,900 --> 00:05:18,850
It's going to say phunk find all channel can and this is just a get request so we don't have to actually

68
00:05:18,850 --> 00:05:22,260
pass anything in the body or anything so we don't need anything to Pessin.

69
00:05:22,480 --> 00:05:30,360
So all we need is our completion handler would say at this gate being completion handler.

70
00:05:30,750 --> 00:05:41,050
OK so now we can create our Alamo file request and say Alamo fire dot request and we need to actually

71
00:05:41,050 --> 00:05:47,720
import those of course from the same port Alamo fire and import Swiftie Jaison.

72
00:05:47,730 --> 00:05:48,360
There you go.

73
00:05:48,400 --> 00:05:49,630
So no we got it.

74
00:05:49,630 --> 00:05:51,380
Alamo fire request.

75
00:05:51,440 --> 00:05:52,380
And there we go.

76
00:05:52,660 --> 00:05:59,450
And the you are all that we want to hit is your no underscore get channels.

77
00:05:59,590 --> 00:06:07,810
The method is to get the parameters aren't nil the encoding is of type.

78
00:06:08,690 --> 00:06:11,000
Jaison do what.

79
00:06:11,050 --> 00:06:19,630
Is it just on encoding that default and the headers is we do need a barer headers.

80
00:06:19,770 --> 00:06:20,090
Right.

81
00:06:20,170 --> 00:06:27,010
Barer header and the response that we want we want it to be in the form of G songs so response Dusan

82
00:06:27,460 --> 00:06:35,470
and press return and then return again and then renaming the data response to response.

83
00:06:35,800 --> 00:06:40,210
And then just deleting that placeholder text.

84
00:06:40,210 --> 00:06:43,670
All right so we we pinged the API.

85
00:06:43,780 --> 00:06:48,500
We've sent it the information that we want and now we're going to receive it back.

86
00:06:48,520 --> 00:06:56,020
And so just like we've done in the past we're going to see if response that resolve that error is equal

87
00:06:56,020 --> 00:07:00,460
to you know then we're going to do something with it.

88
00:07:00,560 --> 00:07:09,790
Else we're going to say completion is false and we're going to debug the print we're going to debug

89
00:07:09,790 --> 00:07:10,030
print.

90
00:07:10,030 --> 00:07:21,210
The problem is we're going to say responsa that resolved that error as in the case that I'll just print

91
00:07:21,210 --> 00:07:25,480
out the response if we were successful then what are we going to do.

92
00:07:25,620 --> 00:07:30,360
Well we're going to do like we've done in the past we're going to parse out the Jason.

93
00:07:30,390 --> 00:07:33,540
All right so we're going to create some object out of the data.

94
00:07:33,540 --> 00:07:38,470
So first off let's get the data and should guard that data.

95
00:07:39,060 --> 00:07:44,210
Equal response that result that our untyped just response that data.

96
00:07:44,210 --> 00:07:46,120
There you go.

97
00:07:46,230 --> 00:07:48,830
Else return.

98
00:07:49,560 --> 00:07:52,890
So now we're going to turn that data into some sort of Jason.

99
00:07:53,220 --> 00:07:57,000
But coming back here to postpone it we see that this is actually an array.

100
00:07:57,240 --> 00:08:00,840
So we want to turn our Jason into an array object.

101
00:08:00,960 --> 00:08:11,340
So using Swiftie juice that's super easy we just say if let Jason is equal to Jason and then we're going

102
00:08:11,340 --> 00:08:15,870
to say data are going to pass in the data.

103
00:08:16,140 --> 00:08:18,560
And then we're going to say Dr. Ray.

104
00:08:18,780 --> 00:08:19,780
OK.

105
00:08:20,640 --> 00:08:23,250
And so that gives us an array of objects.

106
00:08:23,340 --> 00:08:30,840
And now we can say for item in chase on sort of we're going to loop through each item in the sun array

107
00:08:31,350 --> 00:08:37,360
and we're going to say no we're going to parse out the actual individual properties of the channel.

108
00:08:37,380 --> 00:08:44,550
Games are going to say let name equal and that's going to be item and drill into it.

109
00:08:44,550 --> 00:08:51,080
Look for the key of name and cast that as a string value.

110
00:08:51,210 --> 00:09:02,670
Then we're going to say left channel description equal item and we're going to search for the key of

111
00:09:03,390 --> 00:09:08,520
description and that's going to be cast to a string value.

112
00:09:08,600 --> 00:09:18,000
Then I say let the equal item and we're going to drill into it and search for the key of underscore

113
00:09:18,090 --> 00:09:21,910
ID string value.

114
00:09:22,140 --> 00:09:23,580
And if you're curious where I'm getting these.

115
00:09:23,580 --> 00:09:25,070
Again just from right here.

116
00:09:25,080 --> 00:09:26,670
So this is the Giese on response.

117
00:09:26,690 --> 00:09:31,630
And so these are the keys that we need to be looking for.

118
00:09:31,630 --> 00:09:31,970
All right.

119
00:09:31,980 --> 00:09:42,180
And then with those with these three variables we can now instantiate and create a new channel object.

120
00:09:42,180 --> 00:09:54,190
OK so sorry can say let channel equal and then using the default struct initializer we can just say

121
00:09:54,460 --> 00:10:02,980
channel title is equal to name channel description is equal to channel description and the ID is equal

122
00:10:02,980 --> 00:10:04,450
to the ID.

123
00:10:04,690 --> 00:10:10,750
OK so each time we loop through one of the items in the juice array we're going to extract the properties

124
00:10:10,750 --> 00:10:16,570
that we need and then we're going to initialize a new channel object from the struct initializer and

125
00:10:16,570 --> 00:10:23,110
then we're going to add this new channel to our channels array that we that we store inside of our message

126
00:10:23,110 --> 00:10:31,710
service to say self-taught channels that append and the new element is this new channel here.

127
00:10:31,750 --> 00:10:37,450
So that's how we look through those Jason objects and add them to our channels array right here and

128
00:10:37,450 --> 00:10:37,800
that's it.

129
00:10:37,810 --> 00:10:42,170
We can say completion is true right here.

130
00:10:42,580 --> 00:10:44,220
Okay.

131
00:10:45,400 --> 00:10:50,230
So that's that's how we've been doing some parsing up till now.

132
00:10:50,230 --> 00:10:51,190
Right.

133
00:10:51,960 --> 00:11:01,130
And I like doing it this way but in swift for there's a new way and it's really nice in some ways.

134
00:11:01,600 --> 00:11:07,000
So I definitely want to show you that we're probably going to continue doing it this way you can make

135
00:11:07,000 --> 00:11:10,260
the choice yourself going forward which way you want to do it.

136
00:11:10,570 --> 00:11:18,590
But the new way is they have new decoded and protocols and it's super nice.

137
00:11:18,760 --> 00:11:20,790
So I'm going to show you how to do that.

138
00:11:20,790 --> 00:11:24,110
So what we're going to do is I'm going to go ahead and just comment out here

139
00:11:28,840 --> 00:11:37,170
and to make it work well we have to do is we have to make our channel conform to the decoded bull protocol.

140
00:11:37,420 --> 00:11:38,150
OK.

141
00:11:38,470 --> 00:11:45,700
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

142
00:11:45,700 --> 00:11:52,980
have to make our vegans find a way to make this visible while we still see.

143
00:11:53,140 --> 00:12:00,140
We have to make our model mirror exactly what we see in the JSL response.

144
00:12:00,320 --> 00:12:03,410
OK so I just can put this over here on my other window.

145
00:12:03,490 --> 00:12:05,820
So these names have to be the same so.

146
00:12:05,860 --> 00:12:06,820
And in the same order.

147
00:12:06,820 --> 00:12:09,750
So this needs to be underscore ID.

148
00:12:10,030 --> 00:12:12,260
This needs to be name.

149
00:12:12,650 --> 00:12:16,490
This needs to be the scription

150
00:12:20,320 --> 00:12:21,290
right description.

151
00:12:21,590 --> 00:12:25,900
And then the thing the other thing that it really like is that you have to have everything.

152
00:12:26,030 --> 00:12:31,970
So before were just ignoring this underscore underscore V I'm not really sure what it is some sort of

153
00:12:32,570 --> 00:12:38,270
Mangu database identifier or something but because it's in the Jason we also have to have it here in

154
00:12:38,270 --> 00:12:39,310
our model.

155
00:12:39,470 --> 00:12:45,520
So I'm going to go ahead and add that the C control copy and paste.

156
00:12:46,040 --> 00:12:52,990
And so then what you can do is this should be underscore underscore.

157
00:12:53,050 --> 00:12:54,590
Be all right.

158
00:12:54,590 --> 00:13:01,140
So then what we can do is we can come back into message service and we can actually do the parsing and

159
00:13:01,160 --> 00:13:02,510
practically one line.

160
00:13:02,660 --> 00:13:04,340
And so that's what's really nice about it.

161
00:13:04,670 --> 00:13:07,740
So we're going to we're going to do is say I do.

162
00:13:08,840 --> 00:13:14,480
And the reason we have to have a do and catch block is because we're going to be using the sun decoder

163
00:13:14,480 --> 00:13:15,970
which can throw an error.

164
00:13:16,010 --> 00:13:25,410
So we're going to say self thought channels is equal to try.

165
00:13:25,630 --> 00:13:37,370
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

166
00:13:37,910 --> 00:13:39,110
is.

167
00:13:39,830 --> 00:13:43,980
So we're going to say the type is channel.

168
00:13:44,030 --> 00:13:48,010
So we made it an array because we know that it's an array so channel that self.

169
00:13:48,200 --> 00:13:57,170
And then you just pass the data right here and that is all of the juice on parsing that you need it

170
00:13:57,200 --> 00:14:01,590
kind of just takes your channel model.

171
00:14:01,730 --> 00:14:08,930
And it just crams everything that comes from the juice on it decodes it and stuffs it right into whatever

172
00:14:08,930 --> 00:14:11,510
variables you have in the message service.

173
00:14:11,510 --> 00:14:17,930
Or in your in your channel model as long as it matches correctly and then like I said because this is

174
00:14:17,930 --> 00:14:21,620
a try it has the potential to throw an error so we need to handle that.

175
00:14:21,730 --> 00:14:28,380
So when we say here catch that error and then we're just going to debug.

176
00:14:28,400 --> 00:14:31,610
Print that error OK.

177
00:14:31,910 --> 00:14:34,220
Has any.

178
00:14:34,700 --> 00:14:35,350
And.

179
00:14:35,360 --> 00:14:35,720
Yes.

180
00:14:35,750 --> 00:14:38,130
That is that's it.

181
00:14:38,150 --> 00:14:39,330
That's all you got to do.

182
00:14:39,620 --> 00:14:42,040
So let's go ahead and test this.

183
00:14:42,260 --> 00:14:50,900
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

184
00:14:50,900 --> 00:14:53,130
it and let's run this Frum's.

185
00:14:53,300 --> 00:14:55,720
Let's just put it in the chat.

186
00:14:55,810 --> 00:14:58,230
VC view did load.

187
00:14:58,440 --> 00:15:06,400
OK so when I say Message Service instance you know actually we're going to need to.

188
00:15:08,120 --> 00:15:09,810
I think we have a logged in user.

189
00:15:09,920 --> 00:15:12,240
So I'm going to be super careful here.

190
00:15:12,470 --> 00:15:21,650
Message Service instance dot find all channel and say success and we're not really doing anything with

191
00:15:21,650 --> 00:15:21,800
it.

192
00:15:21,800 --> 00:15:28,100
So I'm just going to run it oh we got an error here says expect to decode a string but found a number

193
00:15:28,160 --> 00:15:29,230
instead.

194
00:15:29,230 --> 00:15:30,110
And that's my bad.

195
00:15:30,110 --> 00:15:39,130
Here in the channel model if we look at post-bin this property here is actually in a number so let's

196
00:15:39,230 --> 00:15:41,490
instead cast that to an int.

197
00:15:41,690 --> 00:15:46,710
And if you make it optional with this decoded will if it's not there then it just puts Nyland there.

198
00:15:46,850 --> 00:15:51,110
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

199
00:15:51,110 --> 00:15:52,210
a.

200
00:15:52,910 --> 00:15:54,450
Optional.

201
00:15:54,470 --> 00:15:54,920
All right.

202
00:15:54,920 --> 00:15:56,220
And here we go.

203
00:15:56,330 --> 00:15:58,460
So we can see here.

204
00:15:58,930 --> 00:16:04,010
Yeah we got we can see some of the properties that we would expect in the channel name general description.

205
00:16:04,010 --> 00:16:06,260
Welcome to the water cooler.

206
00:16:07,000 --> 00:16:15,110
Yes so it is it is printing out the channel information so yeah that parsing is not parsing is working.

207
00:16:15,110 --> 00:16:18,660
And so it's kind of just up to you how you want to do it.

208
00:16:19,190 --> 00:16:26,510
So like I said the drawbacks for me are that you have to have everything in the model and you have to

209
00:16:26,510 --> 00:16:32,410
have the naming convention the same as what the Jaison on returns.

210
00:16:32,560 --> 00:16:40,670
But yeah I mean this is super super nice being able to just do that one line of that one line of code

211
00:16:40,670 --> 00:16:50,000
instead of having to parse through lots of different properties and stuff but still in the end I actually

212
00:16:50,000 --> 00:16:57,020
prefer the old way just because I can ignore some things in the in the model.

213
00:16:57,080 --> 00:17:04,220
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

214
00:17:04,220 --> 00:17:09,530
naming convention and also being able to ignore properties in the sun that we just don't really care

215
00:17:09,530 --> 00:17:10,120
about.

216
00:17:10,220 --> 00:17:16,580
So I want to save that and then come back to our message service and here I am going to go ahead and

217
00:17:16,580 --> 00:17:19,380
command Z all the way back.

218
00:17:19,390 --> 00:17:25,880
Well yeah like I said I just wanted to show you that because you know this is a course on swift for

219
00:17:26,270 --> 00:17:27,360
it is new.

220
00:17:27,590 --> 00:17:31,490
So you guys definitely deserve to learn about it.

221
00:17:31,640 --> 00:17:38,840
Personally I still prefer doing it this way and I feel like it just gives it gives me more control over

222
00:17:39,140 --> 00:17:40,500
what my models look like.

223
00:17:40,520 --> 00:17:47,090
And also you know not being forced to use everything that they use on spits back at us.

224
00:17:47,480 --> 00:17:49,550
But yeah so up to you.

225
00:17:49,730 --> 00:17:55,100
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

226
00:17:55,100 --> 00:18:04,070
to print out the let's say self that channels and then I'll pick the whole axis.

227
00:18:04,070 --> 00:18:06,840
The zeroth item and say that

228
00:18:09,440 --> 00:18:15,990
channel channel title do that right and we're loaded and sure enough there we go.

229
00:18:16,150 --> 00:18:20,340
General is the name of the first channel all right.

230
00:18:20,560 --> 00:18:22,040
So how are we doing on time.

231
00:18:22,040 --> 00:18:25,220
Looks like we're out of time here but there you go.

232
00:18:25,220 --> 00:18:33,920
We have created the function to hit our API and bring back to us all of the channels that are that are

233
00:18:33,920 --> 00:18:35,280
currently on the API.

234
00:18:35,480 --> 00:18:39,880
All right so in the next lessons we're actually going to do something with this data.

235
00:18:40,010 --> 00:18:47,900
We're going to work it in our table view inside of the channel AVC and we're going to start make it

236
00:18:47,900 --> 00:18:50,570
so that we can actually visualize the channel data there.

237
00:18:50,570 --> 00:18:51,170
All right.

238
00:18:51,470 --> 00:18:54,160
So that's about it for now.

239
00:18:54,170 --> 00:18:59,180
I'm going to go ahead and finish off by adding the progress that we have made and committing it.

240
00:18:59,360 --> 00:19:00,730
And I will see you in the next one.

