1
00:00:08,070 --> 00:00:13,020
Hey guys this is Caleb with Debb slopes and in this video we're going to set up the sending of messages

2
00:00:13,020 --> 00:00:14,370
in group feed visi.

3
00:00:14,370 --> 00:00:16,780
We're going to upload them to firebase in our group.

4
00:00:16,800 --> 00:00:22,350
We're also going to go ahead and we're going to set up a function in our data service to download all

5
00:00:22,350 --> 00:00:28,140
of our feed messages for the desired group that we want and we're also going to do a really cool table

6
00:00:28,140 --> 00:00:32,880
view scroll animation so that when a message is sent it'll scroll the table view all the way to the

7
00:00:32,880 --> 00:00:33,570
bottom.

8
00:00:33,700 --> 00:00:36,560
And so that we can see the most current message is very cool.

9
00:00:36,570 --> 00:00:42,510
So let's go ahead and let's pull open our X code project and we're actually going to start by setting

10
00:00:42,510 --> 00:00:47,730
up how the send button works we're going to set it up so that when we type a message and press send.

11
00:00:47,730 --> 00:00:50,680
It's going to upload a post to our desired group.

12
00:00:50,700 --> 00:00:52,080
Now I don't know if you remember this.

13
00:00:52,080 --> 00:00:57,630
It was a while ago but we've already set up a function in here called upload post.

14
00:00:57,630 --> 00:01:01,070
And when we were doing that we added with group key.

15
00:01:01,200 --> 00:01:07,470
It's optional and we didn't use it but we need to basically set up what happens when we want to upload

16
00:01:07,470 --> 00:01:09,690
a post to a certain group.

17
00:01:09,750 --> 00:01:11,110
And so that's really easy.

18
00:01:11,120 --> 00:01:17,090
Thankfully all we have to do is basically go into our groups reference.

19
00:01:17,220 --> 00:01:20,730
We have to find the specific group thanks to our group key.

20
00:01:21,000 --> 00:01:26,790
Then what we're going to do is we're going to basically go into the messages array then we're going

21
00:01:26,790 --> 00:01:30,620
to go into the messages child which may or may not be created.

22
00:01:30,630 --> 00:01:35,340
Thankfully firebase will create it for us and then we're going to go ahead and go into that child and

23
00:01:35,370 --> 00:01:38,490
update the child values to include all the message content that we need.

24
00:01:38,640 --> 00:01:40,970
So let's set that up really fast.

25
00:01:41,040 --> 00:01:43,700
Like I said we're going to go into ref groups.

26
00:01:43,710 --> 00:01:50,310
We're going to go ahead and go into the child for the group key which actually we don't need to do it

27
00:01:50,310 --> 00:01:52,270
like that we can just say group key.

28
00:01:52,290 --> 00:01:53,410
All right very cool.

29
00:01:53,430 --> 00:01:56,560
We need to force on Rapido because it is optional coming in.

30
00:01:56,880 --> 00:02:01,230
So now that we are in the specific group let's just take a look at firebase so we can remember what

31
00:02:01,230 --> 00:02:02,280
we're doing here.

32
00:02:02,720 --> 00:02:02,980
OK.

33
00:02:03,000 --> 00:02:03,810
So cool.

34
00:02:03,810 --> 00:02:09,120
In each group you'll notice there are three components but I want to add in a messages array that's

35
00:02:09,120 --> 00:02:11,990
going to hold all of the messages that we need.

36
00:02:12,210 --> 00:02:15,840
And so what we're going to do is we basically just passed in the key.

37
00:02:15,840 --> 00:02:23,370
So now we're in the proper group so we're going to go ahead and we're going to pass a child in that

38
00:02:23,370 --> 00:02:24,650
we have not yet created.

39
00:02:24,660 --> 00:02:27,730
But when we send a message it will automatically create it.

40
00:02:27,870 --> 00:02:30,140
It's going to be called messages.

41
00:02:30,420 --> 00:02:36,870
And then what we're going to do is for each message we're going to create a child by Auto-ID it's going

42
00:02:36,870 --> 00:02:41,610
to just be a random thing because each message doesn't necessarily need to be linked by its I.D. to

43
00:02:41,610 --> 00:02:46,470
a specific user because inside that child we're going to pass in the sender's I.D..

44
00:02:46,530 --> 00:02:53,670
So inside this new child inside of messages we're going to go ahead and update child values with a dictionary.

45
00:02:53,670 --> 00:02:56,500
This is where we will pass it content.

46
00:02:56,850 --> 00:02:57,270
OK.

47
00:02:57,300 --> 00:03:04,350
Just like a feed just like a feed cell if you remember the feed cell has content and a Sender ID and

48
00:03:04,380 --> 00:03:09,590
the content will come from the message passed in with the string appear message.

49
00:03:09,810 --> 00:03:13,650
Then we're going to have another one called Sender ID.

50
00:03:13,920 --> 00:03:14,470
OK.

51
00:03:14,700 --> 00:03:17,260
And that will come in from the ID we pass in.

52
00:03:17,380 --> 00:03:18,700
OK remember that.

53
00:03:18,870 --> 00:03:19,470
Very cool.

54
00:03:19,470 --> 00:03:20,580
So that's super simple.

55
00:03:20,580 --> 00:03:23,320
It's basically the same thing as down here.

56
00:03:23,580 --> 00:03:26,160
But this will create a messages folder inside.

57
00:03:26,160 --> 00:03:33,300
Any group will assign each message a random ID and then inside that will have content message and the

58
00:03:33,320 --> 00:03:35,190
sender ID from the ID.

59
00:03:35,190 --> 00:03:36,810
So that's good to go.

60
00:03:36,810 --> 00:03:42,090
Now let's go ahead and let's right get all messages for a desired group.

61
00:03:42,240 --> 00:03:46,740
That is the function we want and we're going to put it right beneath it all feed messages because it's

62
00:03:46,740 --> 00:03:48,550
kind of a similar function.

63
00:03:48,570 --> 00:03:51,180
So let's go ahead and put it right in the middle here.

64
00:03:51,360 --> 00:03:59,440
And like I said the name is phunk get all messages for desired group.

65
00:03:59,460 --> 00:04:01,480
Now I'm just going to use.

66
00:04:02,100 --> 00:04:03,870
I won't use an internal parameter for this.

67
00:04:03,870 --> 00:04:05,410
I'm just going to pass it a group.

68
00:04:05,670 --> 00:04:08,680
Then we need a handler that's going to return all the messages.

69
00:04:08,760 --> 00:04:09,480
OK.

70
00:04:09,480 --> 00:04:11,840
So go ahead and type handler.

71
00:04:11,910 --> 00:04:14,000
It's going to be escaping like always.

72
00:04:14,280 --> 00:04:20,760
And then we're going to have to go in and put some parentheses here some curly brackets and for what

73
00:04:20,760 --> 00:04:25,090
actually escapes the closure we're going to just call it messages array.

74
00:04:25,440 --> 00:04:29,280
And it's going to be an array of type message which we already created.

75
00:04:29,520 --> 00:04:31,070
Just like up here.

76
00:04:31,080 --> 00:04:33,060
Very cool very simple.

77
00:04:33,090 --> 00:04:39,300
So what we need to do is we basically need to go through and find the right group.

78
00:04:39,300 --> 00:04:44,100
We need to find the messages and then we need to cycle through and create all the messages that we need

79
00:04:44,400 --> 00:04:46,070
and append them back up to the group.

80
00:04:46,080 --> 00:04:51,960
Basically like we did here we observed the feed we created a snapshot we cycled through all of them

81
00:04:51,990 --> 00:04:55,140
created message objects appended them and then return them.

82
00:04:55,140 --> 00:04:59,940
So we're going to do the same thing but let's start with an array that we're going to actually append

83
00:04:59,940 --> 00:05:01,560
all of our messages to.

84
00:05:01,560 --> 00:05:07,200
And let's call it var group message array and it's going to be an array of message.

85
00:05:07,690 --> 00:05:09,210
Let's instantiate it.

86
00:05:09,310 --> 00:05:19,180
And now let's go ahead and let's observe the desired groups messages child so type ref groups dot child.

87
00:05:19,180 --> 00:05:24,480
Now of course the key is going to be the group or sorry the desired group.

88
00:05:25,540 --> 00:05:27,440
We're going to force on rapid there.

89
00:05:27,590 --> 00:05:30,430
Now we're going to go ahead and oh you know what.

90
00:05:30,430 --> 00:05:31,410
Sorry it's not optional.

91
00:05:31,420 --> 00:05:35,490
So we don't need to we don't need to unwrap it that way.

92
00:05:35,530 --> 00:05:43,300
So we are in the proper group that we've selected then we need to go into the messages child so choose

93
00:05:43,300 --> 00:05:46,810
child by path string and type messages.

94
00:05:47,020 --> 00:05:50,490
Then inside of that we want to observe the whole thing.

95
00:05:50,500 --> 00:05:54,300
So we're going to type observe single event of type value.

96
00:05:54,310 --> 00:05:58,170
We're going to observe all the values and this is only going to happen once.

97
00:05:58,270 --> 00:06:03,090
But we're going to call an observer from the actual view controller so that when a change happens it'll

98
00:06:03,100 --> 00:06:05,680
cycle through and update all the changes.

99
00:06:05,680 --> 00:06:10,930
And this will this will prevent basically any issues of this being called multiple times and loading

100
00:06:10,930 --> 00:06:11,980
the data multiple times.

101
00:06:11,980 --> 00:06:15,580
I ran into an issue with that but I found that this is a great solution.

102
00:06:15,580 --> 00:06:23,920
So for the closure go ahead and call data snapshot group message snapshot because it's coming specifically

103
00:06:23,920 --> 00:06:25,170
from the group.

104
00:06:25,450 --> 00:06:31,530
And now we're going to use guard like we have to create a constant holding that snapshot guard let a

105
00:06:31,540 --> 00:06:41,290
group message snapshot equals group message snapshot children all objects.

106
00:06:41,290 --> 00:06:45,310
And we're going to cast it as a an array of data snapshot.

107
00:06:45,550 --> 00:06:53,290
If we can't do that or else return and now we have a group message snapshot so now let's go ahead and

108
00:06:53,290 --> 00:07:03,610
cycle through that four group met groups with two O's group message in group message snapshot.

109
00:07:04,030 --> 00:07:07,350
We're going to go ahead and basically do what we did up here.

110
00:07:07,450 --> 00:07:13,120
We're going to get the content and the sender ID then we're going to go ahead and pull that together

111
00:07:13,150 --> 00:07:14,680
into a message object.

112
00:07:14,710 --> 00:07:16,660
And we're going to append that to our message.

113
00:07:16,690 --> 00:07:25,270
So let content equals group message child snapshot for path content.

114
00:07:26,020 --> 00:07:32,190
And we want the value and we want to cast it as a string so that it can be added as text data.

115
00:07:32,560 --> 00:07:33,930
Then we want the sender ID.

116
00:07:34,120 --> 00:07:40,240
So go ahead and type let sender ID equals group message child snapshot.

117
00:07:40,240 --> 00:07:43,150
The path of course is sender ID.

118
00:07:43,150 --> 00:07:45,950
We want the value of that and we want to cast that as a strength.

119
00:07:46,080 --> 00:07:46,300
OK.

120
00:07:46,300 --> 00:07:48,610
Just like we did with the content.

121
00:07:48,730 --> 00:07:55,750
Now we're going to create a message object by typing let message equals message and let's instantiate

122
00:07:55,750 --> 00:07:59,360
that bad boy and we're going to give him some content.

123
00:07:59,770 --> 00:08:02,870
There we go and we're going to pass it a Sender ID as well.

124
00:08:03,190 --> 00:08:04,420
Excellent.

125
00:08:04,510 --> 00:08:10,660
Next after we have created our message object we're going to append that to our group message array.

126
00:08:10,810 --> 00:08:14,650
And you know what just to be specific I'm going to call this group message.

127
00:08:14,980 --> 00:08:21,370
So now that we want to append that we're going to type group message array append and you'll see it

128
00:08:21,370 --> 00:08:31,220
requires a message which we have in group message perfect case so once that's all done after the for

129
00:08:31,220 --> 00:08:36,740
loop is finished we're going to call our handler which asks us to return an array of message which we

130
00:08:36,740 --> 00:08:39,080
have in group message array.

131
00:08:39,260 --> 00:08:46,370
This function is going to be handy for returning all the messages for the specific group that we want.

132
00:08:46,400 --> 00:08:51,060
Let's go ahead and let's set this up in group feed Visi now.

133
00:08:51,380 --> 00:08:59,030
So when we go into view we'll appear we load all of the titles stuff we load all of the emails.

134
00:08:59,030 --> 00:09:06,470
That's great but now we're going to go ahead and observe our groups reference and we're going to call

135
00:09:06,470 --> 00:09:08,900
get all messages for desired group.

136
00:09:08,930 --> 00:09:15,840
So let's go ahead and call data service instance ref groups and we want to observe all changes.

137
00:09:15,890 --> 00:09:19,240
If anything changes we want to update all the messages.

138
00:09:19,310 --> 00:09:27,160
So go ahead and call observe and we're going to type value because we want to observe all values changing.

139
00:09:27,230 --> 00:09:32,150
Now the snapshot for this one we don't care about because we're not actually using it we're just using

140
00:09:32,150 --> 00:09:33,930
this to observe our groups reference.

141
00:09:33,950 --> 00:09:41,750
But what we are going to do is call data service instance to get all messages for a desired group.

142
00:09:41,900 --> 00:09:47,450
Well what group are we going to pass in the group that we downloaded when we initialized the view controller

143
00:09:47,460 --> 00:09:53,330
so called self group because we are now inside of a closure so we have to call self.

144
00:09:53,660 --> 00:09:58,490
And that's the group that we want and this will give us all the messages back that we need.

145
00:09:58,490 --> 00:10:02,640
So let's call this returned group messages.

146
00:10:02,930 --> 00:10:03,830
I'm just going to copy that.

147
00:10:03,830 --> 00:10:05,570
That's a long name.

148
00:10:05,570 --> 00:10:12,230
And now that we have this returned we should set an array locally in this view controller that will

149
00:10:12,230 --> 00:10:13,950
hold all those messages.

150
00:10:14,180 --> 00:10:15,310
So let's go ahead and do that.

151
00:10:15,320 --> 00:10:22,850
Let's create var group messages and that's going to be of course an array of message and we're going

152
00:10:22,850 --> 00:10:23,890
to instantiate it.

153
00:10:24,110 --> 00:10:25,460
Really cool.

154
00:10:25,460 --> 00:10:34,830
Next we're going to go ahead and let's think we need to set self dot group messages to be equal to returned

155
00:10:34,850 --> 00:10:38,180
group messages whichever messages got returned back.

156
00:10:38,180 --> 00:10:39,790
That's what we want to do.

157
00:10:39,800 --> 00:10:40,070
All right.

158
00:10:40,070 --> 00:10:47,780
And now now that we have those group messages returned we can call self table view reload data and all

159
00:10:47,780 --> 00:10:52,030
of the messages will flood in and show up where they're supposed to be.

160
00:10:52,130 --> 00:10:57,260
But we have a problem folks we have not yet set up our table view in this view controller.

161
00:10:57,260 --> 00:10:58,630
Let's do that now.

162
00:10:58,730 --> 00:11:02,360
I'm going to tab down here so you can see plenty of things.

163
00:11:02,630 --> 00:11:10,370
And we're going to do this with an extension by typing extension group feed Visi and we're going to

164
00:11:10,370 --> 00:11:12,310
conform to you table view.

165
00:11:12,320 --> 00:11:17,310
Delegate and you table your data source like we have like 100 times in this course.

166
00:11:17,900 --> 00:11:25,530
And we need number of sections in table view number of rows in Section and cell for road index path.

167
00:11:25,610 --> 00:11:26,410
That's great.

168
00:11:26,600 --> 00:11:29,620
Let's return one section.

169
00:11:29,780 --> 00:11:37,430
Let's go ahead and return the group messages array count because we need to return how many messages

170
00:11:37,430 --> 00:11:39,700
we have that's just makes sense.

171
00:11:39,710 --> 00:11:44,450
And now what we're going to do is we're going to set up a cell and we're going to go ahead and initialize

172
00:11:44,450 --> 00:11:45,890
it with everything that we need.

173
00:11:45,920 --> 00:11:54,480
So go ahead and type guard let cell like we have equals table view dot dequeue.

174
00:11:54,520 --> 00:11:59,800
So delegate dequeue reusable cell with identifier for index path.

175
00:12:00,230 --> 00:12:04,160
And of course we have group feed cell.

176
00:12:04,160 --> 00:12:06,410
We've already set up that name in the main storyboard.

177
00:12:06,770 --> 00:12:08,820
And we're just going to give it the index path.

178
00:12:08,870 --> 00:12:09,490
OK.

179
00:12:09,860 --> 00:12:19,370
Now we need to cast this as a group feed cell and if that doesn't work else we're going to return a

180
00:12:19,760 --> 00:12:23,980
table view cell just instantiate it like so.

181
00:12:24,230 --> 00:12:29,580
But assuming this works which it will we're going to go ahead and configure our cell.

182
00:12:29,690 --> 00:12:36,290
So go ahead and type cell doc configure cell and like always it needs a profile image and email and

183
00:12:36,290 --> 00:12:36,950
content.

184
00:12:36,950 --> 00:12:43,760
So let's give it that from our group message array we can pull out the message for the index and get

185
00:12:43,760 --> 00:12:45,920
all the data we need by typing let.

186
00:12:45,920 --> 00:12:53,810
Message equals group messages and we can just pull the index path dot row to get the specific message

187
00:12:53,810 --> 00:12:54,470
all the way down.

188
00:12:54,470 --> 00:12:56,650
We've done this throughout the whole course.

189
00:12:56,780 --> 00:13:03,890
So let's go ahead and let's pull out message dot sender ID.

190
00:13:03,970 --> 00:13:05,050
Oh that's a problem.

191
00:13:05,090 --> 00:13:06,250
We'll fix that in a second.

192
00:13:06,590 --> 00:13:09,950
So we don't have an e-mail yet but we can get it.

193
00:13:10,240 --> 00:13:15,740
So for the content though we can definitely get message content for the e-mail.

194
00:13:15,740 --> 00:13:17,960
We're going to have to use our user name function.

195
00:13:18,080 --> 00:13:24,460
So go ahead and call data service instance don't get e-mail.

196
00:13:24,650 --> 00:13:29,090
Let's see to get username for your cause remember we called it username.

197
00:13:29,090 --> 00:13:31,400
We really should have called e-mail.

198
00:13:31,880 --> 00:13:39,620
But what we can do is we can pass the message sender ID and the handler of course is an e-mail so we

199
00:13:39,620 --> 00:13:45,500
can move our configuration of the cell into this function and for the e-mail we can just pass an e-mail

200
00:13:45,500 --> 00:13:48,490
that we get back from our user named function.

201
00:13:48,580 --> 00:13:54,680
The profile limits though is another story and we can actually just go ahead and call you image named

202
00:13:55,100 --> 00:13:57,020
and it's just default profile image.

203
00:13:57,020 --> 00:14:03,470
We're not going to actually download or upload images for this section of the course but that is something

204
00:14:03,470 --> 00:14:05,620
that you could do to extend this app.

205
00:14:05,620 --> 00:14:10,190
Hint hint now that it's configured we're going to go ahead and return the cell.

206
00:14:10,430 --> 00:14:13,910
And now we should be good to go to get all feed messages.

207
00:14:13,910 --> 00:14:18,370
The only issue we haven't set it up to send yet we can't do anything if we push that send button.

208
00:14:18,370 --> 00:14:22,100
So let's go ahead and let's send a message.

209
00:14:22,190 --> 00:14:26,360
We need to make sure though that we actually have typed something so let's make sure that the message

210
00:14:26,360 --> 00:14:28,710
textfield is not nil or empty.

211
00:14:28,760 --> 00:14:34,930
So go ahead and type if message textfield text is not equal to nothing.

212
00:14:35,020 --> 00:14:36,460
It's not empty.

213
00:14:36,470 --> 00:14:38,540
We're going to go ahead and disable the textfield.

214
00:14:38,540 --> 00:14:42,530
We're going to disable the button as soon as we press it so that we can't press it multiple times and

215
00:14:42,530 --> 00:14:43,720
send multiple messages.

216
00:14:43,730 --> 00:14:50,980
It's just kind of a nice way to just verify that we don't send accidental input so call message.

217
00:14:51,010 --> 00:15:01,550
Textfield dot is enabled What is enabled is false and send button dot is enabled is also going to be

218
00:15:01,550 --> 00:15:02,650
false.

219
00:15:02,750 --> 00:15:09,200
Then after that we're going to call data service instance and from this point it's just like uploading

220
00:15:09,200 --> 00:15:10,520
a post to the feed.

221
00:15:10,670 --> 00:15:14,240
We're going to take message textfield that text unwrap it.

222
00:15:14,240 --> 00:15:16,910
That's the message we're going to send the I.D..

223
00:15:16,940 --> 00:15:21,200
We're going to send is off from firebase Auth.

224
00:15:23,880 --> 00:15:25,780
we forgot to import firebase.

225
00:15:25,930 --> 00:15:29,400
Let's go to that import firebase sneaky thing.

226
00:15:29,530 --> 00:15:33,760
And now for the off Id We can call off off.

227
00:15:33,850 --> 00:15:41,080
Current User ID and it's going to yell at us in just a second but this time instead of sending it to

228
00:15:41,080 --> 00:15:46,630
the feed we're going to pass in a group key which we can get from group Dom key.

229
00:15:46,780 --> 00:15:50,640
We need to force unwrap it of course because it is optional.

230
00:15:50,980 --> 00:15:55,470
And you know what it's yelling at us that we need to force unwrap the off as well.

231
00:15:55,480 --> 00:15:56,880
That's fine.

232
00:15:56,960 --> 00:16:01,520
Now oh interesting so I guess oh no no sorry.

233
00:16:01,520 --> 00:16:02,400
Because I pulled it.

234
00:16:02,410 --> 00:16:03,710
Optionally here that's fine.

235
00:16:03,710 --> 00:16:10,160
We have a value and send complete is going to be a boolean that comes back.

236
00:16:10,160 --> 00:16:17,600
Let's just say status and we're going to say well no maybe complete that makes a little more sense if

237
00:16:17,600 --> 00:16:24,230
complete meaning if the send is complete we're going to re-enable message textfield that isn't enabled

238
00:16:24,980 --> 00:16:33,950
equals true and send button did it again send button is enabled is also true.

239
00:16:33,950 --> 00:16:34,790
Very very cool.

240
00:16:34,790 --> 00:16:40,040
And when we send it we also want to blank out the message textfield so that it's empty again because

241
00:16:40,040 --> 00:16:41,570
we don't want to send the same message twice.

242
00:16:41,570 --> 00:16:45,980
So go ahead and type message textfield dot text is empty.

243
00:16:45,980 --> 00:16:46,550
Very cool.

244
00:16:46,550 --> 00:16:48,950
And of course it's yelling at us we're in a closure.

245
00:16:48,950 --> 00:16:55,340
So we need to do self dot all these things now now whoops we deleted the whole thing let's try that

246
00:16:55,340 --> 00:16:55,670
again.

247
00:16:55,670 --> 00:16:57,630
Self-taught send button is enabled.

248
00:16:57,950 --> 00:16:58,730
Let's go try it out.

249
00:16:58,730 --> 00:17:04,400
This should work to send a message and to download all of them at the same exact time.

250
00:17:04,400 --> 00:17:09,400
Let's give it a shot simulator is opening up.

251
00:17:10,010 --> 00:17:12,260
It is building and running.

252
00:17:12,260 --> 00:17:14,000
Here we go let's try it.

253
00:17:14,000 --> 00:17:16,500
Click on groups click on nerd herd.

254
00:17:16,520 --> 00:17:17,920
We have our e-mails.

255
00:17:17,960 --> 00:17:19,390
Let's send a message.

256
00:17:19,700 --> 00:17:20,720
Hey guys.

257
00:17:20,720 --> 00:17:21,360
And you know what.

258
00:17:21,500 --> 00:17:23,290
Let's watch firebase just for fun.

259
00:17:23,390 --> 00:17:26,530
Here's our group and here we go.

260
00:17:26,540 --> 00:17:27,050
Send

261
00:17:30,860 --> 00:17:32,050
and nothing happens.

262
00:17:32,050 --> 00:17:34,410
Interesting messages shows up here.

263
00:17:34,720 --> 00:17:40,840
So it looks like we pressed send and nothing happened.

264
00:17:41,200 --> 00:17:50,960
Well we said if complete it should re-enable everything and it should show in the table view but weird.

265
00:17:50,980 --> 00:17:53,480
Yeah it appears it's complete it's an firebase.

266
00:17:53,500 --> 00:18:02,470
Let's go look at that upload post function and let's see oh we never send We never used our händler

267
00:18:02,470 --> 00:18:03,670
to say that we were done.

268
00:18:03,670 --> 00:18:05,700
That's totally why it's not completing.

269
00:18:05,740 --> 00:18:09,040
So let's call our handler here send complete and let's pass it.

270
00:18:09,040 --> 00:18:10,430
True.

271
00:18:10,450 --> 00:18:10,960
There we go.

272
00:18:10,960 --> 00:18:14,050
That should be the problem we were just not telling it that the message was done.

273
00:18:14,050 --> 00:18:19,380
So it had no idea whether or not it should know that it's done or not so let's let's try this out.

274
00:18:19,600 --> 00:18:24,700
Let's go into groups let's open it up and we're still not getting any messages we'll have to investigate

275
00:18:24,700 --> 00:18:25,380
that in a second.

276
00:18:25,390 --> 00:18:32,850
But let's try to send a message Lou when I send it it looks like it sends everything goes away.

277
00:18:33,040 --> 00:18:36,380
But our table view is definitely not doing anything.

278
00:18:36,610 --> 00:18:42,860
Oh we didn't set up the table if you delegate or datasource simple stuff folks to this table view.

279
00:18:42,910 --> 00:18:45,730
Delegate equals self table view.

280
00:18:45,840 --> 00:18:47,810
Datasource equals self.

281
00:18:47,830 --> 00:18:49,450
And let's build and run it.

282
00:18:49,480 --> 00:18:51,230
That should actually solve the problem.

283
00:18:51,230 --> 00:18:56,970
It's sometimes the simplest things that you just gloss over things I've done like a million times anyway.

284
00:18:56,980 --> 00:18:58,860
So let's open it up.

285
00:18:58,990 --> 00:18:59,920
There's our messages.

286
00:18:59,920 --> 00:19:00,830
That's amazing.

287
00:19:00,880 --> 00:19:02,820
Let's send it.

288
00:19:03,010 --> 00:19:05,450
Batman rules.

289
00:19:06,070 --> 00:19:10,790
And when I when I send it let's see what happens.

290
00:19:10,840 --> 00:19:11,350
There it is.

291
00:19:11,350 --> 00:19:11,770
Boom.

292
00:19:11,800 --> 00:19:14,770
The message appears exactly as we hoped.

293
00:19:14,770 --> 00:19:16,880
That is so so cool.

294
00:19:16,930 --> 00:19:20,340
Amazing it popped up in real time just like you would expect.

295
00:19:20,380 --> 00:19:26,050
Now the last thing we need to do is we need to set up a scroll animation for whenever we reload data

296
00:19:26,080 --> 00:19:33,030
and get some new messages because we should have it animate to show the most recent message every time.

297
00:19:33,220 --> 00:19:37,060
And to do that we first need to make sure we have messages because if there is nothing in the table

298
00:19:37,070 --> 00:19:39,040
view this is going to cause a crash.

299
00:19:39,040 --> 00:19:48,950
So if self doc group messages that count is greater than zero.

300
00:19:49,570 --> 00:19:57,730
What we're going to do is we're going to call a self table view scroll to row an index path at your

301
00:19:57,760 --> 00:20:00,060
table view scroll position and animated.

302
00:20:00,220 --> 00:20:05,400
Well of course we want it to be animated and the position we're going to have it scroll to none.

303
00:20:05,480 --> 00:20:09,560
OK and you'll see why in a second you can have it scroll to the middle bottom or top.

304
00:20:09,670 --> 00:20:11,650
But sometimes there can be issues.

305
00:20:11,650 --> 00:20:17,550
So none basically just tells it to scroll all the way to the index path that we set.

306
00:20:17,590 --> 00:20:24,430
Now the thing is the index path is an interesting thing because we cannot tell it to scroll to the bottom

307
00:20:24,460 --> 00:20:27,790
but we can tell it to go to group messages don't count.

308
00:20:27,790 --> 00:20:37,750
So if we give it an index path and we call Dom in it to get the row and section what we can do is we

309
00:20:37,750 --> 00:20:44,500
can basically say the section is zero of course because we only have one section and we're using that

310
00:20:46,240 --> 00:20:47,970
we're using zero indexing here.

311
00:20:48,160 --> 00:20:55,090
Now for the the row we want to scroll to the very bottom row and in order to get to that we could just

312
00:20:55,090 --> 00:20:59,440
call self group messages don't count.

313
00:20:59,440 --> 00:21:03,410
So let's say we had 30 messages I could scroll to the 30th row.

314
00:21:03,610 --> 00:21:04,150
Right.

315
00:21:04,210 --> 00:21:07,040
If we had 31 suddenly it would scroll to the 31st row.

316
00:21:07,150 --> 00:21:12,370
But the issue is that the count shows the true value so if there are 30 messages it would come back

317
00:21:12,400 --> 00:21:13,380
as 30.

318
00:21:13,540 --> 00:21:19,630
But the array only goes up to 29 on the index because raise use zero indexing.

319
00:21:19,630 --> 00:21:21,550
So let's go ahead and do self-talk.

320
00:21:21,550 --> 00:21:24,240
Group messages don't count minus 1.

321
00:21:24,280 --> 00:21:29,110
And believe it or not guys this will make a really cool animation that will animate to the very bottom

322
00:21:29,110 --> 00:21:31,390
of our table view whenever we send a message.

323
00:21:31,390 --> 00:21:33,250
So let's go ahead and build and run.

324
00:21:33,400 --> 00:21:37,120
Let's go check this out and let's see how we did and we'll call this video.

325
00:21:37,120 --> 00:21:37,880
Good.

326
00:21:38,350 --> 00:21:38,680
OK.

327
00:21:38,680 --> 00:21:43,190
Go to groups go to your group let's send some messages.

328
00:21:43,210 --> 00:21:47,600
Hi yo yo.

329
00:21:47,830 --> 00:21:50,190
And this one should animate nicely.

330
00:21:50,200 --> 00:21:51,310
Nice.

331
00:21:51,340 --> 00:21:54,700
See that it animates nicely but problem.

332
00:21:54,700 --> 00:21:57,130
We're covering the table view.

333
00:21:58,300 --> 00:22:01,120
We can see that it's animating but we need to fix that constraint.

334
00:22:01,210 --> 00:22:04,260
So let's go into the storyboard here.

335
00:22:04,330 --> 00:22:08,660
Let's go ahead and fix that really quickly and then we'll call this video quits.

336
00:22:12,490 --> 00:22:12,980
All right.

337
00:22:13,240 --> 00:22:17,980
So the table view the bottom constraint here goes to the very bottom of the view and that views just

338
00:22:18,100 --> 00:22:20,210
happens to be on top of the table view.

339
00:22:20,560 --> 00:22:25,880
Let's get rid of our bottom constraint and we're going to go ahead and it 8 to the bottom.

340
00:22:25,900 --> 00:22:27,170
Well not eight but zero.

341
00:22:27,190 --> 00:22:28,360
So that it matches exactly.

342
00:22:28,360 --> 00:22:29,200
Up next to it.

343
00:22:29,440 --> 00:22:33,940
Press Enter to set that constraint and let's build and run one more time.

344
00:22:33,940 --> 00:22:35,610
That should solve our issue.

345
00:22:35,620 --> 00:22:38,520
Guys this app is coming together it's looking amazing.

346
00:22:38,650 --> 00:22:40,850
Great great job so far.

347
00:22:40,900 --> 00:22:43,350
And guys we've done a lot of work to make this awesome.

348
00:22:43,360 --> 00:22:47,140
So let's go ahead and head in there is our most recent message.

349
00:22:47,140 --> 00:22:55,530
Let's send a new one Harvey stinks and send and it animates up just like we were hoping.

350
00:22:55,540 --> 00:23:00,070
Now the cool thing is when you enter into this let's say people have sent new messages since you last

351
00:23:00,070 --> 00:23:01,810
went and watch this.

352
00:23:01,990 --> 00:23:08,140
When you go in that animates you see that maybe any to send a few more maybe send a few more here kind

353
00:23:08,140 --> 00:23:09,660
of spamming my channel here.

354
00:23:09,670 --> 00:23:11,920
OK let's go back and watch this.

355
00:23:12,190 --> 00:23:15,180
Do you see that it animates when it pops up it's really really cool.

356
00:23:15,280 --> 00:23:18,730
And I will be able to see it better later when we animate this in from the left.

357
00:23:18,730 --> 00:23:20,350
But guys amazing work.

358
00:23:20,350 --> 00:23:23,890
We now are getting group messages now to prove it.

359
00:23:23,890 --> 00:23:28,220
It's not coming into this one faded boom.

360
00:23:28,900 --> 00:23:31,310
Different messages for different groups.

361
00:23:31,330 --> 00:23:32,630
Super super cool.

362
00:23:32,650 --> 00:23:33,910
Amazing work.

363
00:23:33,910 --> 00:23:39,280
Let's head over to the next video where we're going to go ahead and we're going to set up a view controller

364
00:23:39,280 --> 00:23:44,800
extension that's going to animate this view controller to the left instead of popping up from the bottom.

365
00:23:44,980 --> 00:23:46,090
And then we're done.

366
00:23:46,090 --> 00:23:47,560
This app is finished.

367
00:23:47,560 --> 00:23:48,760
Amazing work.

368
00:23:48,850 --> 00:23:50,770
And let's head over to that next video now.
