What's up guys.
Jacob Bledsoe with Dev slopes dot com and in the last lesson we got everything working for our pace
and now we have distance and now the last thing that we need to do is be able to pause and to resume
our run right.
So this lesson we're going to get our pause and resume function functioning.
So then we can move on and start digging into the awesome realm stuff right.
So before we get started let's pull up our terminal and I am going to just create a new branch for this
lesson.
Check out beshe be less than 10 right.
And now with that going let's go ahead and just start in our current run.
VC right.
And underneath and run we need to just create a pause function that we can call right every time the
pause button is pushed.
So let's just make a funk pause run.
OK.
And what has to what do we have to do in our pause run we need to stop recording and location change.
Right.
We also need to we also need to stop the durations So the timer we need to pause the timer and then
we also need to I think that's it.
Pause the timer and stop recording location right
so pause the timer.
Let's see what would we do if we pull up our timer.
I think so we can do invalidate in if you call Invalidate it just stops.
It basically disables the timer from continuing counting because when we created the timer we just set
it to repeat forever right.
So invalidating it stops it.
So we'll no longer keep track but this won't clear our timer because we have our counter variable that's
holding the duration time.
So that's great.
So this will stop our timer in the pause in the pause function right and then we also need to stop updating
location.
So we call that right here in and run we need to do that again here.
So manager stop updating location.
Great.
And we also need to change our button image right.
We have this cool.
We have a pause button and we have a play button.
So we need to take our play button or pause button and we need to change the image.
So if you call pause button that set image we will now pass in.
I believe the asset is called presumed button.
There we go.
And you don't hit the pass in a string of a name any more just it'll find the asset name and it connects
it for you.
And we want this for state normal.
That is fine.
So this will change the pause button to a resume button which is awesome.
What else do we need to do.
We also need to set our last location to nil this location if we don't set the last location to nil
it.
When you hit resume run it's going to take the very last location wherever you paused your run and add
all the distance in between.
And we don't want that to happen either.
So we want last location to get set.
When you resume again right.
So we actually should probably nilla fi start location and last location.
So both those start over when you hit resume.
And our distance isn't going to reset because we've just been adding to it.
So that is fine as well.
So let's see in paw's run.
Let's go ahead and do start location equals nil and last location equals nil.
And then that will require our location array to start keeping track.
You know it won't add like if you pause Iran and cross the street and play it again it won't add that
extra feet or the extra distance in your total distance.
So that's nullified that's great.
And so now we need to call this function from our action down here right.
So right now if we just said pause run that probably wouldn't work right because our pause button can
either resume or pause right.
So we're just calling pause run and our timer is already invalidated.
We're probably going to get a crash.
So we should check to see if our timers invalidated and we can do that by just an if statement and call
timer that is valid.
And if it's valid it'll be true.
Right.
And so if the timer is true that means the run is in progress and we would want the pause button to
pause our run right.
And then so else if it's not valid that means we would want our run to start again right so we could
call our start run function start run.
OK.
Awesome.
So let's go ahead and run this and see if it works.
All right finds our location.
Let's click on Start Run and the timer is going our pace is calculating and our distance is moving.
Awesome.
So now if we pause this
and it looks like something goofy is happening with our asset we're going to have to figure that out.
But it did change it to the resume button which is cool.
Our duration stopped our pace stopped calculating and our distance is not changing.
So now if we pressed it again
our timer started moving again and it looks like our distance is moving again as well.
But our play button has to change back to the the pause state so we know that clicking it would pause
it.
And we also have to figure out why it looks like our constraints might have broken.
So let's go ahead and check that out.
So in the Start Run we need to do the set image but for the pause button right.
But we don't want that here.
We still want this to be resume.
I don't even see it
maybe we add the asset let me see
our resume.
Yeah we have our resume.
OK.
So down here we want this to be our resume button perfect and up and start run.
We want our pause button set image to be Paul.
I forgot what it was called.
Let's go to our assets pause button.
OK.
So pause button No.
Why is this not pulling up.
Pause button.
There we go.
And then this will be for state normal as well.
OK so now that should switch back and forth so when we pause our run it changes the image.
And when we start our run it changes the image.
Let's check out our story board because something I didn't mean to double click something goofy is going
on with our button.
So.
So let's see.
Pull up and are Inspektor here.
OK.
Something with this constraint doesn't seem right and you might not have had this weird shift it might've
just happened to me.
I'm going to delete this constraint here and I'm going to redo it and just do horizontal spacing.
We'll see if that does anything.
Otherwise we're going to have to dig into this a little more.
So let's run the simulator.
Start our run.
All right durations going distance going pause.
OK.
So it's still moving it.
And then when we play it's coming back.
OK.
So what I think is happening is I actually have a constraint maybe on the image itself
but at least it's changing now.
So it's just very strange because this is the same as a resume.
All right let's figure this out.
All right I'm going to delete this again and I'm going to make sure I have a aspect ratio on this and
just make sure it went in there.
Yeah it's one to one that is good.
And then I'm going to click and drag to the label and do horizontal spacing like that.
All right with the aspect ratio hopefully that will help keep the other items centered.
Let's run this.
And now the constraint to the end labels actually to the edge of our button.
So I think it will work better.
So let's start the Ron and then pause and it's still being silly.
Stop.
OK.
So a quick way to figure out what's going on here.
We can just change this image let's change it to the resume button.
Oh see that's our problem.
So the size of a button is actually changing on us.
It's keeping the 1 to 1 ratio but it's making the buttons smaller.
And I'm glad we did that.
So let's go ahead and change it back to the pause button and we want to set the size up we'll just make
it 75 by 75.
And now if we change the image to resume there we go now we're good.
OK so now let's go ahead and run it and everything should be pretty
star run.
Everything starts moving.
Great.
And then pause there we go our play button or resume button is now in the same spot as the pause and
we can now toggle our runs and then our distance will continue.
Awesome.
And then you can end the run by swiping.
Very cool.
All right.
So let's go back into our current Visi and we pretty much have everything we need right for
running.
I can't think of anything that we're missing at the moment.
We have our duration.
We have our distance and we're a bit we're able to pause and resume our runs.
Let's see in our end run.
So go down here in our end Swype our end run swiped where I commented and run code goes here.
Why don't we call our end run function and and run function.
So now we don't have to be down in this code at all anymore when we swipe and end the run it dismisses
the VC and calls our end run function.
So we came up to our end run function it stops updating location and up here is where we would add our
object to relm.
OK so I'm going to stop the the exercise here this lesson and in the next lesson we're going to start
setting up our our realm data.
So we're going to start setting up our role model for our run.
So that'll be really cool.
I will see you guys in the next lesson.
But before we sign off pull up that terminal and do a get ad period get commit Dasch am and let's see
we got our paws slash resume button function.
OK great.
And I will see you in the next lesson guys.