@blotch/angular-lynx-native 22.2.20 → 22.2.22

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 (70) hide show
  1. package/README.md +8 -4
  2. package/dist/android/build.d.ts +6 -1
  3. package/dist/android/build.d.ts.map +1 -1
  4. package/dist/android/build.js +14 -3
  5. package/dist/android/icon.d.ts +29 -0
  6. package/dist/android/icon.d.ts.map +1 -0
  7. package/dist/android/icon.js +142 -0
  8. package/dist/android/project.d.ts +2 -1
  9. package/dist/android/project.d.ts.map +1 -1
  10. package/dist/android/project.js +5 -2
  11. package/dist/android/run.d.ts +10 -1
  12. package/dist/android/run.d.ts.map +1 -1
  13. package/dist/android/run.js +46 -1
  14. package/dist/dev-build.d.ts +16 -0
  15. package/dist/dev-build.d.ts.map +1 -0
  16. package/dist/dev-build.js +44 -0
  17. package/dist/generate.d.ts +26 -7
  18. package/dist/generate.d.ts.map +1 -1
  19. package/dist/generate.js +25 -10
  20. package/dist/index.d.ts +13 -4
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +77 -13
  23. package/dist/ios/build.d.ts +68 -12
  24. package/dist/ios/build.d.ts.map +1 -1
  25. package/dist/ios/build.js +210 -45
  26. package/dist/ios/iphone.d.ts +99 -0
  27. package/dist/ios/iphone.d.ts.map +1 -0
  28. package/dist/ios/iphone.js +207 -0
  29. package/dist/ios/project.d.ts +10 -1
  30. package/dist/ios/project.d.ts.map +1 -1
  31. package/dist/ios/project.js +27 -4
  32. package/dist/ios/run.d.ts +18 -3
  33. package/dist/ios/run.d.ts.map +1 -1
  34. package/dist/ios/run.js +34 -5
  35. package/dist/options.d.ts +49 -3
  36. package/dist/options.d.ts.map +1 -1
  37. package/dist/options.js +33 -1
  38. package/dist/permissions.d.ts.map +1 -1
  39. package/dist/permissions.js +6 -0
  40. package/dist/png.d.ts +38 -0
  41. package/dist/png.d.ts.map +1 -1
  42. package/dist/png.js +167 -0
  43. package/dist/run.d.ts +54 -6
  44. package/dist/run.d.ts.map +1 -1
  45. package/dist/run.js +16 -1
  46. package/package.json +5 -1
  47. package/templates/android/README.md +5 -1
  48. package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServer.kt +159 -0
  49. package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServerModules.kt +163 -0
  50. package/templates/android/app/src/debug/res/xml/network_security_config.xml +13 -0
  51. package/templates/android/app/src/main/AndroidManifest.xml +16 -0
  52. package/templates/android/app/src/main/java/dev/angularlynx/shell/AssetsModule.kt +22 -0
  53. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +24 -1
  54. package/templates/android/app/src/main/java/dev/angularlynx/shell/ResourceFetchers.kt +12 -6
  55. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareModule.kt +274 -0
  56. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +57 -0
  57. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +95 -0
  58. package/templates/android/app/src/main/res/mipmap/ic_launcher_round.xml +6 -0
  59. package/templates/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml +6 -0
  60. package/templates/android/app/src/main/res/xml/share_paths.xml +8 -0
  61. package/templates/android/app/src/release/java/dev/angularlynx/shell/DevServer.kt +24 -0
  62. package/templates/ios/App/AngularLynxWebGL.h +7 -0
  63. package/templates/ios/App/AngularLynxWebGL.m +27 -0
  64. package/templates/ios/App/AngularLynxWebGLContext.m +139 -0
  65. package/templates/ios/App/AssetsModule.swift +21 -3
  66. package/templates/ios/App/BundleResourceFetcher.swift +46 -2
  67. package/templates/ios/App/DevServer.swift +190 -0
  68. package/templates/ios/App/DevServerModules.swift +223 -0
  69. package/templates/ios/App/LynxViewController.swift +11 -1
  70. package/templates/ios/App/ShareModule.swift +215 -0
@@ -30,6 +30,8 @@ final class LynxViewController: UIViewController {
30
30
  config.register(LinkingModule.self)
31
31
  // The photo picker behind LynxImagePicker (ImagePickerModule).
32
32
  config.register(ImagePickerModule.self)
33
+ // The share sheet behind LynxShare (ShareModule).
34
+ config.register(ShareModule.self)
33
35
  // <canvas>'s WebGL (AngularLynxWebGL.h), which the runtime calls from
34
36
  // the main thread. It's Objective-C, which Swift reaches without a
35
37
  // bridging header by name.
@@ -38,6 +40,8 @@ final class LynxViewController: UIViewController {
38
40
  }
39
41
  // Bundled files for the main thread's fetch and Image (AssetsModule).
40
42
  config.register(AssetsModule.self)
43
+ // `ng serve --platform ios`'s live reload, in the development build.
44
+ DevServer.shared.register(in: config)
41
45
  let lynxView = LynxView { builder in
42
46
  builder.config = config
43
47
  // Lets main-thread code, where Angular runs, call the config's modules
@@ -57,7 +61,13 @@ final class LynxViewController: UIViewController {
57
61
 
58
62
  layoutLynxView()
59
63
  lynxView.updateGlobalProps(with: globalProps())
60
- lynxView.loadTemplate(fromURL: "main.lynx.bundle", initData: nil)
64
+ // The bundle inside the app, or in the development build, the dev
65
+ // server's, which it loads again on every edit.
66
+ let templateURL = DevServer.shared.templateURL
67
+ lynxView.loadTemplate(fromURL: templateURL, initData: nil)
68
+ DevServer.shared.attach(to: view) { [weak self] in
69
+ self?.lynxView?.loadTemplate(fromURL: templateURL, initData: nil)
70
+ }
61
71
 
62
72
  // Send the new theme when the phone switches between light and dark
63
73
  // while the app is open. Before iOS 17, traitCollectionDidChange does.
@@ -0,0 +1,215 @@
1
+ import Lynx
2
+ import UIKit
3
+
4
+ /// The native module behind the runtime's `LynxShare`, which calls it as
5
+ /// `NativeModules.AngularLynxShare` (from the background thread, where
6
+ /// NativeModules lives).
7
+ ///
8
+ /// Lynx has no `navigator.share()`. The system share sheet,
9
+ /// UIActivityViewController, stands in for it. Each file is written to a
10
+ /// temporary file of its own name first, rather than handed over as data or
11
+ /// a UIImage, so "Save to Files" and AirDrop get the name the app gave it,
12
+ /// and the sheet knows its type from the extension: a JPEG gets "Save Image".
13
+ ///
14
+ /// The files come as base64, since the runtime's relay from the main thread
15
+ /// and the module's arguments carry only what JSON can.
16
+ final class ShareModule: NSObject, LynxModule {
17
+ static var name: String { "AngularLynxShare" }
18
+
19
+ static var methodLookup: [String: String] {
20
+ ["share": NSStringFromSelector(#selector(share(_:callback:)))]
21
+ }
22
+
23
+ /// Whether a share sheet is open. Only touched on the main thread.
24
+ private var open = false
25
+
26
+ override init() {
27
+ super.init()
28
+ }
29
+
30
+ // Swift can't import an optional initializer as optional, so Lynx's
31
+ // initWithParam: is required here. Registered without a param, the module
32
+ // ignores it.
33
+ init(param: Any) {
34
+ super.init()
35
+ }
36
+
37
+ /// Opens the share sheet with `request`'s `files` (`{ base64, name, type }`),
38
+ /// `title`, `text` and `url`, and calls back once with `{ shared }`, or with
39
+ /// `{ error: "failed", message }`.
40
+ @objc func share(_ request: [String: Any], callback: @escaping LynxCallbackBlock) {
41
+ // Decoding and writing the files takes a moment for a photo: on the
42
+ // module's own thread, before the sheet, which is UI, opens on the main
43
+ // thread.
44
+ let directory = FileManager.default.temporaryDirectory
45
+ .appendingPathComponent("AngularLynxShare", isDirectory: true)
46
+ .appendingPathComponent(UUID().uuidString, isDirectory: true)
47
+ var items: [Any] = []
48
+ do {
49
+ let files = request["files"] as? [[String: Any]] ?? []
50
+ if !files.isEmpty {
51
+ try FileManager.default.createDirectory(
52
+ at: directory, withIntermediateDirectories: true)
53
+ }
54
+ var used = Set<String>()
55
+ for file in files {
56
+ guard let base64 = file["base64"] as? String,
57
+ let data = Data(base64Encoded: base64)
58
+ else {
59
+ throw ShareFailure("A file's bytes couldn't be read.")
60
+ }
61
+ let name = Self.uniqueName(Self.safeName(file["name"] as? String), in: &used)
62
+ let url = directory.appendingPathComponent(name, isDirectory: false)
63
+ try data.write(to: url)
64
+ items.append(url)
65
+ }
66
+ } catch {
67
+ try? FileManager.default.removeItem(at: directory)
68
+ callback(["error": "failed", "message": "The files couldn't be written to share: \(error.localizedDescription)"])
69
+ return
70
+ }
71
+ let title = request["title"] as? String
72
+ let text = request["text"] as? String
73
+ let link = (request["url"] as? String).flatMap(URL.init(string:))
74
+ // The title goes with the text, or the link, as its subject (Mail's
75
+ // subject line). Shared on its own, as the Web Share API allows, it's
76
+ // the text.
77
+ if let text {
78
+ items.append(TextSource(text, subject: title))
79
+ } else if link == nil, let title, items.isEmpty {
80
+ items.append(TextSource(title, subject: nil))
81
+ }
82
+ if let link {
83
+ items.append(text == nil ? TextSource(link, subject: title) : link)
84
+ }
85
+ DispatchQueue.main.async {
86
+ self.present(items, directory: directory, callback: callback)
87
+ }
88
+ }
89
+
90
+ private func present(
91
+ _ items: [Any], directory: URL, callback: @escaping LynxCallbackBlock
92
+ ) {
93
+ let cleanUp = { try? FileManager.default.removeItem(at: directory) }
94
+ guard !open else {
95
+ cleanUp()
96
+ callback(["error": "failed", "message": "A share sheet is already open."])
97
+ return
98
+ }
99
+ guard let presenter = topViewController() else {
100
+ cleanUp()
101
+ callback(["error": "failed", "message": "There's no screen to open the share sheet over."])
102
+ return
103
+ }
104
+ let controller = UIActivityViewController(activityItems: items, applicationActivities: nil)
105
+ // "Save Image" adds to the photo library, which crashes an app whose
106
+ // Info.plist gives no reason for it. The apps `ng build --platform` makes
107
+ // have one; a project ejected before they did leaves the action out
108
+ // rather than crash.
109
+ if Bundle.main.object(forInfoDictionaryKey: "NSPhotoLibraryAddUsageDescription") == nil {
110
+ controller.excludedActivityTypes = [.saveToCameraRoll]
111
+ }
112
+ // On iPad the sheet is a popover, which needs somewhere to point:
113
+ // the middle of the screen, without an arrow, since the tap that opened
114
+ // it was in the LynxView, which UIKit can't see into.
115
+ if let popover = controller.popoverPresentationController, let view = presenter.view {
116
+ popover.sourceView = view
117
+ popover.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
118
+ popover.permittedArrowDirections = []
119
+ }
120
+ // The callback takes one answer, and UIKit may finish a sheet more than
121
+ // once: iOS 27 logs completing twice as the sheet closes.
122
+ var answered = false
123
+ controller.completionWithItemsHandler = { [weak self, weak controller] activity, completed, _, error in
124
+ if answered { return }
125
+ // From iOS 13, closing an action's own screen without finishing it,
126
+ // such as Save to Files' picker, calls this with the action, while the
127
+ // sheet stays open for another choice. The sheet itself closing calls
128
+ // it without one.
129
+ if !completed, error == nil, activity != nil, controller?.viewIfLoaded?.window != nil {
130
+ return
131
+ }
132
+ answered = true
133
+ self?.open = false
134
+ cleanUp()
135
+ if let error {
136
+ callback(["error": "failed", "message": error.localizedDescription])
137
+ } else {
138
+ callback(["shared": completed])
139
+ }
140
+ }
141
+ open = true
142
+ presenter.present(controller, animated: true)
143
+ }
144
+
145
+ /// A file name the file system takes: no folders, and something there.
146
+ private static func safeName(_ name: String?) -> String {
147
+ let base = (name ?? "")
148
+ .replacingOccurrences(of: "/", with: "_")
149
+ .replacingOccurrences(of: ":", with: "_")
150
+ .trimmingCharacters(in: .whitespacesAndNewlines)
151
+ return base.isEmpty || base == "." || base == ".." ? "file" : base
152
+ }
153
+
154
+ /// `photo.jpg`, then `photo 2.jpg`: two files of one name in one share
155
+ /// would otherwise overwrite each other.
156
+ private static func uniqueName(_ name: String, in used: inout Set<String>) -> String {
157
+ var candidate = name
158
+ var number = 2
159
+ let url = URL(fileURLWithPath: name)
160
+ let stem = url.deletingPathExtension().lastPathComponent
161
+ let ext = url.pathExtension
162
+ while used.contains(candidate.lowercased()) {
163
+ candidate = ext.isEmpty ? "\(stem) \(number)" : "\(stem) \(number).\(ext)"
164
+ number += 1
165
+ }
166
+ used.insert(candidate.lowercased())
167
+ return candidate
168
+ }
169
+
170
+ /// The view controller on top, which the share sheet opens over.
171
+ private func topViewController() -> UIViewController? {
172
+ let window = UIApplication.shared.connectedScenes
173
+ .compactMap { $0 as? UIWindowScene }
174
+ .flatMap(\.windows)
175
+ .first(where: \.isKeyWindow)
176
+ var top = window?.rootViewController
177
+ while let presented = top?.presentedViewController {
178
+ top = presented
179
+ }
180
+ return top
181
+ }
182
+ }
183
+
184
+ private struct ShareFailure: LocalizedError {
185
+ let errorDescription: String?
186
+ init(_ message: String) { errorDescription = message }
187
+ }
188
+
189
+ /// Text or a link with a subject: UIActivityViewController takes a subject,
190
+ /// which Mail uses for its subject line, only from an item source.
191
+ private final class TextSource: NSObject, UIActivityItemSource {
192
+ private let item: Any
193
+ private let subject: String?
194
+
195
+ init(_ item: Any, subject: String?) {
196
+ self.item = item
197
+ self.subject = subject
198
+ }
199
+
200
+ func activityViewControllerPlaceholderItem(_ controller: UIActivityViewController) -> Any {
201
+ item
202
+ }
203
+
204
+ func activityViewController(
205
+ _ controller: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?
206
+ ) -> Any? {
207
+ item
208
+ }
209
+
210
+ func activityViewController(
211
+ _ controller: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?
212
+ ) -> String {
213
+ subject ?? ""
214
+ }
215
+ }