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 User: Equatable { | |
var firstName: String | |
var lastName: String | |
} | |
@main | |
struct MyApp: App { | |
@State var value = User(firstName: "", lastName: "") | |
@State var showEdit = false |
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 | |
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 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 AnalyticsReducer { | |
associatedtype State | |
associatedtype Action | |
func analytics(before: State, after: State, action: Action) -> Effect<Action> | |
} | |
public struct _AnalyticsReducer<Base: Reducer, Analytics: AnalyticsReducer>: Reducer where Analytics.State == Base.State, Analytics.Action == Base.Action { | |
@usableFromInline | |
let base: Base |
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 | |
let isUITesting = /* your UI test detection here */ | |
@main | |
struct EntryPoint { | |
static func main() { | |
if isUITesting { | |
UITestApp.main() |
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 | |
public protocol DefaultValue { | |
associatedtype Value: Codable | |
static var value: Value { get } | |
} | |
@propertyWrapper | |
public struct Default<Default: DefaultValue>: Codable { |
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
@propertyWrapper @MainActor public struct AsyncPublished<Value> { | |
public typealias AsyncAction = (Value) async throws -> Void | |
private class State { | |
var wrappedValue: Value | |
var isWorking: Bool = false | |
var current: Task<Void, Never>? | |
var action: AsyncAction = { _ in fatalError("Please configure action in parent object") } | |
init(wrappedValue: Value) { |
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 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 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
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