View UITask.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 UIKit | |
import Combine | |
public extension Task { | |
/// Keep a reference to a task that can be cancelled. | |
func store(in set: inout Set<AnyCancellable>) { | |
set.insert(AnyCancellable { | |
self.cancel() | |
}) | |
} |
View fetch.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
func fetch<Result: Codable>(_ endpoint: Endpoint<Result>, cacheBehavior: CacheBehavior = .cacheElseNetwork) async throws -> Result { | |
switch cacheBehavior { | |
case .cacheElseNetwork: | |
if let cached = self.cache[endpoint.cacheKey] { | |
return cached.payload | |
} else { | |
return try await self.networkFetch(endpoint) | |
} | |
case .networkOnly: | |
return try await self.networkFetch(endpoint) |
View Binding+NilCoalescing.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 | |
func ?? <T>(optional: Binding<T?>, defaultValue: @escaping @autoclosure () -> T) -> Binding<T> { | |
return Binding { | |
optional.wrappedValue ?? defaultValue() | |
} set: { newValue in | |
optional.wrappedValue = newValue | |
} | |
} |
View TextScaling.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 ContentView: View { | |
var text: Text { | |
Text("Bacon ipsum dolor amet fatback rump beef ribs jerky pork loin strip steak.") | |
.font(.headline) + | |
Text("\nCorned beef t-bone leberkas ball tip tongue burgdoggen picanha swine porchetta flank hamburger strip steak tail pork. Filet mignon prosciutto venison tongue meatball shankle pancetta. Hamburger prosciutto turkey chicken venison tenderloin porchetta spare ribs burgdoggen cupim pork turducken. Short ribs andouille kielbasa short loin beef. Ham kevin pork loin bacon, pastrami turducken jowl pig venison pork shank beef picanha.") | |
.font(.body) | |
} | |
var body: some View { | |
text |
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
struct Child: View { | |
var count: Int | |
var body: some View { | |
Text("Child: \(count)") | |
.useEffect(Int(floor(Double(count) / 2))) { source in | |
// this only gets called when the source changes | |
print("onAppear \(source)") | |
} | |
} |
View ObservablePublisher.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 | |
import SwiftUI | |
class ObservablePublisher<Output>: ObservableObject { | |
@Published var output: Output? | |
@Published var error: Swift.Error? | |
private var observer: AnyCancellable? | |
init() {} |
View IgnoreHashable.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
@propertyWrapper | |
struct IgnoreHashable<T>: Hashable { | |
var wrappedValue: T | |
init(wrappedValue: T) { | |
self.wrappedValue = wrappedValue | |
} | |
func hash(into hasher: inout Hasher) {} | |
View Loader.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
class Loader<Output>: ObservableObject { | |
@Published var isLoding: Bool = false | |
@Published var error: Swift.Error? | |
@Published var value: Output? | |
private var observer: AnyCancellable? | |
func load<P: Publisher>(_ publisher: P) where P.Output == Output { | |
self.observer?.cancel() | |
View Lock.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 Lock { | |
private var unfairLock = os_unfair_lock_s() | |
mutating func lock(_ work: () -> Void) { | |
self.lock() | |
work() | |
self.unlock() | |
} | |
mutating func trylock(_ work: () -> Void) -> Bool { |
NewerOlder