Skip to content

Commit

Permalink
fix: Fix delegation order
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jun 4, 2024
1 parent 1eb2005 commit 9d00a6e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package/ios/Core/CameraSession+Orientation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import AVFoundation
import Foundation

extension CameraSession: CameraOrientationCoordinatorDelegate {
extension CameraSession: OrientationManager.Delegate {
var outputOrientation: Orientation {
return orientationManager.outputOrientation
}
Expand Down
2 changes: 1 addition & 1 deletion package/ios/Core/CameraSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CameraSession: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate, AVC
override init() {
super.init()

orientationManager.setDelegate(self)
orientationManager.delegate = self
NotificationCenter.default.addObserver(self,
selector: #selector(sessionRuntimeError),
name: .AVCaptureSessionRuntimeError,
Expand Down
34 changes: 15 additions & 19 deletions package/ios/Core/OrientationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
import AVFoundation
import Foundation

/**
Provides Orientation updates to the consumer.
The orientation updates are only pushed as long as a [delegate] is set.
*/
class OrientationManager: CameraOrientationCoordinatorDelegate {
private var orientationCoordinator: CameraOrientationCoordinator?
private var targetOutputOrientation = OutputOrientation.device
private weak var previewLayer: CALayer?
private weak var device: AVCaptureDevice?
private weak var delegate: CameraOrientationCoordinatorDelegate?

Check failure on line 20 in package/ios/Core/OrientationManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
weak var delegate: Delegate?

/**
The orientation of the preview view.
Expand Down Expand Up @@ -76,10 +81,6 @@ class OrientationManager: CameraOrientationCoordinatorDelegate {
orientationCoordinator?.setDelegate(self)
}

func setDelegate(_ delegate: CameraOrientationCoordinatorDelegate) {
self.delegate = delegate
}

func setTargetOutputOrientation(_ targetOrientation: OutputOrientation) {
if targetOutputOrientation == targetOrientation {
// already the same
Expand All @@ -92,20 +93,15 @@ class OrientationManager: CameraOrientationCoordinatorDelegate {
delegate?.onOutputOrientationChanged(outputOrientation: outputOrientation)
delegate?.onPreviewOrientationChanged(previewOrientation: previewOrientation)
}

func onPreviewOrientationChanged(previewOrientation _: Orientation) {
guard let delegate else {
return
}
// update delegate listener
delegate.onPreviewOrientationChanged(previewOrientation: previewOrientation)

Check failure on line 96 in package/ios/Core/OrientationManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
func onOrientationChanged() {
// Notify both delegate listeners about the new orientation change
delegate?.onPreviewOrientationChanged(previewOrientation: previewOrientation)
delegate?.onOutputOrientationChanged(outputOrientation: outputOrientation)
}

func onOutputOrientationChanged(outputOrientation _: Orientation) {
guard let delegate else {
return
}
// update delegate listener
delegate.onOutputOrientationChanged(outputOrientation: outputOrientation)

Check failure on line 102 in package/ios/Core/OrientationManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
protocol Delegate: AnyObject {
func onPreviewOrientationChanged(previewOrientation: Orientation)
func onOutputOrientationChanged(outputOrientation: Orientation)
}
}
3 changes: 1 addition & 2 deletions package/ios/Core/Utils/CameraOrientationCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ protocol CameraOrientationCoordinator {
// MARK: - CameraOrientationCoordinatorDelegate

protocol CameraOrientationCoordinatorDelegate: AnyObject {
func onPreviewOrientationChanged(previewOrientation: Orientation)
func onOutputOrientationChanged(outputOrientation: Orientation)
func onOrientationChanged()
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ class LegacyCameraOrientationCoordinator: CameraOrientationCoordinator {
outputOrientation = orientation

// Notify delegate listener
delegate?.onPreviewOrientationChanged(previewOrientation: previewOrientation)
delegate?.onOutputOrientationChanged(outputOrientation: outputOrientation)
delegate?.onOrientationChanged()
}

func setDelegate(_ delegate: any CameraOrientationCoordinatorDelegate) {
self.delegate = delegate
delegate.onPreviewOrientationChanged(previewOrientation: previewOrientation)
delegate.onOutputOrientationChanged(outputOrientation: outputOrientation)
// Notify delegate listener
delegate.onOrientationChanged()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ class ModernCameraOrientationCoordinator: CameraOrientationCoordinator {
// Observe Preview Rotation
previewObserver = rotationCoordinator.observe(\.videoRotationAngleForHorizonLevelPreview) { [weak self] _, _ in
if let self {
self.delegate?.onPreviewOrientationChanged(previewOrientation: self.previewOrientation)
self.delegate?.onOrientationChanged()
}
}
// Observe Output Rotation
outputObserver = rotationCoordinator.observe(\.videoRotationAngleForHorizonLevelCapture) { [weak self] _, _ in
if let self {
self.delegate?.onOutputOrientationChanged(outputOrientation: self.outputOrientation)
self.delegate?.onOrientationChanged()
}
}
}

func setDelegate(_ delegate: any CameraOrientationCoordinatorDelegate) {
self.delegate = delegate
delegate.onOutputOrientationChanged(outputOrientation: outputOrientation)
delegate.onPreviewOrientationChanged(previewOrientation: previewOrientation)
delegate.onOrientationChanged()
}
}

0 comments on commit 9d00a6e

Please sign in to comment.