-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[camera_avfoundation] Wrappers swift migration - part 5 #10641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RobertOdrowaz
wants to merge
1
commit into
flutter:main
Choose a base branch
from
leancodepl:feature/camera-wrappers-swift-migration-part5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...ndation/ios/camera_avfoundation/Sources/camera_avfoundation/CameraPermissionManager.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import AVFoundation | ||
| import Flutter | ||
|
|
||
| /// Completion handler for camera permission requests. | ||
| typealias CameraPermissionRequestCompletionHandler = (FlutterError?) -> Void | ||
|
|
||
| /// Manages camera and audio permission requests. | ||
| class CameraPermissionManager: NSObject { | ||
| let permissionService: PermissionServicing | ||
|
|
||
| init(permissionService: PermissionServicing) { | ||
| self.permissionService = permissionService | ||
| super.init() | ||
| } | ||
|
|
||
| /// Requests camera access permission. | ||
| /// | ||
| /// If it is the first time requesting camera access, a permission dialog will show up on the | ||
| /// screen. Otherwise AVFoundation simply returns the user's previous choice, and in this case the | ||
| /// user will have to update the choice in Settings app. | ||
| /// | ||
| /// @param handler if access permission is (or was previously) granted, completion handler will be | ||
| /// called without error; Otherwise completion handler will be called with error. Handler can be | ||
| /// called on an arbitrary dispatch queue. | ||
| func requestCameraPermission( | ||
| completionHandler handler: @escaping CameraPermissionRequestCompletionHandler | ||
| ) { | ||
| requestPermission(forAudio: false, handler: handler) | ||
| } | ||
|
|
||
| /// Requests audio access permission. | ||
| /// | ||
| /// If it is the first time requesting audio access, a permission dialog will show up on the | ||
| /// screen. Otherwise AVFoundation simply returns the user's previous choice, and in this case the | ||
| /// user will have to update the choice in Settings app. | ||
| /// | ||
| /// @param handler if access permission is (or was previously) granted, completion handler will be | ||
| /// called without error; Otherwise completion handler will be called with error. Handler can be | ||
| /// called on an arbitrary dispatch queue. | ||
| func requestAudioPermission( | ||
| completionHandler handler: @escaping CameraPermissionRequestCompletionHandler | ||
| ) { | ||
| requestPermission(forAudio: true, handler: handler) | ||
| } | ||
|
|
||
| private func requestPermission( | ||
| forAudio: Bool, | ||
| handler: @escaping CameraPermissionRequestCompletionHandler | ||
| ) { | ||
| let mediaType: AVMediaType = forAudio ? .audio : .video | ||
|
|
||
| switch permissionService.authorizationStatus(for: mediaType) { | ||
| case .authorized: | ||
| handler(nil) | ||
|
|
||
| case .denied: | ||
| let flutterError: FlutterError | ||
| if forAudio { | ||
| flutterError = FlutterError( | ||
| code: "AudioAccessDeniedWithoutPrompt", | ||
| message: | ||
| "User has previously denied the audio access request. Go to Settings to enable audio access.", | ||
| details: nil | ||
| ) | ||
| } else { | ||
| flutterError = FlutterError( | ||
| code: "CameraAccessDeniedWithoutPrompt", | ||
| message: | ||
| "User has previously denied the camera access request. Go to Settings to enable camera access.", | ||
| details: nil | ||
| ) | ||
| } | ||
| handler(flutterError) | ||
|
|
||
| case .restricted: | ||
| let flutterError: FlutterError | ||
| if forAudio { | ||
| flutterError = FlutterError( | ||
| code: "AudioAccessRestricted", | ||
| message: "Audio access is restricted.", | ||
| details: nil | ||
| ) | ||
| } else { | ||
| flutterError = FlutterError( | ||
| code: "CameraAccessRestricted", | ||
| message: "Camera access is restricted.", | ||
| details: nil | ||
| ) | ||
| } | ||
| handler(flutterError) | ||
|
|
||
| case .notDetermined: | ||
| permissionService.requestAccess(for: mediaType) { granted in | ||
| // handler can be invoked on an arbitrary dispatch queue. | ||
| if granted { | ||
| handler(nil) | ||
| } else { | ||
| let flutterError: FlutterError | ||
| if forAudio { | ||
| flutterError = FlutterError( | ||
| code: "AudioAccessDenied", | ||
| message: "User denied the audio access request.", | ||
| details: nil | ||
| ) | ||
| } else { | ||
| flutterError = FlutterError( | ||
| code: "CameraAccessDenied", | ||
| message: "User denied the camera access request.", | ||
| details: nil | ||
| ) | ||
| } | ||
| handler(flutterError) | ||
| } | ||
| } | ||
|
|
||
| @unknown default: | ||
| assertionFailure("Unknown authorization status") | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...vfoundation/ios/camera_avfoundation/Sources/camera_avfoundation/PermissionServicing.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import AVFoundation | ||
|
|
||
| /// A protocol for permission-related operations on AVCaptureDevice. | ||
| /// It exists to allow mocking permission checks in tests. | ||
| protocol PermissionServicing: NSObjectProtocol { | ||
| func authorizationStatus(for mediaType: AVMediaType) -> AVAuthorizationStatus | ||
| func requestAccess( | ||
| for mediaType: AVMediaType, | ||
| completion handler: @escaping @Sendable (Bool) -> Void | ||
| ) | ||
| } | ||
|
|
||
| /// Default implementation of PermissionServicing that forwards calls to AVCaptureDevice. | ||
| class DefaultPermissionService: NSObject, PermissionServicing { | ||
| func authorizationStatus(for mediaType: AVMediaType) -> AVAuthorizationStatus { | ||
| return AVCaptureDevice.authorizationStatus(for: mediaType) | ||
| } | ||
|
|
||
| func requestAccess( | ||
| for mediaType: AVMediaType, | ||
| completion handler: @escaping @Sendable (Bool) -> Void | ||
| ) { | ||
| AVCaptureDevice.requestAccess(for: mediaType, completionHandler: handler) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.