Skip to content

Instantly share code, notes, and snippets.

protocol CodeReader {
func startReading(completion: @escaping (String) -> Void)
func stopReading()
var videoPreview: CALayer {get}
}
extension AVCodeReader: CodeReader {
func startReading(completion: @escaping (String) -> Void) {
self.didRead = completion
captureSession?.startRunning()
}
func stopReading() {
captureSession?.stopRunning()
}
}
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!
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)
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
}
@danielCarlosCE
danielCarlosCE / GMSPlacePickerExtension.swift
Created March 20, 2017 17:17
A Swift extension to Google's GMSPlacePicker. Decoupling our code a little bit more ;)
enum Result<T> {
case success(T)
case failure(Error)
}
protocol PlacePicker {
func pick(result: @escaping (Result<Place>) -> Void)
}
protocol Place {
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)
}
private extension XCTestCase {
func XCTAssertThrows<T, E>(_ expression: @autoclosure () throws -> T, specificError: E) where E: Error, E: Equatable {
XCTAssertThrowsError(try expression()) { error in
XCTAssertEqual(error as? E, specificError)
}
}
}
import UIKit
import PlaygroundSupport
var str = "Hello, playground"
let view = UIView(frame: CGRect(x: 0, y: 0, width: 600, height: 600))
view.backgroundColor = .red
let bottomLabel = UILabel()
bottomLabel.text = "Hello world!"
bottomLabel.sizeToFit()
@danielCarlosCE
danielCarlosCE / Command.swift
Last active May 17, 2018 13:25
A simple and a not-so-simple use of Command Pattern.
import UIKit
//MARK: Common
enum Result<T> {
case success(T)
case failure(Error)
}
struct Task {
var title: String