Created
October 24, 2020 18:03
-
-
Save rorodriguez116/f77fa522aa6c5f7b60c6a93c96362ef8 to your computer and use it in GitHub Desktop.
CameraViewModel -- Handles CameraServices and acts as middleman to CameraView.
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
import Combine | |
import AVFoundation | |
final class CameraViewModel: ObservableObject { | |
private let service = CameraService() | |
@Published var photo: Photo! | |
@Published var showAlertError = false | |
@Published var isFlashOn = false | |
var alertError: AlertError! | |
var session: AVCaptureSession | |
private var subscriptions = Set<AnyCancellable>() | |
init() { | |
self.session = service.session | |
service.$photo.sink { [weak self] (photo) in | |
guard let pic = photo else { return } | |
self?.photo = pic | |
} | |
.store(in: &self.subscriptions) | |
service.$shouldShowAlertView.sink { [weak self] (val) in | |
self?.alertError = self?.service.alertError | |
self?.showAlertError = val | |
} | |
.store(in: &self.subscriptions) | |
service.$flashMode.sink { [weak self] (mode) in | |
self?.isFlashOn = mode == .on | |
} | |
.store(in: &self.subscriptions) | |
} | |
func configure() { | |
service.checkForPermissions() | |
service.configure() | |
} | |
func capturePhoto() { | |
service.capturePhoto() | |
} | |
func flipCamera() { | |
service.changeCamera() | |
} | |
func zoom(with factor: CGFloat) { | |
service.set(zoom: factor) | |
} | |
func switchFlash() { | |
service.flashMode = service.flashMode == .on ? .off : .on | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment