Skip to content

Instantly share code, notes, and snippets.

final actor Worker: ObservableObject {
@MainActor @Published private(set) var lastWorkDoneAt: Date?
private var counter = 0
func doWork() {
counter += 1
DispatchQueue.main.async {
self.lastWorkDoneAt = .now
struct TestLayout: Layout {
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
// Assuming this layout is in a fixed size container and isn't sizing itself based on the subviews.
return proposal.replacingUnspecifiedDimensions()
}
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
let availableWidth = bounds.size.width
let offeredWidth = availableWidth / CGFloat(subviews.count)
let subviewProposal = ProposedViewSize(width: offeredWidth, height: bounds.size.height)
// The original Container protocol.
protocol Container {
associatedtype Item
func deleteItem(_: Item)
}
// Let's make a couple of concrete containers that use our protocol:
class StringContainer: Container {
typealias Item = String
//
// PagingView.swift
// Wallaroo - https://wallaroo.app
//
// Created by Sean Heber (@BigZaphod) on 8/9/22.
//
import SwiftUI
// This exists because SwiftUI's paging setup is kind of broken and/or wrong out of the box right now.
struct ContentView: View {
@State var data: [String] = []
@State var navPath = NavigationPath()
var body: some View {
NavigationStack(path: $navPath) {
ZStack {
Button("Hit It") {
navPath.append(0)
}
//
// Created by Sean Heber on 8/11/22.
//
import Foundation
enum ExponentialBackoffError : Error {
case retryLimitExceeded
}
// It is absolutely insane that velocity isn't public.
// INSANE.
// I don't get it.
// So anyway, this works but is kind of horrible.
// It's probably pretty safe to use and it should fallback to 0 if something goes wrong.
// Maybe.
extension DragGesture.Value {
var secretVelocity: CGSize {
let mirror = Mirror(reflecting: self)
guard let velocity = mirror.children.first(where: { $0.label == "velocity" }) else {
//
// View+Assign.swift
// Created by Sean on 7/29/22.
//
import SwiftUI
// You cannot safely assign to a state variable during view update - such as inside the block of a GeometryReader.
// Rather than do an unsafe hack like DispatchQueue.main.async or resorting to a PreferenceKey or even Combine, we
// can simply defer the assignment to a time when it *is* safe to update State- such as inside of the task block!
//
// A simplified implementation of https://gist.github.com/sturdysturge/e5163a9e95826adbeff9824d5aa1d111
// Which has an associated article here: https://betterprogramming.pub/build-a-secure-swiftui-property-wrapper-for-the-keychain-e0f8e39d554b
// Requires the Keychain Access package: https://github.com/kishikawakatsumi/KeychainAccess
//
import SwiftUI
import KeychainAccess
@propertyWrapper
//
// A Swift property wrapper for adding "indirect" to struct properties.
// Enum supports this out of the box, but for some reason struct doesn't.
//
// This is useful when you want to do something recursive with structs like:
//
// struct Node {
// var next: Node?
// }
//