Skip to content

Instantly share code, notes, and snippets.

@TheWorstProgrammerEver
TheWorstProgrammerEver / CustomColors.swift
Last active March 11, 2022 05:47
Swift custom colours based on enum example
import SwiftUI
enum CustomColors : String {
case
listBackground,
iPhone13ShowcaseBackground,
iPhone13BoxContentBackground,
iPadMiniBoxContentBackground
@TheWorstProgrammerEver
TheWorstProgrammerEver / Example.swift
Created March 5, 2022 08:22
SwiftUI keyboard dismissal / focus lost handler.
import SwiftUI
struct Example : View {
@State private var first: String = ""
@State private var second: String = ""
var body: some View {
Form {
@TheWorstProgrammerEver
TheWorstProgrammerEver / InfoPList+SwiftUI.swift
Created March 5, 2022 08:12
Swift Info.plist as JSON, injectable via Environment for SwiftUI
struct InfoPListEnvironmentKey : EnvironmentKey {
private static var value: InfoPList = .load()
static var defaultValue: InfoPList { value }
}
extension EnvironmentValues {
@TheWorstProgrammerEver
TheWorstProgrammerEver / StandardPreviews.swift
Created March 5, 2022 08:05
SwiftUI StandardPreviews - reusable previews with dark- and light-mode, large- and small-text (Dynamic Type) variations.
import SwiftUI
struct StandardPreviews<Content: View> : View {
var light: Bool = true
var dark: Bool = true
var large: Bool = true
var small: Bool = true
@TheWorstProgrammerEver
TheWorstProgrammerEver / CacheService.swift
Created March 5, 2022 07:58
Swift Cache Service (leveraging Codable / JSON support) and simple implementation with UserDefaults
import Foundation
protocol CacheService {
func load<T: Cachable>(_ identifier: String) throws -> T?
func save<T: Cachable>(_ identifier: String, _ cachable: T) throws
func clear<T: Cachable>(_ type: T.Type) throws
@TheWorstProgrammerEver
TheWorstProgrammerEver / Container.swift
Created February 6, 2022 10:16
Swift Service Locator / IoC Container / Dependency Registry & Resolver (haha gross)
class Container : DependencyRegistry, DependencyResolver {
private var registrations: [String : () -> Any] = [:]
func register<T>(_ t: T.Type, _ f: @escaping @autoclosure () -> T) {
registrations[key(for: t)] = f
}
func resolve<T>() -> T {
registrations[key(for: T.self)]!() as! T
@TheWorstProgrammerEver
TheWorstProgrammerEver / URLSession-MultipartFormData.swift
Last active March 5, 2022 08:01
Swift URLRequest Multipart Form-Data
import Foundation
import UIKit
extension URLRequest {
typealias BuilderFunction = ((MultipartFormDataInclusion) -> Void) -> Void
struct MultipartFormDataInclusion {
var data: Data
@TheWorstProgrammerEver
TheWorstProgrammerEver / Proof.swift
Created February 19, 2021 05:17
SwiftUI Sheet NavigationView NavigationLink EnvironmentObject Environment presentationMode bug.
/*
Found a weird AF SwiftUI bug today.
It's hard to articulate succinctly. I have a demo project that replicates it.
Basically if you:
- Have a modally presented (via sheet) view which contains a NavigationView and a NavigationLink which leads to a destination view which has an EnvironmentObject dependency AS WELL AS a dependency on the .presentationMode environment value, and...
- The user partially dismisses the modally presented view (with the swipe down gesture) without committing to the dismissal, and
- Your code inside the modally presented view hierarchy accesses the EnvironmentObject dependency...
@TheWorstProgrammerEver
TheWorstProgrammerEver / UrlTemplate.swift
Last active March 5, 2022 07:56
Type-driven URL templating in Swift
import Foundation
struct StringTemplate<T> {
var template: String
init(_ template: String) {
self.template = template
}
func fill(_ t: T) -> String {
@TheWorstProgrammerEver
TheWorstProgrammerEver / colour-contrast.js
Last active November 14, 2022 12:59
WCAG Colour Contrast Checker in JS.
// https://www.w3.org/TR/WCAG21/#dfn-contrast-ratio
const contrastLightAndDark = (lighter, darker) => (lighter + 0.05) / (darker + 0.05)
const relativeLuminance = (r, g, b) => 0.2126 * C(r) + 0.7152 * C(g) + 0.0722 * C(b)
const C = c => {
const colourScalar = c / 255
return colourScalar <= 0.03928
? colourScalar / 12.92