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