Skip to content

Commit 49566c7

Browse files
[camera_avfoundation] Wrappers swift migration - part 2 (#10284)
Migrates camera wrappers as part of flutter/flutter#119109 This PR migrates wrappers to Swift: * `FLTCameraDeviceDiscovering` * `FLTDeviceOrientationProviding` In line with Swift conventions, the `FLT` prefixes are removed. The `Default` class implementations are replaced with protocol conformance on base `AV` classes. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 53d6138 commit 49566c7

File tree

14 files changed

+81
-107
lines changed

14 files changed

+81
-107
lines changed

packages/camera/camera_avfoundation/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.22+4
2+
3+
* Migrates `FLTCameraDeviceDiscovering` and `FLTDeviceOrientationProviding` classes to Swift.
4+
15
## 0.9.22+3
26

37
* Updates examples to use the new RadioGroup API instead of deprecated Radio parameters.

packages/camera/camera_avfoundation/example/ios/RunnerTests/Mocks/MockCameraDeviceDiscoverer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import camera_avfoundation
5+
@testable import camera_avfoundation
66

77
// Import Objective-C part of the implementation when SwiftPM is used.
88
#if canImport(camera_avfoundation_objc)
99
import camera_avfoundation_objc
1010
#endif
1111

12-
/// Mock implementation of `FLTCameraDeviceDiscovering` protocol which allows injecting a custom
12+
/// Mock implementation of `FLTCameraDeviceDiscoverer` protocol which allows injecting a custom
1313
/// implementation for session discovery.
14-
final class MockCameraDeviceDiscoverer: NSObject, FLTCameraDeviceDiscovering {
14+
final class MockCameraDeviceDiscoverer: NSObject, CameraDeviceDiscoverer {
1515
var discoverySessionStub:
1616
(
1717
(

packages/camera/camera_avfoundation/example/ios/RunnerTests/Mocks/MockDeviceOrientationProvider.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import camera_avfoundation
5+
@testable import camera_avfoundation
66

77
// Import Objective-C part of the implementation when SwiftPM is used.
88
#if canImport(camera_avfoundation_objc)
99
import camera_avfoundation_objc
1010
#endif
1111

12-
final class MockDeviceOrientationProvider: NSObject, FLTDeviceOrientationProviding {
12+
final class MockDeviceOrientationProvider: NSObject, DeviceOrientationProvider {
1313
var orientationStub: (() -> UIDeviceOrientation)?
1414

15-
func orientation() -> UIDeviceOrientation {
15+
var orientation: UIDeviceOrientation {
1616
return orientationStub?() ?? .unknown
1717
}
1818
}

packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation/CameraConfiguration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class CameraConfiguration {
4343
var assetWriterFactory: AssetWriterFactory
4444
var inputPixelBufferAdaptorFactory: InputPixelBufferAdaptorFactory
4545
var videoDimensionsConverter: VideoDimensionsConverter
46-
var deviceOrientationProvider: FLTDeviceOrientationProviding
46+
var deviceOrientationProvider: DeviceOrientationProvider
4747
let initialCameraName: String
4848
var orientation: UIDeviceOrientation
4949

@@ -67,7 +67,7 @@ class CameraConfiguration {
6767
self.captureDeviceInputFactory = captureDeviceInputFactory
6868
self.initialCameraName = initialCameraName
6969
self.orientation = UIDevice.current.orientation
70-
self.deviceOrientationProvider = FLTDefaultDeviceOrientationProvider()
70+
self.deviceOrientationProvider = DefaultDeviceOrientationProvider()
7171

7272
self.videoDimensionsConverter = { format in
7373
return CMVideoFormatDescriptionGetDimensions(format.formatDescription)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import AVFoundation
6+
7+
// Import Objective-C part of the implementation when SwiftPM is used.
8+
#if canImport(camera_avfoundation_objc)
9+
import camera_avfoundation_objc
10+
#endif
11+
12+
/// A protocol which abstracts the discovery of camera devices.
13+
/// It is a thin wrapper around `AVCaptureDiscoverySession` and it exists to allow mocking in tests.
14+
protocol CameraDeviceDiscoverer {
15+
func discoverySession(
16+
withDeviceTypes deviceTypes: [AVCaptureDevice.DeviceType],
17+
mediaType: AVMediaType,
18+
position: AVCaptureDevice.Position
19+
) -> [FLTCaptureDevice]
20+
}
21+
22+
/// The default implementation of the `CameraDeviceDiscoverer` protocol.
23+
/// It wraps a call to `AVCaptureDeviceDiscoverySession`.
24+
class DefaultCameraDeviceDiscoverer: NSObject, CameraDeviceDiscoverer {
25+
func discoverySession(
26+
withDeviceTypes deviceTypes: [AVCaptureDevice.DeviceType],
27+
mediaType: AVMediaType,
28+
position: AVCaptureDevice.Position
29+
) -> [FLTCaptureDevice] {
30+
let discoverySession = AVCaptureDevice.DiscoverySession(
31+
deviceTypes: deviceTypes,
32+
mediaType: mediaType,
33+
position: position
34+
)
35+
36+
let devices = discoverySession.devices
37+
let deviceControllers = devices.map { device in
38+
FLTDefaultCaptureDevice(device: device) as FLTCaptureDevice
39+
}
40+
41+
return deviceControllers
42+
}
43+
}

packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation/CameraPlugin.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class CameraPlugin: NSObject, FlutterPlugin {
1414
private let registry: FlutterTextureRegistry
1515
private let messenger: FlutterBinaryMessenger
1616
private let globalEventAPI: FCPCameraGlobalEventApi
17-
private let deviceDiscoverer: FLTCameraDeviceDiscovering
17+
private let deviceDiscoverer: CameraDeviceDiscoverer
1818
private let permissionManager: FLTCameraPermissionManager
1919
private let captureDeviceFactory: VideoCaptureDeviceFactory
2020
private let captureSessionFactory: CaptureSessionFactory
@@ -31,7 +31,7 @@ public final class CameraPlugin: NSObject, FlutterPlugin {
3131
registry: registrar.textures(),
3232
messenger: registrar.messenger(),
3333
globalAPI: FCPCameraGlobalEventApi(binaryMessenger: registrar.messenger()),
34-
deviceDiscoverer: FLTDefaultCameraDeviceDiscoverer(),
34+
deviceDiscoverer: DefaultCameraDeviceDiscoverer(),
3535
permissionManager: FLTCameraPermissionManager(
3636
permissionService: FLTDefaultPermissionService()),
3737
deviceFactory: { name in
@@ -50,7 +50,7 @@ public final class CameraPlugin: NSObject, FlutterPlugin {
5050
registry: FlutterTextureRegistry,
5151
messenger: FlutterBinaryMessenger,
5252
globalAPI: FCPCameraGlobalEventApi,
53-
deviceDiscoverer: FLTCameraDeviceDiscovering,
53+
deviceDiscoverer: CameraDeviceDiscoverer,
5454
permissionManager: FLTCameraPermissionManager,
5555
deviceFactory: @escaping VideoCaptureDeviceFactory,
5656
captureSessionFactory: @escaping CaptureSessionFactory,

packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation/DefaultCamera.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ final class DefaultCamera: NSObject, Camera {
5757
/// Allows for alternate implementations in tests.
5858
private let videoDimensionsConverter: VideoDimensionsConverter
5959

60-
private let deviceOrientationProvider: FLTDeviceOrientationProviding
60+
private let deviceOrientationProvider: DeviceOrientationProvider
6161
private let motionManager = CMMotionManager()
6262

6363
private(set) var captureDevice: FLTCaptureDevice
@@ -858,7 +858,7 @@ final class DefaultCamera: NSObject, Camera {
858858
return
859859
}
860860

861-
let orientation = deviceOrientationProvider.orientation()
861+
let orientation = deviceOrientationProvider.orientation
862862
try? captureDevice.lockForConfiguration()
863863
// A nil point resets to the center.
864864
captureDevice.setFocusPointOfInterest(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import UIKit
6+
7+
/// A protocol which provides the current device orientation.
8+
/// It exists to allow replacing UIDevice in tests.
9+
protocol DeviceOrientationProvider {
10+
/// Returns the physical orientation of the device.
11+
var orientation: UIDeviceOrientation { get }
12+
}
13+
14+
/// A default implementation of DeviceOrientationProvider which uses orientation
15+
/// of the current device from UIDevice.
16+
@objc public class DefaultDeviceOrientationProvider: NSObject, DeviceOrientationProvider {
17+
@objc public var orientation: UIDeviceOrientation {
18+
return UIDevice.current.orientation
19+
}
20+
}

packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation_objc/FLTCameraDeviceDiscovering.m

Lines changed: 0 additions & 30 deletions
This file was deleted.

packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation_objc/FLTDeviceOrientationProviding.m

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)