Skip to content

Instantly share code, notes, and snippets.

View JasonCanCode's full-sized avatar

Jason JasonCanCode

View GitHub Profile
@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 {
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 / 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)
}
@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 / 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 / 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 / StoryboardHelper.swift
Last active November 30, 2018 18:33
Easily generate a new instance of a View Controller that exists within a storyboard
import UIKit
struct StoryboardHelper {
/// Assumes that the storyboard identifier for the View Controller class provided matches its name and that it has been added to the `storyboardNameForViewController` switch statement.
static func new<T>() -> T? {
let nibName = String(describing: T.self)
let storyboardName = storyboardNameForViewController(named: nibName)
return getViewController(named: nibName, fromStoryboardNamed: storyboardName) as? T
}
@JasonCanCode
JasonCanCode / NibViewType.swift
Created March 20, 2017 18:10
Protocol used by a UIView subclass for xibs
import UIKit
/**
The adopting UIView is the File Ower of a nib by the same name. All IBOutlets are wired through the File Owner (not the content view itself) and the content view is also wired to the IBOutlet `view`.
NOTE - `loadFromNib:` must be called within a convenience initializer.
Keep in mind that any UIView instance method that you would normally use `self` should be called `view` instead.
*/
protocol NibViewType: UIAppearance {
@JasonCanCode
JasonCanCode / AsyncImageLoader.swift
Last active May 12, 2022 17:25
Safely load and cache images asynchronously
import UIKit
// MARK: - ImageLoader Protocol
public typealias ImageLoaderHandler = (UIImage?, Error?) -> Void
public protocol ImageLoader {
func updateImage(fromURLString urlString: String?, placeholderImage: UIImage?, completionHandler: @escaping ImageLoaderHandler)
func imageFromCache(_ urlString: String?) -> UIImage?
}
@JasonCanCode
JasonCanCode / AlertHelper.swift
Last active August 3, 2018 15:52
A convenient way to create and present AlertControllers.
import UIKit
extension UIAlertAction {
convenience init(title: String, handler: ((UIAlertAction?) -> Void)? = nil) {
self.init(title: title, style: .default, handler: handler)
}
}
/// - Tag: AlertHelper
struct AlertHelper {