1
00:00:08,160 --> 00:00:10,390
Everybody this is Caleb with deps slopestyle.

2
00:00:10,550 --> 00:00:15,440
And in this video what we're going to do is carry on where we left off last time which was creating

3
00:00:15,440 --> 00:00:16,260
our group.

4
00:00:16,370 --> 00:00:20,010
We can add users now into a chosen user array.

5
00:00:20,030 --> 00:00:25,190
The Done button appears we're going to set up what happens when you tap on that done button today.

6
00:00:25,400 --> 00:00:28,600
So go ahead and open that XCode project.

7
00:00:28,640 --> 00:00:32,120
And we need to think about where our app currently stands.

8
00:00:32,120 --> 00:00:33,990
Let's pull it up and let's take a look.

9
00:00:34,220 --> 00:00:38,820
If we go in and I search for some users I add in maybe three friends.

10
00:00:38,900 --> 00:00:44,990
And if I want to click done if I want to create this group I'm going to upload it to firebase but as

11
00:00:44,990 --> 00:00:49,370
it currently stands we only have the emails of the users.

12
00:00:49,370 --> 00:00:56,690
And if we're going to be getting their messages later we're going to need to basically pull down their

13
00:00:56,900 --> 00:01:02,170
user ID because if you remember in firebase Let's pull that up.

14
00:01:02,420 --> 00:01:06,030
Every message has content and a Sender ID.

15
00:01:06,140 --> 00:01:09,730
Now every account has an ID and an e-mail.

16
00:01:09,770 --> 00:01:15,820
But right now as it currently stands we have sort of separated our data into only having an email.

17
00:01:15,950 --> 00:01:21,200
So we need to write a function in our data service that's going to allow us to convert an email into

18
00:01:21,200 --> 00:01:23,150
the appropriate ID from firebase.

19
00:01:23,150 --> 00:01:30,140
So let's go into our data service and we're going to write a function called get IDs for usernames and

20
00:01:30,140 --> 00:01:35,630
it's going to allow us to pass an array of user names and get all of the associated IDs that go with

21
00:01:35,630 --> 00:01:36,310
them.

22
00:01:36,320 --> 00:01:39,320
So go ahead and type phunk get IDs.

23
00:01:39,380 --> 00:01:50,090
Only one t IDs for usernames and go ahead and use an internal parameter there of usernames colon and

24
00:01:50,090 --> 00:01:53,040
then the user names of course all come in as strings.

25
00:01:53,060 --> 00:01:59,800
So that's fine we can leave it at that but we're going to need to return an array of strings as well.

26
00:01:59,840 --> 00:02:02,930
So we're going to use a handler for that because we're using firebase.

27
00:02:02,960 --> 00:02:10,570
So make it an escaping handler and of course we're going to use a function that returns an empty function

28
00:02:11,180 --> 00:02:16,350
and the value that we're going to pass into the handler is going to be called Let's just call it you

29
00:02:16,440 --> 00:02:17,840
ID array.

30
00:02:18,170 --> 00:02:21,030
And like I said it's an array of type string.

31
00:02:21,290 --> 00:02:27,260
So what we're going to do now is observe our users reference we're going to observe a single value meaning

32
00:02:27,260 --> 00:02:31,850
it's just going to cycle through one time it's going to return all the IDs for the user names that we

33
00:02:31,850 --> 00:02:33,260
pass in.

34
00:02:33,260 --> 00:02:34,560
So here we go.

35
00:02:34,580 --> 00:02:37,180
Let's go ahead and let's call refusers.

36
00:02:37,280 --> 00:02:40,180
We're going to observe a single event

37
00:02:43,230 --> 00:02:48,130
and of course the data type is value and we're going to create a snapshot.

38
00:02:48,150 --> 00:02:53,730
Now this is going to be user snapshot and we're basically doing this very similarly to how we've been

39
00:02:53,730 --> 00:03:00,100
doing it in the past with guard let users snapshot equals user snapshot.

40
00:03:00,120 --> 00:03:07,670
Children are not all objects and we're going to cast it operationally as an array of data snapshot.

41
00:03:07,920 --> 00:03:08,740
Very cool.

42
00:03:08,760 --> 00:03:13,260
Otherwise we're just going to return.

43
00:03:13,870 --> 00:03:14,590
Very cool.

44
00:03:14,760 --> 00:03:16,420
So that's great.

45
00:03:16,440 --> 00:03:21,630
Now assuming we get a value assuming we have users which if the button is showing that means we have

46
00:03:21,630 --> 00:03:24,800
added users so we definitely will get users back.

47
00:03:24,930 --> 00:03:27,720
Not to mention that we're pulling this from firebase anyway Sorry.

48
00:03:27,720 --> 00:03:30,600
So it's going to pull all the users from firebase.

49
00:03:30,690 --> 00:03:35,400
But you know how we've been creating an array to hold everything doing a for loop pending things to

50
00:03:35,400 --> 00:03:39,360
the for loop and then passing that array back to the handler.

51
00:03:39,360 --> 00:03:40,960
We're going to do the same thing here.

52
00:03:41,040 --> 00:03:43,890
We're going to create an array called Idea array.

53
00:03:43,890 --> 00:03:49,060
So type of var ID array and it's just going to be an empty array.

54
00:03:49,270 --> 00:03:52,620
I know Nazzaro an empty array an array of type string.

55
00:03:52,620 --> 00:03:53,830
My mistake.

56
00:03:53,820 --> 00:03:59,850
So now what we're going to do is we're going to cycle through every user in the user snapshot and we're

57
00:03:59,850 --> 00:04:03,040
going to compare it against the user names array.

58
00:04:03,060 --> 00:04:08,130
So go ahead and type for user in user snapshot.

59
00:04:08,130 --> 00:04:12,100
So now we're cycling through the user snapshot we're pulling out every user.

60
00:04:12,210 --> 00:04:16,890
And what we're going to do now is we're going to check to see if the user names array contains a certain

61
00:04:16,890 --> 00:04:17,730
user's e-mail.

62
00:04:17,730 --> 00:04:22,090
So go ahead and type if usernames dock contains.

63
00:04:22,260 --> 00:04:24,900
And now we need to get the e-mail value for this user.

64
00:04:24,930 --> 00:04:32,460
If you remember we did that this way right here user child snapshot for path value as string.

65
00:04:32,460 --> 00:04:37,110
Now we're going to go ahead and we're going to copy this and we're going to paste it down here and we're

66
00:04:37,110 --> 00:04:43,680
going to check if the user name user usernames array contains the e-mail from the user.

67
00:04:43,940 --> 00:04:50,340
If it contains it what we're going to do is we're going to go ahead and append it to the idea.

68
00:04:50,370 --> 00:04:52,470
But we're not going to append the e-mail.

69
00:04:52,470 --> 00:04:57,270
We're going to append the key for this user because that's their unique ID.

70
00:04:57,270 --> 00:05:02,420
So go ahead and type ID array append user Akaki.

71
00:05:02,740 --> 00:05:03,660
OK.

72
00:05:03,720 --> 00:05:07,290
So that is how we're going to fill up our ID array.

73
00:05:07,350 --> 00:05:12,090
Then at the end of the for loop we're going to go ahead and call our handler and we're going to give

74
00:05:12,090 --> 00:05:14,780
it an array of strings which is ID array.

75
00:05:14,910 --> 00:05:15,790
Pretty cool.

76
00:05:16,080 --> 00:05:19,690
That's all we need to do to get all the IDs for a certain user.

77
00:05:19,890 --> 00:05:23,780
And that's what we're going to do to download all the ideas that we need.

78
00:05:23,970 --> 00:05:29,240
And then after we get all of the ideas we're going to pass them up to firebase to create our group.

79
00:05:29,250 --> 00:05:32,790
So now we need to write the function to create a group.

80
00:05:32,880 --> 00:05:38,220
Now we need to think about a group if we go into our simulator here a group has three things a title

81
00:05:38,790 --> 00:05:42,910
a description and it's also going to have a list of users that are in it.

82
00:05:42,930 --> 00:05:43,440
OK.

83
00:05:43,740 --> 00:05:50,070
So we're going to create a function to create a group go ahead and type phunk create group and we're

84
00:05:50,070 --> 00:05:51,690
going to create a group with a title.

85
00:05:51,720 --> 00:05:59,820
Of course an internal parameter of title we're going to create a group with a description and description

86
00:05:59,850 --> 00:06:04,970
so description I don't think that's spelled right.

87
00:06:04,970 --> 00:06:07,520
No description.

88
00:06:07,520 --> 00:06:08,140
There we go.

89
00:06:08,300 --> 00:06:16,490
So with title and description and we also need to create a group for a set of user IDs and we'll just

90
00:06:16,490 --> 00:06:19,590
say IDs that's going to be an array of string.

91
00:06:19,850 --> 00:06:20,620
OK.

92
00:06:20,690 --> 00:06:26,720
Now that's great and all but we're going to also need a handler to basically let us know when the send

93
00:06:26,720 --> 00:06:27,640
is complete.

94
00:06:28,840 --> 00:06:32,740
We're also going to need a handler to determine when the group is officially created.

95
00:06:32,740 --> 00:06:34,610
So go ahead and create a handler.

96
00:06:34,990 --> 00:06:39,880
And it's going to be escaping just like we've been doing.

97
00:06:40,050 --> 00:06:41,650
Should be an expert at this by.

98
00:06:41,660 --> 00:06:42,910
By the end.

99
00:06:42,910 --> 00:06:45,920
Go ahead and we're just going to use a boolean to check.

100
00:06:46,030 --> 00:06:50,170
We'll just say a group created of type Boolean.

101
00:06:50,170 --> 00:06:55,470
And that's what we'll pass back to tell wherever we call this that we're done creating a group and we'll

102
00:06:55,480 --> 00:06:58,550
just use that to dismiss the view controller.

103
00:06:58,660 --> 00:07:00,100
But OK this looks great.

104
00:07:00,190 --> 00:07:01,780
We're ready to create a group now.

105
00:07:01,930 --> 00:07:04,540
We're going to use our groups reference.

106
00:07:04,570 --> 00:07:10,690
We have not used that yet but it's up here and it takes us to the firebase database child called Groups

107
00:07:10,780 --> 00:07:12,990
or at least it will create it if it's not there.

108
00:07:13,060 --> 00:07:15,780
So go ahead and call ref groups.

109
00:07:15,860 --> 00:07:22,300
Ref groups and we're going to go ahead and just every time we create a group it's going to be created

110
00:07:22,300 --> 00:07:25,370
with a random ID because it doesn't really matter.

111
00:07:25,690 --> 00:07:30,150
The idea of the group because the group can be random it's not associated to one particular person.

112
00:07:30,220 --> 00:07:36,940
So go ahead and type child by Auto-ID and then inside that child what we're going to do is update the

113
00:07:36,940 --> 00:07:38,200
child values.

114
00:07:38,200 --> 00:07:41,280
Now it asks for a dictionary and we're going to give it one.

115
00:07:41,290 --> 00:07:48,040
So for this group it's going to need a title and the title value will come from the title that we pass

116
00:07:48,040 --> 00:07:48,800
in.

117
00:07:48,970 --> 00:07:54,250
It's going to need a description and we're going to get that from the description we pass in.

118
00:07:54,880 --> 00:07:57,820
It's also going to need a member's list.

119
00:07:57,820 --> 00:08:00,280
And so what we're going to do is we're going to pass it.

120
00:08:00,280 --> 00:08:01,430
Members.

121
00:08:01,510 --> 00:08:02,090
OK.

122
00:08:02,440 --> 00:08:04,470
And it's going to get that from IDS.

123
00:08:04,480 --> 00:08:07,110
That's the array of strings that we pass in.

124
00:08:07,450 --> 00:08:08,720
And that's what we're going to pass in.

125
00:08:08,740 --> 00:08:10,910
It's not easy it's really easy actually.

126
00:08:10,910 --> 00:08:12,260
So that's great.

127
00:08:12,370 --> 00:08:17,050
And then once that is done we're going to go ahead and just call our handler and we're going to pass

128
00:08:17,050 --> 00:08:21,090
it through OK because our group was then successfully created.

129
00:08:21,400 --> 00:08:26,690
Now I could be using a completion handler here and doing some error handling but really the only errors

130
00:08:26,710 --> 00:08:32,460
that we're going to run into are errors of Internet connectivity so I'm just going to assume you have

131
00:08:32,470 --> 00:08:34,310
good enough Internet that it's not going to be a problem.

132
00:08:34,420 --> 00:08:40,720
If you want to you can go ahead and add the completion handler and you can say if it's successful handler

133
00:08:40,720 --> 00:08:41,530
returns true.

134
00:08:41,530 --> 00:08:44,050
Otherwise return false and then print the error.

135
00:08:44,200 --> 00:08:45,060
That's up to you.

136
00:08:45,070 --> 00:08:48,730
I'm not going to for the extent and purposes of this video.

137
00:08:48,910 --> 00:08:55,740
But for now let's go back to create groups Fisi and let's set up our done button to create a group.

138
00:08:55,850 --> 00:09:00,970
OK let's go back up and we have an I.B. action here for that done button.

139
00:09:01,300 --> 00:09:06,200
Now we need to think of a few things we need to have some conditions met before we can press done.

140
00:09:06,240 --> 00:09:12,630
And before we can send our group up to firebase and basically let's let's take a look.

141
00:09:12,640 --> 00:09:19,580
So obviously we knew the title we needed description and we need some members right.

142
00:09:19,690 --> 00:09:21,040
Can't just add it without any members.

143
00:09:21,040 --> 00:09:26,380
But if you remember properly the done button uncheck everybody it's hidden.

144
00:09:26,380 --> 00:09:32,110
If there's no one added so we can't really do that unless it's added anyway so let's just leave it.

145
00:09:32,230 --> 00:09:37,940
We don't need to worry about this or the users because the button is hidden and shown based on that.

146
00:09:38,140 --> 00:09:42,280
But we do need to monitor the text to make sure that there is a title and a description.

147
00:09:42,520 --> 00:09:49,600
So go ahead and we're going to just check if a group is a group.

148
00:09:49,600 --> 00:09:58,990
Maybe it's just title your title textfield text is not equal to an empty string and description textfield

149
00:09:59,740 --> 00:10:03,160
text is not equal to an empty string.

150
00:10:03,160 --> 00:10:08,050
Now we need to do is we need to download all those user ID is because remember we only have emails we

151
00:10:08,050 --> 00:10:16,120
don't have user IDs so let's go ahead and let's do that by calling data service instance this instance

152
00:10:16,150 --> 00:10:20,840
not in that instance don't get ID's for user names.

153
00:10:21,130 --> 00:10:25,360
And where are we going to get those usernames chosen user array.

154
00:10:25,390 --> 00:10:25,660
Right.

155
00:10:25,660 --> 00:10:29,740
Because we've already added them all right here we have an array of chosen users.

156
00:10:29,740 --> 00:10:33,020
Now the handler returns an array of string.

157
00:10:33,220 --> 00:10:37,350
And if we think about that that's just an array of all the IDs that we got.

158
00:10:37,350 --> 00:10:40,360
So let's just say IDs array.

159
00:10:40,750 --> 00:10:42,760
And of course that gets returned here.

160
00:10:42,760 --> 00:10:48,820
Now we need to think about this though at this moment we have Marty a great Scott market Deb slopes

161
00:10:48,880 --> 00:10:54,940
Harvey Dent but if you remember the account I'm logged into is Batman at Gotham dot net.

162
00:10:54,970 --> 00:10:57,070
So by default that's not going to show up.

163
00:10:57,070 --> 00:10:59,130
I won't even be in my own group.

164
00:10:59,170 --> 00:11:06,130
So what I need to do is actually add myself to this array and to do that I'm just going to create a

165
00:11:06,130 --> 00:11:14,710
temporary variable that's just going to hold the ideas array and I'll call it var user IDs equals IDs

166
00:11:14,710 --> 00:11:15,650
array.

167
00:11:15,760 --> 00:11:22,260
I'm going to go ahead and call user IDs a pen and I'm going to add myself so off.

168
00:11:22,360 --> 00:11:24,510
Oops sorry off.

169
00:11:24,990 --> 00:11:25,740
Auth.

170
00:11:29,450 --> 00:11:31,390
do I not have oh great groups.

171
00:11:31,410 --> 00:11:33,210
VC does not have firebase.

172
00:11:33,210 --> 00:11:34,350
How funny is that.

173
00:11:34,350 --> 00:11:35,840
All right ad firebase.

174
00:11:35,880 --> 00:11:46,770
And now go back to user IDs that append auth dot off current user and we're going to pass in the ID

175
00:11:46,770 --> 00:11:47,720
not the email.

176
00:11:47,970 --> 00:11:50,310
OK and if I build this you'll see it gets an error.

177
00:11:50,370 --> 00:11:52,800
We have to force unwrap it just like so.

178
00:11:53,160 --> 00:11:58,470
And now user IDs has all four of my for are three of my friends and myself.

179
00:11:58,470 --> 00:11:59,670
So that's awesome.

180
00:11:59,760 --> 00:12:04,020
But now I need to create the group on firebase because I just press the done button.

181
00:12:04,050 --> 00:12:14,760
We just created that function so called data service instance WIPs instance create group with Title.

182
00:12:14,760 --> 00:12:22,500
We're going to take title textfield text and description is from description textfield text for user

183
00:12:22,500 --> 00:12:23,590
IDs.

184
00:12:23,640 --> 00:12:29,900
We'll pass it in user IDs that's the one we just pulled down from firebase and added ourselves to.

185
00:12:30,000 --> 00:12:32,950
And now the handler is going to be for.

186
00:12:33,480 --> 00:12:39,360
Let's just say group created capitalized this group created.

187
00:12:39,360 --> 00:12:39,760
All righty.

188
00:12:39,780 --> 00:12:41,790
So very cool.

189
00:12:41,940 --> 00:12:50,200
If the group is created We're going to go ahead and call self dismiss true and then Knill.

190
00:12:50,230 --> 00:12:52,530
OK so we're just dismissing the view controller.

191
00:12:52,530 --> 00:13:00,630
If the group has been created successfully else we're going to go ahead and just print group could not

192
00:13:00,660 --> 00:13:02,380
be created.

193
00:13:02,430 --> 00:13:04,260
Please try again.

194
00:13:04,260 --> 00:13:10,080
And of course you could always set this so that it shows a UI alert controller that's up to you.

195
00:13:10,200 --> 00:13:16,520
But let's go ahead and let's try this let's try to add our group and let's see how we do build and run.

196
00:13:16,560 --> 00:13:18,310
We're getting some errors here.

197
00:13:18,810 --> 00:13:19,580
Oh of course.

198
00:13:19,590 --> 00:13:19,880
Sorry.

199
00:13:19,880 --> 00:13:25,290
Since we're in a closure we need to hold a reference to self or just tell it that it's coming from this

200
00:13:25,290 --> 00:13:26,110
view controller.

201
00:13:26,280 --> 00:13:29,430
So add self-deceit title textfield text et cetera.

202
00:13:29,460 --> 00:13:31,200
And it looks like our build succeeded.

203
00:13:31,200 --> 00:13:32,420
This is great.

204
00:13:32,430 --> 00:13:33,690
Go into groups.

205
00:13:33,900 --> 00:13:35,460
Tap the plus button.

206
00:13:35,460 --> 00:13:39,470
Let's try to let's add some friends here Marty.

207
00:13:39,480 --> 00:13:41,790
Great Scott Harvey at dot com.

208
00:13:41,790 --> 00:13:46,880
Chuck by more and let's go ahead and let's give our group well let's try it.

209
00:13:46,920 --> 00:13:48,750
Can't do anything unless it has a title.

210
00:13:48,750 --> 00:13:51,480
Let's call this the nerd herd.

211
00:13:51,810 --> 00:13:55,800
The nerdiest nerds around.

212
00:13:55,800 --> 00:13:56,690
Nice.

213
00:13:56,700 --> 00:14:03,120
So now if I select done since I have added a title a description and I have selected people it should

214
00:14:03,150 --> 00:14:05,600
allow me to click done.

215
00:14:06,420 --> 00:14:06,830
All right.

216
00:14:06,870 --> 00:14:08,680
So it worked it dismissed.

217
00:14:08,790 --> 00:14:11,300
That means that our group was created successfully.

218
00:14:11,300 --> 00:14:14,220
Let's pull it firebase which I believe is hiding back here.

219
00:14:14,220 --> 00:14:14,880
And let's go look.

220
00:14:14,880 --> 00:14:15,570
OK.

221
00:14:15,570 --> 00:14:16,130
Amazing.

222
00:14:16,140 --> 00:14:20,850
We now have a groups reference and if we look inside has a random ID.

223
00:14:20,910 --> 00:14:21,590
Let's open it up.

224
00:14:21,590 --> 00:14:22,590
We have a description.

225
00:14:22,650 --> 00:14:27,630
The nerdiest nerds around the title Nerd Herd and the members take a look at this.

226
00:14:27,630 --> 00:14:28,950
This is amazing.

227
00:14:28,950 --> 00:14:36,180
We have four users three that I added and one that is myself's remember users find Batman at Gotham

228
00:14:37,230 --> 00:14:39,950
that is.

229
00:14:40,280 --> 00:14:40,860
There is.

230
00:14:40,860 --> 00:14:41,100
OK.

231
00:14:41,100 --> 00:14:45,210
So this ID C to D There I am c to d.

232
00:14:45,240 --> 00:14:48,030
The last one in the Iraq because I added myself last.

233
00:14:48,030 --> 00:14:49,200
Very very cool.

234
00:14:49,200 --> 00:14:55,500
So guys we can now successfully create groups in firebase we can upload them in the next video what

235
00:14:55,500 --> 00:15:00,960
we're going to do is we're going to set up our group cell so that in here whenever a group is added

236
00:15:01,080 --> 00:15:05,250
if we're added to to a group it'll show up in real time immediately.

237
00:15:05,250 --> 00:15:05,850
It's amazing.

238
00:15:05,850 --> 00:15:09,960
So in the next video we're going to write that function get our group showing up.

239
00:15:09,970 --> 00:15:13,820
And guys this is this is so cool our app is coming together.

240
00:15:13,860 --> 00:15:15,800
It's starting to look really nice.

241
00:15:15,810 --> 00:15:16,560
Nicely done.

242
00:15:16,560 --> 00:15:21,390
Let's go ahead and move on to the next video where we will set up that table view and pull down all

243
00:15:21,390 --> 00:15:22,020
of our group's.

