1
00:00:07,710 --> 00:00:08,350
Everyone.

2
00:00:08,400 --> 00:00:11,700
Welcome back Johnny be here with Dev's Lokes dot com.

3
00:00:11,760 --> 00:00:17,640
And in this lesson we are going to refine our loggin logic as well as some other stuff having to do

4
00:00:17,640 --> 00:00:19,730
with selecting a channel.

5
00:00:19,890 --> 00:00:27,600
And this lesson is kind of mostly getting ready to work with messages.

6
00:00:27,810 --> 00:00:33,090
So it might be a little bit all over the place but I'll try to make it as clear as possible.

7
00:00:33,090 --> 00:00:40,110
We just got a lot of little little things that we need to fix up before we're ready to continue getting

8
00:00:40,110 --> 00:00:40,630
messages.

9
00:00:40,630 --> 00:00:41,110
All right.

10
00:00:41,400 --> 00:00:47,790
So we're going to start out here in the channel Visi And here in the ad channel preste first thing that

11
00:00:47,790 --> 00:00:52,650
we're going to do is we're going to do a check to say that we can only add a channel if we're already

12
00:00:52,650 --> 00:01:02,190
logged into I want to see if off service dot instance dot is logged in then we are able to show that

13
00:01:02,190 --> 00:01:02,600
motile.

14
00:01:02,650 --> 00:01:05,980
I want to cut that and paste it right in here.

15
00:01:06,450 --> 00:01:14,340
And then another thing that I noticed was that when we are logged in and then we log out the channels

16
00:01:14,340 --> 00:01:15,980
they stay there.

17
00:01:15,990 --> 00:01:16,250
All right.

18
00:01:16,260 --> 00:01:19,470
So we need those channels to disappear after we log out.

19
00:01:19,500 --> 00:01:23,970
So we're going Kovar come over to our message service starts with the file and we're going to create

20
00:01:24,060 --> 00:01:29,460
a function to do that so I'm going to quickly just for a quick clean up.

21
00:01:29,490 --> 00:01:31,310
Is this function a little bit.

22
00:01:31,330 --> 00:01:35,940
I know it's good to say phunk clear Channel's

23
00:01:39,310 --> 00:01:42,340
And that's just channels dot every move.

24
00:01:42,400 --> 00:01:42,960
All right.

25
00:01:42,970 --> 00:01:48,150
So that's just clearing out our array of channels that we have here.

26
00:01:48,370 --> 00:01:49,450
And while we're in here.

27
00:01:50,020 --> 00:01:56,230
Another variable that we're going to need later is we're going to have a variable for the selected channels

28
00:01:56,230 --> 00:02:02,140
so whichever channel that we have currently selected We're going to store that here is to say selected

29
00:02:02,890 --> 00:02:03,810
channel.

30
00:02:04,040 --> 00:02:08,290
And but that's going to be an optional because if we're not logged in then obviously we don't have a

31
00:02:08,500 --> 00:02:14,790
selected channel so we're going to see Channel and question mark for optional right.

32
00:02:15,040 --> 00:02:19,790
It's going to save that and let's test let's test this.

33
00:02:20,010 --> 00:02:22,750
But first we have to put it in the we have to call it somewhere.

34
00:02:22,810 --> 00:02:30,430
So when the use of data service when we log out we are also going to call a message service instance

35
00:02:30,550 --> 00:02:35,040
not clear channels so I'm going to say that.

36
00:02:35,290 --> 00:02:43,180
And then the last thing that we need to do is in our policy here in our set up user info which is called

37
00:02:43,660 --> 00:02:53,270
whenever the user data changes we are going to say right here in the else table you dot reload data.

38
00:02:53,650 --> 00:02:57,160
All right so the flow is we press log out.

39
00:02:57,310 --> 00:03:02,510
And that triggers a user data did change notification right here.

40
00:03:02,830 --> 00:03:09,040
And so when we click log out we have set the is logged in Bhool to false.

41
00:03:09,040 --> 00:03:16,840
So this portion of the function will be called and we will at that point the channels array will be

42
00:03:16,840 --> 00:03:17,290
empty.

43
00:03:17,290 --> 00:03:23,060
So when we reload the table view then itll reload with with all empty.

44
00:03:23,080 --> 00:03:23,510
All right.

45
00:03:23,710 --> 00:03:30,660
So let's go ahead and run this just to test it to run on iPhone 6 s and say run.

46
00:03:30,850 --> 00:03:35,990
All right he's going to come over here and we have all of our channels here and I'm going to log out

47
00:03:36,140 --> 00:03:39,120
and boom they are gone.

48
00:03:39,470 --> 00:03:39,870
All right.

49
00:03:39,890 --> 00:03:46,010
So let's continue on the next thing that we're going to do is kind of refine our log and process.

50
00:03:46,010 --> 00:03:51,200
So when we first launch and this is called if we're logged in then we then we do the find use by email

51
00:03:51,200 --> 00:03:54,940
and post a user data did change.

52
00:03:54,950 --> 00:04:00,170
So right now we're calling our find all channel right here but that's not where we actually wanted.

53
00:04:00,420 --> 00:04:06,530
So we're going to we're going to create a function for for the user data to change.

54
00:04:06,620 --> 00:04:08,660
But first we need to have an observer for it.

55
00:04:08,720 --> 00:04:09,120
Right.

56
00:04:09,320 --> 00:04:17,050
So we're going to say notification center dot default dot ad observer.

57
00:04:17,450 --> 00:04:26,010
And you see this guy right here the observer is sulf the selector is that we haven't created it yet

58
00:04:26,840 --> 00:04:31,940
but I can actually just copy the one from Channel Visi just to save us some time right here.

59
00:04:31,980 --> 00:04:37,890
Use the data to change it and when to come back here and paste that right here.

60
00:04:38,200 --> 00:04:40,950
All right so the selector is hash tag.

61
00:04:41,240 --> 00:04:44,880
So look for and then

62
00:04:47,560 --> 00:04:53,280
chapt the dot user data to change and that we don't have this functions.

63
00:04:53,320 --> 00:04:54,340
That's fine.

64
00:04:54,730 --> 00:04:57,020
And the name of the notification is no.

65
00:04:57,250 --> 00:05:01,230
Underscore user did it to change the object is new.

66
00:05:01,500 --> 00:05:07,600
OK so going through the flow again we launched the app from a fresh start but we had previously logged

67
00:05:07,600 --> 00:05:08,050
in.

68
00:05:08,260 --> 00:05:14,620
So we use our find used by email function to get our information for our user.

69
00:05:14,620 --> 00:05:17,670
Then we post the note if you user do the change.

70
00:05:17,680 --> 00:05:24,090
This will be detected here in Chad Visi by this observer as well as in the channel Visi by its observer.

71
00:05:24,400 --> 00:05:26,340
And then here in the.

72
00:05:26,620 --> 00:05:32,560
So then we need to decide what we want to have happen in the chat we see when a user's data changes.

73
00:05:32,560 --> 00:05:33,260
OK.

74
00:05:33,910 --> 00:05:42,220
So we're going to do a check we're going to say if off service that instance dot is logged in then we

75
00:05:42,220 --> 00:05:49,270
are going to say this is where we're going to get our good channels which we'll figure out in just a

76
00:05:49,270 --> 00:05:50,430
second.

77
00:05:50,630 --> 00:05:57,530
Else if no one is logged in then we're going to change our channel label to say Please log in.

78
00:05:57,580 --> 00:06:00,530
And I don't think we actually have an idea for that yet.

79
00:06:00,760 --> 00:06:03,250
Hold down option and click on main story board

80
00:06:06,620 --> 00:06:11,480
and come down here and grab this outlet right.

81
00:06:11,520 --> 00:06:21,110
This guy right here and go control drag and drop you right here and we're going to call this channel

82
00:06:21,530 --> 00:06:28,260
name label it connect and go back.

83
00:06:28,410 --> 00:06:40,320
And so here we're going to say channel name label dot text is equal to please log in.

84
00:06:40,530 --> 00:06:41,030
OK.

85
00:06:41,310 --> 00:06:47,970
So after we log out if we went back to the main chat we see at the top it'll say Please log in.

86
00:06:47,970 --> 00:06:51,660
All right so now let's get the channels here.

87
00:06:51,660 --> 00:06:55,130
Actually I think we're going to put this inside another function down here.

88
00:06:55,140 --> 00:07:00,420
So we're going to phunk on log in get messages.

89
00:07:00,750 --> 00:07:07,310
So right when we log in this function is going to be called and we're gonna say Message Service instance

90
00:07:07,360 --> 00:07:14,910
that get or find out find all channel completion success.

91
00:07:15,240 --> 00:07:24,720
And right now we're not doing anything after the channels are gotten but just in preparation We'll see

92
00:07:24,720 --> 00:07:31,340
if success and then I'll say to stuff with channels.

93
00:07:31,380 --> 00:07:31,710
All right.

94
00:07:31,730 --> 00:07:36,700
So save that and then call that right here.

95
00:07:36,690 --> 00:07:39,930
So this is one loggin get messages

96
00:07:42,910 --> 00:07:48,230
get let's just clean this up.

97
00:07:48,240 --> 00:07:54,340
But that right so let's go ahead and see if what we've done so far is working out.

98
00:07:54,400 --> 00:07:58,880
We haven't done a whole lot just kind of lay down some boilerplate code.

99
00:07:58,880 --> 00:08:04,900
And I think the one thing that will show is we should be able to still see our channels when we when

100
00:08:04,900 --> 00:08:12,910
we log in and say be at the top come to three four five six.

101
00:08:13,140 --> 00:08:20,070
So you'll notice that when we logged in from here without having already been logged in the channels

102
00:08:20,070 --> 00:08:21,060
aren't showing up.

103
00:08:21,090 --> 00:08:28,290
And the reason for that is that the channel view doesn't it doesn't know that the channels have been

104
00:08:28,290 --> 00:08:30,810
loaded and so it doesn't know to reload the data.

105
00:08:30,810 --> 00:08:35,460
So what we're going to do is we're going to create a notification that will be called from Message Service

106
00:08:35,460 --> 00:08:42,270
that swift that will let the channel controller know hey I just pulled down all these channels so go

107
00:08:42,270 --> 00:08:45,730
ahead and up load or reload your table and displayed them all right.

108
00:08:45,750 --> 00:08:49,550
So that's going to be right here in place of this print statement.

109
00:08:49,570 --> 00:08:57,090
We're going to need to create and unification so I'm going to open up Constans where you are here.

110
00:08:57,560 --> 00:08:58,280
OK.

111
00:08:58,350 --> 00:09:09,630
And I'm just going to copy this notification name pasted and we're going to say channels who did and

112
00:09:09,630 --> 00:09:13,710
then changed the name to channels loaded.

113
00:09:13,710 --> 00:09:16,530
All right so now we created the notification name.

114
00:09:16,530 --> 00:09:20,340
Now let's go ahead and create the post and the observers for it.

115
00:09:20,340 --> 00:09:26,550
So the Post is going to be in message service just both here through the same notification center.

116
00:09:26,580 --> 00:09:30,370
That default post we want this one right here.

117
00:09:30,390 --> 00:09:37,260
The name is no to underscore channels loaded the object is no.

118
00:09:37,380 --> 00:09:44,250
OK so after all of the channels have been retrieved from the API we're going to shut off this notification

119
00:09:44,470 --> 00:09:46,560
that that that's ready to go.

120
00:09:46,650 --> 00:09:52,160
All right and then in channel Visi I see them here we need to add an observer.

121
00:09:52,360 --> 00:09:54,660
So I'm going to say notification.

122
00:09:54,750 --> 00:09:58,440
Well let's create our selector function first.

123
00:09:58,470 --> 00:10:01,120
I think that's the better way to go about this.

124
00:10:01,120 --> 00:10:01,370
All right.

125
00:10:01,380 --> 00:10:12,340
So we got at Objective-C funk and call this channel's loaded and the same as this guy right up here.

126
00:10:12,400 --> 00:10:22,530
I say underscore a note of then notification and what we're going to do here is we're just going to

127
00:10:22,920 --> 00:10:27,340
reload our tape abuse data Serenity tape of you reload data.

128
00:10:27,490 --> 00:10:27,900
All right.

129
00:10:27,930 --> 00:10:37,890
Now let's create our observer and say notification center that default does and Observer.

130
00:10:38,070 --> 00:10:41,550
The observer is self the selector is.

131
00:10:41,550 --> 00:10:52,020
Hash tag selector and then channel the C that channels loaded.

132
00:10:52,140 --> 00:10:59,860
That's our function and the name is known to underscore channels loaded.

133
00:11:00,020 --> 00:11:06,550
The object is now going to say that and clean up a little bit more spacing.

134
00:11:06,690 --> 00:11:11,420
And just to go over the flow one more time and Mrs. Server start Swift after the channels are retrieved

135
00:11:11,430 --> 00:11:20,700
we shoot this notification channel Visi observes that notification and calls this channel right here

136
00:11:20,700 --> 00:11:26,580
channel is loaded which simply reloads the table view data.

137
00:11:26,580 --> 00:11:34,100
All right we're going to save that and run it on Friday so we need to test it from a fresh logons when

138
00:11:34,100 --> 00:11:37,790
I log out and then log in and cygnets.

139
00:11:38,280 --> 00:11:39,390
Dot com.

140
00:11:39,390 --> 00:11:41,150
One two three four five six.

141
00:11:41,190 --> 00:11:42,320
Log in.

142
00:11:42,690 --> 00:11:43,430
And there we go.

143
00:11:43,440 --> 00:11:48,910
So now the channels are showing up from our very own logon.

144
00:11:48,930 --> 00:11:49,880
All right.

145
00:11:49,890 --> 00:12:02,130
That looks looks good already that next thing that I want to do is actually have this chat view update

146
00:12:02,160 --> 00:12:05,640
some of its information based on which channel we select.

147
00:12:05,640 --> 00:12:06,100
All right.

148
00:12:06,300 --> 00:12:07,930
So let's go ahead and do that.

149
00:12:07,950 --> 00:12:09,720
We're going to use the notification for that as well.

150
00:12:09,720 --> 00:12:17,700
So I'm going to Constans and we're going to create a new notification name and just copy that and say

151
00:12:17,700 --> 00:12:26,430
you know to underscore channel selected came over here on this channel so elected.

152
00:12:27,120 --> 00:12:28,480
I'm going to save that.

153
00:12:28,860 --> 00:12:34,610
And we're we're going to be doing our work here is mostly in well I guess in both Channel B C and Chad.

154
00:12:34,810 --> 00:12:42,750
DC First off in channel Visi we're going to create the function or we're going to utilize the built

155
00:12:42,750 --> 00:12:47,320
in table view method for selecting and table view items.

156
00:12:47,320 --> 00:12:54,280
When to say did select and that will pull up our function that we need to select row at.

157
00:12:54,300 --> 00:12:54,530
OK.

158
00:12:54,540 --> 00:12:56,810
So what are we doing when we select a row.

159
00:12:56,970 --> 00:13:04,470
We are going to save the selected channel into our message service variable's selected channel and then

160
00:13:04,470 --> 00:13:12,440
we're going to notify the chapt VC that we have selected a channel and then we want to dismiss We want

161
00:13:12,440 --> 00:13:16,330
to close the menu have the chad slide back over.

162
00:13:16,340 --> 00:13:16,830
Right.

163
00:13:16,970 --> 00:13:24,440
So pretty simple we're going to say left channel equal when I say message service instance dot channels

164
00:13:24,890 --> 00:13:28,770
and we're going to grab the channel that corresponds to the one that we select.

165
00:13:28,820 --> 00:13:31,610
So index paff dot row.

166
00:13:31,970 --> 00:13:32,610
OK.

167
00:13:32,750 --> 00:13:39,130
So now we have our selected channel and we're going to set that equal to the message service that instance

168
00:13:39,130 --> 00:13:42,500
does select the channel is equal to that channel.

169
00:13:43,160 --> 00:13:50,480
And then we're going to say notification center we're going to shut off our notification that the channel

170
00:13:50,480 --> 00:13:52,470
has been selected that we just created.

171
00:13:52,500 --> 00:13:56,630
So the default post and this is the one we want.

172
00:13:56,630 --> 00:14:04,430
We're going to post a notification with the name notify channel selected object is new.

173
00:14:05,330 --> 00:14:10,430
All right so again we create the channel based on which one we select set that equal to the selected

174
00:14:10,430 --> 00:14:15,380
channel and you'd offer notification that we have indeed selected a channel.

175
00:14:15,380 --> 00:14:15,590
All right.

176
00:14:15,600 --> 00:14:21,270
And now what we're going to do is make the menu slide back in place.

177
00:14:21,410 --> 00:14:23,060
And that's also pretty easy.

178
00:14:23,060 --> 00:14:33,540
All we got to do is say self reveal View Controller and then say that reveal toggle.

179
00:14:33,560 --> 00:14:38,700
And we want this one here animated with a bowl saurians he revealed toggle kind of made it true.

180
00:14:39,110 --> 00:14:44,690
So we're just saying we're just accessing our revealed view controller and we're calling misfunction

181
00:14:44,720 --> 00:14:50,700
revealed toggle which is what we use to slide back and forth the menu.

182
00:14:50,720 --> 00:14:51,680
All right.

183
00:14:51,870 --> 00:14:55,290
Sue looks good and save that.

184
00:14:55,700 --> 00:14:58,480
And let's let's test that.

185
00:14:58,550 --> 00:15:00,070
Just that so far.

186
00:15:00,680 --> 00:15:05,480
All right so all we're doing here is we're just testing that when we select a.

187
00:15:05,810 --> 00:15:09,320
Yeah there we go that when we select an item the menu closes.

188
00:15:09,710 --> 00:15:14,420
All right so let's close stuff then jump in a chat B.

189
00:15:14,540 --> 00:15:20,900
So now we are shooting out a notification that we have selected a channel so we need to add an observer

190
00:15:20,900 --> 00:15:30,680
for it so that we can listen for that notification and say notification center default and Observer

191
00:15:31,430 --> 00:15:36,920
and the observer is olf the selector Let's go ahead and create that.

192
00:15:36,980 --> 00:15:48,570
So here we're going to say at Objective-C and we say phunk channel selected and underscore and notice.

193
00:15:49,160 --> 00:15:53,200
And that's of type notification.

194
00:15:53,230 --> 00:15:53,930
There you go.

195
00:15:54,170 --> 00:15:59,360
And then we're going to do something will work with that in just a second but our selector we're going

196
00:15:59,360 --> 00:16:03,720
to say so look for his chat.

197
00:16:03,920 --> 00:16:11,390
BBC datt channel selected the name of the notification that we are listening for is note with Channel

198
00:16:11,390 --> 00:16:14,830
selected and the object is new.

199
00:16:15,040 --> 00:16:17,600
All right so there we're going to save that.

200
00:16:17,600 --> 00:16:21,330
And what do we want to have happen when we select the channel.

201
00:16:21,500 --> 00:16:29,140
What we're going to call a function called update with channel.

202
00:16:29,840 --> 00:16:34,040
And the reason we have some of these you may be wondering like why don't we just put whatever we're

203
00:16:34,040 --> 00:16:35,910
going to put right here here.

204
00:16:35,980 --> 00:16:40,040
The reason for that is that we're going to be calling some of these functions in multiple places.

205
00:16:40,040 --> 00:16:42,140
So I'm just pretty optimizing for that.

206
00:16:42,140 --> 00:16:42,930
All right.

207
00:16:43,370 --> 00:16:46,020
OK so here I'm going to say update with channel.

208
00:16:46,520 --> 00:16:49,520
And what are we going to do here in the update with Channel.

209
00:16:49,520 --> 00:16:54,920
Well right now we don't have a whole tend to do all that I really want to do is make it so that when

210
00:16:55,280 --> 00:17:02,720
we do that when we select a channel that we update this right here this label to be the name of the

211
00:17:02,720 --> 00:17:03,450
channel.

212
00:17:03,710 --> 00:17:07,430
So we're going to do is we're going to say let channel name.

213
00:17:07,430 --> 00:17:13,030
We have to unwrap our channel name from the message service that instant starts flipping channels we're

214
00:17:13,030 --> 00:17:17,420
going to say Message Service that instance the selected channel

215
00:17:20,360 --> 00:17:27,230
datt channel title and then we're going to use our coalescing Neal.

216
00:17:27,440 --> 00:17:37,310
So if it can't find a non optional string then just set it equal to an empty string and they're going

217
00:17:37,310 --> 00:17:41,610
to say channel name label.

218
00:17:41,840 --> 00:17:55,190
That text is people too we're going to do our hash tag and then string interpolate channel name.

219
00:17:55,820 --> 00:17:56,800
All right.

220
00:17:56,830 --> 00:17:58,280
So to save that.

221
00:17:58,470 --> 00:18:02,730
And let's run that and see if that works.

222
00:18:02,730 --> 00:18:03,040
All right.

223
00:18:03,060 --> 00:18:04,430
So let's check this out.

224
00:18:04,430 --> 00:18:07,990
Going to click on general dismisses and hey there we go.

225
00:18:08,370 --> 00:18:11,950
Update our channel label right here.

226
00:18:12,200 --> 00:18:19,360
So right now and now we're really starting to look like a chat at so good.

227
00:18:19,470 --> 00:18:30,050
It had a new channel and I wouldn't call this water cooler create the channel boom it sets up we can

228
00:18:30,050 --> 00:18:31,090
add that.

229
00:18:31,370 --> 00:18:32,120
All right.

230
00:18:32,130 --> 00:18:33,500
We're looking really good.

231
00:18:33,500 --> 00:18:38,960
All right so now this was kind of just a whole lot of logic in setting up some functions that were going

232
00:18:38,960 --> 00:18:41,970
to be using here in the next several lessons.

233
00:18:42,110 --> 00:18:43,970
But still some still some pretty cool stuff.

234
00:18:43,960 --> 00:18:51,200
We're updating our using notifications to let different controllers know what's going on and stuff.

235
00:18:51,200 --> 00:18:52,500
So some pretty fun stuff.

236
00:18:52,530 --> 00:18:59,090
And know I'm just going to finish up by adding and committing our changes and then I'll see you guys

237
00:18:59,090 --> 00:19:00,400
all in the next one.
