Skip to content

Instantly share code, notes, and snippets.

@GemmaDelOlmo
GemmaDelOlmo / Strategy.swift
Created November 2, 2022 10:43
Strategy Pattern for UITableView with MVP architecture
//STEPS TO IMPLEMENT STRATEGY PATTERN ON PROTOCOL ORIENTED MVP VIEWCONTROLLER WITH MVVM CELLS
//1: The ViewController will have a collection of cell viewModels
//2: There must be a drawer class for each cell that implements the cell drawer protocol
//3: Each of the cellViewModels must implement the viewModel generic protocol in order to have a reference to the cellDrawer
//DRAWER AND VIEWMODEL PROTOCOLS
protocol <#YOUR_DRAWER_PROTOCOL_NAME#> {
func cellFor(tableView: UITableView, atIndexPath: IndexPath) -> UITableViewCell
func draw(_ cell: UITableViewCell, cellViewModel: Any, presenter: <#CONTROLLER_PRESENTER_PROTOCOL#>)
func heightFor(tableView: UITableView, atIndexPath: IndexPath, cellViewModel: Any) -> CGFloat
@GemmaDelOlmo
GemmaDelOlmo / PaddingLabel.swift
Created April 21, 2022 09:29
UILabel subclass with configurable insets
import Foundation
import UIKit
@IBDesignable public class PaddingLabel: UILabel {
@IBInspectable public var topInset: CGFloat = 5.0
@IBInspectable public var bottomInset: CGFloat = 5.0
@IBInspectable public var leftInset: CGFloat = 7.0
@IBInspectable public var rightInset: CGFloat = 7.0
import Foundation
import UIKit
extension UITableView {
public func reloadData(completion:@escaping ()->()) {
UIView.animate(withDuration: 0, animations: reloadData)
{ _ in completion() }
}
}
import Foundation
import UIKit
protocol ReusableView: class {
static var defaultReuseIdentifier: String { get }
}
extension ReusableView where Self: UIView {
static var defaultReuseIdentifier: String {
return String(describing: self)
import Foundation
import UIKit
protocol NibLoadableView: class {
static var nibName: String { get }
}
extension NibLoadableView where Self: UIView {
static var nibName: String {
return String(describing: self)
import Foundation
extension UIViewController {
class func loadFromNib<T: UIViewController>() -> T {
return T(nibName: String(describing: self), bundle: nil)
}
}
@GemmaDelOlmo
GemmaDelOlmo / CALayer+Borders.swift
Created December 1, 2017 10:46
Extension on CALayer to add borders
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat, inset: CGFloat) {
let border = CALayer()
switch edge {
case UIRectEdge.top:
border.frame = CGRect.init(x: inset, y: 0, width: frame.width - inset * 2, height: thickness)
border.name = "top"
@GemmaDelOlmo
GemmaDelOlmo / String+Localize.swift
Created December 1, 2017 10:45
String extension to easily localize without NSLocalizedString
extension String {
static func localized(_ identifier: String) -> String {
return NSLocalizedString(identifier, comment: "")
}
}
@GemmaDelOlmo
GemmaDelOlmo / UITableView+Reuse.swift
Created December 1, 2017 10:44
UITableViewExtension to help reuse cells
extension UITableView {
func register<T: UITableViewCell>(_: T.Type) where T: NibLoadableView {
let bundle = Bundle(for: T.self)
let nib = UINib(nibName: T.nibName, bundle: bundle)
self.register(nib, forCellReuseIdentifier: T.defaultReuseIdentifier)
}
func registerHeader<T: UIView>(_: T.Type) where T: ReusableView, T: NibLoadableView {
@GemmaDelOlmo
GemmaDelOlmo / Optional+Unwrapping.swift
Last active December 30, 2018 10:59
Optional extension for easily unwrapping one or more variables.
extension Optional {
//Executes the closure if there is some value
func then(_ handler: (Wrapped) -> Void) {
switch self {
case .some(let wrapped): return handler(wrapped)
case .none: break
}
}