Skip to content

Instantly share code, notes, and snippets.

View toddhopkinson's full-sized avatar

Todd Hopkinson toddhopkinson

View GitHub Profile
enum ActiveSheet: Identifiable {
case first, second
var id: Int {
hashValue
}
}
struct YourView: View {
@State var activeSheet: ActiveSheet?
@toddhopkinson
toddhopkinson / L10NTests.swift
Created June 16, 2017 21:51 — forked from Ben-G/L10NTests.swift
Very simple automated test to ensure localizations exist and are well-formed on iOS.
import Foundation
import XCTest
/// Basic sanity check that ensures that we are able to retrieve localized strings for all languages
/// we support.
final class L10NTests: XCTestCase {
func testLocalizations() {
let locales = ["en", "es", "zh-Hans", "zh-Hant", "fi"]
for locale in locales {
@toddhopkinson
toddhopkinson / MapViewController
Created June 15, 2017 21:40
Simple MapViewController
import UIKit
import MapKit
class MapViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
/// Location manager used to start and stop updating location.
let manager = CLLocationManager()
var receivedLocationCount = 0
@toddhopkinson
toddhopkinson / BackgroundTransferSample.swift
Last active September 27, 2023 01:47
Upload Very Large Files In Background on iOS - Alamofire.upload multipart in background
// You have a very very large video file you need to upload to a server while your app is backgrounded.
// Solve by using URLSession background functionality. I will here use Alamofire to enable multipart upload.
class Networking {
static let sharedInstance = Networking()
public var sessionManager: Alamofire.SessionManager // most of your web service clients will call through sessionManager
public var backgroundSessionManager: Alamofire.SessionManager // your web services you intend to keep running when the system backgrounds your app will use this
private init() {
self.sessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.default)
self.backgroundSessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: "com.lava.app.backgroundtransfer"))
// NOTE: important to be aware that your reporting that your async is on is not on main thread
// see also https://developer.apple.com/library/content/samplecode/avexporter/Listings/Swift_AVFoundationExporter_main_swift.html
// An Interested party such as a UIViewController (the call site of embedWatermark, for example) can register as delegate
// and update progress indicators (like a UIProgressView or UILabel).
protocol VideoProcessingProgressDelegate {
func watermarkProgressUpdated(progress: Float)
}
class VideoProcessingProgressExample {
@toddhopkinson
toddhopkinson / IBOutlets_didSet.swift
Last active December 17, 2016 20:51
Setting IBOutlet Styles at didSet
@IBOutlet weak var lavaButton: UIButton! {
didSet {
lavaButton.tintColor = UIColor.lavaColor()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
@toddhopkinson
toddhopkinson / catch_where.swift
Last active May 4, 2018 15:36
catch where clause
enum LavaError: Error {
case LavaNotHotError
case LavaTooHotError
}
struct LavaSpecialResponseCode: Error {
enum ErrorKind: Int {
case missingParams
case badData
}
@toddhopkinson
toddhopkinson / Podfile
Created October 25, 2016 21:00
Podfile that makes available pod to test target for XCTests in an imaginary lavaApp
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target 'lavaApp' do
pod 'ARSLineProgress', '~> 2.0'
pod 'Alamofire', '~> 4.0'
pod 'Willow', '~> 2.0'
pod 'SwiftyJSON', '~> 3.1.1'
@toddhopkinson
toddhopkinson / lldb-commands
Created October 23, 2016 04:44
LLDB Commands That Make Your Life Easier
// get an objects type
lldb: po type(of: yourObject)
// info dump on an object
lldb: po dump(youObject)
@toddhopkinson
toddhopkinson / daysPassed.swift
Last active September 16, 2016 23:17
Days past since date
let birthday = Todd.birthday // gets my birthday
let now = NSDate()
let secondsDifference = now.timeIntervalSinceDate(birthday)
let secondsPerDay = 60*60*24 // 60 seconds * 60 minutes * 24 hours
let daysSinceBirthday = secondsDifference / secondsPerDay