1
00:00:08,050 --> 00:00:13,540
Hey everybody this is Caleb with deps slopes and welcome to the very last lesson in the core data section.

2
00:00:13,660 --> 00:00:19,000
We're going to make a function here that will allow us to set goal progress update ourselves and we're

3
00:00:19,000 --> 00:00:25,810
also going to go ahead and animate in a beautiful complete goal or goal complete view when we are done

4
00:00:25,840 --> 00:00:27,670
completing our goal it's going to be awesome.

5
00:00:27,700 --> 00:00:29,530
So let's pull open that project.

6
00:00:29,650 --> 00:00:35,560
Let's go to main storyboard and we're actually going to add in a view in Interface Builder.

7
00:00:35,560 --> 00:00:40,750
And the reason I'm doing this and not instantiating it programmatically is because there is a weird

8
00:00:40,750 --> 00:00:45,130
bug with the way that views are loaded in table cells.

9
00:00:45,160 --> 00:00:51,970
It seems to favor an iPhone 7 frame size and on an iPhone 7 plus it causes issues that issue doesn't

10
00:00:51,970 --> 00:00:55,770
happen when it's built in Interface Builder so that's why we're building it this way.

11
00:00:55,840 --> 00:00:58,810
Otherwise I would absolutely be building it programmatically.

12
00:00:58,900 --> 00:01:02,400
But for now we're just going to drag a view onto our cell.

13
00:01:02,440 --> 00:01:03,730
We're going to drag a label onto it.

14
00:01:03,730 --> 00:01:07,840
We're going to set up an outlet and just tell when or when it should be shown or hidden.

15
00:01:07,840 --> 00:01:13,600
So go ahead and drag on a view onto your table view cell make sure that you don't land it in one of

16
00:01:13,600 --> 00:01:19,570
the stack views then it's going to be on top of everything just by the way pull it so that it fits the

17
00:01:19,570 --> 00:01:23,640
UI view of the table view cell completely.

18
00:01:23,890 --> 00:01:29,590
And then we're going to actually pin it in place on all four points make sure that it's zero.

19
00:01:29,760 --> 00:01:30,200
OK.

20
00:01:30,310 --> 00:01:35,800
Add four constraints then we're going to set the background color to our yellowish orange and we're

21
00:01:35,800 --> 00:01:43,000
going to set the alpha channel of this color to actually be 75.

22
00:01:43,010 --> 00:01:48,560
Let's just go ahead and enter that manually there 75 and what we're going to do next is we're going

23
00:01:48,560 --> 00:01:54,430
to drag on a UI label and we're going to drop it on the top of that view and center it.

24
00:01:54,790 --> 00:01:55,390
OK.

25
00:01:55,940 --> 00:01:57,080
So that looks great.

26
00:01:57,110 --> 00:02:01,450
Let's change the font from this ugly system font to Avenir next.

27
00:02:01,570 --> 00:02:03,500
Not Avenir Avenir next.

28
00:02:03,500 --> 00:02:04,380
Beautiful.

29
00:02:04,460 --> 00:02:06,270
And we're going to set this to be heavy.

30
00:02:06,290 --> 00:02:12,310
It's going to be very big and celebratory and we're going to make the font size 30.

31
00:02:12,360 --> 00:02:13,830
It looks great.

32
00:02:13,940 --> 00:02:19,550
Now change the color to white and we're going to go ahead and stretch this out nicely.

33
00:02:19,550 --> 00:02:20,660
There we go.

34
00:02:20,660 --> 00:02:25,610
We're going to center it in just a second but we're going to just say goal complete.

35
00:02:25,610 --> 00:02:26,300
All right.

36
00:02:26,420 --> 00:02:29,590
This is just like a big celebration like you did.

37
00:02:29,660 --> 00:02:30,840
Awesome.

38
00:02:30,840 --> 00:02:32,460
OK super cool.

39
00:02:32,460 --> 00:02:39,170
So now go ahead and center that font center the label here and you know what it's looking like the color

40
00:02:39,170 --> 00:02:43,910
here might need to actually be more like 80 percent opacity.

41
00:02:44,090 --> 00:02:47,040
Just to give it a little bit more balance there.

42
00:02:47,120 --> 00:02:48,620
That looks pretty good.

43
00:02:48,650 --> 00:02:49,970
Now for the label.

44
00:02:49,970 --> 00:02:54,980
We're going to just go ahead and pin this horizontally and vertically in the container.

45
00:02:54,980 --> 00:02:56,210
That looks great.

46
00:02:56,210 --> 00:03:03,500
So now go ahead and click on the assistant editor and we're going to go ahead and click on the cell

47
00:03:03,770 --> 00:03:05,830
and find gold cell.

48
00:03:05,840 --> 00:03:10,710
Then we're going to go ahead and go find the view for the gold cell here.

49
00:03:11,000 --> 00:03:13,140
And now we need to make another Iby outlet.

50
00:03:13,220 --> 00:03:17,450
This new view we need to basically connect and then hide.

51
00:03:17,450 --> 00:03:23,300
But then after it high we're going to go ahead and show depending on the progress of our goal.

52
00:03:23,300 --> 00:03:30,260
So go ahead and drag right click and drag and we're just going to say completion view we'll call it

53
00:03:30,260 --> 00:03:35,730
completion view and when we can figure this sell We're going to use the completion value and the goal

54
00:03:35,750 --> 00:03:38,270
progress to tell whether or not we can show it or not.

55
00:03:38,270 --> 00:03:40,370
So go ahead and type if goal.

56
00:03:40,370 --> 00:03:49,610
The one we passed in when we're configuring If goal dot goal progress is equal to goal dot goal completion

57
00:03:49,610 --> 00:03:55,340
value meaning if we've made it if we've completed our goal we're going to go ahead and say self completion

58
00:03:55,340 --> 00:04:03,290
view is hidden equals false meaning we're going to show it else meaning if it's not equal if it's less

59
00:04:03,290 --> 00:04:09,990
then we're going to go ahead and say self completion view is hidden equals true.

60
00:04:10,180 --> 00:04:10,630
OK.

61
00:04:10,790 --> 00:04:13,940
So it's going to hide if it's not complete it will show if it is complete.

62
00:04:13,940 --> 00:04:15,310
Simple is that.

63
00:04:15,320 --> 00:04:16,460
That's pretty easy.

64
00:04:16,580 --> 00:04:22,070
But now we need to go in and we need to write a new function that's going to allow us to set progress

65
00:04:22,460 --> 00:04:26,230
and update the progress label.

66
00:04:26,230 --> 00:04:29,160
OK so this number should change every time that we add one.

67
00:04:29,450 --> 00:04:35,600
So select go Visi and you can go ahead and close the assistant editor for now what we're going to do

68
00:04:35,600 --> 00:04:39,770
is we're going to write a function that's going to allow us to set progress.

69
00:04:40,040 --> 00:04:43,850
So go into your extension and type phunk set.

70
00:04:43,850 --> 00:04:52,070
And we're going to just say progress for goal progress for goal and we're going to use an internal parameter

71
00:04:52,070 --> 00:04:53,180
of progress.

72
00:04:53,360 --> 00:04:57,330
And we're just going to pass in an int 32.

73
00:04:57,570 --> 00:04:57,980
OK.

74
00:04:57,980 --> 00:05:01,570
Because the progress Remember needs to be set as in 32.

75
00:05:01,640 --> 00:05:06,920
We also want to call this at an index path and we're going to just use an internal parameter here called

76
00:05:06,950 --> 00:05:10,080
index path of type index path.

77
00:05:10,220 --> 00:05:10,640
Cool.

78
00:05:10,640 --> 00:05:11,790
So that's great.

79
00:05:11,870 --> 00:05:16,160
Just like all the other functions we need a managed context so I'm actually just going to copy and paste

80
00:05:16,160 --> 00:05:17,000
this here.

81
00:05:17,060 --> 00:05:19,070
We just need a managed context.

82
00:05:19,130 --> 00:05:24,650
We're going to go ahead and in order to find the goal that we want to set the value we're going to get

83
00:05:24,650 --> 00:05:26,930
the current goal from the index path.

84
00:05:26,960 --> 00:05:27,230
OK.

85
00:05:27,230 --> 00:05:30,740
So let's say that I want to modify the goal at the third index path.

86
00:05:30,740 --> 00:05:39,110
We're going to pass that in and we can get that by saying Let current or maybe let chosen goal equals

87
00:05:39,110 --> 00:05:39,980
goals.

88
00:05:40,190 --> 00:05:45,500
And we're going to pull out the one index path and we're going to access the ROE So we pull out the

89
00:05:45,800 --> 00:05:49,140
chosen goal at maybe the third row the fourth or the fifth row.

90
00:05:49,490 --> 00:05:54,620
Now we're going to basically create two properties here that's going to hold the progress and the completion

91
00:05:54,620 --> 00:05:57,260
value then we're going to compare the two.

92
00:05:57,860 --> 00:06:02,460
And if it is less than the completion we're going to go ahead and update the goal progress.

93
00:06:02,520 --> 00:06:08,660
We have an attribute in our entity here that allows us to set goal progress if the current progress

94
00:06:08,750 --> 00:06:11,690
is less than the amount that we need to complete the goal.

95
00:06:11,690 --> 00:06:17,000
We're going to go ahead and update the current goal by 1 otherwise we're going to go ahead and we're

96
00:06:17,000 --> 00:06:19,410
going to just return and get out of this function.

97
00:06:19,420 --> 00:06:20,680
So let's do that now.

98
00:06:20,930 --> 00:06:31,430
If chosen goal datt go progress is less than the chosen goal completion value meaning if maybe we've

99
00:06:31,430 --> 00:06:37,010
only completed three out of seven then what we're going to do is we're going to say current or chosen

100
00:06:37,010 --> 00:06:48,530
goal dot goal progress equals goal dot goal progress plus 1.

101
00:06:48,880 --> 00:06:49,180
Okay.

102
00:06:49,210 --> 00:06:50,140
We're just going to add one.

103
00:06:50,170 --> 00:06:51,370
Super easy.

104
00:06:51,370 --> 00:06:56,290
Now with this in mind we don't really need to pass in a value of progress because we're just going to

105
00:06:56,290 --> 00:06:57,550
pass in one every time.

106
00:06:57,550 --> 00:06:59,700
So let's get rid of that.

107
00:06:59,980 --> 00:07:04,750
But maybe we can call set progress that's a little better set progress at the next path.

108
00:07:04,750 --> 00:07:10,210
We're going to go ahead and call chosen gold gold progress we're going to increment that by one every

109
00:07:10,210 --> 00:07:11,060
single time.

110
00:07:11,140 --> 00:07:16,420
Otherwise if it is more than or equal to we're going to go ahead and just return and get out of this

111
00:07:16,420 --> 00:07:17,590
function.

112
00:07:17,590 --> 00:07:19,060
Simple as that.

113
00:07:19,080 --> 00:07:25,840
Now we have modified this we've set new values but it's worthless unless we call save on the Manage

114
00:07:25,840 --> 00:07:29,590
context so we have passed this in we've pulled it from the managed context.

115
00:07:29,590 --> 00:07:36,480
Now we're going to use a do and catch block to go ahead and do what we need to do.

116
00:07:36,640 --> 00:07:42,090
So just like below we're going to call try manage context not safe.

117
00:07:43,060 --> 00:07:44,680
OK and of course it throws.

118
00:07:44,680 --> 00:07:52,960
So we're going to use Debug print debug description debug print and we're going to say could not set

119
00:07:53,080 --> 00:07:53,740
progress

120
00:07:59,390 --> 00:08:05,840
and we're going to go ahead and we're going to pull out the error from the throw the error that's thrown

121
00:08:06,260 --> 00:08:09,560
and we're going to print out it's localized description OK.

122
00:08:09,920 --> 00:08:10,640
That's it.

123
00:08:10,640 --> 00:08:11,350
And you know what.

124
00:08:11,420 --> 00:08:12,160
While we're here.

125
00:08:12,170 --> 00:08:13,370
Why don't we print out

126
00:08:16,130 --> 00:08:19,890
successfully set progress.

127
00:08:20,270 --> 00:08:23,810
And of course if you were to ever to push an app to production you would go back and remove all these

128
00:08:23,810 --> 00:08:25,610
print statements.

129
00:08:26,120 --> 00:08:31,250
But you know and for the catching of errors you would want to display those on the screen but for this

130
00:08:31,250 --> 00:08:35,070
app we're just going to use the console because that's fine for us for now.

131
00:08:35,390 --> 00:08:38,180
But now we have a function that can set progress for us.

132
00:08:38,180 --> 00:08:39,750
How cool is that.

133
00:08:39,770 --> 00:08:46,160
Literally all we need to do now is we need to call set progress and we need to reload the row that we

134
00:08:46,310 --> 00:08:46,850
have set.

135
00:08:46,850 --> 00:08:47,890
It's that easy.

136
00:08:48,110 --> 00:08:52,010
So let's do that by creating our ad progress action.

137
00:08:52,010 --> 00:08:54,560
So go ahead and type let add.

138
00:08:54,560 --> 00:08:56,780
Action equals UI.

139
00:08:56,780 --> 00:08:58,280
Table View row action.

140
00:08:58,400 --> 00:09:01,490
GUYS THIS IS SERIOUSLY JUST like we did it's so easy.

141
00:09:01,640 --> 00:09:05,570
We're going to call it we're going to give it a style We're going to give it a title and we're going

142
00:09:05,570 --> 00:09:12,020
to use its handler to choose the proper one that we've selected at that certain index path in the table

143
00:09:12,020 --> 00:09:12,580
view.

144
00:09:12,920 --> 00:09:18,590
Now the the style we can just call normal because we're not destroying anything we're not doing anything

145
00:09:18,590 --> 00:09:20,030
is just a normal button.

146
00:09:20,480 --> 00:09:27,150
The title we're just going to say add one in caps and press enter on the handler.

147
00:09:27,350 --> 00:09:31,550
Now just like above we get a row action so call it row action.

148
00:09:31,550 --> 00:09:38,420
And for the index path just call index path and inside of this closure is where we're going to set our

149
00:09:38,420 --> 00:09:39,020
progress.

150
00:09:39,020 --> 00:09:45,580
So inside here we can call self set progress at index path just like that.

151
00:09:45,650 --> 00:09:47,150
It's that easy we just set progress.

152
00:09:47,150 --> 00:09:48,430
We just added one.

153
00:09:48,490 --> 00:09:54,670
OK now we don't want to reload the whole table view because we don't need to that's a waste of resources.

154
00:09:54,790 --> 00:10:01,310
What we can do is we can call table view not reload rows and index path and we can pass in the index

155
00:10:01,310 --> 00:10:05,030
path so that it can just reload that one row that we've modified.

156
00:10:05,030 --> 00:10:09,230
So go ahead and pass in an array containing index path.

157
00:10:09,240 --> 00:10:14,840
Now you see that it's asking for an array of index paths you can put in multiple index paths but we're

158
00:10:14,840 --> 00:10:19,010
just going to use our singular index path from this single action.

159
00:10:19,310 --> 00:10:23,300
And the UI table view row animation is just automatic.

160
00:10:23,300 --> 00:10:24,270
Just like the other one.

161
00:10:24,320 --> 00:10:27,940
OK we get a nice little fade out a nice little animation.

162
00:10:28,010 --> 00:10:31,350
And of course we need to now give it a background color.

163
00:10:31,430 --> 00:10:34,090
So go ahead and type add action.

164
00:10:34,100 --> 00:10:36,950
Background color and we'll just use a color literal.

165
00:10:36,950 --> 00:10:42,890
I love color literals they're so much easier than color or you are used to trying to use hex values

166
00:10:42,890 --> 00:10:44,410
in code it's just ugly.

167
00:10:44,480 --> 00:10:50,330
So I'm going to use my nice little orangey yellow color there and I'm going to return the action as

168
00:10:50,330 --> 00:10:51,330
well because it's an array.

169
00:10:51,410 --> 00:10:52,820
So we can return multiple actions.

170
00:10:52,820 --> 00:10:57,870
You can do a lot of actions in one table VSL you could do three four five whatever.

171
00:10:57,890 --> 00:10:59,320
We're just going to do two though.

172
00:10:59,510 --> 00:11:02,120
But let's think about this when we tap at action.

173
00:11:02,120 --> 00:11:04,100
It should set the progress k.

174
00:11:04,220 --> 00:11:06,380
Meaning it's going to take the current goal progress.

175
00:11:06,410 --> 00:11:12,200
It's going to add one as long as it's less than the completion value if it is less than the completion

176
00:11:12,200 --> 00:11:17,780
value it's going to do that as soon as we get to the completion value once it's equal it's going to

177
00:11:17,780 --> 00:11:20,210
start returning so we can't set progress anymore.

178
00:11:20,210 --> 00:11:21,400
That's good.

179
00:11:21,410 --> 00:11:27,950
And then as soon as go progress is equal to completion value it's going to fade in the nice little view

180
00:11:27,950 --> 00:11:28,890
that we created here.

181
00:11:28,910 --> 00:11:31,240
The orange one that says school complete.

182
00:11:31,430 --> 00:11:33,430
So let's go ahead let's build and run it.

183
00:11:33,500 --> 00:11:38,940
Let's create a goal and let's swipe an ad to see if we can properly update our progress here.

184
00:11:41,590 --> 00:11:42,060
Ok cool.

185
00:11:42,070 --> 00:11:43,380
So we have no goals.

186
00:11:43,750 --> 00:11:46,670
Let's go ahead and let's create one maybe.

187
00:11:46,870 --> 00:11:52,740
Go jogging twice a day and that will be a short.

188
00:11:52,780 --> 00:11:55,380
Well no it's a long term goal let's go jogging.

189
00:11:55,390 --> 00:11:58,510
Well now short term I don't want to jog twice a day for very long.

190
00:11:58,510 --> 00:12:02,550
So choose short term and let's do that for three days.

191
00:12:02,600 --> 00:12:05,600
Press create goal and there's our goal.

192
00:12:05,740 --> 00:12:10,660
If I swipe we get our second action add one and it has that nice background.

193
00:12:10,660 --> 00:12:14,380
Now when I press this we should set progress and reload this cell.

194
00:12:14,410 --> 00:12:19,950
Let's try it look at that the progress is updated from core data.

195
00:12:19,950 --> 00:12:21,330
Now let's let's quit out.

196
00:12:21,380 --> 00:12:21,640
OK.

197
00:12:21,660 --> 00:12:25,880
Let's go ahead and let's double tap the home button here let's quit out all the way.

198
00:12:25,950 --> 00:12:29,790
Let's check to make sure that this is being persisted by core data.

199
00:12:30,150 --> 00:12:30,570
It is.

200
00:12:30,570 --> 00:12:31,360
Look at that.

201
00:12:31,470 --> 00:12:34,650
We get all of our data here persisted by core data.

202
00:12:34,650 --> 00:12:35,840
We set three here.

203
00:12:35,910 --> 00:12:41,340
So let's go ahead and let's see once we get to 3 It should show up complete.

204
00:12:41,400 --> 00:12:42,320
Look at that.

205
00:12:42,330 --> 00:12:43,740
That is amazing.

206
00:12:43,740 --> 00:12:51,050
Now I am seeing a problem here it appears that we are somehow below our labels here we don't want that.

207
00:12:51,300 --> 00:12:54,210
So let's go ahead and let's figure out what the deal is.

208
00:12:54,210 --> 00:12:56,500
Because it appears to be above everything.

209
00:12:57,300 --> 00:13:02,100
But you know what we may actually have to pull it up and out of everything and then drop it back in

210
00:13:02,100 --> 00:13:05,010
the table view cell to make sure that it's good to go.

211
00:13:05,010 --> 00:13:12,070
So let's let's drop it in and let's set the constraints here to be 0 0 0 and 0.

212
00:13:12,270 --> 00:13:14,460
Sometimes we have some weird constraint issues.

213
00:13:14,460 --> 00:13:15,180
OK.

214
00:13:15,270 --> 00:13:15,690
Awesome.

215
00:13:15,690 --> 00:13:19,500
So it looks like we accidentally we're not doing it the right way.

216
00:13:19,830 --> 00:13:21,600
But now it's on top of everything.

217
00:13:21,630 --> 00:13:22,990
Let's build and run.

218
00:13:23,220 --> 00:13:25,630
And let's see if it shows up on top.

219
00:13:26,980 --> 00:13:27,370
Awesome.

220
00:13:27,390 --> 00:13:28,030
OK beautiful.

221
00:13:28,030 --> 00:13:29,830
That's exactly how it's supposed to look.

222
00:13:29,900 --> 00:13:30,350
Complete.

223
00:13:30,400 --> 00:13:32,760
And now that it's done we can get rid of it.

224
00:13:33,070 --> 00:13:33,790
Let's create some more.

225
00:13:33,790 --> 00:13:35,440
I'm just going to type random stuff here.

226
00:13:35,460 --> 00:13:39,440
Let's do maybe to cool this out another one.

227
00:13:39,520 --> 00:13:40,450
Long term this time.

228
00:13:40,450 --> 00:13:43,590
And how about for sweet.

229
00:13:43,720 --> 00:13:48,050
OK let's set these one two three four.

230
00:13:48,070 --> 00:13:49,010
It's complete.

231
00:13:49,060 --> 00:13:50,020
Beautiful.

232
00:13:50,020 --> 00:13:53,660
Let's set the food is great.

233
00:13:53,660 --> 00:13:54,410
What.

234
00:13:54,760 --> 00:13:55,240
That's weird.

235
00:13:55,240 --> 00:13:57,770
How about to create a goal.

236
00:13:58,060 --> 00:13:58,770
Awesome guys.

237
00:13:58,780 --> 00:13:59,560
Do you see this.

238
00:13:59,560 --> 00:14:01,860
It's showing up everything is loading perfectly.

239
00:14:01,870 --> 00:14:03,440
It looks amazing.

240
00:14:03,460 --> 00:14:04,830
Guys I love this app.

241
00:14:04,840 --> 00:14:09,280
We can go ahead and set goals we can save them we can delete them.

242
00:14:09,340 --> 00:14:10,420
Our app is done guys.

243
00:14:10,430 --> 00:14:11,880
Goalpost is finished.

244
00:14:11,890 --> 00:14:14,880
This is an app you could put up on to the App Store right now.

245
00:14:15,100 --> 00:14:17,270
So give yourselves a big round of applause.

246
00:14:17,320 --> 00:14:18,390
Pat yourself on the back.

247
00:14:18,400 --> 00:14:19,450
Congratulate yourself.

248
00:14:19,450 --> 00:14:21,140
Go get a drink go get some food.

249
00:14:21,220 --> 00:14:22,210
Give your friend a high five.

250
00:14:22,210 --> 00:14:23,620
Give yourself a high five.

251
00:14:23,620 --> 00:14:24,460
Amazing work guys.

252
00:14:24,460 --> 00:14:31,410
We have built an app that uses core data to save to fetch to retrieve to remove all kinds of data.

253
00:14:31,420 --> 00:14:33,910
You can do all kinds of things now with core data.

254
00:14:33,910 --> 00:14:39,010
It's an amazing way to have persistent storage in iOS and I'm really really just super stoked that you

255
00:14:39,010 --> 00:14:40,750
guys took this section of the course.

256
00:14:40,810 --> 00:14:41,690
Amazing work.

257
00:14:41,710 --> 00:14:45,630
And let's head over to a quick little challenge video where I'm going to ask you to extend this app.

