Skip to content

Instantly share code, notes, and snippets.

View IanKeen's full-sized avatar
🏂

Ian Keen IanKeen

🏂
View GitHub Profile
@IanKeen
IanKeen / Array+MultiSort.swift
Created November 18, 2026 18:38
Sort an array by multiple keypaths
extension Array {
public func sorted<each T: Comparable>(by keyPaths: repeat KeyPath<Element, each T>) -> Array {
return sorted { lhs, rhs in
for keyPath in repeat each keyPaths {
if lhs[keyPath: keyPath] < rhs[keyPath: keyPath] {
return true
} else if lhs[keyPath: keyPath] > rhs[keyPath: keyPath] {
return false
}
@IanKeen
IanKeen / PublishIfChanged.swift
Created November 17, 2026 18:03
PropertyWrapper: PublishIfChanged is a drop-in-replacement for `@Published` that only refreshes UI when a value has changed
@propertyWrapper
public struct PublishedIfChanged<Value> {
// MARK: - Publisher
public struct Publisher: Combine.Publisher {
public typealias Output = Value
public typealias Failure = Never
private let subject: CurrentValueSubject<Value, Never>
fileprivate init(_ subject: CurrentValueSubject<Value, Never>) {
@IanKeen
IanKeen / View+Refreshable.swift
Created June 12, 2025 23:44
SwiftUI: Better `refreshable` - Prevent View redraws cancelling `refreshable` by making it stable
extension View {
public func _refreshable(_ action: @escaping () async -> Void) -> some View {
modifier(RefreshableTaskModifier(action: action))
}
}
private struct RefreshId {
var value: UUID
var continuation: CheckedContinuation<Void, Never>
}
@IanKeen
IanKeen / PromptWindowController.swift
Created April 1, 2025 17:04 — forked from joethephish/PromptWindowController.swift
Substage's flippy out command bar
import AppKit
import SwiftUI
class PromptWindowController: NSWindowController, NSWindowDelegate {
// Essential properties
private var promptWindowContext: PromptWindowContext
private let finderContext: FinderContext
init() {
@IanKeen
IanKeen / DynamicProperties.swift
Created February 19, 2025 19:16
Support stored properties in extensions
public protocol DynamicProperties: AnyObject {
subscript<T>(dynamic key: String) -> T? { get set }
}
private extension String {
var unsafePointer: UnsafeRawPointer {
return UnsafeRawPointer(bitPattern: hashValue)!
}
}
extension DynamicProperties {
@IanKeen
IanKeen / ObservableAppStorage.swift
Created October 10, 2024 17:06
PropertyWrapper: ObservableAppStorage - Make UserDefaults work with @observable models (without all the boilerplate)
import Observation
public protocol _Observable: Observable {
nonisolated
func _access<Member>(keyPath: KeyPath<Self, Member>)
nonisolated
func _withMutation<Member, MutationResult>(
keyPath: KeyPath<Self, Member>,
_ mutation: () throws -> MutationResult
@IanKeen
IanKeen / AllowDecodingFailure.swift
Created October 3, 2024 18:36
PropertyWrapper: AllowDecodingFailure will catch decoding errors and return `nil` rather than failing the operation
@propertyWrapper
public struct AllowDecodingFailure<T: Codable>: Codable {
public var wrappedValue: T?
public var error: Error?
public var projectedValue: AllowDecodingFailure<T> { self }
public init(wrappedValue: T?) {
self.wrappedValue = wrappedValue
self.error = nil
}
@IanKeen
IanKeen / AllowDecodingFailures.swift
Created June 14, 2024 17:25
PropertyWrapper: Allows for single elements in an array to "fail" decoding without failing the entire operation
@propertyWrapper
public struct AllowDecodingFailures<T: Codable>: Codable {
public var wrappedValue: [T]
public var errors: [Error]
public var projectedValue: AllowDecodingFailures<T> { self }
private struct Empty: Decodable { }
public init(wrappedValue: [T]) {
self.init(wrappedValue: wrappedValue, errors: [])
@IanKeen
IanKeen / AsyncPublished.swift
Created May 21, 2024 17:58
PropertyWrapper: AsyncPublished
@propertyWrapper @MainActor public struct AsyncPublished<Value> {
public typealias AsyncAction = (Value) async throws -> Void
private class State {
var wrappedValue: Value
var isWorking: Bool = false
var current: Task<Void, Never>?
var action: AsyncAction = { _ in fatalError("Please configure action in parent object") }
init(wrappedValue: Value) {
@IanKeen
IanKeen / AnalyticsReducer.swift
Created January 16, 2024 04:27
TCA: Example of creating an Analytics component to get before/after state
public protocol AnalyticsReducer {
associatedtype State
associatedtype Action
func analytics(before: State, after: State, action: Action) -> Effect<Action>
}
public struct _AnalyticsReducer<Base: Reducer, Analytics: AnalyticsReducer>: Reducer where Analytics.State == Base.State, Analytics.Action == Base.Action {
@usableFromInline
let base: Base