Skip to content

Instantly share code, notes, and snippets.

View JasonCanCode's full-sized avatar

Jason JasonCanCode

View GitHub Profile
@JasonCanCode
JasonCanCode / NetworkLogger.swift
Last active May 11, 2024 20:19
Print out helpful information on network requests when debugging
import Foundation
#if DEBUG
/// Use this to print out helpful information on network requests when debugging.
/// The functions return strings so I can decide whether to use print statemens or custom breakpoints to log them.
public enum NetworkLogger {
static func cURLLog(request: URLRequest) -> String {
"\n🌎 \(request.cURL)\n"
@JasonCanCode
JasonCanCode / Bindable.swift
Last active December 22, 2021 18:03
Give a property the power of a BehaviorRelay while preserving the object itself. The object can remain a constant property of a View Model and still be updated through Rx bindings.
import Foundation
import RxCocoa
import RxSwift
/// To persist updates through either the `wrappedValue` or `relay` while keeping the implementing property a value type.
private var cache = NSCache<AnyObject, AnyObject>()
@propertyWrapper
struct Bindable<Element> {
var projectedValue: Bindable { self }
@JasonCanCode
JasonCanCode / NSAttributedString+Components.swift
Last active May 13, 2021 20:49
Extending NSAttributedString with a convenient way to generate text with mixed attributes
import Foundation
extension NSAttributedString {
struct FontTypes: OptionSet {
static let `default` = FontTypes(rawValue: 1 << 0)
static let bold = FontTypes(rawValue: 1 << 1)
static let italic = FontTypes(rawValue: 1 << 2)
static let underlined = FontTypes(rawValue: 1 << 3)
let rawValue: Int8
@JasonCanCode
JasonCanCode / add_git_hooks.sh
Created January 21, 2021 17:28
Git commit hooks for running SwiftLint
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
echo "Running from" $DIR
ln -s $DIR/pre-commit $DIR/../.git/hooks/pre-commit
@JasonCanCode
JasonCanCode / PagedContentViewController.swift
Created November 4, 2020 14:27
Convenient class embedding multiple view controllers inside a UIPageViewController
import UIKit
public protocol PageContainerDelegate: class {
func switchedPage(to index: Int, isLastPage: Bool)
}
open class PagedContentViewController: UIPageViewController {
public weak var containerDelegeate: PageContainerDelegate?
open var orderedViewControllers: [UIViewController] = [] {
didSet {
@JasonCanCode
JasonCanCode / JSONCodable.swift
Created October 30, 2020 16:56
A protocol to add JSON conversion abilities to any Codable object
import Foundation
public typealias JSON = [String: Any]
/// A Codable object with added JSON conversion abilities
public protocol JSONCodable: Codable, JSONAttributable {
// Replace declaration in adoptor's extension to change expected date format
static var dateFormatter: DateFormatter { get }
}
@JasonCanCode
JasonCanCode / A_LinkRouter.swift
Last active April 13, 2023 16:31
Streamlined structure for inspecting a link and informing the right coordinator to take action.
import Foundation
/// Manages all the link handlers and routes a url to the appropriate one.
public class LinkRouter: NSObject {
private(set) static var shared: LinkRouter = LinkRouter()
public var appScheme: String?
private var handlers: [LinkHandlerType] = []
public init(appScheme: String? = nil) {
@JasonCanCode
JasonCanCode / UIView+Shadow.swift
Created October 17, 2019 14:57
For when you work with a designer that goes crazy with the shadows
import UIKit
extension UIView {
func addShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.15
layer.shadowOffset = CGSize(width: 0, height: 3.0)
layer.shadowRadius = 4
layer.masksToBounds = false
}
@JasonCanCode
JasonCanCode / DynamicHeaderExample.swift
Last active April 18, 2021 18:45
Dynamic Table View Header Height
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
sizeTableViewHeaderToFit()
}
// MARK: - Dynamic Header Height
func sizeTableViewHeaderToFit() {
guard let headerView = tableView.tableHeaderView else {
import UIKit
extension UIImageView {
func setAnimationImagesWithGif(named name: String, animationDuration: TimeInterval? = nil) {
guard let bundleURL = Bundle.main.url(forResource: name, withExtension: "gif"),
let imageData = try? Data(contentsOf: bundleURL),
let source = CGImageSourceCreateWithData(imageData as CFData, nil) else {
return
}
let frameCount = CGImageSourceGetCount(source)