Skip to content

Instantly share code, notes, and snippets.

@moyerr
moyerr / AdaptiveStyle.swift
Created July 16, 2026 17:52
Add ShapeStyle support to @ConentBuilder, allowing for more dynamic custom ShapeStyle implementations
/// A ShapeStyle that adapts dynamically based on the environment's ColorScheme
///
/// This exists as a basic example of how to apply a @ContentBuilder to ShapeStyles
struct AdaptiveStyle<Light: ShapeStyle, Dark: ShapeStyle>: ShapeStyle {
let light: Light
let dark: Dark
@ContentBuilder
func resolve(in environment: EnvironmentValues) -> some ShapeStyle {
if environment.colorScheme == .light {
@moyerr
moyerr / AsyncContent.swift
Last active July 16, 2026 17:55
An AsyncContent view for SwiftUI
/// The current phase of the asynchronous loading operation.
enum AsyncContentPhase<Value: Sendable> {
/// No value is loaded yet.
case empty
/// The value loaded successfully.
case success(Value)
/// The load operation failed with an error.
case failure(any Error)
/// The loaded value, if any.
@moyerr
moyerr / Updater.swift
Created April 2, 2026 19:25
An environment action for mutating state owned by an ancestor view
/// An action that updates a value stored in the environment.
///
/// ## Overview
///
/// Use an `Updater` to allow descendant views to modify a value owned by
/// an ancestor view, without passing Bindings or specific update actions for each property.
/// The updater accepts a ``WritableKeyPath`` and a new value, then applies
/// the change through a closure you provide at initialization.
///
/// ### Setting up the updater
@moyerr
moyerr / StreamChanges.swift
Created February 5, 2025 17:24
A SwiftUI ViewModifier for receiving state changes in an AsyncStream
extension View {
func streamChanges<Value: Equatable & Sendable>(
of value: Value,
provideStream: @escaping @Sendable (AsyncStream<Value>) async -> Void
) -> some View {
modifier(ChangeStream(value: value, provideStream: provideStream))
}
}
private struct ChangeStream<Value: Equatable & Sendable>: ViewModifier {
@moyerr
moyerr / SelfSizingSheetPresentation.swift
Created October 30, 2024 14:42
A modifier that provides automatic sizing for sheets in SwiftUI (iOS 16+)
import SwiftUI
struct SelfSizingSheetPresentation: ViewModifier {
private enum BoundsPreference: PreferenceKey {
static let defaultValue: Anchor<CGRect>? = nil
static func reduce(value: inout Value, nextValue: () -> Value) {
value = nextValue()
}
}
@moyerr
moyerr / AsyncIntervalSequence.swift
Created October 3, 2024 19:10
An AsyncSequence that iterates at most once every `interval`
/// An AsyncSequence that iterates at most once every `interval`
public struct AsyncIntervalSequence<Base: AsyncSequence, C: Clock> {
public let base: Base
public let interval: C.Instant.Duration
public let clock: C
public init(_ base: Base, interval: C.Instant.Duration, clock: C) {
self.base = base
self.interval = interval
self.clock = clock
@moyerr
moyerr / AsyncTimeoutSequence.swift
Created October 3, 2024 19:09
An AsyncSequence that will terminate with a `Timeout` error if iteration time exceeds the provided `timeout`.
/// An AsyncSequence that will terminate with a `Timeout` error if
/// iteration time exceeds the provided `timeout`.
struct AsyncTimeoutSequence<Base: AsyncSequence, C: Clock> {
struct Timeout: Error {}
let base: Base
let timeout: C.Instant.Duration
let clock: C
init(_ base: Base, timeout: C.Instant.Duration, clock: C) {
@moyerr
moyerr / RecursiveModifier.swift
Created November 7, 2023 18:01
Apply a modifier to a view repeatedly with a recursive modifier
import SwiftUI
struct RecursiveModifier<Data: Collection, Transformed: View>: ViewModifier {
let data: Data
let transform: (Data.Element, AnyView) -> Transformed
func body(content: Content) -> some View {
var subsequence = data[...]
if let first = subsequence.popFirst() {
@moyerr
moyerr / SwiftUINavigation.swift
Created July 7, 2023 17:17
A demonstration of navigation in SwiftUI using NavigationStack and Environment actions
import SwiftUI
// MARK: Navigation
enum Screen: String, Hashable, CaseIterable, CustomStringConvertible {
case first, second, third
var description: String {
rawValue.capitalized
}
struct AsyncWrappedSequence<Base: Sequence>: AsyncSequence {
typealias Element = Base.Element
struct AsyncIterator<BaseIterator>: AsyncIteratorProtocol where BaseIterator == Base.Iterator {
var base: BaseIterator
mutating func next() async -> BaseIterator.Element? {
base.next()
}
}