|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// All errors related with the `OSBARCManager` structure. |
| 4 | +public enum OSBARCManagerError: Error { |
| 5 | + case cameraAccessDenied |
| 6 | + case scanningCancelled |
| 7 | +} |
| 8 | + |
| 9 | +/// Structure responsible for managing all barcode scanning flow. |
| 10 | +struct OSBARCManager { |
| 11 | + /// Responsible for verifying the device's authorisation to its camera. It also handles the required flow to enable this authorisation. |
| 12 | + private let permissionsBehaviour: OSBARCPermissionsProtocol |
| 13 | + /// Responsible for triggering the barcode scanner view. |
| 14 | + private let scannerBehaviour: OSBARCScannerProtocol |
| 15 | + |
| 16 | + /// Constructor. |
| 17 | + /// - Parameters: |
| 18 | + /// - permissionsBehaviour: Responsible for verifying the device's authorisation to its camera. It also handles the required flow to enable this authorisation. |
| 19 | + /// - scannerBehaviour: Responsible for triggering the barcode scanner view. |
| 20 | + init(permissionsBehaviour: OSBARCPermissionsProtocol, scannerBehaviour: OSBARCScannerProtocol) { |
| 21 | + self.permissionsBehaviour = permissionsBehaviour |
| 22 | + self.scannerBehaviour = scannerBehaviour |
| 23 | + } |
| 24 | + |
| 25 | + /// Convenience constructor |
| 26 | + /// - Parameter coordinator: Object responsible for managing the screen flow, in response to the user's interaction. |
| 27 | + init(coordinator: OSBARCCoordinatorProtocol) { |
| 28 | + let permissionsBehaviour = OSBARCPermissionsBehaviour(coordinator: coordinator) |
| 29 | + let scannerBehaviour = OSBARCScannerBehaviour(coordinator: coordinator) |
| 30 | + self.init(permissionsBehaviour: permissionsBehaviour, scannerBehaviour: scannerBehaviour) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/// Implementation of the `OSBARCManagerProtocol` methods. |
| 35 | +extension OSBARCManager: OSBARCManagerProtocol { |
| 36 | + func scanBarcode(with instructionsText: String, _ buttonText: String?, _ cameraModel: OSBARCCameraModel, and orientationModel: OSBARCOrientationModel) async throws -> String { |
| 37 | + // validates if the user has access to the device's camera. |
| 38 | + let hasCameraAccess = await self.permissionsBehaviour.hasCameraAccess() |
| 39 | + if !hasCameraAccess { throw OSBARCManagerError.cameraAccessDenied } |
| 40 | + // requests the scanner to start, treating its result value. |
| 41 | + return try await withCheckedThrowingContinuation { |
| 42 | + self.startScanning(with: instructionsText, buttonText, cameraModel, and: orientationModel, continuation: $0) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Triggers the scanner view. |
| 47 | + /// - Parameters: |
| 48 | + /// - instructionsText: Text to be displayed on the scanner view. |
| 49 | + /// - buttonText: Text to be displayed for the scan button, if this is configured. `Nil` value means that the button will not be shown. |
| 50 | + /// - cameraModel: Camera to use for input gathering. |
| 51 | + /// - orientationModel: Scanner view's orientation. |
| 52 | + /// - continuation: Object responsible for returning the method's result to its caller. |
| 53 | + private func startScanning( |
| 54 | + with instructionsText: String, |
| 55 | + _ buttonText: String?, |
| 56 | + _ cameraModel: OSBARCCameraModel, |
| 57 | + and orientationModel: OSBARCOrientationModel, |
| 58 | + continuation: CheckedContinuation<String, any Error> |
| 59 | + ) { |
| 60 | + DispatchQueue.main.async { |
| 61 | + self.scannerBehaviour.startScanning(with: instructionsText, buttonText, cameraModel, and: orientationModel) { scannedCode in |
| 62 | + if !scannedCode.isEmpty { |
| 63 | + continuation.resume(returning: scannedCode) |
| 64 | + } else { |
| 65 | + continuation.resume(throwing: OSBARCManagerError.scanningCancelled) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments