HEY WHAT'S UP GUYS I'M Jacob whatso with deps slopes dot com and in the last lesson we got our duration
label working off of just formatting seconds to show you know hours minutes and seconds so that's really
cool.
And in this one now we're going to get our pace working and then we'll have all the data we need to
finally set up our role models so that's really exciting.
But first before we get started let's open up terminal and I'm going to just create a new branch get
check out Desch be lesson 9.
Right.
And that is good.
Whoops.
OK.
So underneath our update counter function we're going to create a function for pace.
And this is just going to be called calculate calculate pace.
Okay.
And we'll just opening and closing brackets that.
Okay so what do we need to calculate our pace.
We need time and we need distance right so time divided by distance would be your pace.
So let's go ahead and create a function.
We want to pass in time and it's going to be in seconds for us so we can just use our counter and that'll
be of type Integer.
And then we need distance as well.
And that will be miles since we already have that.
And that will be of type double.
Okay.
And then we're going to return a string so we can just simply update our label with this function right.
One thing we're going to need though is we need a pace variable and we want that variable assessable
up here with our distance and counter.
So just so basically when we create our relm object after the runs done we're going to need these three
items.
So let's just make a VAR.
And that is going to be pace and it is also going to be an integer so just equals zero.
And basically when we create our own object we're going to need these three things.
So down here we can just update our pace with just dividing seconds by Miles Right.
So let's just go ahead and do.
Pace equals second.
I should say seconds.
That's kind of weird.
So seconds.
And then just divide this by miles.
And it's going to yell at us because Miles is a double right.
So we need to cast our seconds as a double for now.
So that will work there.
And then it's going to yell at us because pace is an integer which is also fine.
We just are going to cast this as an integer.
Now now that the math is done and it's just going to kind of it's going to force some round in if you
don't like the way it's round and you can always format it a little further.
But for for all our purposes right now this is going to be pretty accurate.
The way this is and then we can just simply return pace dot.
And then we can call our format time duration to string.
And now we have our pace getting returned in the format that we want.
So that's really cool.
And now where would we update our pace down here where we're updating locations is a great spot to do
it right.
So if we underneath our distance label or we're set in that we just want to before we update pace we
don't want our pace to update if things are zeroes and like negative numbers for whatever reason.
Because obviously that means our pases zero.
So let's just do a quick check.
This will stop things from crashing.
So if our counter is greater than zero and if run distance oh man if run if and if run distance is also
greater than zero then we want to call our pace so we'll just say pace label.
Text
equals calculate pace and now we'll just pass in our counter and we'll also pass in our run distance
and I think that is it.
Let's go ahead and run our app and we're going to see pace now.
So we'll start Ron and
doesn't look like it's working for us.
Okay.
So let's figure out figure out what we're doing wrong here.
So if run distance is greater than zero.
That's right.
If counter is greater than zero we want to calculate our pace let's go look at our pace function one
more time all right.
Divide in our seconds by miles which that is correct.
And it I just want to just do a print statement here
pace and we'll pass in our variable pace.
Just let's see if we have something come in and let's run this again.
And let's start this Ron
pases zero.
Okay.
Pases 0 Okay.
So something is going on with our well our counter's work in let's just see here.
So our counter
are run distance is increasing but our pace is not liking what we're doing up here.
So let's figure this out.
What if we just do this and we're just going to return a empty string for now.
Make this a double for now as I want to see if our
casting here is screwing anything up let's run it one more time.
OK.
There we go.
Oh you know what's happening.
We don't want to pass in Miles.
Let's hear
it.
We do want to pass and Miles so we're not passing in Miles that's the problem.
So I just threw in run distance and run distances in meters right now so we're dividing it by a huge
number.
And we had to call our meters to Miles function or extension that we created.
So meters 10 miles to two decimal places is great.
All right up here we can now return this to the way it was
cast this as an integer again return pace dot pases a double.
Now we have to change that back to an integer.
OK so pace dat format time duration to string.
OK let's run this again and everything should work.
We just were dividing by way too big of number since we forgot to convert it back to Miles.
And let's start our run
and there we go we have a pace.
So obviously three minutes is crazy.
That makes more sense.
It takes a little bit.
It is average.
So as you start running.
So our average pace is now seven minutes and 46 seconds which is pretty good.
We have our duration counter going and our distance so we're pretty awesome.
Now we just got to get our pause button working because our button works and we're almost ready to set
up relm So in the next lesson we will get that pause button work in and we will also change the pause
button from pause to play depending on what state we're in.
And then after that I think we'll go we'll move on to getting our role models set up so we can actually
save our data instead of like right now we're stuck.
We can only see our current run if we end that run we lose our data.
So I will see you guys in the next lesson.