1
00:00:07,740 --> 00:00:09,060
Hey everyone welcome back.

2
00:00:09,100 --> 00:00:11,470
Johnny B here with slopes dot com.

3
00:00:11,470 --> 00:00:17,980
In the last lesson we work on getting our log and flow set up and being able to select channels and

4
00:00:17,980 --> 00:00:23,580
updating our chat B.C info a little bit and so we're going to continue on in that direction.

5
00:00:23,590 --> 00:00:29,470
And in this lesson we're actually going to write our functions to be able to fetch the messages stored

6
00:00:29,470 --> 00:00:30,840
on our API.

7
00:00:31,150 --> 00:00:37,480
And I know right now that you guys are going to have any messages on your servers yet because we haven't

8
00:00:37,480 --> 00:00:39,690
gotten to that point yet but that's OK.

9
00:00:39,760 --> 00:00:44,090
I'll show you what the returning G song and everything will look like.

10
00:00:44,350 --> 00:00:49,700
So the first thing that we're going to need to do is know what our model needs to look like.

11
00:00:49,780 --> 00:00:59,320
So we open up postman and if you find the find all messages for Channel web request we can take a look

12
00:00:59,320 --> 00:01:06,120
at it it is a get by request so we're just retrieving information and the header is a bear.

13
00:01:06,250 --> 00:01:15,010
So we we need to be logged in with an authorization token to access it and the new URL is a message

14
00:01:15,400 --> 00:01:19,560
slash by channel and then the channel ID.

15
00:01:19,560 --> 00:01:19,990
All right.

16
00:01:20,140 --> 00:01:29,180
So if you check out your Mangu database here it's going to open up right here on the our m labs.

17
00:01:29,320 --> 00:01:37,360
So I'm in the I'm here in the collections are the channels collection and this shows our channels that

18
00:01:37,360 --> 00:01:38,010
we have.

19
00:01:38,350 --> 00:01:46,110
So you can see here we have the name of the channel just general the description and the oad right here

20
00:01:46,450 --> 00:01:52,570
that's the channel ID that we're going to be using in our app to has a unique identifier.

21
00:01:52,570 --> 00:01:53,280
All right.

22
00:01:53,720 --> 00:02:02,350
So and then that unique identifier is what we pass into the API request to retrieve the messages.

23
00:02:02,380 --> 00:02:06,410
And if I press send then this is what is returned.

24
00:02:06,490 --> 00:02:14,950
It's an array of J song objects in each chase object object or presents a chat message.

25
00:02:15,090 --> 00:02:22,930
And so the things that we need to account for in our model is the ID message body user ID channel ID

26
00:02:23,170 --> 00:02:28,100
user name user Avatar and Avatar color and the time stamp.

27
00:02:28,130 --> 00:02:28,570
Right.

28
00:02:28,690 --> 00:02:35,350
So we need the the user id and user name and all that information to be able to you know make a clear

29
00:02:35,350 --> 00:02:42,890
indication of who it is that is sending the sending the message and youll see here this time stamp will

30
00:02:42,910 --> 00:02:44,350
get to this later.

31
00:02:44,500 --> 00:02:49,130
The server returns it in a format that will have to convert later on.

32
00:02:49,200 --> 00:02:54,520
But anyways Yes so these are the properties of our model that we need to account for.

33
00:02:54,520 --> 00:02:56,130
So let's go ahead and create that model.

34
00:02:56,140 --> 00:03:03,490
We're going to come up here to model and say Right click on model group and say new file and this is

35
00:03:03,490 --> 00:03:08,280
going to be a swift file and we're going to call it message.

36
00:03:08,770 --> 00:03:10,560
I'm going to create it.

37
00:03:10,840 --> 00:03:18,570
All right and just like our channel this is going to be a struct structure and we call it message and

38
00:03:18,570 --> 00:03:26,470
we're just going to type out all of our properties of our structure that we need as a public private

39
00:03:27,580 --> 00:03:34,290
set ups not capitalize our message.

40
00:03:34,480 --> 00:03:37,120
And that's a strong hand.

41
00:03:37,170 --> 00:03:41,730
And I'm going to just copy this one to see

42
00:03:44,580 --> 00:03:46,190
and see how many are there.

43
00:03:46,200 --> 00:03:53,350
I'm an open post man and we need 1 2 3 4 5.

44
00:03:53,490 --> 00:03:57,140
One 8 1 2 3 4 6 7.

45
00:03:57,150 --> 00:03:58,940
We need one more.

46
00:03:58,950 --> 00:04:02,130
All right and those are user name

47
00:04:04,890 --> 00:04:09,660
channel ID user Avatar

48
00:04:12,030 --> 00:04:14,580
user have tar Culloden

49
00:04:17,320 --> 00:04:23,120
ID and time stamp.

50
00:04:23,140 --> 00:04:23,900
See that is one.

51
00:04:23,900 --> 00:04:29,570
One two three four five six seven I guess it was only seven.

52
00:04:29,770 --> 00:04:30,220
Looks good.

53
00:04:30,220 --> 00:04:36,930
Now we have our model that will be able to use to save objects.

54
00:04:36,940 --> 00:04:43,420
All right so let's go ahead and now create the Alamo of request web request that emulates the postman

55
00:04:43,420 --> 00:04:49,720
that we just saw and work on that to do that we are going to need a u r l constant end point.

56
00:04:49,730 --> 00:04:55,380
So go ahead and open up our utilities and go to Constans and here under.

57
00:04:55,420 --> 00:05:04,700
You are always just going to copy this guy here paste it and this is going to be you are ill get messages

58
00:05:07,000 --> 00:05:15,780
find the base you are l and then it is message slash by channel.

59
00:05:15,770 --> 00:05:25,610
I say that then let's go into our message service start swift and we were going to create a new function

60
00:05:25,610 --> 00:05:27,560
to get all messages.

61
00:05:27,560 --> 00:05:27,970
All right.

62
00:05:27,980 --> 00:05:38,260
That's why I say find a funk find all messages for Channel.

63
00:05:38,540 --> 00:05:38,850
OK.

64
00:05:38,880 --> 00:05:46,570
And we're going to pass into that a channel ID because that's what we need to be able to make the request

65
00:05:46,790 --> 00:05:48,760
and say channel Heidi.

66
00:05:48,770 --> 00:05:56,900
And that's going to be a string and then we're going to have her completion handler at completion at

67
00:05:57,150 --> 00:05:58,040
scaping

68
00:06:00,460 --> 00:06:09,320
and completion handler cause the bottom pane.

69
00:06:09,640 --> 00:06:13,180
Give us some room here.

70
00:06:13,180 --> 00:06:13,510
All right.

71
00:06:13,570 --> 00:06:14,600
Let's do this.

72
00:06:14,610 --> 00:06:22,240
Rinse the Alamo fire request and that says the request we want you are l is the one that we just created

73
00:06:22,240 --> 00:06:25,670
you are hell underscored get messages the method.

74
00:06:25,670 --> 00:06:27,720
Remember this one is a get.

75
00:06:28,060 --> 00:06:29,790
The parameters are.

76
00:06:29,830 --> 00:06:32,430
No we don't have a body to pass.

77
00:06:32,500 --> 00:06:43,570
The encoding is just on encoding that default and the headers we do need this to be a better header.

78
00:06:43,570 --> 00:06:44,350
There you go.

79
00:06:44,460 --> 00:06:48,230
And we want the response to come back in the form of a.

80
00:06:48,490 --> 00:06:56,260
Then offer the closure I'm going to press to return and change its name to response and encode and get

81
00:06:56,260 --> 00:06:57,660
rid of that.

82
00:06:57,700 --> 00:06:58,180
All right.

83
00:06:58,190 --> 00:06:58,670
Nice.

84
00:06:58,690 --> 00:07:02,240
So now we're going to see if very response start result.

85
00:07:05,180 --> 00:07:08,550
That error is equal to.

86
00:07:08,670 --> 00:07:12,070
You know we're going to do something else.

87
00:07:12,450 --> 00:07:17,310
We are going to take completion is false.

88
00:07:17,310 --> 00:07:23,050
And then we are going to want to put the de-bug above that so we're going to debug.

89
00:07:23,100 --> 00:07:28,350
Print the errors when we say response that result.

90
00:07:30,050 --> 00:07:36,480
Dot error as any then we're going to see completion to false.

91
00:07:36,790 --> 00:07:43,870
But if we are successful then we are going to look through the object the jakes the objects in our response

92
00:07:44,260 --> 00:07:48,880
and parse out the properties of a message that we need.

93
00:07:48,880 --> 00:07:55,030
All of these then we're going to create a new message and then we're going to append it to an array

94
00:07:55,030 --> 00:07:57,120
of messages that we're going to create right here.

95
00:07:57,160 --> 00:08:05,780
So messages equal to an array of message and initialize.

96
00:08:06,100 --> 00:08:10,980
OK so we're only going to be storing the messages for one channel out of time.

97
00:08:11,050 --> 00:08:17,720
So once we select one channel we're going to load up the messages for that channel and then after.

98
00:08:17,920 --> 00:08:23,110
And then if we select a different channel we're going to erase the messages the current messages and

99
00:08:23,110 --> 00:08:28,930
then replace that with the new channels messages to actually going to need a clear messages function

100
00:08:28,930 --> 00:08:29,490
right here today.

101
00:08:29,500 --> 00:08:40,980
We're going to see phunk clear messages that's going to be messages that remove all that we go.

102
00:08:40,990 --> 00:08:44,500
And so right here we can actually clear the messages here.

103
00:08:44,500 --> 00:08:49,770
We're going to say sulf that clear messages.

104
00:08:49,770 --> 00:08:51,060
There you go.

105
00:08:51,160 --> 00:08:52,690
And then we'll then world.

106
00:08:52,720 --> 00:08:58,150
Now we're going to go through the parsing parts when I say so remember the first step is to get the

107
00:08:58,150 --> 00:09:01,640
data and then turn that data into adjacent object using Swiftie Jaison.

108
00:09:01,670 --> 00:09:11,830
So when they say God hold it to equal the response that they tell else return and then we're going to

109
00:09:11,980 --> 00:09:22,480
say if let chase on equal to Jaison and we use the Swifty juice on initialiser right here and the data

110
00:09:22,600 --> 00:09:28,440
is the data that we just created and that is going to be of type array.

111
00:09:28,630 --> 00:09:29,060
OK.

112
00:09:29,230 --> 00:09:36,400
Now we have an array of objects so now we're going to loop through we're going to see for item in chase

113
00:09:36,400 --> 00:09:37,280
on.

114
00:09:37,450 --> 00:09:46,990
So just like we saw in post man so this would be the case on object that we just created.

115
00:09:46,990 --> 00:09:47,730
Now we're going to go through.

116
00:09:47,730 --> 00:09:55,480
We're going to see for each of these in it go through and extract the properties for each one.

117
00:09:55,480 --> 00:09:55,950
All right.

118
00:09:56,230 --> 00:09:57,490
So let's go ahead and do that.

119
00:09:57,490 --> 00:10:09,310
We're going to say let message body equal item and the key is called message body and that's going to

120
00:10:09,310 --> 00:10:11,350
be of type string.

121
00:10:11,470 --> 00:10:12,490
About you.

122
00:10:12,970 --> 00:10:13,240
All right.

123
00:10:13,240 --> 00:10:17,780
And since there is quite a few of these I'm going to just kind of zip through.

124
00:10:17,800 --> 00:10:26,740
I'm going to copy and paste to the seven so one two three four five six seven and this is going to be

125
00:10:27,190 --> 00:10:27,930
channel.

126
00:10:27,940 --> 00:10:31,710
Heidi this one's going to be ID.

127
00:10:31,850 --> 00:10:34,420
This is going to be user name.

128
00:10:34,420 --> 00:10:42,650
This one's going to be user but Tiger is going to be user have a tar color.

129
00:10:43,030 --> 00:10:46,730
And this is going to be time stamp.

130
00:10:46,750 --> 00:10:49,880
Right now we got to go through and switch up all of the keys.

131
00:10:50,320 --> 00:11:04,360
So this key is channel ID and this key is underscore ID the username key is user name the user Avatar

132
00:11:04,360 --> 00:11:16,620
key is user Avatar the user Avatar color key is user of our color and the time stamp key is time stamp

133
00:11:17,850 --> 00:11:18,910
right there we go.

134
00:11:19,010 --> 00:11:25,310
So now we have all of the properties in variables that we need so we can create a new message I'm going

135
00:11:25,310 --> 00:11:38,510
to stay message equal message with the struct initialiser and the message is message body.

136
00:11:38,510 --> 00:11:51,500
The username is a user name channel ID is channel I the user Avatar is used for Avatar use the Avatar

137
00:11:51,500 --> 00:11:55,360
color is user have a tar color.

138
00:11:55,490 --> 00:12:01,880
The ID is ID and the timestamp is time stamp Whoy.

139
00:12:01,930 --> 00:12:02,510
All right.

140
00:12:02,540 --> 00:12:06,640
So we are parsing through each individual item in J song.

141
00:12:06,650 --> 00:12:12,140
We are grabbing out the information and creating a new message object with that information and then

142
00:12:12,170 --> 00:12:18,920
we can append that new one that we just created two messages array and say self that message is that

143
00:12:18,960 --> 00:12:23,600
append and the new element is that message that we just created.

144
00:12:24,030 --> 00:12:24,320
All right.

145
00:12:24,350 --> 00:12:28,930
And then we can see completion is true.

146
00:12:31,460 --> 00:12:32,130
All right.

147
00:12:32,680 --> 00:12:43,040
I had to leave these spaces and Im going to save that and I think that that pretty much covers it that

148
00:12:43,060 --> 00:12:44,890
looks pretty good.

149
00:12:44,950 --> 00:12:50,560
So to test this lets go ahead and actually print the print this data out and say self that you are going

150
00:12:50,560 --> 00:12:55,180
to say print self-taught messages.

151
00:12:55,740 --> 00:12:56,110
OK.

152
00:12:56,140 --> 00:13:04,930
And I say this and then we're going to jump over to our chat in D.C. actually because once we log in

153
00:13:05,890 --> 00:13:10,290
the first thing we do is we go and we find all the channels right.

154
00:13:10,360 --> 00:13:16,300
So if we go and we get all of the channels there are either going to be no channels if is that like

155
00:13:16,300 --> 00:13:22,630
the very first time you have logged into the app and no one has created channels before.

156
00:13:22,690 --> 00:13:27,580
So there could be no channels or there could be some channels if there are no channels then we don't

157
00:13:27,580 --> 00:13:32,920
want to try and go and get messages because there's there's no channel there's no messages.

158
00:13:32,920 --> 00:13:35,070
All right so what we're going to do is we're going to do a check for that.

159
00:13:35,070 --> 00:13:39,210
We're going to see if message service dot

160
00:13:42,250 --> 00:13:51,660
instance that channels dot count is greater than zero.

161
00:13:51,970 --> 00:13:57,400
So we've logged in we've gotten all of our channels and if there are more than zero if there is at least

162
00:13:57,400 --> 00:14:05,680
one we're going to do is we're going to set the message service instance dot selected channel equal

163
00:14:05,680 --> 00:14:14,260
to the first one the message service in that instance that channels the zeroth item.

164
00:14:14,260 --> 00:14:20,110
So this is like if you have been logged in before and then you reenter the app and this is just going

165
00:14:20,110 --> 00:14:24,700
to by default set the first channel as the selected one.

166
00:14:24,700 --> 00:14:32,200
All right so we're going to say that and then we're going to say self-taught update with channel if

167
00:14:32,200 --> 00:14:34,270
there are no channels.

168
00:14:34,600 --> 00:14:45,960
We're going to go ahead and change our self channel name label that text to say no channels.

169
00:14:47,520 --> 00:14:48,120
OK.

170
00:14:48,640 --> 00:14:55,210
So we log in we find all of the channels if there's at least one channel then we set our selected channel

171
00:14:55,270 --> 00:15:00,400
equal to it and then we're going to update the channel then we're going to call this update with channel.

172
00:15:00,730 --> 00:15:03,480
So here an update with channel.

173
00:15:03,490 --> 00:15:04,230
What do we want to do.

174
00:15:04,270 --> 00:15:07,920
Well we're going to create a new function called get messages.

175
00:15:07,930 --> 00:15:10,990
And so this is where we call the function that we just created.

176
00:15:10,990 --> 00:15:14,100
So when I say phunk get messages

177
00:15:16,720 --> 00:15:22,980
it here we're going to say we're going to call the function we need a channel ID.

178
00:15:22,990 --> 00:15:23,500
Right.

179
00:15:23,770 --> 00:15:33,100
So we're going to first unwrap that channel IDs for an inside guard that channel ID equal message service

180
00:15:33,880 --> 00:15:39,940
instance does selected channel dot channel ID

181
00:15:42,630 --> 00:15:45,310
or Slik challenge that ID that we go.

182
00:15:45,620 --> 00:15:47,580
Else we we're going to return.

183
00:15:47,750 --> 00:15:54,110
OK so now we have the channel ID the selected channel ID to create some space for us here.

184
00:15:54,920 --> 00:16:03,080
And now we can call the get the get messages and the message service dot instance dot find all messages

185
00:16:03,080 --> 00:16:09,800
for Channel and the channel that we want to retrieve messages for our channel ID that we just unwrapped

186
00:16:10,130 --> 00:16:11,420
completion.

187
00:16:11,420 --> 00:16:13,630
I'm going to change to success.

188
00:16:13,640 --> 00:16:18,570
And right now we're not doing anything with that information.

189
00:16:18,830 --> 00:16:25,860
But let's put our get messages function right here and update with channel so get messages.

190
00:16:26,360 --> 00:16:29,660
And that should that should work for us.

191
00:16:29,720 --> 00:16:31,070
So I'm going to run this.

192
00:16:31,730 --> 00:16:36,140
And while it's coming up I'm going to walk you through the flow once again.

193
00:16:36,140 --> 00:16:44,670
All right so we logon we do our find all channel function which returns an array of functions.

194
00:16:44,750 --> 00:16:51,080
If there's at least one function then we are going to set the selected channel equal to the zero channel

195
00:16:51,560 --> 00:16:54,280
then we're going to update with channel.

196
00:16:54,440 --> 00:17:01,880
What that does is we set our channel name and we try to and we attempt to get messages when we attempt

197
00:17:01,880 --> 00:17:07,940
to get messages we are getting the channel ID from our selected channel and then we're passing that

198
00:17:07,940 --> 00:17:15,710
in to find a message for a channel where we look through if there are any if there are any messages

199
00:17:15,710 --> 00:17:22,340
then we loop through and extract the information that we need and then create new message objects and

200
00:17:22,340 --> 00:17:26,240
add them to our messages rate and message service.

201
00:17:26,450 --> 00:17:31,860
And so we said to print it out so let's see if we got anything here.

202
00:17:32,450 --> 00:17:32,780
You know what.

203
00:17:32,780 --> 00:17:37,250
I accidentally put the completion in the wrong place.

204
00:17:37,490 --> 00:17:42,860
So I have it inside of the for loop so I'm going to cut these two lines and these should actually be

205
00:17:43,160 --> 00:17:47,990
outside of the area.

206
00:17:48,200 --> 00:17:48,450
All right.

207
00:17:48,450 --> 00:17:49,800
Let's try that again.

208
00:17:50,380 --> 00:17:50,900
OK.

209
00:17:51,020 --> 00:17:53,360
So that wasn't the only mistake I made.

210
00:17:53,600 --> 00:17:56,220
I made a rather large one right here.

211
00:17:56,220 --> 00:18:03,050
You're all that we need to ping is not just is not just this right.

212
00:18:03,170 --> 00:18:05,570
Your will should also have appended at the end of it.

213
00:18:05,580 --> 00:18:17,720
The channel ID so going to do some string interpolation and say Your will get messages and then we also

214
00:18:17,720 --> 00:18:23,780
need to tack onto it the channel ID string that we have passed into it.

215
00:18:23,890 --> 00:18:26,820
So I want to say channel Heidi.

216
00:18:27,080 --> 00:18:28,100
There we go.

217
00:18:28,190 --> 00:18:31,030
Save that and run.

218
00:18:31,040 --> 00:18:31,260
All right.

219
00:18:31,270 --> 00:18:32,810
So third time's the charm.

220
00:18:32,810 --> 00:18:37,040
Finally we are seeing the message data that is being printed out.

221
00:18:37,040 --> 00:18:44,120
So that is that is good because see here we have the Avatar color user names Emma the message hey Johnny

222
00:18:44,120 --> 00:18:45,580
what's going on.

223
00:18:45,590 --> 00:18:47,610
So yeah it's working out well.

224
00:18:47,660 --> 00:18:55,240
So just to recap what we did in this lesson we created a model for the messages.

225
00:18:55,640 --> 00:19:02,690
We created a function in our message service that's swift to go and retrieve all of the messages for

226
00:19:02,720 --> 00:19:04,120
a given channel.

227
00:19:04,420 --> 00:19:11,300
Then in our chat Visi we did a little bit of work and created a few more functions so that when we log

228
00:19:11,300 --> 00:19:15,920
in we do a check for any check for more than one channel.

229
00:19:15,980 --> 00:19:24,980
We select a channel and then we update the view based on that based on which channel is selected and

230
00:19:25,010 --> 00:19:29,650
that is where we created our new function from getting messages which goes and fetches the messages

231
00:19:29,660 --> 00:19:31,610
for a selected channel.

232
00:19:31,880 --> 00:19:32,950
So things are looking good.

233
00:19:32,950 --> 00:19:38,630
Probably you guys are having fun and in the next Listen we're going to work on adding messages so that

234
00:19:38,630 --> 00:19:42,260
you guys can actually see something be returned with that function.

235
00:19:42,530 --> 00:19:44,740
And yes so we're we're making great progress.

236
00:19:44,750 --> 00:19:49,790
And I'm going to finish up here by adding and committing our changes and then I'll see you guys in the

237
00:19:49,790 --> 00:19:50,270
next one.
