From 4c72d79fbfe25a910093bad13436c1c481c97be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20Cisin=CC=81ski?= Date: Mon, 4 Mar 2024 00:25:04 +0100 Subject: [PATCH] TF-1508 Adding "Camera not available" error to common scanner --- TwoFAS/CodeSupport/Camera/Camera.swift | 4 ++- .../Interactors/ModuleInteractorFactory.swift | 3 ++- .../Flow/CameraScannerFlowController.swift | 6 +++++ .../CameraScannerModuleInteractor.swift | 26 ++++++++++++++++++- .../Presenter/CameraScannerPresenter.swift | 21 +++++++++++++++ .../View/CameraScannerViewController.swift | 5 ++++ 6 files changed, 62 insertions(+), 3 deletions(-) diff --git a/TwoFAS/CodeSupport/Camera/Camera.swift b/TwoFAS/CodeSupport/Camera/Camera.swift index 1663e549..fb7e4140 100644 --- a/TwoFAS/CodeSupport/Camera/Camera.swift +++ b/TwoFAS/CodeSupport/Camera/Camera.swift @@ -81,7 +81,9 @@ public final class Camera { extension Camera: CameraControllerDelegate { func cameraDidInitialize() {} - func cameraFailedToInitilize(with error: CameraController.CameraError) {} + func cameraFailedToInitilize(with error: CameraController.CameraError) { + Log("Camera - can't start: \(error)", module: .camera) + } func cameraStartedPreview() { delegate?.didStartScanning() } diff --git a/TwoFAS/TwoFAS/Interactors/ModuleInteractorFactory.swift b/TwoFAS/TwoFAS/Interactors/ModuleInteractorFactory.swift index b3f382bd..581e7123 100644 --- a/TwoFAS/TwoFAS/Interactors/ModuleInteractorFactory.swift +++ b/TwoFAS/TwoFAS/Interactors/ModuleInteractorFactory.swift @@ -161,7 +161,8 @@ final class ModuleInteractorFactory { func cameraScannerModuleInteractor() -> CameraScannerModuleInteracting { CameraScannerModuleInteractor( newCodeInteractor: InteractorFactory.shared.newCodeInteractor(), - pushNotificationPermission: InteractorFactory.shared.pushNotificationRegistrationInteractor() + pushNotificationPermission: InteractorFactory.shared.pushNotificationRegistrationInteractor(), + cameraPermissionInteractor: InteractorFactory.shared.cameraPermissionInteractor() ) } diff --git a/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Flow/CameraScannerFlowController.swift b/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Flow/CameraScannerFlowController.swift index bbc00968..f017b61d 100644 --- a/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Flow/CameraScannerFlowController.swift +++ b/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Flow/CameraScannerFlowController.swift @@ -44,6 +44,7 @@ protocol CameraScannerFlowControlling: AnyObject { func toPushPermissions(extensionID: ExtensionID) func toRename(currentName: String, secret: String) func toServiceWasCreated(serviceData: ServiceData) + func toCameraNotAvailable() } final class CameraScannerFlowController: FlowController { @@ -189,6 +190,11 @@ extension CameraScannerFlowController: CameraScannerFlowControlling { viewController.present(alert, animated: true, completion: nil) } + func toCameraNotAvailable() { + let ac = AlertController.cameraNotAvailable + ac.show(animated: true, completion: nil) + } + func toPushPermissions(extensionID: ExtensionID) { guard let navi = viewController.navigationController else { return } navi.setNavigationBarHidden(true, animated: false) diff --git a/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Interactor/CameraScannerModuleInteractor.swift b/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Interactor/CameraScannerModuleInteractor.swift index 9a63cce4..08709fb7 100644 --- a/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Interactor/CameraScannerModuleInteractor.swift +++ b/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Interactor/CameraScannerModuleInteractor.swift @@ -26,6 +26,10 @@ protocol CameraScannerModuleInteracting: AnyObject { var shouldRename: ((String, String) -> Void)? { get set } var wasUserAskedAboutPush: Bool { get } + func isCameraAvailable() -> Bool + func isCameraAllowed() -> Bool + func registerCamera(callback: @escaping (Bool) -> Void) + func addCode(_ code: Code, force: Bool) func codeExists(_ code: Code) -> Bool func filterImportableCodes(_ codes: [Code]) -> [Code] @@ -38,19 +42,39 @@ protocol CameraScannerModuleInteracting: AnyObject { final class CameraScannerModuleInteractor { private let newCodeInteractor: NewCodeInteracting private let pushNotificationPermission: PushNotificationRegistrationInteracting + private let cameraPermissionInteractor: CameraPermissionInteracting var serviceWasCreated: ((ServiceData) -> Void)? var shouldRename: ((String, String) -> Void)? - init(newCodeInteractor: NewCodeInteracting, pushNotificationPermission: PushNotificationRegistrationInteracting) { + init( + newCodeInteractor: NewCodeInteracting, + pushNotificationPermission: PushNotificationRegistrationInteracting, + cameraPermissionInteractor: CameraPermissionInteracting + ) { self.newCodeInteractor = newCodeInteractor self.pushNotificationPermission = pushNotificationPermission + self.cameraPermissionInteractor = cameraPermissionInteractor newCodeInteractor.serviceWasCreated = { [weak self] in self?.serviceWasCreated?($0) } newCodeInteractor.shouldRename = { [weak self] in self?.shouldRename?($0, $1) } } } extension CameraScannerModuleInteractor: CameraScannerModuleInteracting { + func isCameraAvailable() -> Bool { + cameraPermissionInteractor.isCameraAvailable + } + + func isCameraAllowed() -> Bool { + cameraPermissionInteractor.isCameraAllowed + } + + func registerCamera(callback: @escaping (Bool) -> Void) { + cameraPermissionInteractor.register { status in + callback(status == .granted) + } + } + var wasUserAskedAboutPush: Bool { pushNotificationPermission.wasUserAsked } diff --git a/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Presenter/CameraScannerPresenter.swift b/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Presenter/CameraScannerPresenter.swift index 604f7ada..4bf18150 100644 --- a/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Presenter/CameraScannerPresenter.swift +++ b/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/Presenter/CameraScannerPresenter.swift @@ -39,6 +39,20 @@ final class CameraScannerPresenter { } extension CameraScannerPresenter { + func viewDidAppear() { + if interactor.isCameraAvailable() { + if !interactor.isCameraAllowed() { + interactor.registerCamera { [weak self] isGranted in + if !isGranted { + self?.handleCameraNotAvailable() + } + } + } + } else { + handleCameraNotAvailable() + } + } + func handleOpenGallery() { lockScanning = true flowController.toGallery() @@ -137,6 +151,13 @@ extension CameraScannerPresenter { } } + func handleCameraNotAvailable() { + view?.enableOverlay() + view?.feedback() + + flowController.toCameraNotAvailable() + } + func handleCameraError(_ str: String) { view?.enableOverlay() view?.feedback() diff --git a/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/View/CameraScannerViewController.swift b/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/View/CameraScannerViewController.swift index e412bb75..06849715 100644 --- a/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/View/CameraScannerViewController.swift +++ b/TwoFAS/TwoFAS/Root/Modules/Camera Scanner/View/CameraScannerViewController.swift @@ -66,6 +66,11 @@ final class CameraScannerViewController: UIViewController { navigationController?.setNavigationBarHidden(true, animated: false) } + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + presenter.viewDidAppear() + } + override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated)