This file contains hidden or 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 Invalidation { | |
static let display = Invalidation { $0.setNeedsDisplay() } | |
static let layout = Invalidation { $0.setNeedsLayout() } | |
let action: (UIView) -> Void | |
} | |
@propertyWrapper | |
struct Invalidating<Value> { | |
private let invalidations: [Invalidation] |
This file contains hidden or 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 Parent: View { | |
@State private var foo = "foo" { | |
didSet { print("Parent", foo) } | |
} | |
var body: some View { | |
VStack { | |
Inner(value: foo) | |
Button("Parent Double") { |
This file contains hidden or 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
@propertyWrapper | |
struct Storage<T: AppStorageConvertible>: RawRepresentable { | |
var rawValue: String { wrappedValue.storedValue } | |
var wrappedValue: T | |
init?(rawValue: String) { | |
guard let value = T.init(rawValue) else { return nil } | |
self.wrappedValue = value | |
} | |
init(wrappedValue: T) { |
This file contains hidden or 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 Publisher { | |
public typealias Pair<T> = (previous: T?, current: T) | |
public func withPrevious() -> AnyPublisher<Pair<Output>, Failure> { | |
return scan(nil) { previous, current -> Pair<Output>? in | |
return Pair(previous: previous?.current, current: current) | |
} | |
.compactMap { $0 } | |
.eraseToAnyPublisher() | |
} |
This file contains hidden or 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 | |
} | |
} |
This file contains hidden or 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 | |
} |
This file contains hidden or 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 { |
This file contains hidden or 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) |
This file contains hidden or 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 { |
This file contains hidden or 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() } | |
} |