Hey everybody this is Caleb with depth slopes and welcome to the very last lesson in the core data section
we're going to make a function here that will allow us to set goal progress update ourselves and we're
also going to go ahead and animate in a beautiful complete goal or goal complete view when we are done
completing our goal.
It's going to be awesome.
So let's pull open that project.
Let's go to the main story board and we're actually going to add in a view in Interface Builder and
the reason I'm doing this and not instantiating it programmatically is because there is a weird bug
with the way that views are loaded in table view cells.
It seems to favor an iPhone 7 frame size and on an iPhone 7 plus it causes issues that issue doesn't
happen when it's built in the interface builder so that's why we're building it this way.
Otherwise I would absolutely be building it programmatically.
But for now we're just going to drag a UI view onto our cell.
We're going to drag a label onto it.
We're going to set up an outlet and just tell when or when it should be shown or hidden.
So go ahead and drag on a view onto your table view cell make sure that you don't land it in one of
the stack views then it's going to be on top of everything just by the way pull it so that it fits the
UI view of the table view cell completely.
And then we're going to actually pin it in place on all four points make sure that it's.
Add four constraints then we're going to set the background color to our yellowish orange and we're
going to set the alpha channel of this color to actually be 75.
Let's just go ahead and enter that manually there 75.
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
top of that view and center it.
OK.
So that looks great.
Let's change the font from this ugly system font to Avenir next.
It's not Avenir Avenue next.
Beautiful.
And we're going to set this to be heavy.
It's going to be very big and celebratory and we're going to make the font size 30 looks great.
Now change the color to white and we're going to go ahead and stretch this out nicely.
There we go.
We're going to center it in just a second but we're going to just say goal complete.
All right.
This is just like a big celebration like you did.
Awesome.
Super cool.
So now go ahead and center that font center the label here and you know what it's looking like the color
here might need to actually be more like 80 percent opacity.
Just to give it a little bit more balance there.
That looks pretty good.
Now for the label.
We're going to just go ahead and pin this horizontally and vertically in the container.
That looks great.
So now go ahead and click on the assistant editor and we're going to go ahead and click on the cell
and find gold cell.
Then we're going to go ahead and go find the view for the gold cell here.
And now we need to make another outlet.
This new view we need to basically connect and then hide.
But then after it hides we're going to go ahead and show depending on the progress of our goal.
So go ahead and drag right click and drag and we're just going to say completion view we'll call it
completion view.
And when we configure the cell we're going to use the completion value and the goal progress to tell
whether or not we can show it or not.
So go ahead and type if goal.
The one we passed in when we're configuring If goal dot goal progress is equal to goal dot goal completion
value meaning if we've made it if we've completed our goal we're going to go ahead and say self completion
view is hidden equals false meaning we're going to show it else meaning if it's not equal if it's less
then we're going to go ahead and say self doc completion view is hidden equals true.
OK.
So it's going to hide if it's not complete it'll show if it is complete.
Simple as that.
That's pretty easy.
But now we need to go in.
And we need to write a new function that's going to allow us to set progress and update the progress
label case so this number should change every time that we add one.
So select go Visi and you can go ahead and close the assistant editor for now what we're going to do
is we're going to write a function that's going to allow us to set progress so go into your extension
and type phunk set.
And we're going to just say progress for goal progress for goal and we're going to use an internal parameter
of progress.
And we're just going to pass in an int 32.
Because the progress Remember needs to be set as in 32.
We also want to call this at an index path and we're going to just use an internal parameter here called
index path of type indexed path.
Cool.
So that's great.
Just like all the other functions we need a managed context.
So I'm actually just going to copy and paste this here.
We just need a managed context.
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
the current goal from the index path.
OK.
So let's say that I want to modify the goal at the third index path we're going to pass that in and
we can get that by saying Let current or maybe let chosen goal equals goals.
And we're going to pull out the one index path and we're going to access the ROE So we pull out the
chosen goal at maybe the third row the fourth or the fifth row.
Now we're going to basically create two properties here that's going to hold the progress and the completion
value then we're going to compare the two.
And if it is less than the completion we're going to go ahead and update the goal progress.
We have an attribute in our entity here that allows us to set goal progress if the current progress
is less than the amount that we need to complete the goal.
We're going to go ahead and update the current goal by 1 otherwise we're going to go ahead and we're
going to just return and get out of this function.
So let's do that now.
If chosen goal dot go progress is less than the chosen goal completion value meaning if maybe we've
only completed three out of seven then what we're going to do is we're going to say current or chosen
goal dot goal progress equals goal datt goal progress plus 1.
Okay.
We're just going to add one.
Super easy.
Now with this in mind we don't really need to pass in a value of progress because we're just going to
pass in one every time.
So let's get rid of that.
But maybe we can call set progress that's a little better set progress at the next path.
We're going to go ahead and call chosen gold.
Goal progress we're going to increment that by one every single time.
Otherwise if it is more than or equal to we're going to go ahead and just return and get out of this
function.
Simple as that.
Now we have modified this we've set new values but it's worthless unless we call save on the Manage
context so we have passed this in we've pulled it from the managed context.
Now we're going to use a do and catch block to go ahead and do what we need to do.
So just like below we're going to call try manage context not safe.
OK and of course it throws.
So we're going to use Debug print debug description debug print and we're going to say could not set
progress
and we're going to go ahead and we're going to pull out the error from the throw the error that's thrown
and we're going to print out it's localized description.
OK.
That's it.
And you know what.
While we're here why don't we print out
successfully set progress.
And of course if you were to ever to push an app to production you would go back and remove all these
print statements.
But you know and for catching of errors you would want to display those on the screen.
But for this app we're just going to use the console because that's fine for us for now.
But now we have a function that can set progress for us.
How cool is that.
Literally all we need to do now is we need to call set progress and we need to reload the row that we
have set.
It's that easy.
So let's do that by creating our ad progress action.
So go ahead and type let add.
Action equals UI.
Table View row action.
GUYS THIS IS SERIOUSLY JUST like we did it's so easy.
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
to use its handler to choose the proper one that we've selected at that certain index path in the table
view.
Now the the style we can just call normal because we're not destroying anything we're not doing anything
is just a normal button.
The title we're just going to say add one in caps and press enter on the handler.
Now just like above we get a row action so call it row action.
And for the index path just call index path and inside of this closure is where we're going to set our
progress.
So inside here we can call self set progress at index path just like that.
It's that easy we just set progress.
We just added 1 K..
Now we don't want to reload the whole table view because we don't need to that's a waste of resources
what we can do is we can call table view reload rows and index path and we can past in the index path
so that it can just reload that one row that we've modified.
So go ahead and pass in an array containing index path.
Now you see that it's asking for an array of index path you can put in multiple index paths but we're
just going to use our singular index path from this single action and the UI table view row animation
is just automatic.
Just like the other one.
Ok we get a nice little fade out a nice little animation.
And of course we need to now give it a background color.
So go ahead and type add action.
Background color and we'll just use a color literal.
I love color literals they're so much easier than color or you are used to trying to use hex values
in code is just ugly.
So I'm going to use my nice little orangey yellow color there and I'm going to return the ADD action
as well because it's an array.
So we can return multiple actions.
You can do a lot of actions in one table if you said you could do three four five whatever.
We're just going to do two though.
But let's think about this when we tap action.
It should set the progress K meaning it's going to take the current goal progress.
It's going to add one as long as it's less than the completion value.
If it is less than the completion value it's going to do that as soon as we get to the completion value
once it's equal it's going to start returning so we can't set progress anymore.
That's good.
And then as soon as goal progress is equal to goal completion value it's going to fade in the nice little
UI view that we created here.
The orange one that says Go complete.
So let's go ahead let's build and run it.
Let's create a goal and let's swipe an ad to see if we can properly update our progress here.
Ok cool.
So we have no goals.
Let's go ahead and let's create one maybe.
Go jogging twice a day and that will be a short.
Well no it's a long term goal let's go jogging.
Well now short term I don't want to jog twice a day for very long.
So choose short term and let's do that for three days.
Press create goal and there's our goal if I swipe.
We get our second action add one and it has that nice background.
Now when I press this we should set progress and reload this cell.
Let's try it.
Look at that the progress is updated from core data.
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
out all the way.
Let's check to make sure that this is being persisted by core data.
It is.
Look at that.
We get all of our data here persisted by core data.
We set three here.
So let's go ahead and let's see once we get to 3 It should show up.
Complete look at that.
That is amazing.
Now I am seeing a problem here it appears that we are somehow below our labels here we don't want that.
So let's go ahead and let's figure out what the deal is.
Because it appears to be above everything.
But you know what we may actually have to pull it up and out of everything and then drop it back in
the table view cell to make sure that it's good to go.
So let's let's drop it in and let's set the constraints here to be 0 0 0 and 0.
Sometimes we have some weird constraint issues.
OK.
Awesome so it looks like we accidentally we're not doing it the right way.
But now it's on top of everything.
Let's build and run.
And let's see if it shows up on top awesome.
OK beautiful That's exactly how it's supposed to look good complete.
And now that it's done we can get rid of it.
Let's create some more.
I'm just going to type random stuff here.
Let's do maybe to cool this out another one.
Long term this time.
And how about for sweet.
OK let's set these one two three four.
It's complete.
Beautiful.
Let's set the food is great.
What.
That's weird.
How about to create a goal.
Awesome guys.
Do you see this.
It's showing up everything is loading perfectly.
It looks amazing.
Guys I love this app.
We can go ahead and set goals we can save them we can delete them our app is done guys goalpost is finished.
This is an app you could put up on to the App Store right now.
So give yourselves a big round of applause.
Pat yourself on the back.
Congratulate yourself.
Go get a drink go get some food.
Give your friend a high five.
Give yourself a high five.
Amazing work guys.
We have built an app that uses core data to save to fetch to retrieve to remove all kinds of data.
You can do all kinds of things now with core data.
It's an amazing way to have persistent storage in iOS and I'm really really just super stoked that you
guys took this section of the course.
Amazing work.
And let's head over to a quick little challenge video where I'm going to ask you to extend this app.