Skip to content

Instantly share code, notes, and snippets.

Working from home

Shaps shaps80

Working from home
View GitHub Profile
shaps80 / temp.swift
Created Jan 16, 2020
Just a temporary Gist
View temp.swift
print("Hello World")
shaps80 / AuthenticatingSceneDelegate.swift
Last active Jan 3, 2021
A UIWindowSceneDelegate that provides lifecycle events and an API for deferring specific tasks automatically for you. Simplifies the implementation of LocalAuthentication or some other authentication implementation.
View AuthenticatingSceneDelegate.swift
import UIKit
/// Defines a token for determining the validity of a session
public protocol AuthenticationToken: Codable {
/// The token is currently valid
var isValid: Bool { get }
/// An encoded representation for storage
var encoded: Data? { get }
shaps80 / UIView+KeyView..swift
Created Dec 13, 2019
A UIView helper extension that finds the 'key' view in the controller's hierarchy. Useful for finding a UITableView/UICollectionView or any other view that's NOT the controller's root view.
View UIView+KeyView..swift
import UIKit
internal extension UIViewController {
/// Returns the 'key' view for this controller. I.e. the top-most visible view that covers the view's entire bounds.
/// This is generally the controller's root view, but it could also be a nested table/collection view, etc...
@objc var _keyView: UIView {
return view._keyViews(in: self).last ?? view
shaps80 / CardDecorating.swift
Last active Dec 5, 2019
A simple protocol for creating a 'Card' style component in Swift.
View CardDecorating.swift
import UIKit
/// Decorates a view as a `Card`. Requires that the view is embedded in some `contentView`
public protocol CardDecorating {
/// View that will be styled as a card. Should hold the `contentView`.
var cardView: UIView { get }
/// Holds the main content. Subviews should be added to this view.
var contentView: UIView { get }
View PlaybackGestureRecognizer.swift
import UIKit
/// A gesture optimised for use in video playback. Both delta-offset and target-delta based translations are supported
public final class PlaybackGestureRecognizer: LongPressGestureRecognizer {
/// Specifies the direction of the scrub
public enum Direction {
/// The user is scrubbed forward through time
case forward
/// The user is scrubbed backward through time
shaps80 / URLExpander.swift
Last active Apr 16, 2019
Expands a URL by doing a HEAD request against its URL and returning the redirected URL, or the original if the URL wasn't shortened. You can call `expand(url:)` multiple times and the queue guarantees that only 1 request will be triggered at a time, in calling order.
View URLExpander.swift
import Foundation
final class URLExpander: NSObject, URLSessionTaskDelegate {
private lazy var session: URLSession = {
let config = URLSessionConfiguration.default
return URLSession(configuration: config, delegate: self, delegateQueue: queue)
private let queue: OperationQueue
shaps80 / Collection+ConsecutiveRanges.swift
Last active Jan 17, 2019
Returns an array of ranges representing the indices of consecutive elements in a collection matching a given predicate.
View Collection+ConsecutiveRanges.swift
public extension Collection where Index == Int {
func consecutiveRanges(matching predicate: (Element, Element) -> Bool) -> [ClosedRange<Index>] {
return zip(self, indices).reduce(into: [ClosedRange<Index>](), { result, tuple in
let (a, b) = tuple
let previousIndex = { result.index(before: result.endIndex) }()
if let range = result.last, isMatching(self[previousIndex], a) {
result[previousIndex] = ClosedRange(uncheckedBounds: (range.lowerBound, b))
} else {
shaps80 / DateFormatter+Docs.swift
Last active Dec 10, 2018
DateFormatter with better header docs
View DateFormatter+Docs.swift
import Foundation
public extension DateFormatter {
Makes a new DateFormatter with the specified format, calendar and locale.
Characters Example Description
View 152 Percent.xccolortheme
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
<string>1 1 1 0.997649</string>
<string>SFMono-Light - 11.0</string>
<string>1 0.827451 0 1</string>
shaps80 / ResponderController.swift
Created Nov 5, 2018
A convenient controller for working with the responder chain on iOS
View ResponderController.swift
import UIKit
/// Provides a convenient controller for working with the responder chain
public final class ResponderController: NSObject, NSCopying {
/// When true, the responder will loop through the responders when reaching the ends
public var isContinuous: Bool = true