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
// Setup | |
class Child: ObservableObject { | |
@Published var value = "" | |
} | |
class Parent: ObservableObjectContainer { | |
let single = Child() | |
let array = [Child(), Child()] | |
func updateSingle() { |
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 SwiftUI | |
struct Location: Equatable { | |
var file: String | |
var line: UInt | |
} | |
private struct ForEachKey: EnvironmentKey { | |
static var defaultValue: [Location] = [] | |
} | |
private extension EnvironmentValues { |
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 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 |
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 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 { |
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 SwiftUI | |
extension View { | |
public func focused<T>(file: StaticString = #file, _ state: FocusState<T>, equals value: T) -> some View { | |
modifier(FocusedModifier(state: state, id: value, file: file)) | |
} | |
} | |
@propertyWrapper |
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 Foundation | |
import SwiftUI | |
extension EnvironmentValues { | |
public func value<T>(_: T.Type = T.self, forKey key: String) -> T? { | |
guard let value = first(where: { name($0, equals: key) }) else { | |
print("No EnvironmentValue with key '\(key)' found.") | |
return nil | |
} |
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 LoadingIndicator: View { | |
@State private var startingOffset: CGFloat = 0 | |
@State private var space: CGSize = .zero | |
@State private var size: CGSize = .zero | |
@State private var progress: Double = 0 | |
@Binding private var isLoading: Bool { | |
didSet { | |
if isLoading { action() } | |
else { progress = 0 } | |
} |
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 SwiftUI | |
extension View { | |
public func discover<T: UIView>( | |
where predicate: @escaping (T) -> Bool = { _ in true }, | |
_ closure: @escaping (T) -> Void | |
) -> some View { | |
overlay( | |
DiscoveryView(predicate: predicate, setup: closure) | |
.frame(width: 0, height: 0) |
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 LosslessKey<T: Hashable & LosslessStringConvertible, U> { | |
var wrappedValue: [T: U] | |
} | |
extension LosslessKey: Codable where T: Codable, U: Codable { | |
init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
var result: [T: U] = [:] | |
for (key, value) in try container.decode([String: U].self) { | |
guard let key = T.init(key) 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
public protocol CustomEquatable: Equatable { } | |
private extension CustomEquatable { | |
var equatables: [AnyEquatable] { | |
return Mirror(reflecting: self) | |
.children | |
.compactMap { ($0.value as? EquatingProperty)?.anyEquatable } | |
} | |
} |