forked from damus-io/damus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add CameraModel and CameraService for interacting with the camera
- Loading branch information
1 parent
7744787
commit 472f81b
Showing
4 changed files
with
859 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// CameraModel.swift | ||
// damus | ||
// | ||
// Created by Suhail Saqan on 8/5/23. | ||
// | ||
|
||
import Foundation | ||
import AVFoundation | ||
import Combine | ||
|
||
final class CameraModel: ObservableObject { | ||
private let service = CameraService() | ||
|
||
@Published var showAlertError = false | ||
|
||
@Published var isFlashOn = false | ||
|
||
@Published var willCapturePhoto = false | ||
|
||
@Published var isCameraButtonDisabled = false | ||
|
||
@Published var isPhotoProcessing = false | ||
|
||
@Published var isRecording = false | ||
|
||
@Published var captureMode: CameraMediaType = .image | ||
|
||
@Published public var mediaItems: [MediaItem] = [] | ||
|
||
@Published var thumbnail: Thumbnail! | ||
|
||
var alertError: AlertError! | ||
|
||
var session: AVCaptureSession | ||
|
||
private var subscriptions = Set<AnyCancellable>() | ||
|
||
init() { | ||
self.session = service.session | ||
|
||
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) | ||
|
||
service.$willCapturePhoto.sink { [weak self] (val) in | ||
self?.willCapturePhoto = val | ||
} | ||
.store(in: &self.subscriptions) | ||
|
||
service.$isCameraButtonDisabled.sink { [weak self] (val) in | ||
self?.isCameraButtonDisabled = val | ||
} | ||
.store(in: &self.subscriptions) | ||
|
||
service.$isPhotoProcessing.sink { [weak self] (val) in | ||
self?.isPhotoProcessing = val | ||
} | ||
.store(in: &self.subscriptions) | ||
|
||
service.$isRecording.sink { [weak self] (val) in | ||
self?.isRecording = val | ||
} | ||
.store(in: &self.subscriptions) | ||
|
||
service.$captureMode.sink { [weak self] (mode) in | ||
self?.captureMode = mode | ||
} | ||
.store(in: &self.subscriptions) | ||
|
||
service.$mediaItems.sink { [weak self] (mode) in | ||
self?.mediaItems = mode | ||
} | ||
.store(in: &self.subscriptions) | ||
|
||
service.$thumbnail.sink { [weak self] (thumbnail) in | ||
guard let pic = thumbnail else { return } | ||
self?.thumbnail = pic | ||
} | ||
.store(in: &self.subscriptions) | ||
} | ||
|
||
func configure() { | ||
service.checkForPermissions() | ||
service.configure() | ||
} | ||
|
||
func stop() { | ||
service.stop() | ||
} | ||
|
||
func capturePhoto() { | ||
service.capturePhoto() | ||
} | ||
|
||
func startRecording() { | ||
service.startRecording() | ||
} | ||
|
||
func stopRecording() { | ||
service.stopRecording() | ||
} | ||
|
||
func flipCamera() { | ||
service.changeCamera() | ||
} | ||
|
||
func zoom(with factor: CGFloat) { | ||
service.set(zoom: factor) | ||
} | ||
|
||
func switchFlash() { | ||
service.flashMode = service.flashMode == .on ? .off : .on | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// CameraService+Extensions.swift | ||
// damus | ||
// | ||
// Created by Suhail Saqan on 8/5/23. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
import AVFoundation | ||
|
||
extension AVCaptureVideoOrientation { | ||
init?(deviceOrientation: UIDeviceOrientation) { | ||
switch deviceOrientation { | ||
case .portrait: self = .portrait | ||
case .portraitUpsideDown: self = .portraitUpsideDown | ||
case .landscapeLeft: self = .landscapeRight | ||
case .landscapeRight: self = .landscapeLeft | ||
default: return nil | ||
} | ||
} | ||
|
||
init?(interfaceOrientation: UIInterfaceOrientation) { | ||
switch interfaceOrientation { | ||
case .portrait: self = .portrait | ||
case .portraitUpsideDown: self = .portraitUpsideDown | ||
case .landscapeLeft: self = .landscapeLeft | ||
case .landscapeRight: self = .landscapeRight | ||
default: return nil | ||
} | ||
} | ||
} |
Oops, something went wrong.