Skip to content

Instantly share code, notes, and snippets.

View JasonCanCode's full-sized avatar

Jason JasonCanCode

View GitHub Profile
@JasonCanCode
JasonCanCode / PhotoImageUpdatable.swift
Created November 28, 2018 21:53
Helper protocol for populating an image view with a taken photo or one from the library.
import AVFoundation
import MobileCoreServices
import Photos
import UIKit
enum CaptureError: Error {
case cameraUnavailable
case cameraDenied
case libraryUnavailable
}
@JasonCanCode
JasonCanCode / TextFieldsKeyboardHandler.swift
Last active August 15, 2022 17:01
A helper class for handling the keyboard for views with one or more text fields
import UIKit
class TextFieldsKeyboardHandler: NSObject, UITextFieldDelegate {
private weak var delegate: TextFieldsKeyboardHandlerDelegate!
/// Used to resolve issues with old devices having offset y origins where nav bars are present
private var restingYOrigin: CGFloat = 0
/// Replace for a different handling of the keyboard offset
lazy var updateKeyboardFrame: (CGRect) -> Void = updateViewOffset
init(delegate: TextFieldsKeyboardHandlerDelegate, shouldAddDismissGesture: Bool = true) {
@JasonCanCode
JasonCanCode / XCTestCase+Helper.swift
Last active November 13, 2018 21:16
An easier way to access UI elements or check for existence within a UI Test.
extension XCTestCase {
var app: XCUIApplication { return XCUIApplication() }
func checkExistenceOfElements(_ typesAndTexts: [(XCUIElement.ElementType, String)], timeout: TimeInterval = 3) {
for (type, text) in typesAndTexts {
checkExistenceOfElement(type, text, timeout: timeout)
}
}
func checkExistenceOfElement(_ type: XCUIElement.ElementType, _ text: String, timeout: TimeInterval = 3) {
@JasonCanCode
JasonCanCode / TimestampPickerView.swift
Last active August 26, 2019 20:05
A UIPickerView subclass that behaves like a UIDatePicker but for time with seconds included. Military time is optional.
import UIKit
class TimestampPickerView: UIPickerView {
private(set) var date: Date = Date()
var minimumDate: Date?
var maximumDate: Date?
var isMilitaryTime: Bool {
get {
return timestampDatasource?.isMilitaryTime ?? false
}
@JasonCanCode
JasonCanCode / PostFixTextField.swift
Created October 8, 2018 18:32
Have trailing text persist in a text field.
import UIKit
class PostFixTextField: UITextField {
@IBInspectable var postfixColor: UIColor?
@IBInspectable var shouldRemovePostfixOnEditing: Bool = false
@IBInspectable var postfixText: String = "" {
willSet {
removePostFix()
}
didSet {
import UIKit
/**
A convenient way to filter your table with a search bar.
Example use:
extension MyTableViewController: SearchTableType {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
@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))
@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 / 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 / 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