Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
JadenGeller / OnChangeTask.swift
Last active July 19, 2025 05:48
async onChange with cancelation
struct OnChangeTask<T: Equatable>: ViewModifier {
let value: T
let action: () async -> Void
@State private var updateCount = 0
func body(content: Content) -> some View {
content
.onChange(of: value) {
updateCount += 1
}
@JadenGeller
JadenGeller / ZoomNavigator.swift
Last active July 13, 2025 07:02
Keep view state and identity while animating into it from a list
/// A view that displays a collection of items in a scrollable list that can "zoom" to focus on a single selected item.
///
/// `ZoomNavigator` provides a navigation pattern similar to the iOS lock screen wallpaper picker,
/// where items can be viewed together in a scrollable list or individually in full screen.
/// The same view instance is maintained during transitions, preserving state and identity.
///
/// When no item is selected (`selection` is `nil`), all items are displayed in a scrollable list.
/// When an item is selected, the view filters to show only that item, expanded to fill the available space.
struct ZoomNavigator<Data: RandomAccessCollection, ID: Hashable, Content: View, Background: View, Modifier: ViewModifier>: View {
var data: Data
@JadenGeller
JadenGeller / ServiceLinkerPattern.md
Last active July 6, 2025 19:05
Service Linker Pattern

The Service Linker Pattern

Build compositional service architectures where dependencies wire themselves, boundaries cost nothing, and testing happens at exactly the right level.

// Two services that both need users - automatically shared!
struct AppBundle: LinkOrder & LinkNotification {
    let db = PostgreSQL()
    let email = SendGrid()
}
@JadenGeller
JadenGeller / $Parser.swift
Last active September 12, 2024 04:50
parsing custom attributed text format
import SwiftUI
struct AttributedStringBuilder {
@Stack var attributes: RichTextAttributes = .init()
var string: AttributedString = ""
mutating func text(_ span: String) {
string += AttributedString(span, attributes: .init(attributes))
}
@JadenGeller
JadenGeller / Stack.swift
Created September 6, 2024 20:49
push/pop state property wrapper
@propertyWrapper
struct Stack<Value> {
private var history: [Value]
init(wrappedValue initialValue: Value) {
self.history = [initialValue]
}
var wrappedValue: Value {
get { history[history.endIndex - 1] }

Markdown Extensions

Component Directives

  • Component Directive: A code block that defines a custom component.
  • Info String: Starts with a $COMPONENT-NAME, which is @ followed by an $IDENTIFIER (e.g., @component-name).
  • Front Matter: Enclosed within --- delimiters, specifies component attributes.
  • Nesting: Use more backticks for the parent block than the child, respecting CommonMark.
  • Content: Valid markdown, which may nest component directives.

Examples

@JadenGeller
JadenGeller / bfs.py
Last active July 3, 2024 02:51
itertools bfs
from itertools import zip_longest, chain
from dataclasses import dataclass, field
@dataclass
class Tree:
value: any
children: list = field(default_factory=list)
def levels(self):
yield [self]
struct RangeSliceCollection<Base: Collection, Ranges: Collection>: Collection where Ranges.Element == Range<Base.Index> {
var base: Base
var ranges: Ranges
var startIndex: Ranges.Index { ranges.startIndex }
var endIndex: Ranges.Index { ranges.endIndex }
func index(after i: Ranges.Index) -> Ranges.Index {
ranges.index(after: i)
}
extension Task where Success == Never, Failure == Never {
static let perpetual = Task {
await withCheckedContinuation { (_: CheckedContinuation<Void, Never>) in }
preconditionFailure()
}
}
@JadenGeller
JadenGeller / Versioned.swift
Last active September 6, 2024 20:49
useful property wrapper for prefix sums
@propertyWrapper
struct Versioned<Value>: RandomAccessCollection {
private var history: [Value]
var startIndex: Int { 0 }
var endIndex: Int { allValues.endIndex }
subscript(position: Int) -> Value {
get { allValues[position] }
set { allValues[position] = newValue }