Skip to content

Commit 06e16dd

Browse files
authored
[WCiOS17] Address scanner deprecations (#16268)
2 parents ec7fa98 + a2de9a0 commit 06e16dd

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

WooCommerce/Classes/ViewRelated/Products/Scanner/CodeScannerViewController.swift

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import AVFoundation
2+
import Combine
23
import UIKit
34
import Vision
45

@@ -36,6 +37,8 @@ final class CodeScannerViewController: UIViewController {
3637
private let sessionQueue = DispatchQueue(label: "qrlogincamerasession.queue.serial")
3738
private let session = AVCaptureSession()
3839
private var requests = [VNRequest]()
40+
private var rotationCoordinator: AVCaptureDevice.RotationCoordinator?
41+
private var cancellables = Set<AnyCancellable>()
3942

4043
private lazy var throttler: Throttler = Throttler(seconds: 0.1)
4144

@@ -160,10 +163,8 @@ private extension CodeScannerViewController {
160163
/// Enables and starts live stream video, if available.
161164
func startLiveVideo() {
162165
session.sessionPreset = .photo
163-
if #available(iOS 16.0, *) {
164-
if session.isMultitaskingCameraAccessSupported {
165-
session.isMultitaskingCameraAccessEnabled = true
166-
}
166+
if session.isMultitaskingCameraAccessSupported {
167+
session.isMultitaskingCameraAccessEnabled = true
167168
}
168169

169170
guard let captureDevice = AVCaptureDevice.default(for: .video),
@@ -187,6 +188,15 @@ private extension CodeScannerViewController {
187188
previewLayer.videoGravity = .resizeAspectFill
188189
previewLayer.frame = videoOutputImageView.bounds
189190
videoOutputImageView.layer.addSublayer(previewLayer)
191+
rotationCoordinator = AVCaptureDevice.RotationCoordinator(device: captureDevice, previewLayer: previewLayer)
192+
193+
rotationCoordinator?.publisher(for: \.videoRotationAngleForHorizonLevelPreview)
194+
.sink { [weak self] angle in
195+
guard let self = self,
196+
let connection = self.previewLayer?.connection else { return }
197+
connection.videoRotationAngle = angle
198+
}
199+
.store(in: &cancellables)
190200

191201
sessionQueue.async { [weak self] in
192202
self?.session.startRunning()
@@ -289,28 +299,35 @@ private extension CodeScannerViewController {
289299
// MARK: Orientation Handling
290300
//
291301
private extension CodeScannerViewController {
302+
enum VideoRotationAngle {
303+
static let portrait: CGFloat = 90
304+
static let landscapeRight: CGFloat = 0
305+
static let landscapeLeft: CGFloat = 180
306+
static let portraitUpsideDown: CGFloat = 270
307+
}
308+
292309
func updatePreviewLayerOrientation() {
293-
if let connection = previewLayer?.connection, connection.isVideoOrientationSupported {
310+
if let connection = previewLayer?.connection {
294311
let orientation = view.window?.windowScene?.interfaceOrientation
295-
let videoOrientation: AVCaptureVideoOrientation
312+
let videoRotationAngle: CGFloat
296313
switch orientation {
297314
case .portrait:
298-
videoOrientation = .portrait
315+
videoRotationAngle = VideoRotationAngle.portrait
299316
case .landscapeRight:
300-
videoOrientation = .landscapeRight
317+
videoRotationAngle = VideoRotationAngle.landscapeRight
301318
case .landscapeLeft:
302-
videoOrientation = .landscapeLeft
319+
videoRotationAngle = VideoRotationAngle.landscapeLeft
303320
case .portraitUpsideDown:
304-
videoOrientation = .portraitUpsideDown
321+
videoRotationAngle = VideoRotationAngle.portraitUpsideDown
305322
default:
306-
videoOrientation = .portrait
323+
videoRotationAngle = VideoRotationAngle.portrait
307324
}
308-
updatePreviewLayerVideoOrientation(connection: connection, orientation: videoOrientation)
325+
updatePreviewLayerVideoRotationAngle(connection: connection, angle: videoRotationAngle)
309326
}
310327
}
311328

312-
func updatePreviewLayerVideoOrientation(connection: AVCaptureConnection, orientation: AVCaptureVideoOrientation) {
313-
connection.videoOrientation = orientation
329+
func updatePreviewLayerVideoRotationAngle(connection: AVCaptureConnection, angle: CGFloat) {
330+
connection.videoRotationAngle = angle
314331
}
315332
}
316333

WooCommerce/Classes/ViewRelated/Products/Scanner/CodeScannerViewController.xib

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="23727" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
33
<device id="retina6_1" orientation="portrait" appearance="light"/>
44
<dependencies>
5-
<deployment identifier="iOS"/>
6-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22685"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23721"/>
76
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
87
<capability name="System colors in document resources" minToolsVersion="11.0"/>
98
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
@@ -36,14 +35,14 @@
3635
<rect key="frame" x="0.0" y="0.0" width="414" height="269"/>
3736
<subviews>
3837
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="wCb-QS-xTf">
39-
<rect key="frame" x="20" y="20" width="75" height="34.5"/>
38+
<rect key="frame" x="20" y="20" width="75" height="40.5"/>
4039
<state key="normal" title="Button"/>
4140
<buttonConfiguration key="configuration" style="plain" title="Button"/>
4241
</button>
4342
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="uYb-k2-4wm">
4443
<rect key="frame" x="338.5" y="20" width="55.5" height="34"/>
4544
<state key="normal" title="Button"/>
46-
<buttonConfiguration key="configuration" style="plain" image="arrow.triangle.2.circlepath.camera" catalog="system"/>
45+
<buttonConfiguration key="configuration" style="plain" image="arrow.trianglehead.2.clockwise.rotate.90.camera" catalog="system"/>
4746
</button>
4847
</subviews>
4948
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
@@ -94,7 +93,7 @@
9493
</view>
9594
</objects>
9695
<resources>
97-
<image name="arrow.triangle.2.circlepath.camera" catalog="system" width="128" height="93"/>
96+
<image name="arrow.trianglehead.2.clockwise.rotate.90.camera" catalog="system" width="128" height="93"/>
9897
<systemColor name="systemBackgroundColor">
9998
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
10099
</systemColor>

0 commit comments

Comments
 (0)