View SizeClass.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//v1: just find out if sizeclass is regular | |
@propertyWrapper | |
struct SizeClass: DynamicProperty { | |
@Environment(\.horizontalSizeClass) var horizontalSizeClass | |
@Environment(\.verticalSizeClass) var verticalSizeClass | |
var wrappedValue: Bool { | |
horizontalSizeClass == .regular && verticalSizeClass == .regular | |
} | |
} |
View AnyEquatable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct AnyEquatable: Equatable { | |
public let base: Any | |
private let isEqual: (_ other: Any) -> Bool | |
public init<T: Equatable>(_ value: T) { | |
self.base = value | |
self.isEqual = { other in | |
guard let other = other as? T else { return false } | |
return other == value | |
} |
View RetryWhen.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
import Foundation | |
public extension Publisher { | |
func retryWhen(max: Int = .max, delay: DispatchQueue.SchedulerTimeType.Stride = 0, _ predicate: @escaping Publishers.RetryWhen<Self>.Predicate) -> Publishers.RetryWhen<Self> { | |
.init(upstream: self, max: max, delay: delay, predicate: predicate) | |
} | |
} | |
extension Publishers { |
View UserDefaultsKey.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct UserDefaultsKey<T: Codable> { | |
var name: String | |
var `default`: T | |
} | |
extension UserDefaults { | |
func get<T>(_ key: UserDefaultsKey<T>) -> T { | |
guard | |
let data = data(forKey: key.name), | |
let box = try? JSONDecoder().decode(Box<T>.self, from: data) |
View AnyCodable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct AnyCodable: Codable { | |
public var base: Any | |
public init(_ base: Any) { | |
self.base = base | |
} | |
public init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
guard !container.decodeNil() else { |
View Namespace.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Create a generic namespace we can ue to 'hang' values off | |
public struct Namespace<Base> { } | |
// 2. Add a namespace to a type(s) with the desired name | |
extension Color { | |
public static var theme: Namespace<Self> { .init() } | |
} | |
extension Font { | |
public static var theme: Namespace<Self> { .init() } | |
} |
View Example.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Setup | |
class Child: ObservableObject { | |
@Published var value = "" | |
} | |
class Parent: ObservableObjectContainer { | |
let child = Child() | |
func updateChild() { | |
child.value = "hello world" |
View .swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct Location: Equatable { | |
var file: String | |
var line: UInt | |
} | |
private struct ForEachKey: EnvironmentKey { | |
static var defaultValue: [Location] = [] | |
} | |
private extension EnvironmentValues { |
View TopLevelDecoder+Nested.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension TopLevelDecoder { | |
func decode<T: Decodable>(_ type: T.Type, from input: Input, under: String) throws -> T { | |
let decoder = try decode(_Decoder.self, from: input).decoder | |
let container = try decoder.container(keyedBy: AnyCodingKey.self) | |
return try container.decode(T.self, forKey: .init(under)) | |
} | |
} | |
private struct _Decoder: Decodable { | |
var decoder: Decoder |
View Require.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Require<Content: View>: View { | |
@StateObject private var registrar: ViewRegistrar | |
@ViewBuilder private var content: () -> Content | |
public init(_ value: Int, @ViewBuilder content: @escaping () -> Content) { | |
self._registrar = .init(wrappedValue: ViewRegistrar(limit: value)) | |
self.content = content | |
} | |
public var body: some View { |
NewerOlder