Hey everybody what is going on in this video.
We're going to go ahead and handle that Syria kit data that has been passed into our app delegate and
set in our data service and we're going to go ahead and actually send notification from our app delegate
all the way to our view controller in this video so that we can know when the data has properly been
set and used that to present it in our labels.
So let's go ahead and pull open our X code project and we're going to go into our app delegate here
and we're going to go underneath where we set our start work out intent in our data service and we're
going to send a notification saying hey someone is wanting to start a workout.
Let's let's do it.
So we're going to go ahead and call notification center default to access the default notification center
and we're just going to go ahead and call post and you can just choose the one right here with a name
and an object.
So go ahead and we're going to pass in NSA notification name and to do that you just need to put some
parentheses and add a name at the end and we're just going to say work out work out started notification.
That's going to be its name and the object.
We don't need to necessarily pass anything in because we already set the intent here.
But the thing we need to think about is in our intent handler let me open it up here if the spoken phrase
is walk right or run we pass in with you know handling it in the app versus failure no matching workout.
Remember when I said cycle it's still printed out cycle.
And so I think what I'm going to do is I'm actually going to pass nyl to make sure that it's nil and
it's not getting set at all from Siri so that we don't actually pass in data we don't want.
And so what's happening now is once that's passed to our app we're posting a notification saying hey
you controller there is now a value in start work out intent and that will help us to successfully update
our labels.
So let's go to work out Visi like so.
And what we're going to do is we're first going to go ahead and set up an observer to be paying attention
to the workouts started notification and whenever it's called it's going to go ahead and call a function
in this class.
So let's call notification center default at observer and we're going to choose the one here with an
observer selector name an object and the observer is going to be self right.
This view controllers paying attention.
The selector is going to be for the function that we call the name we already know we can call an s
notification name and that name is going to be Workout started notification.
Now the object just like before will be nil and the selector.
This is where we need to pass a function.
So let's create a function that's going to handle what happens when we get that data from our data service.
So call write a function here called phunk handle Syrie request.
Make sure you type it properly.
And let's actually give you some more room here.
It's kind of squishy down there near the bottom so handle Syrie request.
And what we're going to do is we're basically going to create a property to hold our intent the gold
value which is like the amount of seconds and also the workout type walk or run.
So use guard let here because remember our data service here holds an optional value because there might
not be a value.
Right it might not get set.
So we're going to say guard let intent equals data service for instance that start work out intent.
OK we're going to actually set three properties here.
So assuming that we can you know successfully set the intent we're going to use that to create goal
value and we're going to set that to be equal to intent doggle value and that's going to be a double
value then we're going to type let workout type work out type equals intent.
Work out.
Name Okay.
And we're going to pull out the spoken phrase and that's either going to be walk or run and it's cool
because we can even say you know started jogging workout for 20 minutes and it's going to say running
because jogging is essentially running just with the different speed.
So assuming we can get all three of these optional values we're going to say.
Else.
For what happens if we can't get them.
We're going to go ahead and make sure the timer label is still hidden because we are going to show it
but we're going to hide it if it doesn't work.
So if we can't get these values it's going to stay hidden.
And what is this named.
I named it type label I believe.
Yeah type label dot is hidden is hidden equals true.
OK then we're going to return so that we don't crash.
So if we can't get the value we're going to make sure that our labels and type labels stay hidden and
you'll see why I did that in just a second but assuming we get an intent we get a gold value and a workout
type.
We're going to go ahead and unhide our timer label and type labels so type type label dot is hidden
whip's hidden equals false.
OK because then they're going to show because we actually have a value.
Sorry guys I can't type today.
False and Taimur label that is hidden equals false as well.
So now they're both showing.
Then we're going to go ahead and set the value of those labels to show the type of work out.
So type label text equals workout type OK and that's a string because that's the spoken phrase that
comes through.
But you know what if I remember right in my label here it says type colon.
And then I want to display workout so I need to make my label say that too.
So let's go ahead and write a string right.
Type a space and then we're going to use string encapsulation here to type workout type.
And the interesting thing is is that this is going to come in as lowercase because if you remember in
our consul that prints out run comes in as lowercase cycle came in as lowercase no doesn't really matter.
But we're going to say Dot capitalized so that we can capitalize that word.
So it looks a little more professional then.
Below this we have our timer label so our timer label dot text is going to be equal to a string and
we're going to go ahead and pass in our goal value.
OK.
And we want it to say the time like this you know like two minutes 37 seconds left.
And that's where the gold values going to come into play.
But if we printed out if we actually print it you'll you'll see that it's a problem.
So let's go ahead let's do that really quick.
And I want you to see how it comes out.
But before we do that we need to set our selector So the selector is going to be pound sign selector
and then we're going to pass in the function handle Syrie request.
Now it's going to throw an error check it out it's going to say hey you can't do that and that's because
in swift for selector methods need to be exposed to objective Sisa just click fix and it'll say at 0
BJC before the function.
That's just something you have to do now.
And yeah.
So I don't know about you but I want to go try it.
Let's build and run it.
Let's go see how we do.
We should get an intent and we should print out what we what we want in our app.
So go ahead and hold it down and let's let's think about start a three minute walking workout which
leg day
case when we open it the label should not be hidden and it should show the type and the time OK look
at that.
So they're not hidden.
It says walk because that's what we told it.
And it says 180 left.
I said three minutes right 60 seconds times three is 180.
So we get a double of 180 for the seconds and that makes sense.
But what we need to do is to write a little extension that will convert that seconds value into minutes
and seconds separated with a colon.
So let's go ahead and let's build that now by right clicking on extensions clicking a new file and then
creating a swift file.
OK.
Now we're going to name this double x XTi and we're going to go ahead and save it and make sure that
the target leg day is selected.
And now we have a nice little extension and so we can create an extension of type double K and we can
write a little function.
And now any value that's the double We can call this function on.
So go ahead and write phunk convert to clock time and return a string.
OK.
Now what we're going to do is we're going to take the total amount of seconds and in order to get the
minutes we're going to just divide whatever the double value is by 60.
OK.
So check this out we're going to type let minutes equals self divided by 60.
OK.
So what if I said you know three minutes and 27 seconds you know maybe that's like one.
Ninety five seconds or something that's not exactly right.
But if I divide that by 60 you'll see we don't get a whole number we get three point twenty five.
So how is this going to work.
Well what we're going to do is we're actually going to take self and we're going to cast it as an int.
And what this is going to do is it's going to take the value of the double.
So in our instance one hundred ninety five and maybe it's one ninety five point six five six whatever.
And it's going to go ahead and divide that by 60.
OK so it's three point to six blah blah blah.
Now that is interesting right because we have our minutes set.
But what it's going to do is it's basically going to take since it's an integer it's not going to pay
attention to the point to six it'll only keep the three which is great because that's you know three
minutes.
Now for the seconds that's a different story we need to basically take the same value but we need to
figure out what's left over.
Right.
What is the leftover chunk.
Here it's point to six.
So to figure out the seconds we're going to say let seconds equals and we're going to pass in self.
So if it was you know one point one hundred ninety five seconds point six three whatever it would just
be one hundred ninety five.
Right then what we're going to do is we're going to take the Madieu low and we're going to do medu low
60.
And so how this is going to work is it's basically going to take the first value.
OK so maybe 1 9 5 point.
Well I guess it would just be 1 9 5 because the end will cut off the end and it's going to divide it
by the second number which is 60.
And then what it's going to do is it's going to keep the remainder.
OK so it's going to keep the remainder which at this point is.
Right.
So we can actually subtract three and we have point 5 1 minute left over.
And what we're going to do then is just multiply that by 60 and we get 15 seconds.
So that's how it's going to work is it's going to divide cut the rest.
And then we're going to actually return that using a string format.
So we need to return a string.
So we're going to return.
Whoops we're going to return a string.
Sorry guys my Arkie is sticky.
We're going to return a string and we're going to use string with format which is right here and we're
going to use the one with arguments.
So the format for the string is going to be percent sign 0 D.
And that's going to be for the minutes.
And that's basically saying that we're just going to display a whole number then a colon then we're
going to do.
Percent sign 0 to D and that's saying the numbers that come in are going to take up two places.
So there's two slots for the numbers and the arguments that come in are minutes for the first parameter
and second for the second parameter.
So whatever we get here we're going to cast it as seconds a point to five are going to cast it to them
as 15 seconds and three minutes is going to just get cast as a three and there's going to be a colon
separating them.
So now all we need to do is go ahead and type goal value dot convert to clock time and it's going to
actually print out the string for the converted clock time.
Let's BuildOn run let's see how we did.
Extensions are really cool.
So here we go it's building and running.
It's going to open up on my simulator here.
And let's try it.
Start a three minute 27 second walking workout.
Which act would you like to use.
Leg day.
Can you ready.
Here we go.
Boom.
Three minutes 27 seconds left and it's a clock time which makes much more sense than 180 seconds.
When we said three minutes and 27 seconds so.
Amazing amazing stuff here guys.
Check it out.
We're able to tell Siri to do something we can pass in that data from the intent and we can take action
on it in our application.
Super super amazing work guys.
This has been an intro to Siri kit.
And then the next video I'm going to give you a challenge to make this app even more impressive.
Super duper good stuff for your resume or for a portfolio So let's head over to the next video for your
challenge.
I'll see you there.