|
| 1 | +import AppIntents |
| 2 | +import Foundation |
| 3 | +import WordPressData |
| 4 | + |
| 5 | +/// Schedules a draft, pending, or already-scheduled post for a future date |
| 6 | +/// after the user confirms. |
| 7 | +struct SchedulePostIntent: AppIntent { |
| 8 | + static let title = LocalizedStringResource( |
| 9 | + "ios-appintents.schedulePost.title", |
| 10 | + defaultValue: "Schedule Post", |
| 11 | + table: "AppIntents", |
| 12 | + comment: "Title of the App Intent that schedules a post. Shown in the Shortcuts app and Spotlight." |
| 13 | + ) |
| 14 | + static let description = IntentDescription( |
| 15 | + LocalizedStringResource( |
| 16 | + "ios-appintents.schedulePost.description", |
| 17 | + defaultValue: "Schedules one of your draft, pending, or scheduled posts to publish at a future date.", |
| 18 | + table: "AppIntents", |
| 19 | + comment: "Description of the App Intent that schedules a post. Shown in the Shortcuts app." |
| 20 | + ) |
| 21 | + ) |
| 22 | + |
| 23 | + @Parameter( |
| 24 | + title: LocalizedStringResource( |
| 25 | + "ios-appintents.schedulePost.postParameter", |
| 26 | + defaultValue: "Post", |
| 27 | + table: "AppIntents", |
| 28 | + comment: "Label of the post parameter of the Schedule Post App Intent. Shown in the Shortcuts app." |
| 29 | + ), |
| 30 | + optionsProvider: PublishablePostOptionsProvider() |
| 31 | + ) |
| 32 | + var post: PostEntity |
| 33 | + |
| 34 | + @Parameter( |
| 35 | + title: LocalizedStringResource( |
| 36 | + "publishDatePicker.title", |
| 37 | + defaultValue: "Publish Date", |
| 38 | + table: "Localizable", |
| 39 | + comment: "Label of the date parameter of the Schedule Post App Intent. Shown in the Shortcuts app." |
| 40 | + ) |
| 41 | + ) |
| 42 | + var date: Date |
| 43 | + |
| 44 | + @MainActor |
| 45 | + func perform() async throws -> some IntentResult & ProvidesDialog & ReturnsValue<PostEntity> { |
| 46 | + let context = ContextManager.shared.mainContext |
| 47 | + guard let target = AbstractPost.forAppIntent(identifier: post.id, in: context) else { |
| 48 | + throw AppIntentPublishError.postNotFound |
| 49 | + } |
| 50 | + if let blocker = target.appIntentPublishingBlocker { |
| 51 | + throw AppIntentPublishError(blocker) |
| 52 | + } |
| 53 | + guard date > .now else { |
| 54 | + throw AppIntentPublishError.dateMustBeInFuture |
| 55 | + } |
| 56 | + |
| 57 | + let title = target.titleForDisplay() |
| 58 | + let formattedDate = date.formatted(date: .abbreviated, time: .shortened) |
| 59 | + // TODO: migrate to requestConfirmation(conditions:actionName:dialog:) once the deployment target reaches iOS 18. |
| 60 | + try await requestConfirmation( |
| 61 | + result: .result( |
| 62 | + dialog: IntentDialog( |
| 63 | + LocalizedStringResource( |
| 64 | + "ios-appintents.schedulePost.confirmationDialog", |
| 65 | + defaultValue: "Schedule “\(title)” for \(formattedDate)?", |
| 66 | + table: "AppIntents", |
| 67 | + comment: |
| 68 | + "Confirmation dialog shown by Siri or the Shortcuts app before scheduling. %1$@ is the post title, %2$@ the formatted publish date." |
| 69 | + ) |
| 70 | + ) |
| 71 | + ), |
| 72 | + confirmationActionName: .set |
| 73 | + ) |
| 74 | + // The post can change and the chosen date can lapse while the |
| 75 | + // confirmation dialog is up; re-check both before acting. |
| 76 | + if let blocker = target.appIntentPublishingBlocker { |
| 77 | + throw AppIntentPublishError(blocker) |
| 78 | + } |
| 79 | + guard date > .now else { |
| 80 | + throw AppIntentPublishError.dateMustBeInFuture |
| 81 | + } |
| 82 | + |
| 83 | + try await AppIntentPostSaving.save(target, changes: target.appIntentScheduleParameters(for: date)) |
| 84 | + |
| 85 | + guard let updated = PostEntity(post: target) else { |
| 86 | + throw AppIntentPublishError.postNotFound |
| 87 | + } |
| 88 | + // The server publishes immediately when the date is no longer far |
| 89 | + // enough in the future; the dialog reflects what it actually did. |
| 90 | + let dialog: IntentDialog |
| 91 | + if target.status == .publish { |
| 92 | + dialog = IntentDialog( |
| 93 | + LocalizedStringResource( |
| 94 | + "ios-appintents.schedulePost.publishedDialog", |
| 95 | + defaultValue: "Published “\(title)”.", |
| 96 | + table: "AppIntents", |
| 97 | + comment: |
| 98 | + "Dialog shown by Siri or the Shortcuts app when scheduling published the post immediately because its date was no longer in the future. %1$@ is the post title." |
| 99 | + ) |
| 100 | + ) |
| 101 | + } else { |
| 102 | + let scheduledDate = (target.dateCreated ?? date).formatted(date: .abbreviated, time: .shortened) |
| 103 | + dialog = IntentDialog( |
| 104 | + LocalizedStringResource( |
| 105 | + "ios-appintents.schedulePost.scheduledDialog", |
| 106 | + defaultValue: "Scheduled “\(title)” for \(scheduledDate).", |
| 107 | + table: "AppIntents", |
| 108 | + comment: |
| 109 | + "Dialog shown by Siri or the Shortcuts app after a post was scheduled. %1$@ is the post title, %2$@ the formatted publish date." |
| 110 | + ) |
| 111 | + ) |
| 112 | + } |
| 113 | + return .result(value: updated, dialog: dialog) |
| 114 | + } |
| 115 | +} |
0 commit comments