@onekeyfe/react-native-qr-scanner 6.8.0-28 → 6.8.0-29

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 (2) hide show
  1. package/ios/QrScanner.swift +129 -2
  2. package/package.json +1 -1
@@ -10,6 +10,10 @@ final class QrPreviewView: UIView {
10
10
  layer as! AVCaptureVideoPreviewLayer
11
11
  }
12
12
 
13
+ private var sceneGeometryObservation: NSKeyValueObservation?
14
+ private var legacyOrientationTimer: Timer?
15
+ private var captureRunning = false
16
+
13
17
  private let focusIndicator: UIView = {
14
18
  let view = UIView(frame: CGRect(x: 0, y: 0, width: 72, height: 72))
15
19
  view.layer.borderColor = UIColor.white.cgColor
@@ -27,6 +31,110 @@ final class QrPreviewView: UIView {
27
31
  addSubview(focusIndicator)
28
32
  }
29
33
 
34
+ override func didMoveToWindow() {
35
+ super.didMoveToWindow()
36
+ if #available(iOS 16.0, *) {
37
+ // A scene can rotate without changing this view's bounds.
38
+ sceneGeometryObservation = window?.windowScene?.observe(\.effectiveGeometry) {
39
+ [weak self] _, _ in
40
+ self?.scheduleOrientationUpdate()
41
+ }
42
+ } else {
43
+ updateLegacyOrientationTimer()
44
+ }
45
+ updateCaptureOrientation()
46
+ }
47
+
48
+ override func layoutSubviews() {
49
+ super.layoutSubviews()
50
+ if #unavailable(iOS 16.0) { updateCaptureOrientation() }
51
+ }
52
+
53
+ func setCaptureRunning(_ running: Bool) {
54
+ captureRunning = running
55
+ updateLegacyOrientationTimer()
56
+ }
57
+
58
+ private func updateLegacyOrientationTimer() {
59
+ if #available(iOS 16.0, *) { return }
60
+ guard captureRunning, window?.windowScene != nil else {
61
+ legacyOrientationTimer?.invalidate()
62
+ legacyOrientationTimer = nil
63
+ return
64
+ }
65
+ guard legacyOrientationTimer == nil else { return }
66
+ // A rotation can commit after the device notification and leave our bounds unchanged.
67
+ let timer = Timer(timeInterval: 0.1, repeats: true) { [weak self] timer in
68
+ guard let self else {
69
+ timer.invalidate()
70
+ return
71
+ }
72
+ MainActor.assumeIsolated {
73
+ self.updateCaptureOrientation()
74
+ }
75
+ }
76
+ timer.tolerance = 0.02
77
+ RunLoop.main.add(timer, forMode: .common)
78
+ legacyOrientationTimer = timer
79
+ }
80
+
81
+ private nonisolated func scheduleOrientationUpdate() {
82
+ DispatchQueue.main.async { [weak self] in
83
+ self?.updateCaptureOrientation()
84
+ }
85
+ }
86
+
87
+ func updateCaptureOrientation() {
88
+ guard let scene = window?.windowScene, let connection = previewLayer.connection else { return }
89
+ let orientation: UIInterfaceOrientation
90
+ if #available(iOS 16.0, *) {
91
+ orientation = scene.effectiveGeometry.interfaceOrientation
92
+ } else {
93
+ orientation = scene.interfaceOrientation
94
+ }
95
+ let captureOrientation = Self.videoOrientation(for: orientation)
96
+ if #available(iOS 17.0, *) {
97
+ let videoPort = connection.inputPorts.first { $0.mediaType == .video }
98
+ guard let input = videoPort?.input as? AVCaptureDeviceInput else { return }
99
+ if input.device.deviceType == .external {
100
+ applyRotationAngle(0, to: connection)
101
+ return
102
+ }
103
+ #if compiler(>=6.4) && !targetEnvironment(macCatalyst)
104
+ if #available(iOS 27.0, *) {
105
+ guard let captureOrientation else { return }
106
+ let coordinator = AVCaptureDevice.RotationCoordinator(device: input.device, previewLayer: nil)
107
+ let angle = coordinator.videoRotationAngleRelative(toDeviceOrientation: captureOrientation)
108
+ applyRotationAngle(angle, to: connection)
109
+ return
110
+ }
111
+ #endif
112
+ }
113
+ // The typed orientation lets AVFoundation handle the built-in camera on older systems/SDKs.
114
+ if let captureOrientation, connection.isVideoOrientationSupported,
115
+ connection.videoOrientation != captureOrientation
116
+ {
117
+ connection.videoOrientation = captureOrientation
118
+ }
119
+ }
120
+
121
+ @available(iOS 17.0, *)
122
+ private func applyRotationAngle(_ angle: CGFloat, to connection: AVCaptureConnection) {
123
+ guard connection.videoRotationAngle != angle, connection.isVideoRotationAngleSupported(angle)
124
+ else { return }
125
+ connection.videoRotationAngle = angle
126
+ }
127
+
128
+ static func videoOrientation(for orientation: UIInterfaceOrientation) -> AVCaptureVideoOrientation? {
129
+ switch orientation {
130
+ case .portrait: return .portrait
131
+ case .portraitUpsideDown: return .portraitUpsideDown
132
+ case .landscapeLeft: return .landscapeLeft
133
+ case .landscapeRight: return .landscapeRight
134
+ default: return nil
135
+ }
136
+ }
137
+
30
138
  /// Shows the tap-to-focus box at `point` and fades it out.
31
139
  func showFocusIndicator(at point: CGPoint) {
32
140
  focusIndicator.layer.removeAllAnimations()
@@ -73,7 +181,15 @@ class HybridQrScanner: HybridQrScannerSpec {
73
181
  private var permissionPending = false
74
182
  private var output: AVCaptureMetadataOutput?
75
183
  private var metadataDelegate: MetadataDelegate?
76
- private var running = false
184
+ private var running = false {
185
+ didSet {
186
+ if #available(iOS 16.0, *) { return }
187
+ guard running != oldValue else { return }
188
+ DispatchQueue.main.async { [weak previewView, running] in
189
+ previewView?.setCaptureRunning(running)
190
+ }
191
+ }
192
+ }
77
193
  /// The configured input device. Owned by `sessionQueue`.
78
194
  private var device: AVCaptureDevice?
79
195
  private var input: AVCaptureDeviceInput?
@@ -218,14 +334,19 @@ class HybridQrScanner: HybridQrScannerSpec {
218
334
  if let input { session.removeInput(input) }
219
335
  var installed = false
220
336
  defer {
337
+ var restoredInput = false
221
338
  if !installed {
222
339
  if session.outputs.contains(newOutput) { session.removeOutput(newOutput) }
223
340
  if session.inputs.contains(newInput) { session.removeInput(newInput) }
224
341
  session.sessionPreset = oldPreset
225
- if let input, session.canAddInput(input) { session.addInput(input) }
342
+ if let input, session.canAddInput(input) {
343
+ session.addInput(input)
344
+ restoredInput = true
345
+ }
226
346
  if let output, session.canAddOutput(output) { session.addOutput(output) }
227
347
  }
228
348
  session.commitConfiguration()
349
+ if installed || restoredInput { updatePreviewOrientation() }
229
350
  }
230
351
 
231
352
  let presets: [AVCaptureSession.Preset] = [.hd1920x1080, .hd1280x720, .high, .medium, .low, .inputPriority]
@@ -278,6 +399,12 @@ class HybridQrScanner: HybridQrScannerSpec {
278
399
  captureProps.ready?(Double(generation))
279
400
  }
280
401
 
402
+ private func updatePreviewOrientation() {
403
+ DispatchQueue.main.async { [weak previewView] in
404
+ previewView?.updateCaptureOrientation()
405
+ }
406
+ }
407
+
281
408
  private func observeSubjectArea(of device: AVCaptureDevice) {
282
409
  if let subjectAreaObserver { NotificationCenter.default.removeObserver(subjectAreaObserver) }
283
410
  subjectAreaObserver = NotificationCenter.default.addObserver(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-qr-scanner",
3
- "version": "6.8.0-28",
3
+ "version": "6.8.0-29",
4
4
  "description": "react-native-qr-scanner",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",