1
00:00:06,870 --> 00:00:09,450
Hey everybody what is going on in this video.

2
00:00:09,450 --> 00:00:13,930
We're going to go ahead and handle that Syria kit data that has been passed into our app delegate and

3
00:00:13,960 --> 00:00:20,530
set in our data service and we're going to go ahead and actually send notification from our app delegate

4
00:00:20,860 --> 00:00:25,900
all the way to our view controller in this video so that we can know when the data has properly been

5
00:00:25,900 --> 00:00:29,230
set and used that to present it in our labels.

6
00:00:29,230 --> 00:00:34,990
So let's go ahead and pull open our X code project and we're going to go into our app delegate here

7
00:00:35,530 --> 00:00:41,230
and we're going to go underneath where we set our start work out intent in our data service and we're

8
00:00:41,230 --> 00:00:45,280
going to send a notification saying hey someone is wanting to start a workout.

9
00:00:45,310 --> 00:00:46,290
Let's let's do it.

10
00:00:46,300 --> 00:00:55,540
So we're going to go ahead and call notification center default to access the default notification center

11
00:00:55,540 --> 00:01:00,010
and we're just going to go ahead and call post and you can just choose the one right here with a name

12
00:01:00,910 --> 00:01:01,840
and an object.

13
00:01:01,840 --> 00:01:07,690
So go ahead and we're going to pass in NSA notification name and to do that you just need to put some

14
00:01:07,690 --> 00:01:16,090
parentheses and add a name at the end and we're just going to say work out work out started notification.

15
00:01:16,090 --> 00:01:18,000
That's going to be its name and the object.

16
00:01:18,000 --> 00:01:22,460
We don't need to necessarily pass anything in because we already set the intent here.

17
00:01:22,660 --> 00:01:29,710
But the thing we need to think about is in our intent handler let me open it up here if the spoken phrase

18
00:01:29,710 --> 00:01:37,720
is walk right or run we pass in with you know handling it in the app versus failure no matching workout.

19
00:01:37,720 --> 00:01:41,450
Remember when I said cycle it's still printed out cycle.

20
00:01:41,590 --> 00:01:46,750
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

21
00:01:46,750 --> 00:01:53,180
it's not getting set at all from Siri so that we don't actually pass in data we don't want.

22
00:01:53,320 --> 00:01:59,260
And so what's happening now is once that's passed to our app we're posting a notification saying hey

23
00:01:59,280 --> 00:02:05,440
you controller there is now a value in start work out intent and that will help us to successfully update

24
00:02:05,440 --> 00:02:06,390
our labels.

25
00:02:06,400 --> 00:02:09,730
So let's go to work out Visi like so.

26
00:02:09,760 --> 00:02:15,160
And what we're going to do is we're first going to go ahead and set up an observer to be paying attention

27
00:02:15,970 --> 00:02:20,470
to the workouts started notification and whenever it's called it's going to go ahead and call a function

28
00:02:20,470 --> 00:02:21,510
in this class.

29
00:02:21,670 --> 00:02:28,570
So let's call notification center default at observer and we're going to choose the one here with an

30
00:02:28,570 --> 00:02:33,880
observer selector name an object and the observer is going to be self right.

31
00:02:33,880 --> 00:02:36,070
This view controllers paying attention.

32
00:02:36,070 --> 00:02:41,440
The selector is going to be for the function that we call the name we already know we can call an s

33
00:02:41,440 --> 00:02:48,930
notification name and that name is going to be Workout started notification.

34
00:02:48,940 --> 00:02:52,820
Now the object just like before will be nil and the selector.

35
00:02:52,840 --> 00:02:55,150
This is where we need to pass a function.

36
00:02:55,150 --> 00:03:02,470
So let's create a function that's going to handle what happens when we get that data from our data service.

37
00:03:02,470 --> 00:03:08,610
So call write a function here called phunk handle Syrie request.

38
00:03:08,740 --> 00:03:10,160
Make sure you type it properly.

39
00:03:10,570 --> 00:03:12,340
And let's actually give you some more room here.

40
00:03:12,340 --> 00:03:16,900
It's kind of squishy down there near the bottom so handle Syrie request.

41
00:03:16,900 --> 00:03:22,660
And what we're going to do is we're basically going to create a property to hold our intent the gold

42
00:03:22,660 --> 00:03:27,720
value which is like the amount of seconds and also the workout type walk or run.

43
00:03:27,790 --> 00:03:35,650
So use guard let here because remember our data service here holds an optional value because there might

44
00:03:35,650 --> 00:03:36,500
not be a value.

45
00:03:36,520 --> 00:03:38,100
Right it might not get set.

46
00:03:38,170 --> 00:03:46,200
So we're going to say guard let intent equals data service for instance that start work out intent.

47
00:03:46,480 --> 00:03:49,170
OK we're going to actually set three properties here.

48
00:03:49,180 --> 00:03:55,840
So assuming that we can you know successfully set the intent we're going to use that to create goal

49
00:03:56,230 --> 00:04:01,810
value and we're going to set that to be equal to intent doggle value and that's going to be a double

50
00:04:01,810 --> 00:04:09,060
value then we're going to type let workout type work out type equals intent.

51
00:04:09,250 --> 00:04:10,060
Work out.

52
00:04:10,060 --> 00:04:11,330
Name Okay.

53
00:04:11,680 --> 00:04:16,660
And we're going to pull out the spoken phrase and that's either going to be walk or run and it's cool

54
00:04:16,660 --> 00:04:21,160
because we can even say you know started jogging workout for 20 minutes and it's going to say running

55
00:04:21,160 --> 00:04:24,010
because jogging is essentially running just with the different speed.

56
00:04:24,430 --> 00:04:28,360
So assuming we can get all three of these optional values we're going to say.

57
00:04:28,360 --> 00:04:29,810
Else.

58
00:04:30,340 --> 00:04:32,470
For what happens if we can't get them.

59
00:04:32,710 --> 00:04:37,570
We're going to go ahead and make sure the timer label is still hidden because we are going to show it

60
00:04:37,930 --> 00:04:39,850
but we're going to hide it if it doesn't work.

61
00:04:39,850 --> 00:04:42,380
So if we can't get these values it's going to stay hidden.

62
00:04:42,740 --> 00:04:45,010
And what is this named.

63
00:04:45,160 --> 00:04:47,130
I named it type label I believe.

64
00:04:47,170 --> 00:04:53,930
Yeah type label dot is hidden is hidden equals true.

65
00:04:54,660 --> 00:04:57,690
OK then we're going to return so that we don't crash.

66
00:04:57,700 --> 00:05:01,780
So if we can't get the value we're going to make sure that our labels and type labels stay hidden and

67
00:05:01,780 --> 00:05:07,860
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

68
00:05:07,900 --> 00:05:08,560
type.

69
00:05:08,560 --> 00:05:15,190
We're going to go ahead and unhide our timer label and type labels so type type label dot is hidden

70
00:05:16,210 --> 00:05:19,040
whip's hidden equals false.

71
00:05:19,180 --> 00:05:22,480
OK because then they're going to show because we actually have a value.

72
00:05:22,480 --> 00:05:24,010
Sorry guys I can't type today.

73
00:05:24,010 --> 00:05:31,520
False and Taimur label that is hidden equals false as well.

74
00:05:31,540 --> 00:05:33,140
So now they're both showing.

75
00:05:33,280 --> 00:05:38,950
Then we're going to go ahead and set the value of those labels to show the type of work out.

76
00:05:39,040 --> 00:05:45,520
So type label text equals workout type OK and that's a string because that's the spoken phrase that

77
00:05:45,520 --> 00:05:46,740
comes through.

78
00:05:47,530 --> 00:05:52,080
But you know what if I remember right in my label here it says type colon.

79
00:05:52,180 --> 00:05:56,280
And then I want to display workout so I need to make my label say that too.

80
00:05:56,290 --> 00:05:59,050
So let's go ahead and write a string right.

81
00:05:59,050 --> 00:06:07,580
Type a space and then we're going to use string encapsulation here to type workout type.

82
00:06:07,720 --> 00:06:11,890
And the interesting thing is is that this is going to come in as lowercase because if you remember in

83
00:06:11,890 --> 00:06:17,650
our consul that prints out run comes in as lowercase cycle came in as lowercase no doesn't really matter.

84
00:06:17,650 --> 00:06:22,050
But we're going to say Dot capitalized so that we can capitalize that word.

85
00:06:22,050 --> 00:06:24,480
So it looks a little more professional then.

86
00:06:24,520 --> 00:06:32,400
Below this we have our timer label so our timer label dot text is going to be equal to a string and

87
00:06:32,460 --> 00:06:35,570
we're going to go ahead and pass in our goal value.

88
00:06:35,850 --> 00:06:36,230
OK.

89
00:06:36,250 --> 00:06:41,960
And we want it to say the time like this you know like two minutes 37 seconds left.

90
00:06:42,130 --> 00:06:44,450
And that's where the gold values going to come into play.

91
00:06:44,710 --> 00:06:49,300
But if we printed out if we actually print it you'll you'll see that it's a problem.

92
00:06:49,300 --> 00:06:51,010
So let's go ahead let's do that really quick.

93
00:06:51,010 --> 00:06:52,960
And I want you to see how it comes out.

94
00:06:53,440 --> 00:06:58,810
But before we do that we need to set our selector So the selector is going to be pound sign selector

95
00:06:59,620 --> 00:07:03,610
and then we're going to pass in the function handle Syrie request.

96
00:07:03,640 --> 00:07:07,990
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

97
00:07:07,990 --> 00:07:14,450
in swift for selector methods need to be exposed to objective Sisa just click fix and it'll say at 0

98
00:07:14,450 --> 00:07:16,240
BJC before the function.

99
00:07:16,270 --> 00:07:17,840
That's just something you have to do now.

100
00:07:18,100 --> 00:07:19,320
And yeah.

101
00:07:19,570 --> 00:07:21,480
So I don't know about you but I want to go try it.

102
00:07:21,580 --> 00:07:22,530
Let's build and run it.

103
00:07:22,660 --> 00:07:24,190
Let's go see how we do.

104
00:07:24,400 --> 00:07:28,510
We should get an intent and we should print out what we what we want in our app.

105
00:07:28,660 --> 00:07:38,010
So go ahead and hold it down and let's let's think about start a three minute walking workout which

106
00:07:39,920 --> 00:07:40,400
leg day

107
00:07:44,850 --> 00:07:52,090
case when we open it the label should not be hidden and it should show the type and the time OK look

108
00:07:52,090 --> 00:07:52,700
at that.

109
00:07:52,870 --> 00:07:54,010
So they're not hidden.

110
00:07:54,010 --> 00:07:55,880
It says walk because that's what we told it.

111
00:07:56,050 --> 00:07:57,750
And it says 180 left.

112
00:07:57,760 --> 00:08:01,860
I said three minutes right 60 seconds times three is 180.

113
00:08:01,930 --> 00:08:07,510
So we get a double of 180 for the seconds and that makes sense.

114
00:08:07,510 --> 00:08:14,650
But what we need to do is to write a little extension that will convert that seconds value into minutes

115
00:08:14,650 --> 00:08:17,140
and seconds separated with a colon.

116
00:08:17,140 --> 00:08:23,890
So let's go ahead and let's build that now by right clicking on extensions clicking a new file and then

117
00:08:23,980 --> 00:08:25,740
creating a swift file.

118
00:08:25,920 --> 00:08:26,430
OK.

119
00:08:26,680 --> 00:08:32,800
Now we're going to name this double x XTi and we're going to go ahead and save it and make sure that

120
00:08:32,800 --> 00:08:34,660
the target leg day is selected.

121
00:08:35,140 --> 00:08:42,640
And now we have a nice little extension and so we can create an extension of type double K and we can

122
00:08:42,640 --> 00:08:43,500
write a little function.

123
00:08:43,510 --> 00:08:46,360
And now any value that's the double We can call this function on.

124
00:08:46,360 --> 00:08:55,010
So go ahead and write phunk convert to clock time and return a string.

125
00:08:55,950 --> 00:08:56,800
OK.

126
00:08:57,040 --> 00:09:03,910
Now what we're going to do is we're going to take the total amount of seconds and in order to get the

127
00:09:03,970 --> 00:09:09,720
minutes we're going to just divide whatever the double value is by 60.

128
00:09:09,790 --> 00:09:10,480
OK.

129
00:09:10,510 --> 00:09:17,010
So check this out we're going to type let minutes equals self divided by 60.

130
00:09:17,280 --> 00:09:17,680
OK.

131
00:09:17,740 --> 00:09:22,810
So what if I said you know three minutes and 27 seconds you know maybe that's like one.

132
00:09:22,810 --> 00:09:25,470
Ninety five seconds or something that's not exactly right.

133
00:09:25,480 --> 00:09:30,850
But if I divide that by 60 you'll see we don't get a whole number we get three point twenty five.

134
00:09:30,850 --> 00:09:32,370
So how is this going to work.

135
00:09:32,630 --> 00:09:38,720
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.

136
00:09:38,920 --> 00:09:42,050
And what this is going to do is it's going to take the value of the double.

137
00:09:42,070 --> 00:09:47,240
So in our instance one hundred ninety five and maybe it's one ninety five point six five six whatever.

138
00:09:47,380 --> 00:09:50,030
And it's going to go ahead and divide that by 60.

139
00:09:50,260 --> 00:09:52,750
OK so it's three point to six blah blah blah.

140
00:09:52,750 --> 00:09:58,270
Now that is interesting right because we have our minutes set.

141
00:09:58,270 --> 00:10:03,430
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

142
00:10:03,430 --> 00:10:08,350
attention to the point to six it'll only keep the three which is great because that's you know three

143
00:10:08,350 --> 00:10:09,240
minutes.

144
00:10:09,460 --> 00:10:14,500
Now for the seconds that's a different story we need to basically take the same value but we need to

145
00:10:14,500 --> 00:10:16,140
figure out what's left over.

146
00:10:16,240 --> 00:10:16,540
Right.

147
00:10:16,540 --> 00:10:18,060
What is the leftover chunk.

148
00:10:18,060 --> 00:10:19,920
Here it's point to six.

149
00:10:19,930 --> 00:10:26,780
So to figure out the seconds we're going to say let seconds equals and we're going to pass in self.

150
00:10:26,890 --> 00:10:32,170
So if it was you know one point one hundred ninety five seconds point six three whatever it would just

151
00:10:32,170 --> 00:10:33,960
be one hundred ninety five.

152
00:10:34,090 --> 00:10:39,790
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

153
00:10:39,850 --> 00:10:40,900
60.

154
00:10:40,900 --> 00:10:44,190
And so how this is going to work is it's basically going to take the first value.

155
00:10:44,210 --> 00:10:46,970
OK so maybe 1 9 5 point.

156
00:10:47,020 --> 00:10:51,850
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

157
00:10:52,240 --> 00:10:55,010
by the second number which is 60.

158
00:10:55,360 --> 00:10:58,650
And then what it's going to do is it's going to keep the remainder.

159
00:10:58,850 --> 00:11:02,390
OK so it's going to keep the remainder which at this point is.

160
00:11:02,420 --> 00:11:02,660
Right.

161
00:11:02,690 --> 00:11:08,150
So we can actually subtract three and we have point 5 1 minute left over.

162
00:11:08,440 --> 00:11:13,140
And what we're going to do then is just multiply that by 60 and we get 15 seconds.

163
00:11:13,150 --> 00:11:16,070
So that's how it's going to work is it's going to divide cut the rest.

164
00:11:16,090 --> 00:11:20,950
And then we're going to actually return that using a string format.

165
00:11:20,950 --> 00:11:23,300
So we need to return a string.

166
00:11:23,350 --> 00:11:24,680
So we're going to return.

167
00:11:24,970 --> 00:11:29,260
Whoops we're going to return a string.

168
00:11:29,260 --> 00:11:30,850
Sorry guys my Arkie is sticky.

169
00:11:30,850 --> 00:11:36,640
We're going to return a string and we're going to use string with format which is right here and we're

170
00:11:36,640 --> 00:11:39,040
going to use the one with arguments.

171
00:11:39,070 --> 00:11:44,840
So the format for the string is going to be percent sign 0 D.

172
00:11:45,070 --> 00:11:46,610
And that's going to be for the minutes.

173
00:11:46,720 --> 00:11:54,160
And that's basically saying that we're just going to display a whole number then a colon then we're

174
00:11:54,160 --> 00:11:54,490
going to do.

175
00:11:54,490 --> 00:12:02,210
Percent sign 0 to D and that's saying the numbers that come in are going to take up two places.

176
00:12:02,230 --> 00:12:08,620
So there's two slots for the numbers and the arguments that come in are minutes for the first parameter

177
00:12:09,100 --> 00:12:11,350
and second for the second parameter.

178
00:12:11,350 --> 00:12:15,910
So whatever we get here we're going to cast it as seconds a point to five are going to cast it to them

179
00:12:16,060 --> 00:12:21,550
as 15 seconds and three minutes is going to just get cast as a three and there's going to be a colon

180
00:12:21,610 --> 00:12:22,770
separating them.

181
00:12:22,780 --> 00:12:29,890
So now all we need to do is go ahead and type goal value dot convert to clock time and it's going to

182
00:12:29,890 --> 00:12:34,110
actually print out the string for the converted clock time.

183
00:12:34,150 --> 00:12:35,910
Let's BuildOn run let's see how we did.

184
00:12:35,980 --> 00:12:37,480
Extensions are really cool.

185
00:12:37,840 --> 00:12:40,050
So here we go it's building and running.

186
00:12:40,150 --> 00:12:42,460
It's going to open up on my simulator here.

187
00:12:42,480 --> 00:12:44,190
And let's try it.

188
00:12:44,250 --> 00:12:47,830
Start a three minute 27 second walking workout.

189
00:12:48,740 --> 00:12:50,270
Which act would you like to use.

190
00:12:51,010 --> 00:12:54,620
Leg day.

191
00:12:55,460 --> 00:12:56,290
Can you ready.

192
00:12:56,290 --> 00:12:57,360
Here we go.

193
00:12:57,580 --> 00:12:58,170
Boom.

194
00:12:58,180 --> 00:13:04,480
Three minutes 27 seconds left and it's a clock time which makes much more sense than 180 seconds.

195
00:13:04,480 --> 00:13:06,700
When we said three minutes and 27 seconds so.

196
00:13:06,970 --> 00:13:08,960
Amazing amazing stuff here guys.

197
00:13:08,980 --> 00:13:09,700
Check it out.

198
00:13:09,700 --> 00:13:15,250
We're able to tell Siri to do something we can pass in that data from the intent and we can take action

199
00:13:15,250 --> 00:13:16,800
on it in our application.

200
00:13:16,810 --> 00:13:18,400
Super super amazing work guys.

201
00:13:18,400 --> 00:13:20,940
This has been an intro to Siri kit.

202
00:13:21,100 --> 00:13:25,460
And then the next video I'm going to give you a challenge to make this app even more impressive.

203
00:13:25,540 --> 00:13:31,390
Super duper good stuff for your resume or for a portfolio So let's head over to the next video for your

204
00:13:31,390 --> 00:13:31,960
challenge.

205
00:13:31,960 --> 00:13:33,180
I'll see you there.
