Skip to content

Instantly share code, notes, and snippets.

View JasonCanCode's full-sized avatar

Jason JasonCanCode

View GitHub Profile
@JasonCanCode
JasonCanCode / DeviceHelper.swift
Created March 20, 2017 19:51
Quickly determine the screen type of the device
enum DeviceScreenType {
case fourOrLess, five, six, sixPlus
}
struct DeviceHelper {
static var screenType: DeviceScreenType {
let maxScreenLength = max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
if maxScreenLength < 568.0 {
return .fourOrLess
@JasonCanCode
JasonCanCode / TextFieldKeyboardPresentable.swift
Last active August 3, 2018 15:48
A simpler way to handle multiple UITextFields
import UIKit
protocol TextFieldKeyboardPresentable: class, UITextFieldDelegate {
/// Collection of all fields in a form in the order in which they should be navigated
var textFields: [UITextField] { get }
/// Can be used to take action once a form is completely filled out
func lastTextFieldDidReturn()
/// Can be used to enable a button when text field(s) become valid
func checkValidation()
@JasonCanCode
JasonCanCode / ContextGenerator.swift
Last active December 6, 2018 21:56
Generic generator of an NSManagedObjectContext for interacting with a CoreData database
import CoreData
/// Use `ContextGenerator.contextForDatabase(named:)` to generate an NSManagedObjectContext for interacting with CoreData
class ContextGenerator: NSObject {
class func contextForDatabase(named databaseName: String, inMemoryOnly: Bool = false) -> NSManagedObjectContext? {
return ContextGenerator.init(databaseName: databaseName, inMemoryOnly: inMemoryOnly)?.managedObjectContext
}
private let managedObjectContext: NSManagedObjectContext
private let managedObjectModel: NSManagedObjectModel
@JasonCanCode
JasonCanCode / JSONUpdatable.swift
Last active August 3, 2018 15:45
Make CoreData model objects easier to interact with
import Foundation
protocol JSONUpdatable {
func update(with json: JSON)
}
extension String {
func removePrefix(_ prefixString: String) -> String {
guard self.hasPrefix(prefixString) else {
return self
}
let index = self.index(self.startIndex, offsetBy: prefixString.characters.count)
return self.substring(from: index)
}
}
@JasonCanCode
JasonCanCode / ScrollToRefreshable.swift
Last active August 17, 2017 20:27
Protocol for added pull-to-refresh to any UIScrollView
import UIKit
protocol ScrollToRefreshable: UIScrollViewDelegate {
var refreshableScrollView: UIScrollView { get }
var activityIndicatorView: UIActivityIndicatorView! { get }
func updateViewModel()
}
extension ScrollToRefreshable {
@JasonCanCode
JasonCanCode / LoadingOverlayDisplayable.swift
Last active August 3, 2018 19:03
Easily apply a loading overlay with interaction restrictions
import UIKit
/// Have a UIViewController adopt this to easily block interactions while loading.
protocol LoadingOverlayDisplayable: class {
var loadingOverlayView: LoadingOverlayView? { get set }
}
extension LoadingOverlayDisplayable where Self: UIViewController {
/// Call in `viewDidLoad` to establish the loading overlay view
@JasonCanCode
JasonCanCode / Stopwatch.swift
Last active March 1, 2021 15:33
Helpful tool for printing to console when you are trying to figure out what is taking so long
class Stopwatch {
static let shared = Stopwatch()
let name: String
var shouldLogInRealTime: Bool = false
private var start: Date? {
willSet {
lap = newValue
}
@JasonCanCode
JasonCanCode / MenuDelegate.swift
Last active August 3, 2018 15:51
Menu Delegate for easily handling a side drawer nav
import UIKit
/// Fuctionality for a root view controller that handles the feature container view and the side menu drawer
protocol MenuDelegate: class {
var titleLabel: UILabel! { get } // To show feature name in mock nav bar
var containerView: UIView! { get }
var menuWidthConstraint: NSLayoutConstraint! { get }
var contentViewController: UIViewController! { get set }
func showTimeline()
@JasonCanCode
JasonCanCode / Localizable.swift
Created August 3, 2018 15:56
Used to mark text for localization. Keys include the class using the text and the first 10 characters of the default text. Translators will thank you later.
import Foundation
/// Used to mark text for localization. Keys include the class using the text and the first 10 characters of the default text.
protocol Localizable {}
extension Localizable {
static func localize(_ text: String) -> String {
let caller = String(describing: Self.self)
let prefixIndex = text.index(text.startIndex, offsetBy: min(15, text.count))