|
| 1 | +import AppIntents |
| 2 | +import Foundation |
| 3 | +import WordPressData |
| 4 | + |
| 5 | +/// A post or page on one of the user's sites, as exposed to the system via App Intents. |
| 6 | +/// |
| 7 | +/// The identifier is the same composite string the Spotlight index uses, so a Spotlight |
| 8 | +/// item and its associated app entity always name the same post. |
| 9 | +struct PostEntity: AppEntity { |
| 10 | + static let typeDisplayRepresentation = TypeDisplayRepresentation( |
| 11 | + name: LocalizedStringResource( |
| 12 | + "ios-appintents.postEntity.typeName", |
| 13 | + defaultValue: "Post", |
| 14 | + table: "AppIntents", |
| 15 | + comment: "Type name of the post entity in the Shortcuts app, e.g. shown when picking a post." |
| 16 | + ) |
| 17 | + ) |
| 18 | + |
| 19 | + static var defaultQuery: PostEntityQuery { PostEntityQuery() } |
| 20 | + |
| 21 | + let id: String |
| 22 | + let title: String |
| 23 | + let siteName: String? |
| 24 | + let isPage: Bool |
| 25 | + |
| 26 | + var displayRepresentation: DisplayRepresentation { |
| 27 | + let kind = |
| 28 | + isPage |
| 29 | + ? String( |
| 30 | + localized: LocalizedStringResource( |
| 31 | + "Page", |
| 32 | + defaultValue: "Page", |
| 33 | + table: "Localizable", |
| 34 | + comment: |
| 35 | + "Label shown next to a post's site name in the Shortcuts app when the item is a page rather than a post." |
| 36 | + ) |
| 37 | + ) |
| 38 | + : nil |
| 39 | + let subtitle = [kind, siteName].compactMap { $0 }.joined(separator: " · ") |
| 40 | + guard !subtitle.isEmpty else { |
| 41 | + return DisplayRepresentation(title: "\(title)") |
| 42 | + } |
| 43 | + return DisplayRepresentation(title: "\(title)", subtitle: "\(subtitle)") |
| 44 | + } |
| 45 | + |
| 46 | + /// Fails for posts that only exist locally: without a remote post ID there |
| 47 | + /// is no stable identifier to expose. |
| 48 | + init?(post: AbstractPost) { |
| 49 | + guard let identifier = post.uniqueIdentifier else { |
| 50 | + return nil |
| 51 | + } |
| 52 | + self.id = identifier |
| 53 | + self.title = post.titleForDisplay() |
| 54 | + self.siteName = post.blog.settings?.name ?? post.blog.displayURL as String? |
| 55 | + self.isPage = post is Page |
| 56 | + } |
| 57 | + |
| 58 | + /// A placeholder for a well-formed identifier whose post is no longer in |
| 59 | + /// the local store (e.g. evicted from the cache), so a saved shortcut |
| 60 | + /// keeps working; opening it falls back to a remote fetch. |
| 61 | + init?(identifier: String) { |
| 62 | + guard AbstractPost.AppIntentIdentifier(identifier: identifier) != nil else { |
| 63 | + return nil |
| 64 | + } |
| 65 | + self.id = identifier |
| 66 | + self.title = String( |
| 67 | + localized: LocalizedStringResource( |
| 68 | + "ios-appintents.postEntity.placeholderTitle", |
| 69 | + defaultValue: "Post", |
| 70 | + table: "AppIntents", |
| 71 | + comment: |
| 72 | + "Generic title shown in the Shortcuts app for a post in a saved shortcut that is no longer cached on this device." |
| 73 | + ) |
| 74 | + ) |
| 75 | + self.siteName = nil |
| 76 | + self.isPage = false |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +@available(iOS 18, *) |
| 81 | +extension PostEntity: IndexedEntity {} |
0 commit comments