Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Intents/GetSunriseTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
import Foundation
import AppIntents
import CoreLocation
import Solar
import SunKit

struct GetSunriseTime: AppIntent {
static var title: LocalizedStringResource = "Get Sunrise Time"
static var description = IntentDescription("Calculate the sunrise time on a given date in a given location")

@Parameter(title: "Date")
var date: Date

@Parameter(title: "Location")
var location: CLPlacemark

static var parameterSummary: some ParameterSummary {
Summary("Get the sunrise time on \(\.$date) in \(\.$location)")
}

func perform() async throws -> some IntentResult & ReturnsValue<Date?> & ProvidesDialog {
guard let coordinate = location.location?.coordinate else {
throw $location.needsValueError("What location do you want to see the sunrise for?")
}
let solar = Solar(for: date, coordinate: coordinate)

let sun = Sun(for: date, coordinate: coordinate)

return .result(
value: solar?.sunrise,
dialog: "\((solar?.sunrise ?? date).formatted(date: .omitted, time: .shortened))"
value: sun.sunrise,
dialog: "\((sun.sunrise ?? date).formatted(date: .omitted, time: .shortened))"
)
}
}
Expand Down
20 changes: 10 additions & 10 deletions Intents/GetSunsetTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
import Foundation
import AppIntents
import CoreLocation
import Solar
import SunKit

struct GetSunsetTime: AppIntent {
static var title: LocalizedStringResource = "Get Sunset Time"
static var description = IntentDescription("Calculate the sunset time on a given date in a given location")

@Parameter(title: "Date")
var date: Date

@Parameter(title: "Location")
var location: CLPlacemark

static var parameterSummary: some ParameterSummary {
Summary("Get the sunset time on \(\.$date) in \(\.$location)")
}

func perform() async throws -> some IntentResult & ReturnsValue<Date?> & ProvidesDialog {
guard let coordinate = location.location?.coordinate else {
throw $location.needsValueError("What location do you want to see the sunset for?")
}
let solar = Solar(for: date, coordinate: coordinate)

let sun = Sun(for: date, coordinate: coordinate)

return .result(
value: solar?.sunset,
dialog: "\((solar?.sunset ?? date).formatted(date: .omitted, time: .shortened))"
value: sun.sunset,
dialog: "\((sun.sunset ?? date).formatted(date: .omitted, time: .shortened))"
)
}
}
Expand Down
24 changes: 12 additions & 12 deletions Intents/ViewDaylight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@
import Foundation
import AppIntents
import CoreLocation
import Solar
import SunKit

struct ViewDaylight: AppIntent {
static var title: LocalizedStringResource = "View Daylight"
static var description = IntentDescription("View how much daylight there is on a given day, based on the duration from that days sunrise to sunset.")
static var description = IntentDescription("View how much daylight there is on a given day, based on the duration from that day's sunrise to sunset.")

@Parameter(title: "Date")
var date: Date

@Parameter(title: "Location")
var location: CLPlacemark

static var parameterSummary: some ParameterSummary {
Summary("Get the daylight duration on \(\.$date) in \(\.$location)")
}

func perform() async throws -> some IntentResult & ReturnsValue<TimeInterval> & ProvidesDialog {
guard let coordinate = location.location?.coordinate else {
throw $location.needsValueError("What location do you want to see the daylight for?")
}

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.allowedUnits = [.hour, .minute, .second]
let solar = Solar(for: date, coordinate: coordinate)
let duration = (solar?.sunrise ?? .now).distance(to: solar?.sunset ?? .now)

let sun = Sun(for: date, coordinate: coordinate)

let duration = sun.sunset.timeIntervalSince(sun.sunrise)

return .result(
value: duration,
dialog: "\(formatter.string(from: duration) ?? "") of daylight on \(date.formatted(date: .abbreviated, time: .omitted))"
Expand Down
30 changes: 15 additions & 15 deletions Intents/ViewRemainingDaylight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@

import Foundation
import AppIntents
import Solar
import SunKit
import CoreLocation

struct ViewRemainingDaylight: AppIntent {
static var title: LocalizedStringResource = "View Remaining Daylight"
static var description = IntentDescription("View how much daylight is remaining today, based on the time until sunset.")

@Parameter(title: "Location")
var location: CLPlacemark

static var parameterSummary: some ParameterSummary {
Summary("Get today's remaining daylight in \(\.$location)")
}

func perform() async throws -> some IntentResult & ReturnsValue<TimeInterval> & ProvidesDialog {
guard let coordinate = location.location?.coordinate else {
throw $location.needsValueError("What location do you want to see the daylight for?")
}
let solar = Solar(coordinate: coordinate)!

let sun = Sun(coordinate: coordinate)

var resultValue: TimeInterval

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.allowedUnits = [.hour, .minute, .second]
if (solar.safeSunrise...solar.safeSunset).contains(.now) {
resultValue = Date().distance(to: solar.safeSunset)

if (sun.safeSunrise...sun.safeSunset).contains(.now) {
resultValue = sun.safeSunset.timeIntervalSince(Date())
return .result(
value: resultValue,
dialog: "\(formatter.string(from: resultValue) ?? "") of daylight left today"
)
} else if solar.safeSunset < .now {
} else if sun.safeSunset < .now {
resultValue = 0
return .result(
value: resultValue,
dialog: "No daylight left today. The sun set \(formatter.string(from: solar.safeSunset.distance(to: .now)) ?? "") ago."
dialog: "No daylight left today. The sun set \(formatter.string(from: Date.now.timeIntervalSince(sun.safeSunset)) ?? "") ago."
)
} else if solar.safeSunrise > .now {
resultValue = solar.daylightDuration
} else if sun.safeSunrise > .now {
resultValue = sun.daylightDuration
return .result(
value: resultValue,
dialog: "\(formatter.string(from: resultValue) ?? "") of daylight left today"
Expand Down
8 changes: 8 additions & 0 deletions Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@
}
},
"ceeK/Solar" : {
"extractionState" : "stale",
"localizations" : {
"it" : {
"stringUnit" : {
Expand Down Expand Up @@ -1900,6 +1901,9 @@
}
}
}
},
"Sunlight-dev/SunKit" : {

},
"Sunrise" : {
"localizations" : {
Expand Down Expand Up @@ -2176,8 +2180,12 @@
}
}
}
},
"View how much daylight there is on a given day, based on the duration from that day's sunrise to sunset." : {

},
"View how much daylight there is on a given day, based on the duration from that day’s sunrise to sunset." : {
"extractionState" : "stale",
"localizations" : {
"it" : {
"stringUnit" : {
Expand Down
Loading