Skip to content

Commit 0b665d8

Browse files
committed
Refactor access control
1 parent 19f6c2c commit 0b665d8

File tree

7 files changed

+96
-98
lines changed

7 files changed

+96
-98
lines changed

BarcodeScanner.xcodeproj/project.pbxproj

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

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

2223
/* Begin PBXFileReference section */
23-
2DBF9E0D1F169DEF006B5AA8 /* FocusViewType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FocusViewType.swift; sourceTree = "<group>"; };
24+
2DBF9E0D1F169DEF006B5AA8 /* FocusViewType.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = FocusViewType.swift; sourceTree = "<group>"; tabWidth = 2; };
25+
D504555E1FD8714700E46826 /* UIView+Extensions.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = "UIView+Extensions.swift"; sourceTree = "<group>"; tabWidth = 2; };
2426
D50BE3E51C9FE7A80000A34C /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
2527
D50BE3E61C9FE7A80000A34C /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
2628
D50BE3E71C9FE7A80000A34C /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
2729
D5B2E89F1C3A780C00C0327D /* BarcodeScanner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = BarcodeScanner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
28-
D5C4E08C1CA0BFB9008D9269 /* InfoView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InfoView.swift; sourceTree = "<group>"; };
29-
D5C4E08D1CA0BFB9008D9269 /* TorchMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TorchMode.swift; sourceTree = "<group>"; };
30+
D5C4E08C1CA0BFB9008D9269 /* InfoView.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = InfoView.swift; sourceTree = "<group>"; tabWidth = 2; };
31+
D5C4E08D1CA0BFB9008D9269 /* TorchMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = TorchMode.swift; sourceTree = "<group>"; tabWidth = 2; };
3032
D5C6298B1C3A8BBD007F7B7C /* Info-iOS.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-iOS.plist"; sourceTree = "<group>"; };
31-
D5F1C1C51C9C5113001E17A6 /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
32-
D5F1C1C61C9C5113001E17A6 /* BarcodeScannerController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BarcodeScannerController.swift; sourceTree = "<group>"; };
33-
D5F1C1D21C9C5809001E17A6 /* HeaderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HeaderView.swift; sourceTree = "<group>"; };
34-
D5FC8AD61D252A12004BED88 /* State.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = State.swift; sourceTree = "<group>"; };
33+
D5F1C1C51C9C5113001E17A6 /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; tabWidth = 2; };
34+
D5F1C1C61C9C5113001E17A6 /* BarcodeScannerController.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = BarcodeScannerController.swift; sourceTree = "<group>"; tabWidth = 2; };
35+
D5F1C1D21C9C5809001E17A6 /* HeaderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = HeaderView.swift; sourceTree = "<group>"; tabWidth = 2; };
36+
D5FC8AD61D252A12004BED88 /* State.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = State.swift; sourceTree = "<group>"; tabWidth = 2; };
3537
/* End PBXFileReference section */
3638

3739
/* Begin PBXFrameworksBuildPhase section */
@@ -91,6 +93,7 @@
9193
D5F1C1C51C9C5113001E17A6 /* Config.swift */,
9294
D5F1C1C61C9C5113001E17A6 /* BarcodeScannerController.swift */,
9395
D5F1C1D21C9C5809001E17A6 /* HeaderView.swift */,
96+
D504555E1FD8714700E46826 /* UIView+Extensions.swift */,
9497
);
9598
path = Sources;
9699
sourceTree = "<group>";
@@ -182,6 +185,7 @@
182185
D5F1C1C91C9C5113001E17A6 /* Config.swift in Sources */,
183186
D5F1C1CA1C9C5113001E17A6 /* BarcodeScannerController.swift in Sources */,
184187
2DBF9E0E1F169DEF006B5AA8 /* FocusViewType.swift in Sources */,
188+
D504555F1FD8714700E46826 /* UIView+Extensions.swift in Sources */,
185189
D5FC8AD71D252A12004BED88 /* State.swift in Sources */,
186190
);
187191
runOnlyForDeploymentPostprocessing = 0;

Sources/BarcodeScannerController.swift

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,17 @@ public protocol BarcodeScannerDismissalDelegate: class {
2828
- Not found error message
2929
*/
3030
open class BarcodeScannerController: UIViewController {
31-
3231
/// Video capture device. This may be nil when running in Simulator.
33-
lazy var captureDevice: AVCaptureDevice! = AVCaptureDevice.default(for: AVMediaType.video)
32+
private lazy var captureDevice: AVCaptureDevice! = AVCaptureDevice.default(for: AVMediaType.video)
3433

3534
/// Capture session.
36-
lazy var captureSession: AVCaptureSession = AVCaptureSession()
35+
private lazy var captureSession: AVCaptureSession = AVCaptureSession()
3736

3837
/// Header view with title and close button.
39-
lazy var headerView: HeaderView = HeaderView()
38+
private lazy var headerView: HeaderView = HeaderView()
4039

4140
/// Information view with description label.
42-
lazy var infoView: InfoView = InfoView()
41+
private lazy var infoView: InfoView = InfoView()
4342

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

5150
/// Animated focus view.
52-
lazy var focusView: UIView = {
51+
private lazy var focusView: UIView = {
5352
let view = UIView()
5453
view.layer.borderColor = UIColor.white.cgColor
5554
view.layer.borderWidth = 2
@@ -64,7 +63,7 @@ open class BarcodeScannerController: UIViewController {
6463
}()
6564

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

8281
/// Video preview layer.
83-
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
82+
private var videoPreviewLayer: AVCaptureVideoPreviewLayer?
8483

8584
/// The current controller's status mode.
86-
var status: Status = Status(state: .scanning) {
85+
private var status: Status = Status(state: .scanning) {
8786
didSet {
8887
let duration = status.animated &&
8988
(status.state == .processing
@@ -128,7 +127,7 @@ open class BarcodeScannerController: UIViewController {
128127
public var barCodeFocusViewType: FocusViewType = .animated
129128

130129
/// The current torch mode on the capture device.
131-
var torchMode: TorchMode = .off {
130+
private var torchMode: TorchMode = .off {
132131
didSet {
133132
guard let captureDevice = captureDevice, captureDevice.hasFlash else { return }
134133

@@ -143,7 +142,7 @@ open class BarcodeScannerController: UIViewController {
143142
}
144143

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

165164
/// Flag to lock session from capturing.
166-
var locked = false
165+
private var locked = false
167166

168167
// MARK: - Initialization
169168

@@ -228,7 +227,7 @@ open class BarcodeScannerController: UIViewController {
228227
/**
229228
`UIApplicationWillEnterForegroundNotification` action.
230229
*/
231-
@objc func appWillEnterForeground() {
230+
@objc private func appWillEnterForeground() {
232231
torchMode = .off
233232
animateFocusView()
234233
}
@@ -238,7 +237,7 @@ open class BarcodeScannerController: UIViewController {
238237
/**
239238
Sets up camera and checks for camera permissions.
240239
*/
241-
func setupCamera() {
240+
private func setupCamera() {
242241
let authorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
243242

244243
if authorizationStatus == .authorized {
@@ -263,7 +262,7 @@ open class BarcodeScannerController: UIViewController {
263262
/**
264263
Sets up capture input, output and session.
265264
*/
266-
func setupSession() {
265+
private func setupSession() {
267266
guard let captureDevice = captureDevice else {
268267
return
269268
}
@@ -307,7 +306,7 @@ open class BarcodeScannerController: UIViewController {
307306
/**
308307
Resets the current state.
309308
*/
310-
func resetState() {
309+
private func resetState() {
311310
let alpha: CGFloat = status.state == .scanning ? 1 : 0
312311

313312
torchMode = .off
@@ -323,32 +322,25 @@ open class BarcodeScannerController: UIViewController {
323322
}
324323

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

330-
var rightSafeAreaInset: CGFloat = 0
331-
var topSafeAreaInset: CGFloat = 0
332-
if #available(iOS 11.0, *) {
333-
rightSafeAreaInset = view.safeAreaInsets.right
334-
topSafeAreaInset = view.safeAreaInsets.top
335-
}
336-
337-
var navbarSize: CGFloat = 0
338-
if (isLandscape) {
339-
navbarSize = 32
340-
}
341-
else {
342-
// On iPhone X devices, extend the size of the top nav bar
343-
navbarSize = topSafeAreaInset > 0 ? 88 : 64
344-
}
345-
346332
headerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: navbarSize)
347-
flashButton.frame = CGRect(x: view.frame.width - 50 - rightSafeAreaInset, y: navbarSize + 10 + (flashButtonSize / 2), width: flashButtonSize, height: flashButtonSize)
333+
flashButton.frame = CGRect(
334+
x: view.frame.width - 50 - insets.right,
335+
y: navbarSize + 10 + (flashButtonSize / 2),
336+
width: flashButtonSize,
337+
height: flashButtonSize
338+
)
348339
infoView.frame = infoFrame
349340

350341
if let videoPreviewLayer = videoPreviewLayer {
351342
videoPreviewLayer.frame = view.layer.bounds
343+
352344
if let connection = videoPreviewLayer.connection, connection.isVideoOrientationSupported {
353345
switch (UIApplication.shared.statusBarOrientation) {
354346
case .portrait: connection.videoOrientation = .portrait
@@ -365,6 +357,7 @@ open class BarcodeScannerController: UIViewController {
365357
} else {
366358
center(subview: focusView, inSize: CGSize(width: 218, height: 150))
367359
}
360+
368361
center(subview: settingsButton, inSize: CGSize(width: 150, height: 50))
369362
}
370363

@@ -374,12 +367,13 @@ open class BarcodeScannerController: UIViewController {
374367
- Parameter subview: The subview.
375368
- Parameter size: A new size.
376369
*/
377-
func center(subview: UIView, inSize size: CGSize) {
370+
private func center(subview: UIView, inSize size: CGSize) {
378371
subview.frame = CGRect(
379372
x: (view.frame.width - size.width) / 2,
380373
y: (view.frame.height - size.height) / 2,
381374
width: size.width,
382-
height: size.height)
375+
height: size.height
376+
)
383377
}
384378

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

390384
- Parameter processing: Flag to set the current state to `.Processing`.
391385
*/
392-
func animateFlash(whenProcessing: Bool = false) {
386+
private func animateFlash(whenProcessing: Bool = false) {
393387
let flashView = UIView(frame: view.bounds)
394388
flashView.backgroundColor = UIColor.white
395389
flashView.alpha = 1
@@ -413,7 +407,7 @@ open class BarcodeScannerController: UIViewController {
413407
/**
414408
Performs focus view animation.
415409
*/
416-
func animateFocusView() {
410+
private func animateFocusView() {
417411
focusView.layer.removeAllAnimations()
418412
focusView.isHidden = false
419413

@@ -434,7 +428,7 @@ open class BarcodeScannerController: UIViewController {
434428
/**
435429
Opens setting to allow camera usage.
436430
*/
437-
@objc func settingsButtonDidPress() {
431+
@objc private func settingsButtonDidPress() {
438432
DispatchQueue.main.async {
439433
if let settingsURL = URL(string: UIApplicationOpenSettingsURLString) {
440434
UIApplication.shared.openURL(settingsURL)
@@ -445,16 +439,17 @@ open class BarcodeScannerController: UIViewController {
445439
/**
446440
Sets the next torch mode.
447441
*/
448-
@objc func flashButtonDidPress() {
442+
@objc private func flashButtonDidPress() {
449443
torchMode = torchMode.next
450444
}
451445
}
452446

453447
// MARK: - AVCaptureMetadataOutputObjectsDelegate
454448

455449
extension BarcodeScannerController: AVCaptureMetadataOutputObjectsDelegate {
456-
457-
public func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
450+
public func metadataOutput(_ output: AVCaptureMetadataOutput,
451+
didOutput metadataObjects: [AVMetadataObject],
452+
from connection: AVCaptureConnection) {
458453
guard !locked else { return }
459454
guard !metadataObjects.isEmpty else { return }
460455

@@ -485,7 +480,6 @@ extension BarcodeScannerController: AVCaptureMetadataOutputObjectsDelegate {
485480
// MARK: - HeaderViewDelegate
486481

487482
extension BarcodeScannerController: HeaderViewDelegate {
488-
489483
func headerViewDidPressClose(_ headerView: HeaderView) {
490484
dismissalDelegate?.barcodeScannerDidDismiss(self)
491485
}

Sources/Config.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ func localizedString(_ key: String) -> String {
6969
return key
7070
}
7171

72-
7372
/**
7473
`AVCaptureMetadataOutput` metadata object types.
7574
*/
@@ -91,5 +90,5 @@ public var metadata = [
9190
]
9291

9392
extension AVMetadataObject.ObjectType {
94-
public static let upca: AVMetadataObject.ObjectType = AVMetadataObject.ObjectType(rawValue: "org.gs1.UPC-A")
93+
public static let upca: AVMetadataObject.ObjectType = .init(rawValue: "org.gs1.UPC-A")
9594
}

Sources/FocusViewType.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import UIKit
22

33
public enum FocusViewType {
4-
case animated
5-
case oneDimension
6-
case twoDimensions
4+
case animated
5+
case oneDimension
6+
case twoDimensions
77
}

Sources/HeaderView.swift

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ protocol HeaderViewDelegate: class {
77
/**
88
Header view that simulates a navigation bar.
99
*/
10-
class HeaderView: UIView {
11-
10+
final class HeaderView: UIView {
1211
/// Title label.
13-
lazy var label: UILabel = {
12+
private lazy var label: UILabel = {
1413
let label = UILabel()
1514
label.text = Title.text
1615
label.font = Title.font
@@ -22,13 +21,12 @@ class HeaderView: UIView {
2221
}()
2322

2423
/// Close button.
25-
lazy var button: UIButton = {
24+
private lazy var button: UIButton = {
2625
let button = UIButton(type: .system)
2726
button.setTitle(CloseButton.text, for: UIControlState())
2827
button.titleLabel?.font = CloseButton.font
2928
button.tintColor = CloseButton.color
3029
button.addTarget(self, action: #selector(buttonDidPress), for: .touchUpInside)
31-
3230
return button
3331
}()
3432

@@ -61,33 +59,30 @@ class HeaderView: UIView {
6159
override func layoutSubviews() {
6260
super.layoutSubviews()
6361

64-
var leftSafeAreaPadding: CGFloat = 0
65-
var topSafeAreaPadding: CGFloat = 0
66-
if #available(iOS 11, *) {
67-
leftSafeAreaPadding = safeAreaInsets.left
68-
topSafeAreaPadding = safeAreaInsets.top
69-
}
70-
71-
let leadingPadding: CGFloat = 15 + leftSafeAreaPadding
72-
let topPadding: CGFloat = 8 + topSafeAreaPadding
62+
let insets = viewInsets
63+
let leadingPadding: CGFloat = 15 + insets.left
64+
let topPadding: CGFloat = 8 + insets.top
7365
let labelHeight: CGFloat = 40
7466

7567
button.sizeToFit()
7668

77-
button.frame.origin = CGPoint(x: leadingPadding,
78-
y: ((frame.height - button.frame.height) / 2) + topPadding)
69+
button.frame.origin = CGPoint(
70+
x: leadingPadding,
71+
y: ((frame.height - button.frame.height) / 2) + topPadding
72+
)
7973

8074
label.frame = CGRect(
8175
x: 0, y: ((frame.height - labelHeight) / 2) + topPadding,
82-
width: frame.width, height: labelHeight)
76+
width: frame.width, height: labelHeight
77+
)
8378
}
8479

8580
// MARK: - Actions
8681

8782
/**
8883
Close button action handler.
8984
*/
90-
@objc func buttonDidPress() {
85+
@objc private func buttonDidPress() {
9186
delegate?.headerViewDidPressClose(self)
9287
}
9388
}

0 commit comments

Comments
 (0)