@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,270 @@
1
+ import Lynx
2
+ import UIKit
3
+
4
+ /// A native element that shows its Lynx children in a view outside the
5
+ /// LynxView, such as a system sheet's, a popover's or a dialog's, and keeps
6
+ /// them interactive there.
7
+ ///
8
+ /// Lynx hit-tests a touch from the LynxView it belongs to, against the frames
9
+ /// of the elements inside it, so a child whose view is moved out of the
10
+ /// LynxView still draws but never gets a tap, a touch or a gesture.
11
+ /// `showChildren(in:)` moves the element's own view, with every view Lynx put
12
+ /// in it, so the elements' views stay where Lynx's tree expects them relative
13
+ /// to each other, and gives it an event handler of its own rooted at the
14
+ /// element, as Lynx's `<overlay>` does for the window it shows in. Touches in
15
+ /// the container then reach the element's children as they would in the
16
+ /// LynxView: their `bindtap`, `bindtouchmove` and the rest.
17
+ ///
18
+ /// Its own background, border and box shadow go with it. Lynx draws them in
19
+ /// layers beside its view's layer rather than in it, so they're moved along
20
+ /// with the view, and kept lined up with it while it's in the container.
21
+ ///
22
+ /// A plugin's element subclasses it rather than `LynxUI<UIView>`, lays itself
23
+ /// out with CSS like any element (the width and height it has there are the
24
+ /// ones it keeps in the container), and calls `showChildren(in:)` when its
25
+ /// container appears and `returnChildren()` once it has gone.
26
+ class HostElementUI: LynxUI<UIView> {
27
+ /// Dispatches touches in the element's view while it's shown elsewhere.
28
+ /// Only then: in the LynxView, the LynxView's own handler does, and two
29
+ /// would send each event twice.
30
+ private var eventHandler: LynxEventHandler?
31
+ /// The gesture arena index `eventHandler` was given, to give it back.
32
+ private var arenaIndex = -1
33
+
34
+ /// Whether the element's view is in a container of the element's, rather
35
+ /// than in the LynxView.
36
+ var isShowingChildrenElsewhere: Bool { eventHandler != nil }
37
+
38
+ /// The element's view is one that finds the touch's target for
39
+ /// `eventHandler`, so it isn't for a subclass to change.
40
+ final override func createView() -> UIView? {
41
+ let view = HostedView()
42
+ view.host = self
43
+ return view
44
+ }
45
+
46
+ /// Has `eventHandler` find the element a touch at `point` lands on. Lynx's
47
+ /// handler hit-tests while UIKit does, from its root view's
48
+ /// `hitTest(_:with:)` (a LynxView's, or the container `<overlay>` shows in),
49
+ /// and dispatches the touch's taps and moves to the element it found.
50
+ fileprivate func findTouchTarget(at point: CGPoint, with event: UIEvent?) {
51
+ eventHandler?.hitTest(point, with: event)
52
+ }
53
+
54
+ /// Moves the element's view, with its children, its background, border
55
+ /// and box shadow, to the top left of `container`, and dispatches their
56
+ /// touches from there.
57
+ func showChildren(in container: UIView) {
58
+ let view = view()
59
+ let outer = Self.outermostView(of: self)
60
+ container.addSubview(outer)
61
+ if outer !== view {
62
+ Self.fill(outer)
63
+ }
64
+ if eventHandler == nil, let context, let emitter = context.eventEmitter {
65
+ let handler = LynxEventHandler(rootView: view, withRootUI: self)
66
+ handler.updateUiOwner(context.uiOwner, eventEmitter: emitter)
67
+ arenaIndex = Self.join(handler, toArenaOf: context.eventHandler)
68
+ eventHandler = handler
69
+ }
70
+ pinToContainer()
71
+ }
72
+
73
+ /// Puts the element's view back in the LynxView, where Lynx put it, and
74
+ /// leaves its touches to the LynxView again.
75
+ func returnChildren() {
76
+ guard let handler = eventHandler else { return }
77
+ handler.removeEventGestures()
78
+ if arenaIndex >= 0 {
79
+ handler.removeGestureArenaManager(arenaIndex)
80
+ }
81
+ eventHandler = nil
82
+ arenaIndex = -1
83
+ if let parent {
84
+ returnView(to: parent)
85
+ } else {
86
+ Self.outermostView(of: self).removeFromSuperview()
87
+ }
88
+ keepDrawnLayersWithView()
89
+ // Back to the frame Lynx's layout gives the element, and its layers with
90
+ // it.
91
+ frameDidChange()
92
+ }
93
+
94
+ /// Puts the element's view back in `parent`'s, in the order of `parent`'s
95
+ /// children, as Lynx keeps their views: below the next sibling whose view
96
+ /// is there, or else above the one before it. Not at the element's index
97
+ /// among the children, which counts siblings shown elsewhere too, and the
98
+ /// parent's views that aren't an element's, such as a scroll view's
99
+ /// indicators. Below the sibling's background and above its border, as
100
+ /// Lynx's `insertChild` orders their layers, so that neither draws over the
101
+ /// other where it didn't before.
102
+ private func returnView(to parent: LynxUI<UIView>) {
103
+ let container = parent.view()
104
+ let outer = Self.outermostView(of: self)
105
+ let siblings = parent.children.compactMap { $0 as? LynxUI<UIView> }
106
+ let index = siblings.firstIndex { $0 === self } ?? siblings.count
107
+ let attached = { (sibling: LynxUI<UIView>) in
108
+ Self.outermostView(of: sibling).superview === container
109
+ }
110
+ if let sibling = siblings[(min(index + 1, siblings.count))...].first(where: attached) {
111
+ container.insertSubview(outer, belowSubview: Self.outermostView(of: sibling))
112
+ container.layer.insertSublayer(outer.layer, below: Self.bottomLayer(of: sibling))
113
+ } else if let sibling = siblings[..<index].last(where: attached) {
114
+ container.insertSubview(outer, aboveSubview: Self.outermostView(of: sibling))
115
+ container.layer.insertSublayer(outer.layer, above: Self.topLayer(of: sibling))
116
+ } else {
117
+ container.addSubview(outer)
118
+ }
119
+ if outer !== view() {
120
+ Self.fill(outer)
121
+ }
122
+ }
123
+
124
+ /// The view that holds `ui`'s view and its layers in its parent's: the view
125
+ /// Lynx wraps them in for an opacity drawn as a group or a mask
126
+ /// (`LynxBackgroundManager`'s `opacityView`, with its background and border
127
+ /// layers inside it), or else the view itself.
128
+ private static func outermostView(of ui: LynxUI<UIView>) -> UIView {
129
+ let view = ui.view()
130
+ // `opacityView` is declared nonnull, but Lynx makes it only when it needs
131
+ // one, so it's read as optional.
132
+ guard let wrapper = ui.backgroundManager?.opacityView as UIView?,
133
+ view.superview === wrapper
134
+ else { return view }
135
+ return wrapper
136
+ }
137
+
138
+ /// `ui`'s lowest layer in its parent's: its background's, below its view's.
139
+ private static func bottomLayer(of ui: LynxUI<UIView>) -> CALayer {
140
+ let outer = outermostView(of: ui)
141
+ let bottom = ui.bottomLayer()
142
+ return bottom.superlayer === outer.layer.superlayer ? bottom : outer.layer
143
+ }
144
+
145
+ /// `ui`'s highest layer in its parent's: its border's when it draws over its
146
+ /// view's.
147
+ private static func topLayer(of ui: LynxUI<UIView>) -> CALayer {
148
+ let outer = outermostView(of: ui)
149
+ let top = ui.topLayer()
150
+ return top.superlayer === outer.layer.superlayer ? top : outer.layer
151
+ }
152
+
153
+ /// Sizes the view Lynx wraps an element in for its opacity to the one it's
154
+ /// in, as Lynx does when it makes it.
155
+ private static func fill(_ wrapper: UIView) {
156
+ guard let superview = wrapper.superview else { return }
157
+ wrapper.frame = CGRect(origin: .zero, size: superview.bounds.size)
158
+ }
159
+
160
+ /// Keeps the layers Lynx draws the element's background, border and box
161
+ /// shadow in (the shadows inside the background's, the outline inside the
162
+ /// border's) beside its view's layer, and lined up with it.
163
+ ///
164
+ /// `LynxBackgroundManager` puts them in the view's superlayer, beside the
165
+ /// view's layer rather than in it, and adds them again only when it next
166
+ /// makes one, so moving the view alone leaves them on the page. They go in
167
+ /// the order Lynx's `insertChild` gives them: the background below the
168
+ /// view, and the border above it, or, when the element's `overflow` isn't
169
+ /// hidden, below it and above the background. Lynx gives them the view's
170
+ /// frame, which in the container is the element's place on the page.
171
+ private func keepDrawnLayersWithView() {
172
+ guard let manager = backgroundManager else { return }
173
+ let layer = view().layer
174
+ CATransaction.begin()
175
+ CATransaction.setDisableActions(true)
176
+ defer { CATransaction.commit() }
177
+ if let superlayer = layer.superlayer {
178
+ let hidden = overflow == OVERFLOW_HIDDEN_VAL
179
+ let border = manager.borderLayer
180
+ if let border, border.superlayer !== superlayer {
181
+ if hidden {
182
+ superlayer.insertSublayer(border, above: layer)
183
+ } else {
184
+ superlayer.insertSublayer(border, below: layer)
185
+ }
186
+ }
187
+ if let background = manager.backgroundLayer, background.superlayer !== superlayer {
188
+ if !hidden, let border {
189
+ superlayer.insertSublayer(background, below: border)
190
+ } else {
191
+ superlayer.insertSublayer(background, below: layer)
192
+ }
193
+ }
194
+ }
195
+ let drawn: [CALayer?] = [manager.backgroundLayer, manager.borderLayer, manager.maskLayer]
196
+ for drawn in drawn.compactMap({ $0 }) {
197
+ Self.align(drawn, with: layer)
198
+ }
199
+ }
200
+
201
+ /// Moves `drawn` onto `layer`, keeping its size and transform, as Lynx gives
202
+ /// the two the same frame. Through `position`, which a transform leaves
203
+ /// alone, from each one's own anchor point.
204
+ private static func align(_ drawn: CALayer, with layer: CALayer) {
205
+ let left = layer.position.x - layer.bounds.width * layer.anchorPoint.x
206
+ let top = layer.position.y - layer.bounds.height * layer.anchorPoint.y
207
+ drawn.position = CGPoint(
208
+ x: left + drawn.bounds.width * drawn.anchorPoint.x,
209
+ y: top + drawn.bounds.height * drawn.anchorPoint.y)
210
+ }
211
+
212
+ /// The element's view moved into a window or out of one, UIKit's
213
+ /// `didMoveToWindow()`. For a subclass that presents from the view's
214
+ /// window, as a sheet does: Lynx sets an element's props while it creates
215
+ /// it, before its view is in a window to present from.
216
+ func viewDidMoveToWindow() {}
217
+
218
+ override func frameDidChange() {
219
+ super.frameDidChange()
220
+ if isShowingChildrenElsewhere {
221
+ pinToContainer()
222
+ }
223
+ }
224
+
225
+ /// Puts `handler`'s touches in the gesture arena of the page's handler,
226
+ /// where the runtime's gestures (`LynxGestureDetector`) run, and returns
227
+ /// its index there. The arena's class isn't in Lynx's public headers, so
228
+ /// Swift doesn't see the two members that take it, and they're called
229
+ /// through the Objective-C runtime.
230
+ private static func join(
231
+ _ handler: LynxEventHandler, toArenaOf page: LynxEventHandler?
232
+ ) -> Int {
233
+ let selector = NSSelectorFromString("setGestureArenaManagerAndGetIndex:")
234
+ guard let page, handler.responds(to: selector),
235
+ let arena = page.value(forKey: "gestureArenaManager") as AnyObject?
236
+ else { return -1 }
237
+ typealias Join = @convention(c) (AnyObject, Selector, AnyObject) -> Int
238
+ let join = unsafeBitCast(handler.method(for: selector), to: Join.self)
239
+ return join(handler, selector, arena)
240
+ }
241
+
242
+ /// Lynx sets the view's frame to the element's place on the page, which
243
+ /// means nothing in the container: it keeps its size and moves to the
244
+ /// container's top left, with its layers. Through `center`, which a
245
+ /// transform leaves alone.
246
+ private func pinToContainer() {
247
+ let view = view()
248
+ view.center = CGPoint(
249
+ x: view.bounds.width * view.layer.anchorPoint.x,
250
+ y: view.bounds.height * view.layer.anchorPoint.y)
251
+ keepDrawnLayersWithView()
252
+ }
253
+ }
254
+
255
+ /// A `HostElementUI`'s view, which has the element's event handler hit-test
256
+ /// with UIKit while the element shows its children elsewhere.
257
+ private final class HostedView: UIView {
258
+ weak var host: HostElementUI?
259
+
260
+ override func didMoveToWindow() {
261
+ super.didMoveToWindow()
262
+ host?.viewDidMoveToWindow()
263
+ }
264
+
265
+ override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
266
+ guard let view = super.hitTest(point, with: event) else { return nil }
267
+ host?.findTouchTarget(at: point, with: event)
268
+ return view
269
+ }
270
+ }
@@ -1,3 +1,6 @@
1
+ // angular-lynx:image-picker-camera
2
+ import AVFoundation
3
+ // /angular-lynx:image-picker-camera
1
4
  import ImageIO
2
5
  import Lynx
3
6
  import PhotosUI
@@ -17,13 +20,25 @@ import UniformTypeIdentifiers
17
20
  /// runtime's relay to the main thread carry only what JSON can. Scaling it
18
21
  /// down here first, with `maxDimension`, keeps that small: a 12-megapixel
19
22
  /// photo is several megabytes, a 1024-pixel one a few hundred kilobytes.
23
+ ///
24
+ /// `takePhoto`, the camera, is only in an app whose angular.json sets
25
+ /// `permissions.camera`: App Store Connect flags an app whose code uses the
26
+ /// camera without NSCameraUsageDescription (ITMS-90683), which only that
27
+ /// permission writes. So the generator takes out the lines between the
28
+ /// `angular-lynx:image-picker-camera` markers in every other app, and
29
+ /// `takePhoto` isn't in `methodLookup`, which the runtime answers with
30
+ /// `unavailable`.
20
31
  final class ImagePickerModule: NSObject, LynxModule, PHPickerViewControllerDelegate,
21
32
  UIAdaptivePresentationControllerDelegate
22
33
  {
23
34
  static var name: String { "AngularLynxImagePicker" }
24
35
 
25
36
  static var methodLookup: [String: String] {
26
- ["pick": NSStringFromSelector(#selector(pick(_:callback:)))]
37
+ var methods = ["pick": NSStringFromSelector(#selector(pick(_:callback:)))]
38
+ // angular-lynx:image-picker-camera
39
+ methods["takePhoto"] = NSStringFromSelector(#selector(takePhoto(_:callback:)))
40
+ // /angular-lynx:image-picker-camera
41
+ return methods
27
42
  }
28
43
 
29
44
  /// What a re-encoded image gets when the app doesn't choose a quality.
@@ -225,3 +240,147 @@ request.callback(
225
240
  return top
226
241
  }
227
242
  }
243
+
244
+ // angular-lynx:image-picker-camera
245
+ /// The camera, for `LynxImagePicker.pick({ source: 'camera' })`.
246
+ /// PHPickerViewController has none, so it's UIImagePickerController's, which
247
+ /// asks for the camera with NSCameraUsageDescription (angular.json's
248
+ /// `permissions.camera`).
249
+ extension ImagePickerModule: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
250
+ /// Opens the camera and calls back once with `{ base64, name, type }`, a
251
+ /// JPEG of the photo taken, `{ canceled: true }` when the person closes it,
252
+ /// or `{ error, message }`: `unavailable` on a device without a camera,
253
+ /// such as the Simulator, `denied` when the person doesn't let the app use
254
+ /// it, or `failed`.
255
+ @objc func takePhoto(_ options: [String: Any], callback: @escaping LynxCallbackBlock) {
256
+ // Lynx calls modules off the main thread, and the camera is UI.
257
+ DispatchQueue.main.async {
258
+ self.openCamera(options, callback)
259
+ }
260
+ }
261
+
262
+ private func openCamera(_ options: [String: Any], _ callback: @escaping LynxCallbackBlock) {
263
+ guard open == nil else {
264
+ callback(["error": "failed", "message": "A picker is already open."])
265
+ return
266
+ }
267
+ guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
268
+ callback(["error": "unavailable", "message": "This device has no camera."])
269
+ return
270
+ }
271
+ // UIImagePickerController would open on a black screen for a person who
272
+ // declined the camera, so that's told to the app instead. Asking first
273
+ // also shows iOS's prompt before the camera, rather than over it.
274
+ switch AVCaptureDevice.authorizationStatus(for: .video) {
275
+ case .authorized:
276
+ presentCamera(options, callback)
277
+ case .notDetermined:
278
+ AVCaptureDevice.requestAccess(for: .video) { granted in
279
+ DispatchQueue.main.async {
280
+ if granted {
281
+ self.presentCamera(options, callback)
282
+ } else {
283
+ callback(Self.cameraDenied)
284
+ }
285
+ }
286
+ }
287
+ default:
288
+ callback(Self.cameraDenied)
289
+ }
290
+ }
291
+
292
+ private static let cameraDenied: [String: Any] = [
293
+ "error": "denied", "message": "The app isn't allowed to use the camera.",
294
+ ]
295
+
296
+ private func presentCamera(_ options: [String: Any], _ callback: @escaping LynxCallbackBlock) {
297
+ // Asked again: a second takePhoto can arrive while the prompt is open.
298
+ guard open == nil else {
299
+ callback(["error": "failed", "message": "A picker is already open."])
300
+ return
301
+ }
302
+ guard let presenter = topViewController() else {
303
+ callback(["error": "failed", "message": "There's no screen to open the camera over."])
304
+ return
305
+ }
306
+ let camera = UIImagePickerController()
307
+ camera.sourceType = .camera
308
+ camera.mediaTypes = [UTType.image.identifier]
309
+ camera.delegate = self
310
+ open = Request(
311
+ maxDimension: (options["maxDimension"] as? NSNumber)?.intValue,
312
+ quality: (options["quality"] as? NSNumber)?.doubleValue,
313
+ callback: callback)
314
+ presenter.present(camera, animated: true)
315
+ }
316
+
317
+ func imagePickerController(
318
+ _ picker: UIImagePickerController,
319
+ didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]
320
+ ) {
321
+ picker.dismiss(animated: true)
322
+ guard let request = open else { return }
323
+ open = nil
324
+ guard let image = info[.originalImage] as? UIImage else {
325
+ request.callback(["error": "failed", "message": "The camera gave no photo."])
326
+ return
327
+ }
328
+ let name = Self.photoName()
329
+ // Encoding a 12-megapixel photo takes a moment: off the main thread.
330
+ DispatchQueue.global(qos: .userInitiated).async {
331
+ guard
332
+ let jpeg = Self.jpeg(
333
+ image, maxDimension: request.maxDimension,
334
+ quality: request.quality ?? Self.defaultQuality)
335
+ else {
336
+ request.callback(["error": "failed", "message": "The photo couldn't be encoded."])
337
+ return
338
+ }
339
+ request.callback([
340
+ "base64": jpeg.base64EncodedString(), "name": name, "type": "image/jpeg",
341
+ ])
342
+ }
343
+ }
344
+
345
+ func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
346
+ picker.dismiss(animated: true)
347
+ guard let request = open else { return }
348
+ open = nil
349
+ request.callback(["canceled": true])
350
+ }
351
+
352
+ /// The photo as a JPEG: upright, as drawing a UIImage applies its
353
+ /// orientation, no larger than `maxDimension` on its longest side, and
354
+ /// without the camera's metadata, such as where it was taken, as a picked
355
+ /// photo that's re-encoded comes back.
356
+ private static func jpeg(_ image: UIImage, maxDimension: Int?, quality: Double) -> Data? {
357
+ // The size in pixels, the way the photo is turned.
358
+ let width = image.size.width * image.scale
359
+ let height = image.size.height * image.scale
360
+ let longest = max(width, height)
361
+ guard longest > 0 else { return nil }
362
+ // Never larger than the photo.
363
+ let scale = min(CGFloat(maxDimension ?? Int(longest)), longest) / longest
364
+ let size = CGSize(
365
+ width: max(1, (width * scale).rounded()), height: max(1, (height * scale).rounded()))
366
+ let format = UIGraphicsImageRendererFormat()
367
+ format.scale = 1
368
+ // A JPEG has no transparency, and the camera's photo has none.
369
+ format.opaque = true
370
+ return UIGraphicsImageRenderer(size: size, format: format).jpegData(
371
+ withCompressionQuality: quality
372
+ ) { _ in
373
+ image.draw(in: CGRect(origin: .zero, size: size))
374
+ }
375
+ }
376
+
377
+ /// `IMG_20261129_142305.jpg`, from when the photo was taken: the camera
378
+ /// gives the photo no name.
379
+ private static func photoName() -> String {
380
+ let formatter = DateFormatter()
381
+ formatter.locale = Locale(identifier: "en_US_POSIX")
382
+ formatter.dateFormat = "yyyyMMdd_HHmmss"
383
+ return "IMG_\(formatter.string(from: Date())).jpg"
384
+ }
385
+ }
386
+ // /angular-lynx:image-picker-camera
@@ -52,6 +52,8 @@
52
52
  </array>
53
53
  <key>AngularLynxEdgeToEdge</key>
54
54
  <true/>
55
+ <key>AngularLynxTextScaling</key>
56
+ <true/>
55
57
  <!-- /angular-lynx:info -->
56
58
  </dict>
57
59
  </plist>