Skip to content

Commit

Permalink
Patch 0.9.1
Browse files Browse the repository at this point in the history
refactor:
- Cleaned public methods
  • Loading branch information
FulcrumOne authored May 1, 2024
1 parent 7aaa98a commit eced4ce
Show file tree
Hide file tree
Showing 63 changed files with 281 additions and 221 deletions.
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let package = Package(
.package(url: "https://github.com/Mijick/Timer", branch: "main")
],
targets: [
.target(name: "MijickCameraView", dependencies: [.product(name: "MijickTimer", package: "Timer")], path: "Sources"),
.testTarget(name: "MijickCameraViewTests", dependencies: ["MijickCameraView"], path: "Tests"),
.target(name: "MijickCameraView", dependencies: [.product(name: "MijickTimer", package: "Timer")], path: "Sources")
]
)
15 changes: 0 additions & 15 deletions Sources/CameraOutputType.swift

This file was deleted.

16 changes: 0 additions & 16 deletions Sources/CameraPreview.swift

This file was deleted.

27 changes: 0 additions & 27 deletions Sources/Extensions/AVCaptureDevice++.swift

This file was deleted.

31 changes: 0 additions & 31 deletions Sources/Extensions/Color++.swift

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
import SwiftUI

struct CameraConfig {
var cameraErrorView: (CameraManager.Error, @escaping () -> ()) -> any CameraErrorView = DefaultCameraErrorView.init
var cameraView: (CameraManager, Namespace.ID, @escaping () -> ()) -> any CameraView = DefaultCameraView.init
var mediaPreviewView: ((MCameraMedia, Namespace.ID, @escaping () -> (), @escaping () -> ()) -> any CameraPreview)? = DefaultCameraPreview.init
// MARK: Default Views
var cameraErrorView: ErrorViewBuilder = DefaultCameraErrorView.init
var cameraView: CameraViewBuilder = DefaultCameraView.init
var mediaPreviewView: PreviewViewBuilder? = DefaultCameraPreview.init

var appDelegate: MApplicationDelegate.Type?
// MARK: To Lock Orientation
var appDelegate: MApplicationDelegate.Type? = nil

// MARK: Actions
var onImageCaptured: (Data) -> () = { _ in }
var onVideoCaptured: (URL) -> () = { _ in }

var afterMediaCaptured: () -> () = {}
var onCloseController: () -> () = {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ import SwiftUI

extension Animation {
static var defaultSpring: Animation { .spring(response: 0.4, dampingFraction: 1, blendDuration: 0.1) }
static var defaultEase: Animation { .easeInOut(duration: 0.4) }
static var defaultEase: Animation { .easeInOut(duration: 0.32) }
}
File renamed without changes.
37 changes: 37 additions & 0 deletions Sources/Internal/Extensions/CameraUtilities++.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// CameraUtilities++.swift of MijickCameraView
//
// Created by Tomasz Kurylik
// - Twitter: https://twitter.com/tkurylik
// - Mail: [email protected]
// - GitHub: https://github.com/FulcrumOne
//
// Copyright ©2024 Mijick. Licensed under MIT License.


import AVKit

// MARK: Camera Position
extension CameraPosition {
func get() -> AVCaptureDevice.Position { switch self {
case .back: .back
case .front: .front
}}
}

// MARK: Camera Flash Mode
extension CameraFlashMode {
func get() -> AVCaptureDevice.FlashMode { switch self {
case .off: .off
case .on: .on
case .auto: .auto
}}
}

// MARK: Camera Torch Mode
extension CameraTorchMode {
func get() -> AVCaptureDevice.TorchMode { switch self {
case .off: .off
case .on: .on
}}
}
File renamed without changes.
34 changes: 34 additions & 0 deletions Sources/Internal/Extensions/Color++.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Color++.swift of MijickCameraView
//
// Created by Tomasz Kurylik
// - Twitter: https://twitter.com/tkurylik
// - Mail: [email protected]
// - GitHub: https://github.com/FulcrumOne
//
// Copyright ©2024 Mijick. Licensed under MIT License.


import SwiftUI

extension Color {
static var accent: Color = .init(hex: 0x087F8C)
static var yellow: Color = .init(hex: 0xF0C808)
static var red: Color = .init(hex: 0xD52941)
static var background: Color = .init(hex: 0x040408)
}
extension UIColor {
static var accent: UIColor = .init(hex: 0x087F8C)
static var yellow: UIColor = .init(hex: 0xF0C808)
static var red: UIColor = .init(hex: 0xD52941)
static var background: UIColor = .init(hex: 0x040408)
}


// MARK: - Helpers
fileprivate extension Color {
init(hex: UInt) { self.init(.sRGB, red: Double((hex >> 16) & 0xff) / 255, green: Double((hex >> 08) & 0xff) / 255, blue: Double((hex >> 00) & 0xff) / 255) }
}
fileprivate extension UIColor {
convenience init(hex: UInt) { self.init(red: Double((hex >> 16) & 0xff) / 255, green: Double((hex >> 08) & 0xff) / 255, blue: Double((hex >> 00) & 0xff) / 255, alpha: 1) }
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import SwiftUI

// MARK: - Blurring View
extension UIView {
func applyBlurEffect(style: UIBlurEffect.Style, animationDuration: Double) {
let blurEffectView = UIVisualEffectView()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class CameraManager: NSObject, ObservableObject {
// MARK: Attributes
@Published private(set) var capturedMedia: MCameraMedia? = nil
@Published private(set) var outputType: CameraOutputType = .photo
@Published private(set) var cameraPosition: AVCaptureDevice.Position = .back
@Published private(set) var cameraPosition: CameraPosition = .back
@Published private(set) var zoomFactor: CGFloat = 1.0
@Published private(set) var flashMode: AVCaptureDevice.FlashMode = .off
@Published private(set) var torchMode: AVCaptureDevice.TorchMode = .off
@Published private(set) var flashMode: CameraFlashMode = .off
@Published private(set) var torchMode: CameraTorchMode = .off
@Published private(set) var mirrorOutput: Bool = false
@Published private(set) var isGridVisible: Bool = true
@Published private(set) var isRecording: Bool = false
Expand Down Expand Up @@ -58,7 +58,7 @@ public class CameraManager: NSObject, ObservableObject {

// MARK: - Changing Attributes
extension CameraManager {
func change(outputType: CameraOutputType? = nil, cameraPosition: AVCaptureDevice.Position? = nil, flashMode: AVCaptureDevice.FlashMode? = nil, isGridVisible: Bool? = nil, focusImage: UIImage? = nil, focusImageColor: UIColor? = nil, focusImageSize: CGFloat? = nil) {
func change(outputType: CameraOutputType? = nil, cameraPosition: CameraPosition? = nil, flashMode: CameraFlashMode? = nil, isGridVisible: Bool? = nil, focusImage: UIImage? = nil, focusImageColor: UIColor? = nil, focusImageSize: CGFloat? = nil) {
if let outputType { self.outputType = outputType }
if let cameraPosition { self.cameraPosition = cameraPosition }
if let flashMode { self.flashMode = flashMode }
Expand Down Expand Up @@ -162,9 +162,9 @@ private extension CameraManager {
}}
}
private extension CameraManager {
func setupCameraInput(_ cameraPosition: AVCaptureDevice.Position) throws { switch cameraPosition {
func setupCameraInput(_ cameraPosition: CameraPosition) throws { switch cameraPosition {
case .front: try setupInput(frontCameraInput)
default: try setupInput(backCameraInput)
case .back: try setupInput(backCameraInput)
}}
func setupCameraOutput(_ outputType: CameraOutputType) throws { if let output = getOutput(outputType) {
try setupOutput(output)
Expand Down Expand Up @@ -260,7 +260,7 @@ private extension CameraManager {

// MARK: - Changing Camera Position
extension CameraManager {
func changeCamera(_ newPosition: AVCaptureDevice.Position) throws { if newPosition != cameraPosition && !isChanging {
func changeCamera(_ newPosition: CameraPosition) throws { if newPosition != cameraPosition && !isChanging {
captureCurrentFrameAndDelay(.cameraPositionChange) { [self] in
removeCameraInput(cameraPosition)
try setupCameraInput(newPosition)
Expand All @@ -272,17 +272,17 @@ extension CameraManager {
}}
}
private extension CameraManager {
func removeCameraInput(_ position: AVCaptureDevice.Position) { if let input = getInput(position) {
func removeCameraInput(_ position: CameraPosition) { if let input = getInput(position) {
captureSession.removeInput(input)
}}
func updateCameraPosition(_ position: AVCaptureDevice.Position) {
func updateCameraPosition(_ position: CameraPosition) {
cameraPosition = position
}
}
private extension CameraManager {
func getInput(_ position: AVCaptureDevice.Position) -> AVCaptureInput? { switch position {
func getInput(_ position: CameraPosition) -> AVCaptureInput? { switch position {
case .front: frontCameraInput
default: backCameraInput
case .back: backCameraInput
}}
}

Expand Down Expand Up @@ -351,9 +351,9 @@ extension CameraManager {
}}
}
private extension CameraManager {
func getDevice(_ position: AVCaptureDevice.Position) -> AVCaptureDevice? { switch position {
func getDevice(_ position: CameraPosition) -> AVCaptureDevice? { switch position {
case .front: frontCamera
default: backCamera
case .back: backCamera
}}
func calculateZoomFactor(_ value: CGFloat, _ device: AVCaptureDevice) -> CGFloat {
min(max(value, getMinZoomLevel(device)), getMaxZoomLevel(device))
Expand All @@ -378,30 +378,30 @@ private extension CameraManager {

// MARK: - Changing Flash Mode
extension CameraManager {
func changeFlashMode(_ mode: AVCaptureDevice.FlashMode) throws { if let device = getDevice(cameraPosition), device.hasFlash, !isChanging {
func changeFlashMode(_ mode: CameraFlashMode) throws { if let device = getDevice(cameraPosition), device.hasFlash, !isChanging {
updateFlashMode(mode)
}}
}
private extension CameraManager {
func updateFlashMode(_ value: AVCaptureDevice.FlashMode) {
func updateFlashMode(_ value: CameraFlashMode) {
flashMode = value
}
}

// MARK: - Changing Torch Mode
extension CameraManager {
func changeTorchMode(_ mode: AVCaptureDevice.TorchMode) throws { if let device = getDevice(cameraPosition), device.hasTorch, !isChanging {
func changeTorchMode(_ mode: CameraTorchMode) throws { if let device = getDevice(cameraPosition), device.hasTorch, !isChanging {
try changeTorchMode(device, mode)
updateTorchMode(mode)
}}
}
private extension CameraManager {
func changeTorchMode(_ device: AVCaptureDevice, _ mode: AVCaptureDevice.TorchMode) throws {
func changeTorchMode(_ device: AVCaptureDevice, _ mode: CameraTorchMode) throws {
try device.lockForConfiguration()
device.torchMode = mode
device.torchMode = mode.get()
device.unlockForConfiguration()
}
func updateTorchMode(_ value: AVCaptureDevice.TorchMode) {
func updateTorchMode(_ value: CameraTorchMode) {
torchMode = value
}
}
Expand Down Expand Up @@ -450,7 +450,7 @@ private extension CameraManager {
private extension CameraManager {
func getPhotoOutputSettings() -> AVCapturePhotoSettings {
let settings = AVCapturePhotoSettings()
settings.flashMode = flashMode
settings.flashMode = flashMode.get()
return settings
}
func performCaptureAnimation() {
Expand Down Expand Up @@ -486,7 +486,7 @@ extension CameraManager: AVCapturePhotoCaptureDelegate {
private extension CameraManager {
func createPhotoMedia(_ photo: AVCapturePhoto) -> MCameraMedia? {
guard let imageData = photo.fileDataRepresentation() else { return nil }
return .init(data: imageData, url: nil)
return .init(data: imageData)
}
}

Expand Down Expand Up @@ -535,7 +535,7 @@ private extension CameraManager {

extension CameraManager: AVCaptureFileOutputRecordingDelegate {
public func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: (any Swift.Error)?) {
capturedMedia = MCameraMedia(data: nil, url: outputFileURL)
capturedMedia = MCameraMedia(data: outputFileURL)
}
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import SwiftUI

struct DefaultCameraErrorView: CameraErrorView {
struct DefaultCameraErrorView: MCameraErrorView {
let error: CameraManager.Error
let closeControllerAction: () -> ()

Expand Down Expand Up @@ -44,15 +44,15 @@ private extension DefaultCameraErrorView {
.padding(.leading, 20)
}
func createTitle() -> some View {
Text(title)
Text(getDefaultTitle())
.font(.system(size: 20, weight: .bold))
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
.fixedSize(horizontal: false, vertical: true)
.padding(.horizontal, 60)
}
func createDescription() -> some View {
Text(description)
Text(getDefaultDescription())
.font(.system(size: 15, weight: .regular))
.foregroundColor(Color.white.opacity(0.7))
.multilineTextAlignment(.center)
Expand Down
Loading

0 comments on commit eced4ce

Please sign in to comment.