Skip to content

Instantly share code, notes, and snippets.

View TiagoBras's full-sized avatar

Tiago Bras TiagoBras

View GitHub Profile
@TiagoBras
TiagoBras / ActionSheetAnimationController.swift
Created April 18, 2019 11:37
Animated transition that makes the view controller slide in from the bottom and dims the background
import UIKit
class ActionSheetAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
enum Direction {
case enter, exit
}
let direction: Direction
let duration: TimeInterval
@TiagoBras
TiagoBras / Array+GroupedBy.swift
Created April 18, 2019 11:29
Group array by key with optional sorting
import Foundation
extension Array {
func groupedBy<T: Hashable>(key: (Element) -> T, sort: ((Element, Element) -> Bool)? = nil) -> [T: [Element]] {
var groups = [T: [Element]]()
mainLoop: for element in self {
let elementKey = key(element)
if groups[elementKey] == nil {
@TiagoBras
TiagoBras / UINavigationController+Popping.swift
Created April 18, 2019 11:23
Easily pop to a view controller
import UIKit
extension UINavigationController {
/// Pops to the nearest view controller of type *viewControllerType*.
///
/// - Parameter viewControllerType: UIViewController.Type
/// - Returns: true if it successfully popped to a view controller of the type passed.
@discardableResult func popToViewController(ofType viewControllerType: UIViewController.Type) -> Bool {
if let vc = viewControllers.last(where: { type(of: $0) == viewControllerType }) {
@TiagoBras
TiagoBras / DispatchQueue+Extensions.swift
Created April 18, 2019 11:07
DispatchQueue extension for situations where one needs to run an expensive operation in background and then use its result in the main thread
import Foundation
extension DispatchQueue {
func asyncConsumeInMainQueue<T>(
work: @escaping () throws -> T,
mainSuccess: @escaping (T) -> Void,
mainError: @escaping (Error) -> Void) {
async {
do {
let result = try work()
@TiagoBras
TiagoBras / Date+Formatting.swift
Created April 18, 2019 11:01
How I handle different Date Formats
import Foundation
enum DateFormat: String {
/// Format: yyyy-MM-dd
case yyyyMMdd = "yyyy-MM-dd"
/// Format: yyyy-MM-DD HH:mm
case yyyyMMddHHmm = "yyyy-MM-DD HH:mm"
/// Format: dd/MM/yyyy HH:mm:ss
@TiagoBras
TiagoBras / NavigationCoordinator.swift
Last active April 19, 2019 15:24
Simple Navigation Coordinator
import UIKit
protocol NavigationCoordinator: AnyObject {
/// - Returns: true if it successfully completed the navigation
@discardableResult func navigate(from source: UIViewController,
to destination: UIViewController.Type,
data: NavigationData?) -> Bool
}
@TiagoBras
TiagoBras / observable.swift
Last active October 3, 2017 12:59
Observable in SWIFT
import Foundation
class Observable<T: Equatable> {
typealias HandlerID = UUID
struct Observer {
typealias Handler = (T) -> ()
let id: HandlerID
let handler: Handler
@TiagoBras
TiagoBras / UIImage+Masking.swift
Created September 10, 2017 18:34
iOS - Masking a UIImage with color
// Swift 4
extension UIImage {
func maskWith(color: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
color.setFill()
draw(in: rect)