[ADD] QRCodeViewControllerBuilder object with the corresponding init.

This commit is contained in:
Yannick Loriot 2016-01-11 22:06:36 +01:00
parent ca8d35b742
commit 27e47b5de7
6 changed files with 121 additions and 18 deletions

View File

@ -5,6 +5,7 @@ Released on 2016-01-11.
- [ADD] Return a `QRCodeReaderResult` instead of a string.
- [ADD] Convenience init with QRCode metadata type.
- [ADD] `QRCodeViewControllerBuilder` object with the corresponding init.
## [Version 5.2.1](https://github.com/yannickl/QRCodeReader.swift/releases/tag/5.2.1)
Released on 2015-11-07.

View File

@ -11,6 +11,9 @@
4A0723DD1C22405100F2C410 /* QRCodeReaderResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A0723DB1C223D8100F2C410 /* QRCodeReaderResult.swift */; };
82BD4A0A1BBC21F800172E4E /* ToggleTorchButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82BD4A091BBC21F800172E4E /* ToggleTorchButton.swift */; };
82D83BA71BBC2A3100C94C22 /* ToggleTorchButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82BD4A091BBC21F800172E4E /* ToggleTorchButton.swift */; };
CE345B231C444D6400FCC482 /* QRCodeViewControllerBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE345B221C444D6400FCC482 /* QRCodeViewControllerBuilder.swift */; };
CE345B241C444D6400FCC482 /* QRCodeViewControllerBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE345B221C444D6400FCC482 /* QRCodeViewControllerBuilder.swift */; };
CE345B251C444D6400FCC482 /* QRCodeViewControllerBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE345B221C444D6400FCC482 /* QRCodeViewControllerBuilder.swift */; };
CE412E8F19D9A1E4000F294E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE412E8E19D9A1E4000F294E /* AppDelegate.swift */; };
CE412E9119D9A1E4000F294E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE412E9019D9A1E4000F294E /* ViewController.swift */; };
CE412E9419D9A1E4000F294E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CE412E9219D9A1E4000F294E /* Main.storyboard */; };
@ -64,6 +67,7 @@
/* Begin PBXFileReference section */
4A0723DB1C223D8100F2C410 /* QRCodeReaderResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRCodeReaderResult.swift; sourceTree = "<group>"; };
82BD4A091BBC21F800172E4E /* ToggleTorchButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToggleTorchButton.swift; sourceTree = "<group>"; };
CE345B221C444D6400FCC482 /* QRCodeViewControllerBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRCodeViewControllerBuilder.swift; sourceTree = "<group>"; };
CE412E8919D9A1E4000F294E /* QRCodeReader.swift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = QRCodeReader.swift.app; sourceTree = BUILT_PRODUCTS_DIR; };
CE412E8D19D9A1E4000F294E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
CE412E8E19D9A1E4000F294E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@ -198,6 +202,7 @@
CECCE4CC1A1527DE00071669 /* ReaderOverlayView.swift */,
CECD170A1AA3531C00A9ACDE /* QRCodeViewController.swift */,
82BD4A091BBC21F800172E4E /* ToggleTorchButton.swift */,
CE345B221C444D6400FCC482 /* QRCodeViewControllerBuilder.swift */,
);
name = QRCodeReader;
path = ../QRCodeReader;
@ -351,6 +356,7 @@
82BD4A0A1BBC21F800172E4E /* ToggleTorchButton.swift in Sources */,
4A0723DC1C223D8100F2C410 /* QRCodeReaderResult.swift in Sources */,
CECCE4CD1A1527DE00071669 /* ReaderOverlayView.swift in Sources */,
CE345B231C444D6400FCC482 /* QRCodeViewControllerBuilder.swift in Sources */,
CED23DDC1A15079300BE7A72 /* QRCodeReader.swift in Sources */,
CED23DDE1A1507CB00BE7A72 /* SwitchCameraButton.swift in Sources */,
CE412E8F19D9A1E4000F294E /* AppDelegate.swift in Sources */,
@ -361,6 +367,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CE345B241C444D6400FCC482 /* QRCodeViewControllerBuilder.swift in Sources */,
CE412EA519D9A1E4000F294E /* QRCodeReader_swiftTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -369,6 +376,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CE345B251C444D6400FCC482 /* QRCodeViewControllerBuilder.swift in Sources */,
4A0723DD1C22405100F2C410 /* QRCodeReaderResult.swift in Sources */,
82D83BA71BBC2A3100C94C22 /* ToggleTorchButton.swift in Sources */,
CE8FFEF41BAB4F5400D43F38 /* QRCodeReader.swift in Sources */,

View File

@ -28,7 +28,7 @@ import UIKit
import AVFoundation
class ViewController: UIViewController, QRCodeReaderViewControllerDelegate {
lazy var reader: QRCodeReaderViewController = QRCodeReaderViewController(cancelButtonTitle: "Cancel", coderReader: QRCodeReader(metadataObjectTypes: [AVMetadataObjectTypeQRCode]), showTorchButton: true)
lazy var reader: QRCodeReaderViewController = QRCodeReaderViewController(cancelButtonTitle: "Cancel", codeReader: QRCodeReader(metadataObjectTypes: [AVMetadataObjectTypeQRCode]), showTorchButton: true)
@IBAction func scanAction(sender: AnyObject) {
if QRCodeReader.supportsMetadataObjectTypes() {

View File

@ -66,7 +66,7 @@ public final class QRCodeReader: NSObject, AVCaptureMetadataOutputObjectsDelegat
// MARK: - Managing the Completion Block
/// Block is executing when a QRCode or when the user did stopped the scan.
public var completionBlock: ((QRCodeReaderResult?) -> ())?
public var completionBlock: (QRCodeReaderResult -> Void)?
// MARK: - Creating the Code Reader

View File

@ -45,7 +45,7 @@ public class QRCodeReaderViewController: UIViewController {
public weak var delegate: QRCodeReaderViewControllerDelegate?
/// The completion blocak that will be called when a result is found.
public var completionBlock: ((QRCodeReaderResult?) -> ())?
public var completionBlock: (QRCodeReaderResult? -> Void)?
deinit {
codeReader.stopScanning()
@ -91,39 +91,51 @@ public class QRCodeReaderViewController: UIViewController {
convenience public init(cancelButtonTitle: String, metadataObjectTypes: [String], startScanningAtLoad: Bool = true) {
let reader = QRCodeReader(metadataObjectTypes: metadataObjectTypes)
self.init(cancelButtonTitle: cancelButtonTitle, coderReader: reader, startScanningAtLoad: startScanningAtLoad)
self.init(cancelButtonTitle: cancelButtonTitle, codeReader: reader, startScanningAtLoad: startScanningAtLoad)
}
/**
Initializes a view controller using a cancel button title and a code reader.
- parameter cancelButtonTitle: The title to use for the cancel button.
- parameter coderReader: The code reader object used to scan the bar code.
- parameter codeReader: The code reader object used to scan the bar code.
- parameter startScanningAtLoad: Flag to know whether the view controller start scanning the codes when the view will appear.
- parameter showSwitchCameraButton: Flag to display the switch camera button.
- parameter showTorchButton: Flag to display the toggle torch button. If the value is true and there is no torch the button will not be displayed.
*/
required public init(cancelButtonTitle: String, coderReader reader: QRCodeReader, startScanningAtLoad startScan: Bool = true, showSwitchCameraButton showSwitch: Bool = true, showTorchButton showTorch: Bool = false) {
startScanningAtLoad = startScan
codeReader = reader
showSwitchCameraButton = showSwitch
showTorchButton = showTorch
public convenience init(cancelButtonTitle: String, codeReader reader: QRCodeReader, startScanningAtLoad startScan: Bool = true, showSwitchCameraButton showSwitch: Bool = true, showTorchButton showTorch: Bool = false) {
self.init(builder: QRCodeViewControllerBuilder { builder in
builder.cancelButtonTitle = cancelButtonTitle
builder.reader = reader
builder.startScanningAtLoad = startScan
builder.showSwitchCameraButton = showSwitch
builder.showTorchButton = showTorch
})
}
/**
Initializes a view controller using a builder.
- parameter builder: A QRCodeViewController builder object.
*/
required public init(builder: QRCodeViewControllerBuilder) {
startScanningAtLoad = builder.startScanningAtLoad
codeReader = builder.reader
showSwitchCameraButton = builder.showSwitchCameraButton
showTorchButton = builder.showTorchButton
super.init(nibName: nil, bundle: nil)
view.backgroundColor = UIColor.blackColor()
view.backgroundColor = .blackColor()
codeReader.completionBlock = { [weak self] (resultAsObject) in
codeReader.completionBlock = { [weak self] resultAsObject in
if let weakSelf = self {
weakSelf.completionBlock?(resultAsObject)
if let _resultAsObject = resultAsObject {
weakSelf.delegate?.reader(weakSelf, didScanResult: _resultAsObject)
}
weakSelf.delegate?.reader(weakSelf, didScanResult: resultAsObject)
}
}
setupUIComponentsWithCancelButtonTitle(cancelButtonTitle)
setupUIComponentsWithCancelButtonTitle(builder.cancelButtonTitle)
setupAutoLayoutConstraints()
cameraView.layer.insertSublayer(codeReader.previewLayer, atIndex: 0)
@ -208,7 +220,7 @@ public class QRCodeReaderViewController: UIViewController {
cancelButton.translatesAutoresizingMaskIntoConstraints = false
cancelButton.setTitle(cancelButtonTitle, forState: .Normal)
cancelButton.setTitleColor(UIColor.grayColor(), forState: .Highlighted)
cancelButton.setTitleColor(.grayColor(), forState: .Highlighted)
cancelButton.addTarget(self, action: "cancelAction:", forControlEvents: .TouchUpInside)
view.addSubview(cancelButton)
}

View File

@ -0,0 +1,82 @@
/*
* QRCodeReader.swift
*
* Copyright 2014-present Yannick Loriot.
* http://yannickloriot.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
import Foundation
/**
The QRCodeViewControllerBuilder aims to create a simple configuration object for
the QRCode view controller.
*/
final public class QRCodeViewControllerBuilder {
// MARK: - Configuring the QRCodeViewController Objects
/**
The builder block.
The block gives a reference of builder you can configure.
*/
public typealias QRCodeViewControllerBuilderBlock = (builder: QRCodeViewControllerBuilder) -> Void
/**
The title to use for the cancel button.
*/
public var cancelButtonTitle: String = "Cancel"
/**
The code reader object used to scan the bar code.
*/
public var reader: QRCodeReader = QRCodeReader()
/**
Flag to know whether the view controller start scanning the codes when the view will appear.
*/
public var startScanningAtLoad: Bool = true
/**
Flag to display the switch camera button.
*/
public var showSwitchCameraButton: Bool = true
/**
Flag to display the toggle torch button. If the value is true and there is no torch the button will not be displayed.
*/
public var showTorchButton: Bool = false
// MARK: - Initializing a Flap View
/**
Initialize a QRCodeViewController builder with default values.
*/
public init() {}
/**
Initialize a QRCodeViewController builder with default values.
- parameter buildBlock: A QRCodeViewController builder block to configure itself.
*/
public init(@noescape buildBlock: QRCodeViewControllerBuilderBlock) {
buildBlock(builder: self)
}
}