Skip to content

Commit

Permalink
Add location to View Daylight intent
Browse files Browse the repository at this point in the history
  • Loading branch information
daneden committed Dec 15, 2021
1 parent fda1181 commit 034ae11
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 11 deletions.
Binary file not shown.
27 changes: 23 additions & 4 deletions SolsticeIntents/IntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,22 @@ class IntentHandler: INExtension {

class ViewDaylightIntentHandler: NSObject, ViewDaylightIntentHandling {
func handle(intent: ViewDaylightIntent) async -> ViewDaylightIntentResponse {
if let date = intent.date?.date {
let solarCalculator = SolarCalculator(baseDate: date)
let duration = solarCalculator.today.duration
if let date = intent.date?.date,
let placemark = intent.location,
let location = placemark.location?.coordinate {
let solar = Solar(for: date, coordinate: location)

guard let sunrise = solar?.sunrise, let sunset = solar?.sunset else {
return .failure(error: "Unable to calculate daylight; the date provided may be invalid.")
}

let duration = sunrise.distance(to: sunset)

return .success(result: NSNumber(value: duration), date: Calendar.autoupdatingCurrent.dateComponents(Set(Calendar.Component.allCases), from: date))
return .success(
result: NSNumber(value: duration),
date: Calendar.autoupdatingCurrent.dateComponents(Set(Calendar.Component.allCases), from: date),
location: placemark
)
} else {
return .failure(error: "Unable to calculate daylight; the date provided may be invalid.")
}
Expand All @@ -44,6 +55,14 @@ class ViewDaylightIntentHandler: NSObject, ViewDaylightIntentHandling {
return .needsValue()
}
}

func resolveLocation(for intent: ViewDaylightIntent) async -> INPlacemarkResolutionResult {
if let location = intent.location {
return .success(with: location)
} else {
return .needsValue()
}
}
}

class ViewRemainingDaylightIntentHandler: NSObject, ViewRemainingDaylightIntentHandling {
Expand Down
72 changes: 65 additions & 7 deletions SolsticeIntents/Intents.intentdefinition
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
<key>INIntentKeyParameter</key>
<string>date</string>
<key>INIntentLastParameterTag</key>
<integer>2</integer>
<integer>4</integer>
<key>INIntentManagedParameterCombinations</key>
<dict>
<key>date</key>
<key>date,location</key>
<dict>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
Expand All @@ -49,16 +49,18 @@
<string>ViewDaylight</string>
<key>INIntentParameterCombinations</key>
<dict>
<key>date</key>
<key>date,location</key>
<dict>
<key>INIntentParameterCombinationIsLinked</key>
<true/>
<key>INIntentParameterCombinationIsPrimary</key>
<true/>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
<key>INIntentParameterCombinationTitle</key>
<string>View daylight for ${date}</string>
<key>INIntentParameterCombinationTitleID</key>
<string>6YvyDM</string>
<string>j2Uuyj</string>
</dict>
</dict>
<key>INIntentParameters</key>
Expand Down Expand Up @@ -99,7 +101,7 @@
<key>INIntentParameterPromptDialogCustom</key>
<true/>
<key>INIntentParameterPromptDialogFormatString</key>
<string>Which date would you like to see daylight for?</string>
<string>On which date?</string>
<key>INIntentParameterPromptDialogFormatStringID</key>
<string>KsjApk</string>
<key>INIntentParameterPromptDialogType</key>
Expand All @@ -126,6 +128,48 @@
</dict>
</array>
</dict>
<dict>
<key>INIntentParameterConfigurable</key>
<true/>
<key>INIntentParameterDisplayName</key>
<string>Location</string>
<key>INIntentParameterDisplayNameID</key>
<string>O3jEev</string>
<key>INIntentParameterDisplayPriority</key>
<integer>2</integer>
<key>INIntentParameterMetadata</key>
<dict>
<key>INIntentParameterMetadataType</key>
<string>Name</string>
</dict>
<key>INIntentParameterName</key>
<string>location</string>
<key>INIntentParameterPromptDialogs</key>
<array>
<dict>
<key>INIntentParameterPromptDialogCustom</key>
<true/>
<key>INIntentParameterPromptDialogType</key>
<string>Configuration</string>
</dict>
<dict>
<key>INIntentParameterPromptDialogCustom</key>
<true/>
<key>INIntentParameterPromptDialogFormatString</key>
<string>In which location?</string>
<key>INIntentParameterPromptDialogFormatStringID</key>
<string>pkhuvX</string>
<key>INIntentParameterPromptDialogType</key>
<string>Primary</string>
</dict>
</array>
<key>INIntentParameterSupportsResolution</key>
<true/>
<key>INIntentParameterTag</key>
<integer>4</integer>
<key>INIntentParameterType</key>
<string>Placemark</string>
</dict>
</array>
<key>INIntentResponse</key>
<dict>
Expand All @@ -137,7 +181,7 @@
<key>INIntentResponseCodeConciseFormatStringID</key>
<string>ui2NLN</string>
<key>INIntentResponseCodeFormatString</key>
<string>${result} of daylight on ${date}</string>
<string>${result} of daylight on ${date} in ${location}</string>
<key>INIntentResponseCodeFormatStringID</key>
<string>ZvRTIv</string>
<key>INIntentResponseCodeName</key>
Expand All @@ -159,7 +203,7 @@
</dict>
</array>
<key>INIntentResponseLastParameterTag</key>
<integer>6</integer>
<integer>9</integer>
<key>INIntentResponseOutput</key>
<string>result</string>
<key>INIntentResponseParameters</key>
Expand Down Expand Up @@ -232,6 +276,20 @@
<key>INIntentResponseParameterType</key>
<string>DateComponents</string>
</dict>
<dict>
<key>INIntentResponseParameterDisplayName</key>
<string>Location</string>
<key>INIntentResponseParameterDisplayNameID</key>
<string>iecydw</string>
<key>INIntentResponseParameterDisplayPriority</key>
<integer>4</integer>
<key>INIntentResponseParameterName</key>
<string>location</string>
<key>INIntentResponseParameterTag</key>
<integer>9</integer>
<key>INIntentResponseParameterType</key>
<string>Placemark</string>
</dict>
</array>
</dict>
<key>INIntentTitle</key>
Expand Down

0 comments on commit 034ae11

Please sign in to comment.