Skip to content

Instantly share code, notes, and snippets.

@danielCarlosCE
danielCarlosCE / ContentSizePagingCollectionDelegate.swift
Last active June 28, 2022 15:40
Horizontal paging for collections based on the content size
//
// ContentSizePagingCollectionDelegate.swift
// CardsCarousel
//
// Created by Daniel Carlos Souza Carvalho on 2/20/21.
//
import UIKit
/// Horizontal paging for collections based on the content size
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
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
@danielCarlosCE
danielCarlosCE / ChainResponsibility.playground.swift
Created October 13, 2017 17:42
Chain of Responsibility to handle errors on iOS application
import UIKit
class AppDelegate {
var window: UIWindow?
}
//Chain of Resposability: Handler
protocol ErrorHandler {
var errorHandlerSuccessor: ErrorHandler? {get}
func handleError(error: Error)
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())
@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
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()
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)
}
}
}
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)
}
@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 {