@@ -29,16 +29,12 @@ import AVFoundation
2929
3030/// Convenient controller to display a view to scan/read 1D or 2D bar codes like the QRCodes. It is based on the `AVFoundation` framework from Apple. It aims to replace ZXing or ZBar for iOS 7 and over.
3131public class QRCodeReaderViewController : UIViewController {
32- /// The code reader object used to scan the bar code.
33- public let codeReader : QRCodeReader
32+ private let builder : QRCodeReaderViewControllerBuilder
3433
35- let readerView : QRCodeReaderContainer
36- let startScanningAtLoad : Bool
37- let showCancelButton : Bool
38- let showSwitchCameraButton : Bool
39- let showTorchButton : Bool
40- let showOverlayView : Bool
41- let customPreferredStatusBarStyle : UIStatusBarStyle ?
34+ /// The code reader object used to scan the bar code.
35+ public var codeReader : QRCodeReader {
36+ return builder. reader
37+ }
4238
4339 // MARK: - Managing the Callback Responders
4440
@@ -62,49 +58,33 @@ public class QRCodeReaderViewController: UIViewController {
6258 - parameter builder: A QRCodeViewController builder object.
6359 */
6460 required public init ( builder: QRCodeReaderViewControllerBuilder ) {
65- readerView = builder. readerView
66- startScanningAtLoad = builder. startScanningAtLoad
67- codeReader = builder. reader
68- showCancelButton = builder. showCancelButton
69- showSwitchCameraButton = builder. showSwitchCameraButton
70- showTorchButton = builder. showTorchButton
71- showOverlayView = builder. showOverlayView
72- customPreferredStatusBarStyle = builder. preferredStatusBarStyle
61+ self . builder = builder
7362
7463 super. init ( nibName: nil , bundle: nil )
7564
7665 view. backgroundColor = . black
7766
7867 codeReader. didFindCode = { [ weak self] resultAsObject in
7968 if let weakSelf = self {
80- if let qrv = weakSelf . readerView. displayable as? QRCodeReaderView {
69+ if let qrv = builder . readerView. displayable as? QRCodeReaderView {
8170 qrv. addGreenBorder ( )
8271 }
8372 weakSelf. completionBlock ? ( resultAsObject)
8473 weakSelf. delegate? . reader ( weakSelf, didScanResult: resultAsObject)
8574 }
8675 }
8776
88- codeReader. didFailDecoding = { [ weak self] in
89- if let weakSelf = self {
90- if let qrv = weakSelf. readerView. displayable as? QRCodeReaderView {
91- qrv. addRedBorder ( )
92- }
77+ codeReader. didFailDecoding = {
78+ if let qrv = builder. readerView. displayable as? QRCodeReaderView {
79+ qrv. addRedBorder ( )
9380 }
9481 }
9582
9683 setupUIComponentsWithCancelButtonTitle ( builder. cancelButtonTitle)
9784 }
9885
9986 required public init ? ( coder aDecoder: NSCoder ) {
100- codeReader = QRCodeReader ( )
101- readerView = QRCodeReaderContainer ( displayable: QRCodeReaderView ( ) )
102- startScanningAtLoad = false
103- showCancelButton = false
104- showTorchButton = false
105- showSwitchCameraButton = false
106- showOverlayView = false
107- customPreferredStatusBarStyle = nil
87+ self . builder = QRCodeReaderViewControllerBuilder ( )
10888
10989 super. init ( coder: aDecoder)
11090 }
@@ -114,8 +94,8 @@ public class QRCodeReaderViewController: UIViewController {
11494 override public func viewWillAppear( _ animated: Bool ) {
11595 super. viewWillAppear ( animated)
11696
117- if startScanningAtLoad {
118- readerView. displayable. setNeedsUpdateOrientation ( )
97+ if builder . startScanningAtLoad {
98+ builder . readerView. displayable. setNeedsUpdateOrientation ( )
11999
120100 startScanning ( )
121101 }
@@ -134,38 +114,35 @@ public class QRCodeReaderViewController: UIViewController {
134114 }
135115
136116 public override var preferredStatusBarStyle : UIStatusBarStyle {
137- return customPreferredStatusBarStyle ?? super. preferredStatusBarStyle
117+ return builder . preferredStatusBarStyle ?? super. preferredStatusBarStyle
138118 }
139119
140120 // MARK: - Initializing the AV Components
141121
142122 private func setupUIComponentsWithCancelButtonTitle( _ cancelButtonTitle: String ) {
143- view. addSubview ( readerView. view)
144-
145- let sscb = showSwitchCameraButton && codeReader. hasFrontDevice
146- let stb = showTorchButton && codeReader. isTorchAvailable
123+ view. addSubview ( builder. readerView. view)
147124
148- readerView. view. translatesAutoresizingMaskIntoConstraints = false
149- readerView. setupComponents ( showCancelButton : showCancelButton , showSwitchCameraButton : sscb , showTorchButton : stb , showOverlayView : showOverlayView , reader : codeReader )
125+ builder . readerView. view. translatesAutoresizingMaskIntoConstraints = false
126+ builder . readerView. setupComponents ( with : builder )
150127
151128 // Setup action methods
152129
153- readerView. displayable. switchCameraButton? . addTarget ( self , action: #selector( switchCameraAction) , for: . touchUpInside)
154- readerView. displayable. toggleTorchButton? . addTarget ( self , action: #selector( toggleTorchAction) , for: . touchUpInside)
155- readerView. displayable. cancelButton? . setTitle ( cancelButtonTitle, for: . normal)
156- readerView. displayable. cancelButton? . addTarget ( self , action: #selector( cancelAction) , for: . touchUpInside)
130+ builder . readerView. displayable. switchCameraButton? . addTarget ( self , action: #selector( switchCameraAction) , for: . touchUpInside)
131+ builder . readerView. displayable. toggleTorchButton? . addTarget ( self , action: #selector( toggleTorchAction) , for: . touchUpInside)
132+ builder . readerView. displayable. cancelButton? . setTitle ( cancelButtonTitle, for: . normal)
133+ builder . readerView. displayable. cancelButton? . addTarget ( self , action: #selector( cancelAction) , for: . touchUpInside)
157134
158135 // Setup constraints
159136
160137 for attribute in [ . left, . top, . right] as [ NSLayoutConstraint . Attribute ] {
161- NSLayoutConstraint ( item: readerView. view, attribute: attribute, relatedBy: . equal, toItem: view, attribute: attribute, multiplier: 1 , constant: 0 ) . isActive = true
138+ NSLayoutConstraint ( item: builder . readerView. view, attribute: attribute, relatedBy: . equal, toItem: view, attribute: attribute, multiplier: 1 , constant: 0 ) . isActive = true
162139 }
163140
164141 if #available( iOS 11 . 0 , * ) {
165- view. safeAreaLayoutGuide. bottomAnchor. constraint ( equalTo: readerView. view. bottomAnchor) . isActive = true
142+ view. safeAreaLayoutGuide. bottomAnchor. constraint ( equalTo: builder . readerView. view. bottomAnchor) . isActive = true
166143 }
167144 else {
168- NSLayoutConstraint ( item: readerView. view, attribute: . bottom, relatedBy: . equal, toItem: view, attribute: . bottom, multiplier: 1 , constant: 0 ) . isActive = true
145+ NSLayoutConstraint ( item: builder . readerView. view, attribute: . bottom, relatedBy: . equal, toItem: view, attribute: . bottom, multiplier: 1 , constant: 0 ) . isActive = true
169146 }
170147 }
171148
0 commit comments