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) |
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 { |
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() } | |
} |
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 single = Child() | |
let array = [Child(), Child()] | |
func updateSingle() { |
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 { |
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 |
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 { |
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 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 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 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 } | |
} |