CameraModel.swift (2875B)
1 // 2 // CameraModel.swift 3 // damus 4 // 5 // Created by Suhail Saqan on 8/5/23. 6 // 7 8 import Foundation 9 import AVFoundation 10 import Combine 11 12 final class CameraModel: ObservableObject { 13 private let service = CameraService() 14 15 @Published var showAlertError = false 16 17 @Published var isFlashOn = false 18 19 @Published var willCapturePhoto = false 20 21 @Published var isCameraButtonDisabled = false 22 23 @Published var isPhotoProcessing = false 24 25 @Published var isRecording = false 26 27 @Published var captureMode: CameraMediaType = .image 28 29 @Published public var mediaItems: [MediaItem] = [] 30 31 @Published var thumbnail: Thumbnail! 32 33 var alertError: AlertError! 34 35 var session: AVCaptureSession 36 37 private var subscriptions = Set<AnyCancellable>() 38 39 init() { 40 self.session = service.session 41 42 service.$shouldShowAlertView.sink { [weak self] (val) in 43 self?.alertError = self?.service.alertError 44 self?.showAlertError = val 45 } 46 .store(in: &self.subscriptions) 47 48 service.$flashMode.sink { [weak self] (mode) in 49 self?.isFlashOn = mode == .on 50 } 51 .store(in: &self.subscriptions) 52 53 service.$willCapturePhoto.sink { [weak self] (val) in 54 self?.willCapturePhoto = val 55 } 56 .store(in: &self.subscriptions) 57 58 service.$isCameraButtonDisabled.sink { [weak self] (val) in 59 self?.isCameraButtonDisabled = val 60 } 61 .store(in: &self.subscriptions) 62 63 service.$isPhotoProcessing.sink { [weak self] (val) in 64 self?.isPhotoProcessing = val 65 } 66 .store(in: &self.subscriptions) 67 68 service.$isRecording.sink { [weak self] (val) in 69 self?.isRecording = val 70 } 71 .store(in: &self.subscriptions) 72 73 service.$captureMode.sink { [weak self] (mode) in 74 self?.captureMode = mode 75 } 76 .store(in: &self.subscriptions) 77 78 service.$mediaItems.sink { [weak self] (mode) in 79 self?.mediaItems = mode 80 } 81 .store(in: &self.subscriptions) 82 83 service.$thumbnail.sink { [weak self] (thumbnail) in 84 guard let pic = thumbnail else { return } 85 self?.thumbnail = pic 86 } 87 .store(in: &self.subscriptions) 88 } 89 90 func configure() { 91 service.checkForPermissions() 92 service.configure() 93 } 94 95 func stop() { 96 service.stop() 97 } 98 99 func capturePhoto() { 100 service.capturePhoto() 101 } 102 103 func startRecording() { 104 service.startRecording() 105 } 106 107 func stopRecording() { 108 service.stopRecording() 109 } 110 111 func flipCamera() { 112 service.changeCamera() 113 } 114 115 func zoom(with factor: CGFloat) { 116 service.set(zoom: factor) 117 } 118 119 func switchFlash() { 120 service.flashMode = service.flashMode == .on ? .off : .on 121 } 122 }