-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathReaderPostEntity.swift
More file actions
64 lines (57 loc) · 2.3 KB
/
Copy pathReaderPostEntity.swift
File metadata and controls
64 lines (57 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import AppIntents
import Foundation
import WordPressData
/// A post from the user's Reader, as exposed to the system via App Intents.
///
/// The identifier is the same composite string the Spotlight index uses, so a Spotlight
/// item and its associated app entity always name the same post.
struct ReaderPostEntity: AppEntity {
static let typeDisplayRepresentation = TypeDisplayRepresentation(
name: LocalizedStringResource(
"ios-appintents.readerPostEntity.typeName",
defaultValue: "Reader Post",
table: "AppIntents",
comment: "Type name of the Reader post entity in the Shortcuts app, e.g. shown when picking a post."
)
)
static var defaultQuery: ReaderPostEntityQuery { ReaderPostEntityQuery() }
let id: String
let title: String
let blogName: String?
var displayRepresentation: DisplayRepresentation {
guard let blogName else {
return DisplayRepresentation(title: "\(title)")
}
return DisplayRepresentation(title: "\(title)", subtitle: "\(blogName)")
}
/// Fails for posts missing the site or post ID needed for a stable identifier.
init?(post: ReaderPost) {
guard let identifier = post.uniqueIdentifier else {
return nil
}
self.id = identifier
self.title = post.titleForDisplay()
self.blogName = post.blogNameForDisplay()
}
/// A placeholder for a well-formed identifier whose post is no longer in
/// the local store (Reader rows are purged routinely), so a saved
/// shortcut keeps working; opening it navigates by the IDs alone.
init?(identifier: String) {
guard ReaderPost.AppIntentIdentifier(identifier: identifier) != nil else {
return nil
}
self.id = identifier
self.title = String(
localized: LocalizedStringResource(
"ios-appintents.readerPostEntity.placeholderTitle",
defaultValue: "Reader Post",
table: "AppIntents",
comment:
"Generic title shown in the Shortcuts app for a Reader post in a saved shortcut that is no longer cached on this device."
)
)
self.blogName = nil
}
}
@available(iOS 18, *)
extension ReaderPostEntity: IndexedEntity {}