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