This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol CodeReader { | |
func startReading(completion: @escaping (String) -> Void) | |
func stopReading() | |
var videoPreview: CALayer {get} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override init() { | |
super.init() | |
//Make sure the device can handle video | |
guard let videoDevice = AVCaptureDevice.default(for: .video), | |
let deviceInput = try? AVCaptureDeviceInput(device: videoDevice) else { | |
return | |
} | |
//session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension AVCodeReader: AVCaptureMetadataOutputObjectsDelegate { | |
func metadataOutput(_ captureOutput: AVCaptureMetadataOutput, | |
didOutput metadataObjects: [AVMetadataObject], | |
from connection: AVCaptureConnection) { | |
guard let readableCode = metadataObjects.first as? AVMetadataMachineReadableCodeObject, | |
let code = readableCode.stringValue else { | |
return | |
} | |
//Vibrate the phone |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension AVCodeReader: CodeReader { | |
func startReading(completion: @escaping (String) -> Void) { | |
self.didRead = completion | |
captureSession?.startRunning() | |
} | |
func stopReading() { | |
captureSession?.stopRunning() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReaderViewController: UIViewController { | |
@IBOutlet weak var videoPreview: UIView! | |
private var videoLayer: CALayer! | |
var codeReader: CodeReader! | |
override func viewDidLoad() { | |
videoLayer = codeReader.videoPreview | |
videoPreview.layer.addSublayer(videoLayer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func setUp() { | |
super.setUp() | |
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil) | |
readerVC = mainStoryboard.instantiateViewController(withIdentifier: "ReaderViewController") as! ReaderViewController | |
readerVC.dataSource = MockDataSource() | |
readerVC.codeReader = mockReader | |
//force the life cycle to be called | |
let window = UIApplication.shared.delegate!.window! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func isCodeInLimitBounds(codeReadable: AVMetadataMachineReadableCodeObject) -> Bool { | |
if let videoLayer = previewLayer as? AVCaptureVideoPreviewLayer, | |
let transformedObj = videoLayer.transformedMetadataObject(for: codeReadable), | |
let limitBounds = limitBounds { | |
return limitBounds.contains(transformedObj.bounds) | |
} | |
//ignore 'failure' above | |
return true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Result<T> { | |
case success(T) | |
case failure(Error) | |
} | |
protocol PlacePicker { | |
func pick(result: @escaping (Result<Place>) -> Void) | |
} | |
protocol Place { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testPrepareForSegueDoesSetDestination() { | |
let destination = storyBoard.instantiateViewController(withIdentifier: "InputVC") as! ViewController2 | |
let segue = UIStoryboardSegue(identifier: "addExpense", source: sut, destination: destination) | |
sut.prepare(for: segue, sender: nil) | |
XCTAssertNotNil(destination.completion) | |
XCTAssertEqual(destination.type, .expense) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/// 9 radom numbers followed by two verifying digits | |
var randomValidCpf: String { | |
var cpf = (1...11).map { _ in Int(arc4random_uniform(9)) } | |
cpf[9] = calcVerifyingDigit(originalValue: cpf, weights: (2...10).map{$0}.reversed()) | |
cpf[10] = calcVerifyingDigit(originalValue: cpf, weights: (2...11).map{$0}.reversed()) | |
OlderNewer