Skip to content

Commit 54616e2

Browse files
committed
feat: Update authorization request methods to use async/await pattern for improved concurrency
1 parent 627d62c commit 54616e2

File tree

3 files changed

+18
-29
lines changed

3 files changed

+18
-29
lines changed

app/xcode/Sources/VCamTracking/AudioManager.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public final class AudioManager {
77
public init() {}
88

99
public static var isMicrophoneAuthorized: () -> Bool = { false }
10-
public static var requestMicrophonePermission: (@escaping ((Bool) -> Void)) -> Void = { _ in }
10+
public static var requestMicrophonePermission: @MainActor () async -> Bool = { false }
1111

1212
private var onUpdateAudioBuffer: (@Sendable (AVAudioPCMBuffer, AVAudioTime, TimeInterval) -> Void)?
1313

@@ -24,11 +24,9 @@ public final class AudioManager {
2424
public func startRecording(onStart: @Sendable @escaping (AVAudioFormat) -> Void) {
2525
guard Self.isMicrophoneAuthorized() else {
2626
Logger.log("requestAuthorization")
27-
Self.requestMicrophonePermission { [weak self] authorized in
28-
guard authorized else { return }
29-
Task { @MainActor in
30-
self?.startRecording(onStart: onStart)
31-
}
27+
Task {
28+
guard await Self.requestMicrophonePermission() else { return }
29+
startRecording(onStart: onStart)
3230
}
3331
return
3432
}

app/xcode/Sources/VCamTracking/AvatarCameraManager.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public final class AvatarCameraManager {
1111

1212
private struct PermissionState: Sendable {
1313
var isCameraAuthorized: @Sendable () -> Bool = { false }
14-
var requestCameraPermission: @Sendable (@escaping @Sendable (Bool) -> Void) -> Void = { _ in }
14+
var requestCameraPermission: @Sendable @MainActor () async -> Bool = { false }
1515
}
1616

1717
public nonisolated static var isCameraAuthorized: @Sendable () -> Bool {
1818
get { permissionStorage.withLock { $0.isCameraAuthorized } }
1919
set { permissionStorage.withLock { $0.isCameraAuthorized = newValue } }
2020
}
2121

22-
public nonisolated static var requestCameraPermission: @Sendable (@escaping @Sendable (Bool) -> Void) -> Void {
22+
public nonisolated static var requestCameraPermission: @Sendable @MainActor () async -> Bool {
2323
get { permissionStorage.withLock { $0.requestCameraPermission } }
2424
set { permissionStorage.withLock { $0.requestCameraPermission = newValue } }
2525
}
@@ -47,11 +47,9 @@ public final class AvatarCameraManager {
4747
if Self.isCameraAuthorized() {
4848
webCamera.start()
4949
} else {
50-
Self.requestCameraPermission { [weak self] authorized in
51-
guard authorized else { return }
52-
DispatchQueue.runOnMain {
53-
self?.webCamera.start()
54-
}
50+
Task {
51+
guard await Self.requestCameraPermission() else { return }
52+
webCamera.start()
5553
}
5654
}
5755
}

app/xcode/Sources/VCamUI/DeviceAuthorization.swift

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,22 @@ public enum DeviceAuthorization {
4141
AVCaptureDevice.authorizationStatus(for: type.mediaType) == .authorized
4242
}
4343

44-
public static func requestAuthorization(type: AuthorizationType, completion: @escaping @Sendable (Bool) -> Void) {
44+
@MainActor
45+
public static func requestAuthorization(type: AuthorizationType) async -> Bool {
4546
switch AVCaptureDevice.authorizationStatus(for: type.mediaType) {
4647
case .authorized:
47-
completion(true)
48+
return true
4849

4950
case .notDetermined:
50-
let authType = type
51-
let completion = completion
52-
AVCaptureDevice.requestAccess(for: type.mediaType) { _ in
53-
Task { @MainActor in
54-
requestAuthorization(type: authType, completion: completion)
55-
}
56-
}
51+
await AVCaptureDevice.requestAccess(for: type.mediaType)
52+
return await requestAuthorization(type: type)
5753

5854
case .denied, .restricted:
59-
let authType = type
60-
let completion = completion
61-
Task { @MainActor in
62-
await showAuthorizationError(type: authType)
63-
completion(false)
64-
}
55+
await showAuthorizationError(type: type)
56+
return false
57+
6558
@unknown default:
66-
completion(false)
59+
return false
6760
}
6861
}
6962

0 commit comments

Comments
 (0)