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