Skip to content

Instantly share code, notes, and snippets.

View DanielCardonaRojas's full-sized avatar

Daniel Cardona Rojas DanielCardonaRojas

View GitHub Profile
@DanielCardonaRojas
DanielCardonaRojas / UIImage+Letters.swift
Created November 5, 2019 22:19
Creates an image from initials. Use this with UIImageView
extension UIImage {
static func fromInitials(_ string: String, size: CGSize, backGroundColor: UIColor, textAttributes: [NSAttributedString.Key: Any]? = nil) -> UIImage? {
let text = string.initials
let scale = Float(UIScreen.main.scale)
UIGraphicsBeginImageContextWithOptions(size, false, CGFloat(scale))
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(backGroundColor.cgColor)
context?.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
func memoize<T: Hashable, U>(work: @escaping (T)->U) -> (T)->U {
var memo = Dictionary<T, U>()
return { x in
if let q = memo[x] { return q }
let r = work(x)
memo[x] = r
return r
}
}
@DanielCardonaRojas
DanielCardonaRojas / Notication+Codable.swift
Created October 10, 2019 19:19
Send codable in notification payload.
extension Encodable {
var asDictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] }
}
}
extension Decodable {
static func fromDictionary(from json: Any) -> Self? {
guard let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []) else {
@DanielCardonaRojas
DanielCardonaRojas / UIView+Subviews.swift
Created June 10, 2019 19:47
Query the subview hierarchy
extension UIView {
func allSubviews<T: UIView>(of type: T.Type) -> [T] {
let directSubviews = subviews.compactMap({ $0 as? T })
var nested = [T]()
for s in subviews {
let ss = s.allSubviews(of: type)
nested += ss
}
@DanielCardonaRojas
DanielCardonaRojas / Cached.swift
Created May 30, 2019 14:56
Cached computed properties
struct Cached <V, C> {
typealias Computation = (V) -> C
private var computation: (V) -> C
private var cachedResult: C?
var value: V {
willSet {
cachedResult = nil
}
}
@DanielCardonaRojas
DanielCardonaRojas / UIViewExtensions.swift
Last active December 11, 2020 17:11
UIView general purpuse utilities.
extension UIView {
func makeCircular() -> NSKeyValueObservation {
self.layer.cornerRadius = self.bounds.width/2
clipsToBounds = true
let observation = observe(\UIView.bounds, changeHandler: { this, change in
this.layer.cornerRadius = this.bounds.width/2
})
return observation
}
@DanielCardonaRojas
DanielCardonaRojas / RevealView.swift
Created April 15, 2019 13:15
Collapsing expanding view
@IBDesignable
class RevealView: UIView {
var contentHeight: CGFloat = 300
@IBInspectable var title: String = "" {
didSet {
titleLabel.text = title
}
}
extension UIScrollView {
func scrollSubViewToTop(_ subview: UIView, offset: CGFloat, animated: Bool) {
let point = convert(subview.frame.origin, from: subview.superview ?? subview)
setContentOffset(CGPoint(x: 0, y: point.y - offset), animated: animated)
}
func viewPortOffset(of subview: UIView) -> CGFloat {
let point = convert(subview.frame.origin, from: subview.superview ?? subview)
return point.y - contentOffset.y
}
@DanielCardonaRojas
DanielCardonaRojas / Storyboarded.swift
Created April 13, 2019 14:04
Helper protocol for easy controller instantiation.
/*
It is encouraged to use the same class name for the controller story board identifier,
but this protocol is flexible to allow defining the story from where to instantiate the
controller as well its identifier.
You can choose to keep the convention while just changing the story board where the controller
is located.
e.g:
@DanielCardonaRojas
DanielCardonaRojas / Coordinator.swift
Created April 13, 2019 13:29
Base protocol for adopting Coordinator pattern.
import UIKit
import Foundation
protocol Coordinator: class {
typealias ExitHandler = (Coordinator) -> Void
var childCoordinators: [Coordinator] { get set }
var navigationController: UINavigationController { get set }
var initialViewController: UIViewController? { get }
func willNavigate(from: UIViewController, to: UIViewController)
func start()