1
00:00:04,750 --> 00:00:06,370
Everyone will come back to class.

2
00:00:06,400 --> 00:00:08,800
Joining me here with slopes dot com.

3
00:00:08,880 --> 00:00:14,530
And in this lesson our final lesson we are going to do a number of things we are going to implement

4
00:00:14,530 --> 00:00:18,550
the likes feature and we're going to do some code cleanup and refactoring.

5
00:00:18,550 --> 00:00:19,110
All right.

6
00:00:19,330 --> 00:00:21,160
So let's get started.

7
00:00:21,160 --> 00:00:22,780
This is what our app currently looks like.

8
00:00:22,780 --> 00:00:28,570
We are able to filter on the different categories that we have here except for popular that doesn't

9
00:00:28,570 --> 00:00:30,420
actually do a thing for us right now.

10
00:00:30,640 --> 00:00:32,290
So it's actually work on that.

11
00:00:32,290 --> 00:00:32,540
All right.

12
00:00:32,560 --> 00:00:36,850
So here in our set listener we are filtering.

13
00:00:36,850 --> 00:00:41,520
We set an observer and to filter on category.

14
00:00:41,710 --> 00:00:47,770
But in the popular what we're going to do is we're going to we're going to have all of the thoughts

15
00:00:47,950 --> 00:00:52,400
but instead we're just going to order those by the number of likes.

16
00:00:52,420 --> 00:00:56,380
So what we're going to need to do is we're going to have to have it here and we're going to have two

17
00:00:56,380 --> 00:01:04,150
different observers depending on which of the four categories is selected if it's popular then we're

18
00:01:04,150 --> 00:01:06,520
going to have a different observer.

19
00:01:06,520 --> 00:01:06,920
All right.

20
00:01:07,000 --> 00:01:19,110
So we're going to say if selected category is equal to the thought category that popular raw value.

21
00:01:19,330 --> 00:01:25,480
So if it is popular then we're going to go ahead and copy this because we're going to need a slightly

22
00:01:25,600 --> 00:01:26,730
different one.

23
00:01:26,770 --> 00:01:37,020
When you copy that and paste it here when I close the bottom pain but instead of sorting by category

24
00:01:37,850 --> 00:01:45,560
we're going to do is we're going to order by not time stamp but numb likes.

25
00:01:45,630 --> 00:01:46,740
There we go.

26
00:01:46,830 --> 00:01:53,680
If we select category is anything else then we are going to stick with the one that we had before.

27
00:01:54,000 --> 00:01:54,330
OK.

28
00:01:54,360 --> 00:01:58,860
So I'm going to cut and paste it instead of our else statement.

29
00:01:58,900 --> 00:01:59,950
All right.

30
00:02:01,410 --> 00:02:07,470
I'm going to slip all this into Control II and that formats your code just in case you did it.

31
00:02:07,500 --> 00:02:09,810
And let's go and test this out and run it.

32
00:02:09,810 --> 00:02:10,280
All right.

33
00:02:10,290 --> 00:02:11,680
So let's see here.

34
00:02:11,760 --> 00:02:12,480
Serious.

35
00:02:12,510 --> 00:02:15,030
Those all still work and then click on popular.

36
00:02:15,210 --> 00:02:16,200
And there we go.

37
00:02:16,200 --> 00:02:20,880
All right so now we are displaying everything and ordering it by the number of likes right now we only

38
00:02:20,880 --> 00:02:26,150
have to posts that have any likes and those were manually set in the database.

39
00:02:26,160 --> 00:02:28,250
But it looks like it is working correctly.

40
00:02:29,340 --> 00:02:29,580
All right.

41
00:02:29,580 --> 00:02:30,560
Looking good.

42
00:02:30,900 --> 00:02:31,160
All right.

43
00:02:31,170 --> 00:02:33,690
So you may be looking at this and cringing.

44
00:02:33,690 --> 00:02:40,620
You know we have a lot of code here and we have some big chunks of it that are being repeated and as

45
00:02:40,620 --> 00:02:44,590
part of the dry principle we do not want to repeat ourselves right.

46
00:02:44,910 --> 00:02:51,030
So looking at this I'm not really happy with the all of this parsing data that is happening here inside

47
00:02:51,060 --> 00:02:52,380
of the controller.

48
00:02:52,440 --> 00:02:56,570
I think this could be done inside of our thought model.

49
00:02:56,580 --> 00:02:57,180
OK.

50
00:02:57,360 --> 00:02:59,830
So let's go ahead and in thought.

51
00:02:59,840 --> 00:03:04,890
Thus if we were going to create ourselves a class function and this means that this class can be.

52
00:03:04,890 --> 00:03:11,690
Or this function can be called from the class without actually being from an instance of the class is

53
00:03:11,710 --> 00:03:14,190
going to say class funk.

54
00:03:14,820 --> 00:03:19,130
And we're going to call this parse data.

55
00:03:19,950 --> 00:03:28,710
And we are going to need to import firebase here because we need to take into it a snapshot.

56
00:03:28,920 --> 00:03:29,630
OK.

57
00:03:31,270 --> 00:03:33,990
And this is a type of query snapshot.

58
00:03:34,260 --> 00:03:34,750
Okay.

59
00:03:34,970 --> 00:03:39,880
And then we are going to return an array of thought.

60
00:03:40,270 --> 00:03:41,040
Okay.

61
00:03:41,390 --> 00:03:50,120
And so here we're going to say our thoughts is equal to an array of thought.

62
00:03:50,650 --> 00:03:52,340
We're going to initialize it.

63
00:03:52,550 --> 00:03:57,840
And then down here we are going to return it because it is going to say return thoughts.

64
00:03:58,280 --> 00:04:00,240
All right that'll get rid of that error.

65
00:04:00,560 --> 00:04:04,090
And then going back to our main Visi does Swift.

66
00:04:04,280 --> 00:04:11,210
I'm just going to go ahead and copy or actually cut this whole section here from the guard let snap

67
00:04:11,390 --> 00:04:13,630
all the way down to the end of the for loop.

68
00:04:13,640 --> 00:04:20,390
So Mike cut that and come in here to our thought and piece that right here.

69
00:04:21,020 --> 00:04:27,760
And so now we're going to say Garlett snap snap shot else return and then we'll return thoughts and

70
00:04:27,770 --> 00:04:32,990
empty array of thoughts all right and that looks good.

71
00:04:32,990 --> 00:04:35,980
Let's see here initialiser conditional.

72
00:04:36,080 --> 00:04:38,070
So this should be optional right here.

73
00:04:38,140 --> 00:04:39,060
There we go.

74
00:04:39,650 --> 00:04:42,500
And then we'll get rid of this stuff right here.

75
00:04:42,630 --> 00:04:47,840
Start a new thought and that should actually be good to go.

76
00:04:48,050 --> 00:04:58,670
So then here in our main Visi and we're going to call thought are we going to say self-taught thoughts

77
00:05:00,050 --> 00:05:02,510
equal to thought

78
00:05:05,050 --> 00:05:12,530
that parse data and then we're past into it the snap shot.

79
00:05:12,550 --> 00:05:20,440
All right let's go ahead and do the same thing here on our other listener here.

80
00:05:20,730 --> 00:05:36,570
Right here we're going to delete this and say self that fots is equal to the fact that parsed data and

81
00:05:36,570 --> 00:05:46,140
we passing into the snapshot this try that and run it Kate looks like it is still working as expected.

82
00:05:46,140 --> 00:05:46,960
Very good.

83
00:05:47,350 --> 00:05:50,740
And this looks so much cleaner now as well.

84
00:05:50,740 --> 00:05:51,700
All right so where to go.

85
00:05:53,260 --> 00:05:58,950
All right so I think the only thing that we have left to do is our likes feature here.

86
00:05:59,050 --> 00:06:03,070
So when we click on one of these we are adding a light to it.

87
00:06:03,070 --> 00:06:03,450
All right.

88
00:06:03,490 --> 00:06:05,850
So let's go ahead and work on that.

89
00:06:05,920 --> 00:06:12,040
What we need to do is here in our thought cell does Swift we need to add a gesture recognizer to that

90
00:06:12,070 --> 00:06:12,800
image.

91
00:06:13,030 --> 00:06:22,110
OK so here in the Awake from NIB we are going to say let tap equal you I tap gesture recognizer and

92
00:06:22,120 --> 00:06:23,340
we're going to initialize it.

93
00:06:23,350 --> 00:06:25,530
We're going to select the second one here.

94
00:06:25,550 --> 00:06:32,380
The target is self and the action is going to be a function that we haven't created quite yet but it's

95
00:06:32,380 --> 00:06:39,470
going to be pounde selector and then will create that function right here.

96
00:06:39,700 --> 00:06:47,950
And we've got to say at BJC this is new in O.B. J.C. This is new and so for we have to expose the function

97
00:06:47,950 --> 00:06:54,520
to the Objective-C frameworks with the same objective see funk and I want to call this function like

98
00:06:54,670 --> 00:06:55,420
tapped.

99
00:06:55,630 --> 00:06:56,620
OK.

100
00:06:57,970 --> 00:07:03,500
And so back up here in our selector we can say like tapped.

101
00:07:03,700 --> 00:07:03,930
All right.

102
00:07:03,940 --> 00:07:12,720
And then we can say like likes image that add gesture gesture recognizer and the recognize that we are

103
00:07:12,730 --> 00:07:21,340
adding is tap and then we can say like image we need to tell the image that it should interact with

104
00:07:21,340 --> 00:07:22,240
user interaction.

105
00:07:22,240 --> 00:07:25,960
So is user interaction enabled.

106
00:07:26,320 --> 00:07:27,700
Yes it is true.

107
00:07:27,700 --> 00:07:33,250
All right so now when we tap on the image something we'll be able to happen here in the you like tap

108
00:07:33,310 --> 00:07:34,400
function.

109
00:07:34,420 --> 00:07:36,520
So what do we want to have happen.

110
00:07:36,790 --> 00:07:40,600
So let's check out our database here in our database.

111
00:07:40,610 --> 00:07:42,320
Come back here to date.

112
00:07:42,520 --> 00:07:45,070
We need a path to the document.

113
00:07:45,070 --> 00:07:45,470
OK.

114
00:07:45,610 --> 00:07:49,370
So up until now all of our paths have been just to the collection.

115
00:07:49,390 --> 00:07:50,320
Thoughts.

116
00:07:50,320 --> 00:07:53,480
Now we need a path to the document.

117
00:07:53,530 --> 00:07:54,150
All right.

118
00:07:54,390 --> 00:07:56,280
And so it's a very similar process.

119
00:07:56,300 --> 00:08:02,220
All we got to do is say fire looks like we need to import firebase.

120
00:08:02,590 --> 00:08:11,040
So import firebase and we're going to get a path to the document that corresponds to the one that we

121
00:08:11,040 --> 00:08:12,210
are clicking on.

122
00:08:12,730 --> 00:08:17,010
So to do that we are going to need a variable to reference it.

123
00:08:17,020 --> 00:08:26,210
So here in I'm going to see variables and I'm going to say private var thought this is going to be a

124
00:08:26,240 --> 00:08:27,810
type thought.

125
00:08:27,910 --> 00:08:28,490
OK.

126
00:08:28,780 --> 00:08:35,920
And then here in configure cell I'm going to say self that thought is equal to the thought that is passed

127
00:08:36,010 --> 00:08:38,140
into the function.

128
00:08:38,510 --> 00:08:47,710
Ok so now that we have access to the thought we're going to see a fire store dot fire store and we're

129
00:08:47,710 --> 00:08:55,930
going to say Dot collection path and that one is going to be thoughts.

130
00:08:55,940 --> 00:08:58,700
Ref.

131
00:08:58,910 --> 00:09:09,830
And then we burrow down further dot dot document document path and that one's going to be fought dot

132
00:09:10,190 --> 00:09:11,670
document ID.

133
00:09:11,780 --> 00:09:19,040
So now we have gone from collection and we've gone one layer deeper to the document and so visually

134
00:09:19,070 --> 00:09:23,690
what we've done is we've said we've found a reference to our thoughts collection and then we've gone

135
00:09:23,690 --> 00:09:26,270
down one layer deeper into the document.

136
00:09:26,720 --> 00:09:31,640
And so what we can do then is there's a couple of ways to update the data.

137
00:09:31,640 --> 00:09:36,970
I'll show you one way first and then the next we can say data set data.

138
00:09:37,090 --> 00:09:37,670
OK.

139
00:09:37,970 --> 00:09:40,460
And but we'll need the one with options.

140
00:09:40,580 --> 00:09:47,320
So sit data with options and see what we can do is we can that said data.

141
00:09:47,320 --> 00:09:53,130
I'm going to put this on a new line and what we do is we specify the field that we are updating.

142
00:09:53,420 --> 00:09:56,390
And we feel that we are updating is the number of likes.

143
00:09:56,540 --> 00:10:05,480
So this is going to be numb who likes and the value is the number of likes that it currently has thought

144
00:10:05,480 --> 00:10:07,820
that number likes plus 1.

145
00:10:07,960 --> 00:10:08,650
Okay.

146
00:10:08,850 --> 00:10:09,820
And then the options here.

147
00:10:09,820 --> 00:10:21,320
This is important because if we don't set the options to set options that merge merge and what is going

148
00:10:21,320 --> 00:10:25,580
to do is it's going to overwrite everything else inside of the document.

149
00:10:25,580 --> 00:10:31,810
So what we would end up with is a document with only likes and everything else would be blank.

150
00:10:31,870 --> 00:10:32,350
OK.

151
00:10:32,450 --> 00:10:38,870
So this is one way to update a field for a specific document using the data set data.

152
00:10:38,870 --> 00:10:42,510
All right so let's go ahead and test this and make sure it works.

153
00:10:42,950 --> 00:10:43,270
All right.

154
00:10:43,310 --> 00:10:46,090
Let's go and open up our simulator and moment of truth.

155
00:10:46,190 --> 00:10:49,490
When I click on this we should see this update to 1.

156
00:10:49,650 --> 00:10:51,220
Oh there we go one.

157
00:10:51,440 --> 00:10:52,730
Here's another one.

158
00:10:52,760 --> 00:10:54,100
Up to 16.

159
00:10:54,110 --> 00:11:03,890
All right now in this course we aren't going to handle the case of you know whether you have already

160
00:11:03,890 --> 00:11:04,640
liked one.

161
00:11:04,640 --> 00:11:10,640
And then if you click it it should maybe subtract the like that's going to be in the next target topic

162
00:11:10,640 --> 00:11:13,400
when we implement users and log in.

163
00:11:13,400 --> 00:11:19,250
That way we can actually keep track of who has liked what post because right now there's no way of knowing

164
00:11:19,250 --> 00:11:23,480
whether this user has liked a particular post.

165
00:11:23,480 --> 00:11:27,320
We just don't have that logic in place right now right.

166
00:11:27,470 --> 00:11:30,710
So right now you can add as many likes as you want to any post.

167
00:11:30,710 --> 00:11:31,080
All right.

168
00:11:31,100 --> 00:11:32,660
So enjoy that.

169
00:11:32,950 --> 00:11:37,670
All right and then here in popular now we should see this really working.

170
00:11:37,670 --> 00:11:38,780
So check this out.

171
00:11:38,780 --> 00:11:39,800
These both have five.

172
00:11:39,830 --> 00:11:45,330
But if I click on this one then we should see this one move right up above the previous one yeah.

173
00:11:45,370 --> 00:11:46,270
There we go.

174
00:11:46,280 --> 00:11:50,650
So now you can see kind of how the lights are working.

175
00:11:50,990 --> 00:11:51,310
All right.

176
00:11:51,380 --> 00:11:52,940
Supercool.

177
00:11:53,300 --> 00:11:53,570
All right.

178
00:11:53,570 --> 00:11:56,850
The other way that I wanted to show you how to update data.

179
00:11:56,940 --> 00:11:59,020
So I'm just going to comment this one out.

180
00:11:59,090 --> 00:12:06,180
So this was the method one here is another method method too.

181
00:12:06,590 --> 00:12:13,200
All right you can say in the first part is the same fire store that fire store dobbed collection.

182
00:12:13,340 --> 00:12:15,510
And actually let's see here document.

183
00:12:15,770 --> 00:12:19,520
Let's see if we can show you a different way of doing this part two to document path.

184
00:12:19,550 --> 00:12:28,810
Instead of doing data collection that document what we can do is instead say I'm going to take thoughts.

185
00:12:28,820 --> 00:12:36,140
So this is the collection of thoughts and then we can do a smash and then we can do some string interpolation

186
00:12:36,140 --> 00:12:36,570
here.

187
00:12:36,590 --> 00:12:38,300
Let's see here.

188
00:12:40,330 --> 00:12:46,800
This is document or thought that document ID.

189
00:12:46,960 --> 00:12:47,220
All right.

190
00:12:47,220 --> 00:12:53,630
And so this right here is equivalent to a collection dot document.

191
00:12:53,800 --> 00:12:54,060
All right.

192
00:12:54,060 --> 00:13:00,070
And so just a little bit more succinct if you ended up having a whole bunch of chains together.

193
00:13:00,430 --> 00:13:04,270
Then this would be probably a good way of doing it more succinctly.

194
00:13:04,270 --> 00:13:11,130
All right so then after this I'm going to put this on a new line we were going to say update data.

195
00:13:11,190 --> 00:13:11,740
OK.

196
00:13:11,800 --> 00:13:16,360
And so here we have update data and we have one with a completion and one without it.

197
00:13:16,480 --> 00:13:18,820
Don't really care about the completion at this point.

198
00:13:18,820 --> 00:13:28,810
So then we were going to say the numb likes and this is going to be again the thought doesn't seem like

199
00:13:28,810 --> 00:13:32,770
a plus one I'm just going to copy this and paste it here.

200
00:13:32,770 --> 00:13:33,270
All righty.

201
00:13:33,310 --> 00:13:36,770
So these are the two different ways you can use that sit data.

202
00:13:37,240 --> 00:13:42,010
But remember you have to use the options of options that emerge if you want to merge.

203
00:13:42,010 --> 00:13:46,450
If you don't want emerging You just want to override everything with whatever you're putting right here

204
00:13:46,450 --> 00:13:48,950
then that's that's an option as well.

205
00:13:49,090 --> 00:13:53,300
But the update data that kind of has the merge built into it.

206
00:13:53,300 --> 00:13:55,810
All right so it's going to run this and test it make sure it works.

207
00:13:56,690 --> 00:13:57,240
Click on it.

208
00:13:57,260 --> 00:13:58,530
Click here.

209
00:13:58,790 --> 00:14:00,560
And it looks like something's broken.

210
00:14:00,560 --> 00:14:01,610
Let's check it out.

211
00:14:01,970 --> 00:14:02,180
OK.

212
00:14:02,180 --> 00:14:05,040
So I think the reason is that this is coming out as an optional.

213
00:14:05,150 --> 00:14:10,000
So we're just going to force on rapid and the run that again.

214
00:14:10,000 --> 00:14:11,310
Now I just check this out.

215
00:14:11,420 --> 00:14:12,170
Oh there we go.

216
00:14:12,170 --> 00:14:12,700
All right.

217
00:14:12,710 --> 00:14:15,420
So this is all working now.

218
00:14:15,890 --> 00:14:17,270
All right so we are finished.

219
00:14:17,270 --> 00:14:19,850
How cool is this app.

220
00:14:19,850 --> 00:14:23,250
You know we can add our thoughts.

221
00:14:23,270 --> 00:14:27,340
We can sort on different categories and by popularity.

222
00:14:27,470 --> 00:14:30,220
We have learned so much in this course already.

223
00:14:30,220 --> 00:14:30,550
All right.

224
00:14:30,590 --> 00:14:32,880
So let's let's do a quick recap.

225
00:14:32,880 --> 00:14:37,490
We've learned how to create fire firebase project which involves getting your Google P list adding the

226
00:14:37,490 --> 00:14:43,050
cocoa pods and all of that and then we learned about the architecture and the structure of using fire

227
00:14:43,070 --> 00:14:48,650
store how you have collections and documents and how you can nest those one inside the other.

228
00:14:48,650 --> 00:14:53,480
We learned about how to add documents to our database using various methods.

229
00:14:53,630 --> 00:14:57,380
And then we also learned about how to retrieve that data using observers.

230
00:14:57,380 --> 00:15:05,610
And one time fetches we learn how to filter and sort those and also how to update single singular documents.

231
00:15:05,610 --> 00:15:06,160
Right.

232
00:15:06,500 --> 00:15:08,930
So yeah I hope you guys had a lot of fun with this.

233
00:15:08,930 --> 00:15:10,280
I know that I did.

234
00:15:10,280 --> 00:15:10,550
All right.

235
00:15:10,560 --> 00:15:16,640
And the next target topics that we cover we're going to be looking at authorization and log in as well

236
00:15:16,640 --> 00:15:18,120
as security rules right.

237
00:15:18,350 --> 00:15:19,610
So I will see you around.
