I am trying to create an App Intent that lets a user select a day in the itinerary of a trip. The trip has to be chosen before the days available can be displayed.
When the PlanActivityIntentDemo intent is ran from the shortcuts app, the trip selected is not injected into the appropriate TripItineraryDayQueryDemo Entity Query. Is there a way to get the selected trip to be injected at run time from shortcuts app. Here's some code for illustration:
// Entity Definition:
import AppIntents
struct ShortcutsItineraryDayEntityDemo: Identifiable, Hashable, AppEntity {
typealias DefaultQuery = TripItineraryDayQueryDemo
static var typeDisplayRepresentation: TypeDisplayRepresentation = "Trip Itinerary Day"
var displayRepresentation: DisplayRepresentation {
"Trip Day"
}
var id: String
static var defaultQuery: DefaultQuery {
TripItineraryDayQueryDemo()
}
init() {
self.id = UUID().uuidString
}
}
struct TripItineraryDayQueryDemo: EntityQuery {
// This only works in shortcut editor but not at runtime. Why? How can I fix this issue?
@IntentParameterDependency<PlanActivityIntentDemo>(\.$tripEntity)
var tripEntity
@IntentParameterDependency<PlanActivityIntentDemo>(\.$title)
var intentTitle
func entities(for identifiers: [ShortcutsItineraryDayEntityDemo.ID]) async throws -> [ShortcutsItineraryDayEntityDemo] {
print("entities being called with identifiers: \(identifiers)")
// This method is called when the app needs to fetch entities based on identifiers.
let tripsStore = TripsStore()
guard let trip = tripEntity?.tripEntity.trip,
let itineraryId = trip.firstItineraryId else {
print("No trip or itinerary ID can be found for the selected trip.")
return []
}
return [] // return empty for this demo
}
func suggestedEntities() async throws -> [ShortcutsItineraryDayEntityDemo] {
print("suggested itinerary days being called")
let tripsStore = TripsStore()
guard let trip = tripEntity?.tripEntity.trip,
let itineraryId = trip.firstItineraryId else {
print("No trip or itinerary ID found for the selected trip.")
return []
}
return []
}
}
struct PlanActivityIntentDemo: AppIntent {
static var title: LocalizedStringResource { "Plan New Activity" }
// The selected trip fails to get injected when intent is run from shortcut app
@Parameter(title: "Trip", description: "The trip to plan an activity for", requestValueDialog: "Which trip would you like to plan an activity for?")
var tripEntity: ShortcutsTripEntity
@Parameter(title: "Activity Title", description: "The title of the activity", requestValueDialog: "What do you want to do or see?")
var title: String
@Parameter(title: "Activity Day", description: "Activity Day")
var activityDay: ShortcutsItineraryDayEntity
func perform() async throws -> some ProvidesDialog {
// This is a demo intent, so we won't actually perform any actions.
.result(dialog: "Activity '\(title)' planned")
}
}
Topic:
App & System Services
SubTopic:
Automation & Scripting
Tags:
SwiftUI
App Intents
Apple Intelligence