Skip to content

Instantly share code, notes, and snippets.

Adam Sharp sharplet

View GitHub Profile
View CurrentValuePublisher.swift
// Copyright (c) 2019–20 Adam Sharp and thoughtbot, inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
@sharplet
sharplet / KeychainItem.swift
Created Apr 25, 2020
A lightweight keychain wrapper for querying the keychain databse
View KeychainItem.swift
// Copyright (c) 2019–20 Adam Sharp and thoughtbot, inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
@sharplet
sharplet / Always.swift
Last active Jun 5, 2020
A Combine publisher that repeatedly emits the same value as long as there is demand
View Always.swift
// Copyright (c) 2019–20 Adam Sharp and thoughtbot, inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
@sharplet
sharplet / Example.swift
Created Nov 3, 2017
An improved wrapper for UIImagePickerController
View Example.swift
// Here is a usage example. Refer to ImagePicker.swift below for the implementation!
// 1. Easily configure the picker
let cameraPicker = ImagePicker(sourceType: .camera)
let cropPicker = ImagePicker(sourceType: .photoLibrary, allowsEditing: true)
// Automatically includes both kUTTypeImage and kUTTypeLivePhoto
let livePhotoPicker = ImagePicker(sourceType: .photoLibrary, mediaTypes: [.livePhotos])
// 2. Use the picker
@sharplet
sharplet / SimpleNetworkingExample.swift
Created Sep 14, 2017
Example Source Code for "A Simple Approach to Thread-Safe Networking in iOS Apps"
View SimpleNetworkingExample.swift
import Foundation
import PlaygroundSupport
enum URLResult {
case response(Data, URLResponse)
case error(Error, Data?, URLResponse?)
}
extension URLSession {
@discardableResult
@sharplet
sharplet / trap.swift
Created Nov 23, 2015
Simple signal handling in Swift
View trap.swift
import Darwin
enum Signal: Int32 {
case HUP = 1
case INT = 2
case QUIT = 3
case ABRT = 6
case KILL = 9
case ALRM = 14
case TERM = 15
View git-remove-submodule
#!/bin/sh
#
# Adam Sharp
# Aug 21, 2013
#
# Usage: Add it to your PATH and `git remove-submodule path/to/submodule`.
#
# Does the inverse of `git submodule add`:
# 1) `deinit` the submodule
# 2) Remove the submodule from the index and working directory
@sharplet
sharplet / UIImage+Resize.swift
Last active Oct 3, 2019
Create an image thumbnail with an aspect fill resize mode
View UIImage+Resize.swift
extension UIImage {
enum AspectOrientation {
case portrait
case square
case landscape
}
var aspectRatio: CGFloat {
return size.aspectRatio
}
@sharplet
sharplet / DispatchSourceInterrupt.swift
Created Jun 18, 2019
Demo of DispatchSourceSignal for handling interrupts
View DispatchSourceInterrupt.swift
import Darwin.C
import Dispatch
signal(SIGINT, SIG_IGN)
let source = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main)
var count = 0
source.setEventHandler {
@sharplet
sharplet / Example.swift
Last active May 11, 2019
Mutable property lenses for ReactiveSwift with key paths
View Example.swift
struct User {
var name: String
}
let currentUser = MutableProperty(User(name: "Foo"))
currentUser[\.name] <~ nameTextField.reactive.textValues
You can’t perform that action at this time.