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