-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathAccount+Scriptability.swift
More file actions
238 lines (207 loc) · 6.19 KB
/
Account+Scriptability.swift
File metadata and controls
238 lines (207 loc) · 6.19 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// Account+Scriptability.swift
// NetNewsWire
//
// Created by Olof Hellman on 1/9/18.
// Copyright © 2018 Olof Hellman. All rights reserved.
//
import AppKit
import Account
import Articles
import RSCore
@objc(ScriptableAccount)
@MainActor final class ScriptableAccount: NSObject, UniqueIDScriptingObject, @preconcurrency ScriptingObjectContainer {
let account: Account
init (_ account: Account) {
self.account = account
}
@objc(objectSpecifier)
nonisolated override var objectSpecifier: NSScriptObjectSpecifier? {
let myContainer = MainActor.assumeIsolated { NSApplication.shared }
return myContainer.makeFormUniqueIDScriptObjectSpecifier(forObject: self)
}
@objc(scriptingIsActive)
var scriptingIsActive: Bool {
get {
account.isActive
}
set {
account.isActive = newValue
}
}
@objc(scriptingName)
var scriptingName: NSString {
get {
account.nameForDisplay as NSString
}
set {
account.name = newValue as String
}
}
// MARK: - ScriptingObject protocol
nonisolated var scriptingKey: String {
"accounts"
}
// MARK: --- UniqueIdScriptingObject protocol ---
// I am not sure if account should prefer to be specified by name or by ID
// but in either case it seems like the accountID would be used as the keydata, so I chose ID
@objc(uniqueId)
nonisolated var scriptingUniqueID: Any {
account.accountID
}
// MARK: --- ScriptingObjectContainer protocol ---
nonisolated var scriptingClassDescription: NSScriptClassDescription {
self.classDescription as! NSScriptClassDescription
}
func deleteElement(_ element: ScriptingObject) {
if let scriptableFolder = element as? ScriptableFolder {
BatchUpdate.shared.perform {
account.removeFolder(scriptableFolder.folder) { _ in
}
}
} else if let scriptableFeed = element as? ScriptableFeed {
BatchUpdate.shared.perform {
var container: Container?
if let scriptableFolder = scriptableFeed.container as? ScriptableFolder {
container = scriptableFolder.folder
} else {
container = account
}
account.removeFeed(scriptableFeed.feed, from: container!) { _ in
}
}
}
}
@objc(isLocationRequiredToCreateForKey:)
func isLocationRequiredToCreate(forKey key: String) -> Bool {
false
}
// MARK: --- Scriptable elements ---
@objc(feeds)
var feeds: NSArray {
account.topLevelFeeds.map { ScriptableFeed($0, container: self) } as NSArray
}
@objc(countOfFeeds)
func countOfFeeds() -> Int {
account.topLevelFeeds.count
}
@objc(objectInFeedsAtIndex:)
func objectInFeedsAtIndex(_ index: Int) -> ScriptableFeed? {
let feeds = Array(account.topLevelFeeds)
guard index >= 0 && index < feeds.count else {
return nil
}
return ScriptableFeed(feeds[index], container: self)
}
@objc(valueInFeedsWithUniqueID:)
func valueInFeeds(withUniqueID id: String) -> ScriptableFeed? {
guard let feed = account.existingFeed(withFeedID: id) else {
return nil
}
return ScriptableFeed(feed, container: self)
}
@objc(valueInFeedsWithName:)
func valueInFeeds(withName name: String) -> ScriptableFeed? {
let feeds = Array(account.flattenedFeeds())
guard let feed = feeds.first(where: { $0.name == name }) else {
return nil
}
return ScriptableFeed(feed, container: self)
}
@objc(folders)
var folders: NSArray {
let foldersSet = account.folders ?? Set<Folder>()
let folders = Array(foldersSet)
return folders.map { ScriptableFolder($0, container: self) } as NSArray
}
@objc(countOfFolders)
func countOfFolders() -> Int {
account.folders?.count ?? 0
}
@objc(objectInFoldersAtIndex:)
func objectInFoldersAtIndex(_ index: Int) -> ScriptableFolder? {
let foldersSet = account.folders ?? Set<Folder>()
let folders = Array(foldersSet)
guard index >= 0 && index < folders.count else {
return nil
}
return ScriptableFolder(folders[index], container: self)
}
@objc(valueInFoldersWithUniqueID:)
func valueInFolders(withUniqueID id: NSNumber) -> ScriptableFolder? {
let folderId = id.intValue
let foldersSet = account.folders ?? Set<Folder>()
let folders = Array(foldersSet)
guard let folder = folders.first(where: { $0.folderID == folderId }) else {
return nil
}
return ScriptableFolder(folder, container: self)
}
// MARK: --- Scriptable properties ---
@objc(allFeeds)
var allFeeds: NSArray {
let allFeeds = account.flattenedFeeds()
let scriptableFeeds = allFeeds.map { feed in
ScriptableFeed(feed, container: self)
}
return scriptableFeeds as NSArray
}
@objc(countOfAllFeeds)
func countOfAllFeeds() -> Int {
account.flattenedFeeds().count
}
@objc(objectInAllFeedsAtIndex:)
func objectInAllFeedsAtIndex(_ index: Int) -> ScriptableFeed? {
let allFeeds = Array(account.flattenedFeeds())
guard index >= 0 && index < allFeeds.count else {
return nil
}
return ScriptableFeed(allFeeds[index], container: self)
}
@objc(valueInAllFeedsWithUniqueID:)
func valueInAllFeeds(withUniqueID id: String) -> ScriptableFeed? {
guard let feed = account.existingFeed(withFeedID: id) else {
return nil
}
return ScriptableFeed(feed, container: self)
}
@objc(valueInAllFeedsWithName:)
func valueInAllFeeds(withName name: String) -> ScriptableFeed? {
let feeds = Array(account.flattenedFeeds())
guard let feed = feeds.first(where: {$0.name == name}) else {
return nil
}
return ScriptableFeed(feed, container: self)
}
@objc(opmlRepresentation)
var opmlRepresentation: String {
self.account.OPMLString(indentLevel: 0)
}
@objc(accountType)
var accountType: OSType {
var osType: String = ""
switch account.type {
case .onMyMac:
osType = "Locl"
case .cloudKit:
osType = "Clkt"
case .feedly:
osType = "Fdly"
case .feedbin:
osType = "Fdbn"
case .inkwell:
osType = "Mcro"
case .newsBlur:
osType = "NBlr"
case .freshRSS:
osType = "Frsh"
case .inoreader:
osType = "Inrd"
case .bazQux:
osType = "Bzqx"
case .theOldReader:
osType = "Tord"
}
return osType.fourCharCode
}
}