Skip to content

Commit

Permalink
Merge pull request #78 from hyperoslo/refactor/access-control
Browse files Browse the repository at this point in the history
Refactor: access control
  • Loading branch information
vadymmarkov authored Dec 7, 2017
2 parents 19f6c2c + 3819e67 commit 7564207
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 101 deletions.
2 changes: 1 addition & 1 deletion BarcodeScanner.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "BarcodeScanner"
s.summary = "Simple and beautiful barcode scanner."
s.version = "3.0.0"
s.version = "3.0.1"
s.homepage = "https://github.com/hyperoslo/BarcodeScanner"
s.license = 'MIT'
s.author = { "Hyper Interaktiv AS" => "[email protected]" }
Expand Down
18 changes: 11 additions & 7 deletions BarcodeScanner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
2DBF9E0E1F169DEF006B5AA8 /* FocusViewType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DBF9E0D1F169DEF006B5AA8 /* FocusViewType.swift */; };
D504555F1FD8714700E46826 /* UIView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D504555E1FD8714700E46826 /* UIView+Extensions.swift */; };
D50BE3E91C9FE7A80000A34C /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D50BE3E51C9FE7A80000A34C /* [email protected] */; };
D50BE3EA1C9FE7A80000A34C /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D50BE3E61C9FE7A80000A34C /* [email protected] */; };
D50BE3EB1C9FE7A80000A34C /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D50BE3E71C9FE7A80000A34C /* [email protected] */; };
Expand All @@ -20,18 +21,19 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
2DBF9E0D1F169DEF006B5AA8 /* FocusViewType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FocusViewType.swift; sourceTree = "<group>"; };
2DBF9E0D1F169DEF006B5AA8 /* FocusViewType.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = FocusViewType.swift; sourceTree = "<group>"; tabWidth = 2; };
D504555E1FD8714700E46826 /* UIView+Extensions.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = "UIView+Extensions.swift"; sourceTree = "<group>"; tabWidth = 2; };
D50BE3E51C9FE7A80000A34C /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
D50BE3E61C9FE7A80000A34C /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
D50BE3E71C9FE7A80000A34C /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
D5B2E89F1C3A780C00C0327D /* BarcodeScanner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = BarcodeScanner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D5C4E08C1CA0BFB9008D9269 /* InfoView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InfoView.swift; sourceTree = "<group>"; };
D5C4E08D1CA0BFB9008D9269 /* TorchMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TorchMode.swift; sourceTree = "<group>"; };
D5C4E08C1CA0BFB9008D9269 /* InfoView.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = InfoView.swift; sourceTree = "<group>"; tabWidth = 2; };
D5C4E08D1CA0BFB9008D9269 /* TorchMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = TorchMode.swift; sourceTree = "<group>"; tabWidth = 2; };
D5C6298B1C3A8BBD007F7B7C /* Info-iOS.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-iOS.plist"; sourceTree = "<group>"; };
D5F1C1C51C9C5113001E17A6 /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
D5F1C1C61C9C5113001E17A6 /* BarcodeScannerController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BarcodeScannerController.swift; sourceTree = "<group>"; };
D5F1C1D21C9C5809001E17A6 /* HeaderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HeaderView.swift; sourceTree = "<group>"; };
D5FC8AD61D252A12004BED88 /* State.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = State.swift; sourceTree = "<group>"; };
D5F1C1C51C9C5113001E17A6 /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; tabWidth = 2; };
D5F1C1C61C9C5113001E17A6 /* BarcodeScannerController.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = BarcodeScannerController.swift; sourceTree = "<group>"; tabWidth = 2; };
D5F1C1D21C9C5809001E17A6 /* HeaderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = HeaderView.swift; sourceTree = "<group>"; tabWidth = 2; };
D5FC8AD61D252A12004BED88 /* State.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = State.swift; sourceTree = "<group>"; tabWidth = 2; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -91,6 +93,7 @@
D5F1C1C51C9C5113001E17A6 /* Config.swift */,
D5F1C1C61C9C5113001E17A6 /* BarcodeScannerController.swift */,
D5F1C1D21C9C5809001E17A6 /* HeaderView.swift */,
D504555E1FD8714700E46826 /* UIView+Extensions.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -182,6 +185,7 @@
D5F1C1C91C9C5113001E17A6 /* Config.swift in Sources */,
D5F1C1CA1C9C5113001E17A6 /* BarcodeScannerController.swift in Sources */,
2DBF9E0E1F169DEF006B5AA8 /* FocusViewType.swift in Sources */,
D504555F1FD8714700E46826 /* UIView+Extensions.swift in Sources */,
D5FC8AD71D252A12004BED88 /* State.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
4 changes: 2 additions & 2 deletions Example/BarcodeScannerExample/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- BarcodeScanner (2.1.2)
- BarcodeScanner (3.0.1)

DEPENDENCIES:
- BarcodeScanner (from `../../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: ../../

SPEC CHECKSUMS:
BarcodeScanner: e519f9497d196f228b5c1dd96af0468c666a2880
BarcodeScanner: f884e5a1e0e8e6c95eb7d0c4b00d69b72fd2be83

PODFILE CHECKSUM: eae1a5fa9feaba2427db94d37a48c50c283939b8

Expand Down
80 changes: 37 additions & 43 deletions Sources/BarcodeScannerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@ public protocol BarcodeScannerDismissalDelegate: class {
- Not found error message
*/
open class BarcodeScannerController: UIViewController {

/// Video capture device. This may be nil when running in Simulator.
lazy var captureDevice: AVCaptureDevice! = AVCaptureDevice.default(for: AVMediaType.video)
private lazy var captureDevice: AVCaptureDevice! = AVCaptureDevice.default(for: AVMediaType.video)

/// Capture session.
lazy var captureSession: AVCaptureSession = AVCaptureSession()
private lazy var captureSession: AVCaptureSession = AVCaptureSession()

/// Header view with title and close button.
lazy var headerView: HeaderView = HeaderView()
private lazy var headerView: HeaderView = HeaderView()

/// Information view with description label.
lazy var infoView: InfoView = InfoView()
private lazy var infoView: InfoView = InfoView()

/// Button to change torch mode.
public lazy var flashButton: UIButton = { [unowned self] in
Expand All @@ -49,7 +48,7 @@ open class BarcodeScannerController: UIViewController {
}()

/// Animated focus view.
lazy var focusView: UIView = {
private lazy var focusView: UIView = {
let view = UIView()
view.layer.borderColor = UIColor.white.cgColor
view.layer.borderWidth = 2
Expand All @@ -64,7 +63,7 @@ open class BarcodeScannerController: UIViewController {
}()

/// Button that opens settings to allow camera usage.
lazy var settingsButton: UIButton = { [unowned self] in
private lazy var settingsButton: UIButton = { [unowned self] in
let button = UIButton(type: .system)
let title = NSAttributedString(string: SettingsButton.text,
attributes: [
Expand All @@ -80,10 +79,10 @@ open class BarcodeScannerController: UIViewController {
}()

/// Video preview layer.
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
private var videoPreviewLayer: AVCaptureVideoPreviewLayer?

/// The current controller's status mode.
var status: Status = Status(state: .scanning) {
private var status: Status = Status(state: .scanning) {
didSet {
let duration = status.animated &&
(status.state == .processing
Expand Down Expand Up @@ -128,7 +127,7 @@ open class BarcodeScannerController: UIViewController {
public var barCodeFocusViewType: FocusViewType = .animated

/// The current torch mode on the capture device.
var torchMode: TorchMode = .off {
private var torchMode: TorchMode = .off {
didSet {
guard let captureDevice = captureDevice, captureDevice.hasFlash else { return }

Expand All @@ -143,7 +142,7 @@ open class BarcodeScannerController: UIViewController {
}

/// Calculated frame for the info view.
var infoFrame: CGRect {
private var infoFrame: CGRect {
let height = status.state != .processing ? 75 : view.bounds.height
return CGRect(x: 0, y: view.bounds.height - height,
width: view.bounds.width, height: height)
Expand All @@ -163,7 +162,7 @@ open class BarcodeScannerController: UIViewController {
public weak var dismissalDelegate: BarcodeScannerDismissalDelegate?

/// Flag to lock session from capturing.
var locked = false
private var locked = false

// MARK: - Initialization

Expand Down Expand Up @@ -228,7 +227,7 @@ open class BarcodeScannerController: UIViewController {
/**
`UIApplicationWillEnterForegroundNotification` action.
*/
@objc func appWillEnterForeground() {
@objc private func appWillEnterForeground() {
torchMode = .off
animateFocusView()
}
Expand All @@ -238,7 +237,7 @@ open class BarcodeScannerController: UIViewController {
/**
Sets up camera and checks for camera permissions.
*/
func setupCamera() {
private func setupCamera() {
let authorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)

if authorizationStatus == .authorized {
Expand All @@ -263,7 +262,7 @@ open class BarcodeScannerController: UIViewController {
/**
Sets up capture input, output and session.
*/
func setupSession() {
private func setupSession() {
guard let captureDevice = captureDevice else {
return
}
Expand Down Expand Up @@ -307,7 +306,7 @@ open class BarcodeScannerController: UIViewController {
/**
Resets the current state.
*/
func resetState() {
private func resetState() {
let alpha: CGFloat = status.state == .scanning ? 1 : 0

torchMode = .off
Expand All @@ -323,32 +322,25 @@ open class BarcodeScannerController: UIViewController {
}

// MARK: - Layout
func setupFrame() {
private func setupFrame() {
let flashButtonSize: CGFloat = 37
let isLandscape = view.frame.width > view.frame.height
let insets = view.viewInsets
// On iPhone X devices, extend the size of the top nav bar
let navbarSize: CGFloat = isLandscape ? 32 : insets.top > 0 ? 88 : 64

var rightSafeAreaInset: CGFloat = 0
var topSafeAreaInset: CGFloat = 0
if #available(iOS 11.0, *) {
rightSafeAreaInset = view.safeAreaInsets.right
topSafeAreaInset = view.safeAreaInsets.top
}

var navbarSize: CGFloat = 0
if (isLandscape) {
navbarSize = 32
}
else {
// On iPhone X devices, extend the size of the top nav bar
navbarSize = topSafeAreaInset > 0 ? 88 : 64
}

headerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: navbarSize)
flashButton.frame = CGRect(x: view.frame.width - 50 - rightSafeAreaInset, y: navbarSize + 10 + (flashButtonSize / 2), width: flashButtonSize, height: flashButtonSize)
flashButton.frame = CGRect(
x: view.frame.width - 50 - insets.right,
y: navbarSize + 10 + (flashButtonSize / 2),
width: flashButtonSize,
height: flashButtonSize
)
infoView.frame = infoFrame

if let videoPreviewLayer = videoPreviewLayer {
videoPreviewLayer.frame = view.layer.bounds

if let connection = videoPreviewLayer.connection, connection.isVideoOrientationSupported {
switch (UIApplication.shared.statusBarOrientation) {
case .portrait: connection.videoOrientation = .portrait
Expand All @@ -365,6 +357,7 @@ open class BarcodeScannerController: UIViewController {
} else {
center(subview: focusView, inSize: CGSize(width: 218, height: 150))
}

center(subview: settingsButton, inSize: CGSize(width: 150, height: 50))
}

Expand All @@ -374,12 +367,13 @@ open class BarcodeScannerController: UIViewController {
- Parameter subview: The subview.
- Parameter size: A new size.
*/
func center(subview: UIView, inSize size: CGSize) {
private func center(subview: UIView, inSize size: CGSize) {
subview.frame = CGRect(
x: (view.frame.width - size.width) / 2,
y: (view.frame.height - size.height) / 2,
width: size.width,
height: size.height)
height: size.height
)
}

// MARK: - Animations
Expand All @@ -389,7 +383,7 @@ open class BarcodeScannerController: UIViewController {

- Parameter processing: Flag to set the current state to `.Processing`.
*/
func animateFlash(whenProcessing: Bool = false) {
private func animateFlash(whenProcessing: Bool = false) {
let flashView = UIView(frame: view.bounds)
flashView.backgroundColor = UIColor.white
flashView.alpha = 1
Expand All @@ -413,7 +407,7 @@ open class BarcodeScannerController: UIViewController {
/**
Performs focus view animation.
*/
func animateFocusView() {
private func animateFocusView() {
focusView.layer.removeAllAnimations()
focusView.isHidden = false

Expand All @@ -434,7 +428,7 @@ open class BarcodeScannerController: UIViewController {
/**
Opens setting to allow camera usage.
*/
@objc func settingsButtonDidPress() {
@objc private func settingsButtonDidPress() {
DispatchQueue.main.async {
if let settingsURL = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.openURL(settingsURL)
Expand All @@ -445,16 +439,17 @@ open class BarcodeScannerController: UIViewController {
/**
Sets the next torch mode.
*/
@objc func flashButtonDidPress() {
@objc private func flashButtonDidPress() {
torchMode = torchMode.next
}
}

// MARK: - AVCaptureMetadataOutputObjectsDelegate

extension BarcodeScannerController: AVCaptureMetadataOutputObjectsDelegate {

public func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
public func metadataOutput(_ output: AVCaptureMetadataOutput,
didOutput metadataObjects: [AVMetadataObject],
from connection: AVCaptureConnection) {
guard !locked else { return }
guard !metadataObjects.isEmpty else { return }

Expand Down Expand Up @@ -485,7 +480,6 @@ extension BarcodeScannerController: AVCaptureMetadataOutputObjectsDelegate {
// MARK: - HeaderViewDelegate

extension BarcodeScannerController: HeaderViewDelegate {

func headerViewDidPressClose(_ headerView: HeaderView) {
dismissalDelegate?.barcodeScannerDidDismiss(self)
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func localizedString(_ key: String) -> String {
return key
}


/**
`AVCaptureMetadataOutput` metadata object types.
*/
Expand All @@ -91,5 +90,5 @@ public var metadata = [
]

extension AVMetadataObject.ObjectType {
public static let upca: AVMetadataObject.ObjectType = AVMetadataObject.ObjectType(rawValue: "org.gs1.UPC-A")
public static let upca: AVMetadataObject.ObjectType = .init(rawValue: "org.gs1.UPC-A")
}
6 changes: 3 additions & 3 deletions Sources/FocusViewType.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit

public enum FocusViewType {
case animated
case oneDimension
case twoDimensions
case animated
case oneDimension
case twoDimensions
}
Loading

0 comments on commit 7564207

Please sign in to comment.