Skip to content

Instantly share code, notes, and snippets.

@rorodriguez116
Created October 24, 2020 18:03
Show Gist options
  • Save rorodriguez116/f77fa522aa6c5f7b60c6a93c96362ef8 to your computer and use it in GitHub Desktop.
Save rorodriguez116/f77fa522aa6c5f7b60c6a93c96362ef8 to your computer and use it in GitHub Desktop.
CameraViewModel -- Handles CameraServices and acts as middleman to CameraView.
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