@blotch/angular-lynx-native 22.3.1 → 22.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/README.md +47 -15
  2. package/dist/android/project.d.ts +114 -1
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +440 -5
  5. package/dist/eject.d.ts +15 -1
  6. package/dist/eject.d.ts.map +1 -1
  7. package/dist/eject.js +14 -2
  8. package/dist/features.d.ts +70 -0
  9. package/dist/features.d.ts.map +1 -0
  10. package/dist/features.js +115 -0
  11. package/dist/files.d.ts +24 -0
  12. package/dist/files.d.ts.map +1 -0
  13. package/dist/files.js +41 -0
  14. package/dist/generate.d.ts +34 -3
  15. package/dist/generate.d.ts.map +1 -1
  16. package/dist/generate.js +78 -19
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +16 -2
  20. package/dist/ios/build.d.ts.map +1 -1
  21. package/dist/ios/build.js +8 -2
  22. package/dist/ios/frameworks.d.ts +6 -5
  23. package/dist/ios/frameworks.d.ts.map +1 -1
  24. package/dist/ios/frameworks.js +46 -15
  25. package/dist/ios/project.d.ts +192 -11
  26. package/dist/ios/project.d.ts.map +1 -1
  27. package/dist/ios/project.js +823 -33
  28. package/dist/options.d.ts +208 -1
  29. package/dist/options.d.ts.map +1 -1
  30. package/dist/options.js +154 -0
  31. package/dist/permissions.d.ts +18 -1
  32. package/dist/permissions.d.ts.map +1 -1
  33. package/dist/permissions.js +79 -4
  34. package/dist/plugins.d.ts +245 -0
  35. package/dist/plugins.d.ts.map +1 -0
  36. package/dist/plugins.js +1244 -0
  37. package/package.json +1 -1
  38. package/templates/android/README.md +46 -5
  39. package/templates/android/angular-lynx.properties +2 -0
  40. package/templates/android/app/build.gradle.kts +78 -11
  41. package/templates/android/app/src/main/AndroidManifest.xml +19 -3
  42. package/templates/android/app/src/main/java/dev/angularlynx/shell/AngularLynxPlugins.kt +13 -0
  43. package/templates/android/app/src/main/java/dev/angularlynx/shell/AudioRecorderModule.kt +520 -0
  44. package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
  45. package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
  46. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
  47. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
  48. package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
  49. package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
  50. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
  51. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
  52. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
  53. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
  54. package/templates/android/app/src/main/java/dev/angularlynx/shell/EdgeToEdgeOverlay.kt +24 -0
  55. package/templates/android/app/src/main/java/dev/angularlynx/shell/EngineThreadHint.kt +156 -0
  56. package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
  57. package/templates/android/app/src/main/java/dev/angularlynx/shell/GeolocationModule.kt +557 -0
  58. package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
  59. package/templates/android/app/src/main/java/dev/angularlynx/shell/HostElementUI.kt +331 -0
  60. package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePickerModule.kt +143 -0
  61. package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePipeline.kt +83 -0
  62. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +523 -9
  63. package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
  64. package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
  65. package/templates/android/app/src/main/java/dev/angularlynx/shell/NetworkStatus.kt +121 -0
  66. package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
  67. package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +35 -0
  68. package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +355 -0
  69. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
  70. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +7 -8
  71. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +58 -4
  72. package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
  73. package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
  74. package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
  75. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
  76. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
  77. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
  78. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
  79. package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
  80. package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
  81. package/templates/android/app/src/main/res/values/share_target.xml +11 -0
  82. package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
  83. package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
  84. package/templates/android/app/src/main/res/xml/share_paths.xml +7 -2
  85. package/templates/android/build.gradle.kts +1 -1
  86. package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
  87. package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
  88. package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
  89. package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
  90. package/templates/ios/App/AngularLynxPlugins.swift +22 -0
  91. package/templates/ios/App/AngularLynxVideo.h +37 -0
  92. package/templates/ios/App/AngularLynxVideo.m +435 -0
  93. package/templates/ios/App/AngularLynxWebGL.h +69 -7
  94. package/templates/ios/App/AngularLynxWebGL.m +538 -29
  95. package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
  96. package/templates/ios/App/AnimationFrameRate.swift +66 -0
  97. package/templates/ios/App/AppDelegate.swift +37 -3
  98. package/templates/ios/App/AppleSignInButton.swift +106 -0
  99. package/templates/ios/App/AppleSignInModule.swift +222 -0
  100. package/templates/ios/App/AudioRecorderModule.swift +433 -0
  101. package/templates/ios/App/BarcodeScannerModule.swift +284 -0
  102. package/templates/ios/App/BiometricsModule.swift +256 -0
  103. package/templates/ios/App/BluetoothModule.swift +1086 -0
  104. package/templates/ios/App/CalendarModule.swift +158 -0
  105. package/templates/ios/App/FilePickerModule.swift +240 -0
  106. package/templates/ios/App/GeolocationModule.swift +455 -0
  107. package/templates/ios/App/HapticsModule.swift +90 -0
  108. package/templates/ios/App/HostElementUI.swift +270 -0
  109. package/templates/ios/App/ImagePickerModule.swift +160 -1
  110. package/templates/ios/App/Info.plist +2 -0
  111. package/templates/ios/App/LynxViewController.swift +274 -9
  112. package/templates/ios/App/MotionModule.swift +430 -0
  113. package/templates/ios/App/NetworkStatus.swift +41 -0
  114. package/templates/ios/App/NfcModule.swift +346 -0
  115. package/templates/ios/App/PhotoLibraryModule.swift +169 -0
  116. package/templates/ios/App/ShareTargetModule.swift +150 -0
  117. package/templates/ios/App/ShellModule.swift +17 -0
  118. package/templates/ios/App/StoreReviewModule.swift +61 -0
  119. package/templates/ios/App/TrackingModule.swift +121 -0
  120. package/templates/ios/App/WifiModule.swift +237 -0
  121. package/templates/ios/App/XElementRegistry.swift +13 -3
  122. package/templates/ios/ShareExtension/Info.plist +42 -0
  123. package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
@@ -0,0 +1,387 @@
1
+ import UIKit
2
+ import UniformTypeIdentifiers
3
+
4
+ /// The app's Share Extension: what iOS runs when the person picks the app in
5
+ /// another app's share sheet. The generator adds it to an app whose
6
+ /// angular.json has `shareTarget`, and its Info.plist's activation rule
7
+ /// lists the kinds of things `shareTarget.mimeTypes` names.
8
+ ///
9
+ /// An extension is a process of its own, which iOS ends past about 120 MB,
10
+ /// so it can't run Lynx or the Angular app. It copies what's shared into
11
+ /// the App Group container it shares with the app, under
12
+ /// `AngularLynxShares/`, opens the app with the share scheme, and closes: the
13
+ /// app's ShareTargetModule hands the share to the runtime's
14
+ /// `LynxShareTarget`. Files are copied from the file iOS hands over, never
15
+ /// read into memory, so a long video doesn't come near the limit.
16
+ ///
17
+ /// What it shows is minimal: a card saying it's sending to the app while it
18
+ /// copies, which most shares never see for long.
19
+ final class ShareViewController: UIViewController {
20
+ /// The most bytes of files one share hands the app, all its files
21
+ /// together: 20 MiB, as LynxFilePicker's FILE_PICKER_MAX_BYTES. The app
22
+ /// reads them into memory as base64 to pass them to JavaScript, so a file
23
+ /// that would take it past that is left out.
24
+ private static let maxBytes = 20 * 1024 * 1024
25
+
26
+ /// The types `shareTarget.mimeTypes` lists, from the extension's
27
+ /// Info.plist: what's shared is checked against them, since the
28
+ /// activation rule can only name broad kinds, such as "files".
29
+ private let mimeTypes =
30
+ Bundle.main.object(forInfoDictionaryKey: "AngularLynxShareMimeTypes") as? [String] ?? []
31
+
32
+ /// The attachments are read on their own queues; their results are put
33
+ /// together on this one.
34
+ private let queue = DispatchQueue(label: "dev.angularlynx.share-extension")
35
+
36
+ private var sharedTitle: String?
37
+ private var texts: [String] = []
38
+ /// The items (by index) whose attachments gave text, whose content text
39
+ /// is then left out (see receive()).
40
+ private var itemsWithText = Set<Int>()
41
+ /// The item (by index) whose attachment gave the share's url: its content
42
+ /// text is the page's title, as Safari gives it.
43
+ private var urlItem: Int?
44
+ private var url: String?
45
+ private var files: [[String: String]] = []
46
+ private var bytes = 0
47
+ private var started = false
48
+ private let spinner = UIActivityIndicatorView(style: .medium)
49
+ private let label = UILabel()
50
+
51
+ override func viewDidLoad() {
52
+ super.viewDidLoad()
53
+ view.backgroundColor = UIColor.black.withAlphaComponent(0.2)
54
+ let card = UIView()
55
+ card.backgroundColor = .secondarySystemBackground
56
+ card.layer.cornerRadius = 16
57
+ card.translatesAutoresizingMaskIntoConstraints = false
58
+ spinner.startAnimating()
59
+ label.numberOfLines = 0
60
+ let appName =
61
+ Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? "the app"
62
+ label.text = "Sending to \(appName)…"
63
+ label.font = .preferredFont(forTextStyle: .body)
64
+ label.adjustsFontForContentSizeCategory = true
65
+ let stack = UIStackView(arrangedSubviews: [spinner, label])
66
+ stack.axis = .horizontal
67
+ stack.spacing = 12
68
+ stack.translatesAutoresizingMaskIntoConstraints = false
69
+ card.addSubview(stack)
70
+ view.addSubview(card)
71
+ NSLayoutConstraint.activate([
72
+ card.centerXAnchor.constraint(equalTo: view.centerXAnchor),
73
+ card.centerYAnchor.constraint(equalTo: view.centerYAnchor),
74
+ card.widthAnchor.constraint(lessThanOrEqualTo: view.widthAnchor, constant: -32),
75
+ stack.topAnchor.constraint(equalTo: card.topAnchor, constant: 20),
76
+ stack.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -20),
77
+ stack.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 24),
78
+ stack.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -24),
79
+ ])
80
+ }
81
+
82
+ override func viewDidAppear(_ animated: Bool) {
83
+ super.viewDidAppear(animated)
84
+ guard !started else { return }
85
+ started = true
86
+ receive()
87
+ }
88
+
89
+ // MARK: Reading the share
90
+
91
+ private func receive() {
92
+ guard let folder = Self.sharesFolder() else {
93
+ // Without the App Group there's nowhere the app can read: the
94
+ // entitlement is missing, as when the app was signed without it.
95
+ finish(opening: false)
96
+ return
97
+ }
98
+ let id = "\(Self.sortableNow())-\(UUID().uuidString)"
99
+ // Written under a hidden name, then renamed once complete, so the app
100
+ // never takes a share that's half written.
101
+ let pending = folder.appendingPathComponent(".\(id)", isDirectory: true)
102
+ do {
103
+ try FileManager.default.createDirectory(at: pending, withIntermediateDirectories: true)
104
+ } catch {
105
+ finish(opening: false)
106
+ return
107
+ }
108
+
109
+ let items = extensionContext?.inputItems.compactMap { $0 as? NSExtensionItem } ?? []
110
+ let group = DispatchGroup()
111
+ for (index, item) in items.enumerated() {
112
+ if sharedTitle == nil, let title = item.attributedTitle?.string, !title.isEmpty {
113
+ sharedTitle = title
114
+ }
115
+ for provider in item.attachments ?? [] {
116
+ read(provider, of: index, into: pending, group: group)
117
+ }
118
+ }
119
+ group.notify(queue: queue) {
120
+ // A share sheet that hands over text only as the item's content: the
121
+ // page's title beside the link of the item that gave the url, as
122
+ // Safari's, or else a selection's text, when the item's attachments
123
+ // gave the app none.
124
+ for (index, item) in items.enumerated() {
125
+ guard let content = item.attributedContentText?.string, !content.isEmpty else { continue }
126
+ if index == self.urlItem {
127
+ if self.sharedTitle == nil { self.sharedTitle = content }
128
+ } else if !self.itemsWithText.contains(index), self.accepts("text/plain") {
129
+ self.texts.append(content)
130
+ }
131
+ }
132
+ let complete = self.write(pending, as: folder.appendingPathComponent(id, isDirectory: true))
133
+ DispatchQueue.main.async { self.finish(opening: complete) }
134
+ }
135
+ }
136
+
137
+ /// Reads one attachment, as the first of these the app takes: a link
138
+ /// (`text/uri-list`), text (`text/plain`), the link as text for an app
139
+ /// that takes text but not links (as Android shares a link), or a file of
140
+ /// an accepted type. An attachment often offers several of them, such as
141
+ /// a page's link and its text, so one the app doesn't take falls through
142
+ /// to the next rather than ending the attachment.
143
+ private func read(
144
+ _ provider: NSItemProvider, of itemIndex: Int, into folder: URL, group: DispatchGroup
145
+ ) {
146
+ /// Called on the queue.
147
+ let addText = { (text: String) in
148
+ self.texts.append(text)
149
+ self.itemsWithText.insert(itemIndex)
150
+ }
151
+ let isFileURL = provider.hasItemConformingToTypeIdentifier(UTType.fileURL.identifier)
152
+ let isLink = !isFileURL && provider.hasItemConformingToTypeIdentifier(UTType.url.identifier)
153
+ let isText = !isFileURL && provider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier)
154
+
155
+ if isLink, accepts("text/uri-list") {
156
+ readLink(provider, group: group) { link in
157
+ // The first link is the share's url, as the Web Share API's
158
+ // ShareData has one; any more go with the text.
159
+ if self.url == nil {
160
+ self.url = link
161
+ self.urlItem = itemIndex
162
+ } else {
163
+ addText(link)
164
+ }
165
+ }
166
+ return
167
+ }
168
+ if isText, accepts("text/plain") {
169
+ group.enter()
170
+ provider.loadItem(forTypeIdentifier: UTType.plainText.identifier) { item, _ in
171
+ let text = (item as? String) ?? (item as? Data).flatMap { String(data: $0, encoding: .utf8) }
172
+ self.queue.async {
173
+ if let text, !text.isEmpty { addText(text) }
174
+ group.leave()
175
+ }
176
+ }
177
+ return
178
+ }
179
+ if isLink, accepts("text/plain") {
180
+ readLink(provider, group: group) { link in addText(link) }
181
+ return
182
+ }
183
+
184
+ // A file, in the first of its types that's content (not a link to it)
185
+ // and accepted. A provider lists its types from the richest down, such
186
+ // as public.heic before public.jpeg, which is the one to keep.
187
+ let identifier = provider.registeredTypeIdentifiers.first { identifier in
188
+ guard let type = UTType(identifier), type.conforms(to: .data), !type.conforms(to: .url)
189
+ else { return false }
190
+ return accepts(type.preferredMIMEType ?? "application/octet-stream")
191
+ }
192
+ if let identifier {
193
+ let type = UTType(identifier)
194
+ group.enter()
195
+ _ = provider.loadFileRepresentation(forTypeIdentifier: identifier) { file, _ in
196
+ // The file iOS hands over is deleted once this returns, so it's
197
+ // copied before then.
198
+ let copied = file.flatMap {
199
+ self.copy($0, named: Self.fileName($0, suggested: provider.suggestedName, type: type),
200
+ type: type, into: folder)
201
+ }
202
+ self.queue.async {
203
+ if let copied { self.files.append(copied) }
204
+ group.leave()
205
+ }
206
+ }
207
+ return
208
+ }
209
+
210
+ // A file offered only as a link to it, as some file providers share:
211
+ // its type comes from its extension.
212
+ if isFileURL {
213
+ group.enter()
214
+ provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier) { item, _ in
215
+ var copied: [String: String]?
216
+ if let file = (item as? URL) ?? (item as? Data).flatMap({ URL(dataRepresentation: $0, relativeTo: nil) }),
217
+ file.isFileURL
218
+ {
219
+ let type = UTType(filenameExtension: file.pathExtension)
220
+ if self.accepts(type?.preferredMIMEType ?? "application/octet-stream") {
221
+ let scoped = file.startAccessingSecurityScopedResource()
222
+ copied = self.copy(file, named: file.lastPathComponent, type: type, into: folder)
223
+ if scoped { file.stopAccessingSecurityScopedResource() }
224
+ }
225
+ }
226
+ self.queue.async {
227
+ if let copied { self.files.append(copied) }
228
+ group.leave()
229
+ }
230
+ }
231
+ }
232
+ }
233
+
234
+ /// Loads an attachment's web link and hands it to `use` on the queue.
235
+ private func readLink(
236
+ _ provider: NSItemProvider, group: DispatchGroup, _ use: @escaping (String) -> Void
237
+ ) {
238
+ group.enter()
239
+ provider.loadItem(forTypeIdentifier: UTType.url.identifier) { item, _ in
240
+ let link = (item as? URL) ?? (item as? Data).flatMap { URL(dataRepresentation: $0, relativeTo: nil) }
241
+ self.queue.async {
242
+ if let link, !link.isFileURL { use(link.absoluteString) }
243
+ group.leave()
244
+ }
245
+ }
246
+ }
247
+
248
+ /// Copies `file` into the share, if it fits in what's left of the limit,
249
+ /// and returns its entry for share.json.
250
+ private func copy(_ file: URL, named name: String, type: UTType?, into folder: URL)
251
+ -> [String: String]?
252
+ {
253
+ let size = (try? file.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0
254
+ let path = "\(UUID().uuidString)-\(name)"
255
+ // Reserved under the queue, so two files can't both take the last of
256
+ // the limit.
257
+ let fits = queue.sync { () -> Bool in
258
+ guard bytes + size <= Self.maxBytes else { return false }
259
+ bytes += size
260
+ return true
261
+ }
262
+ guard fits else { return nil }
263
+ guard (try? FileManager.default.copyItem(at: file, to: folder.appendingPathComponent(path))) != nil
264
+ else {
265
+ queue.sync { bytes -= size }
266
+ return nil
267
+ }
268
+ return [
269
+ "path": path,
270
+ "name": name,
271
+ "type": type?.preferredMIMEType ?? "application/octet-stream",
272
+ ]
273
+ }
274
+
275
+ /// The name the app sees: the one the sharing app suggests, with the
276
+ /// type's extension when it has none, or the file's own.
277
+ private static func fileName(_ file: URL, suggested: String?, type: UTType?) -> String {
278
+ guard let suggested, !suggested.isEmpty else { return file.lastPathComponent }
279
+ if (suggested as NSString).pathExtension.isEmpty, let ext = type?.preferredFilenameExtension {
280
+ return "\(suggested).\(ext)"
281
+ }
282
+ return suggested
283
+ }
284
+
285
+ /// Writes the share's share.json and moves the share where the app looks.
286
+ /// Returns false, leaving nothing behind, when there's nothing to hand
287
+ /// over: nothing shared was of a type the app takes.
288
+ private func write(_ pending: URL, as final: URL) -> Bool {
289
+ var manifest: [String: Any] = ["files": files]
290
+ if let sharedTitle { manifest["title"] = sharedTitle }
291
+ if !texts.isEmpty { manifest["text"] = texts.joined(separator: "\n") }
292
+ if let url { manifest["url"] = url }
293
+ guard files.count > 0 || !texts.isEmpty || url != nil,
294
+ let data = try? JSONSerialization.data(withJSONObject: manifest),
295
+ (try? data.write(to: pending.appendingPathComponent("share.json"))) != nil,
296
+ (try? FileManager.default.moveItem(at: pending, to: final)) != nil
297
+ else {
298
+ try? FileManager.default.removeItem(at: pending)
299
+ return false
300
+ }
301
+ return true
302
+ }
303
+
304
+ // MARK: Handing it to the app
305
+
306
+ /// Opens the app, which takes the share, and closes the extension. If
307
+ /// the app doesn't open, the card says to open it, since the share waits
308
+ /// for it.
309
+ private func finish(opening: Bool) {
310
+ guard opening, let scheme = Bundle.main.object(forInfoDictionaryKey: "AngularLynxShareScheme") as? String,
311
+ let url = URL(string: "\(scheme)://share")
312
+ else {
313
+ extensionContext?.completeRequest(returningItems: nil)
314
+ return
315
+ }
316
+ openApp(url) { opened in
317
+ DispatchQueue.main.async {
318
+ guard !opened else {
319
+ self.extensionContext?.completeRequest(returningItems: nil)
320
+ return
321
+ }
322
+ let appName =
323
+ Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? "the app"
324
+ self.label.text = "Saved. Open \(appName) to see it."
325
+ self.spinner.stopAnimating()
326
+ self.spinner.isHidden = true
327
+ DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
328
+ self.extensionContext?.completeRequest(returningItems: nil)
329
+ }
330
+ }
331
+ }
332
+ }
333
+
334
+ /// A Share Extension has no API to open its app: `extensionContext.open`
335
+ /// works only in widgets, and `UIApplication.shared` isn't available to an
336
+ /// extension. The UIApplication is in the view controller's responder
337
+ /// chain, though, and its `open(_:options:completionHandler:)` opens the
338
+ /// app as a link would, so it's called through the Objective-C runtime,
339
+ /// as share extensions of apps on the App Store do. Apple doesn't support
340
+ /// it, so it's only a shortcut: the share waits in the App Group either
341
+ /// way, and the app takes it the next time it comes to the front.
342
+ /// `done` hears whether the app opened.
343
+ private func openApp(_ url: URL, done: @escaping (Bool) -> Void) {
344
+ let selector = NSSelectorFromString("openURL:options:completionHandler:")
345
+ var responder: UIResponder? = self
346
+ while let current = responder {
347
+ if current is UIApplication, current.responds(to: selector),
348
+ let implementation = current.method(for: selector)
349
+ {
350
+ typealias Open = @convention(c) (
351
+ AnyObject, Selector, NSURL, NSDictionary, (@convention(block) (Bool) -> Void)?
352
+ ) -> Void
353
+ let open = unsafeBitCast(implementation, to: Open.self)
354
+ open(current, selector, url as NSURL, NSDictionary(), { opened in done(opened) })
355
+ return
356
+ }
357
+ responder = current.next
358
+ }
359
+ done(false)
360
+ }
361
+
362
+ // MARK: Helpers
363
+
364
+ /// Whether `shareTarget.mimeTypes` takes `mime`: the type itself, its
365
+ /// `type/*`, or `*/*`.
366
+ private func accepts(_ mime: String) -> Bool {
367
+ let mime = mime.lowercased()
368
+ let major = mime.split(separator: "/").first.map(String.init) ?? ""
369
+ return mimeTypes.contains { accepted in
370
+ accepted == "*/*" || accepted == mime || accepted == "\(major)/*"
371
+ }
372
+ }
373
+
374
+ /// The App Group container's share folder (see ShareTargetModule).
375
+ private static func sharesFolder() -> URL? {
376
+ guard let group = Bundle.main.object(forInfoDictionaryKey: "AngularLynxShareGroup") as? String,
377
+ let container = FileManager.default.containerURL(
378
+ forSecurityApplicationGroupIdentifier: group)
379
+ else { return nil }
380
+ return container.appendingPathComponent("AngularLynxShares", isDirectory: true)
381
+ }
382
+
383
+ /// Milliseconds since 1970, zero-padded, so the folders sort oldest first.
384
+ private static func sortableNow() -> String {
385
+ String(format: "%015.0f", Date().timeIntervalSince1970 * 1000)
386
+ }
387
+ }