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
/// A type-erased task that you can store in a collection | |
/// to allow you to cancel at a later date. | |
/// | |
/// Upon deinit of the task, the task will be cancelled | |
/// automatically. Similar to Combine's AnyCancellable. | |
final class AnyTask { | |
/// Call this cancellation block to cancel the task manually. | |
let cancel: () -> Void | |
/// Checks whether the task is cancelled. |
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 extension Task { | |
/// Cancels this `Task` when the surrounding `Task` is cancelled. | |
/// This is necessary if `Task {}` and `Task.detached {}` | |
/// should be automatically cancelled - otherwise, such Tasks | |
/// just run until finished. | |
/// | |
/// Usage: | |
/// | |
/// await Task { await myAsyncFunc() }.autoCancel() | |
func autoCancel() async -> Void { |
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 | |
// Props to @pteasima and @MichalKleinJr: | |
// https://twitter.com/pteasima/status/1544723987606929408?s=21&t=JL1oIuL87Ms_VPBQBZQ7Rg | |
public extension AsyncSequence { | |
func eraseToAsyncStream() -> AsyncStream<Element> { | |
return AsyncStream { continuation in | |
let task = Task { | |
do { | |
for try await value in self { |
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 | |
import Combine | |
struct ContentView: View { | |
var body: some View { | |
TaskList(tasks: [ | |
Task(id: 1, title: "Task 1", isCompleted: false), | |
Task(id: 2, title: "Task 2", isCompleted: false), | |
Task(id: 3, title: "Task 3", isCompleted: true), |
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 | |
#if canImport(Cocoa) | |
import Cocoa | |
#elseif canImport(UIKit) | |
import UIKit | |
#endif | |
public struct EdgeInsets { | |
var top, bottom, leading, trailing: CGFloat |
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
// | |
// A Swift property wrapper for adding "indirect" to struct properties. | |
// Enum supports this out of the box, but for some reason struct doesn't. | |
// | |
// This is useful when you want to do something recursive with structs like: | |
// | |
// struct Node { | |
// var next: Node? | |
// } | |
// |
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
/// Wait for async operation to return value and call callback with the value | |
/// This class is intended to workaround/simplify async/await + actors isolation | |
/// https://twitter.com/krzyzanowskim/status/1523233140914876416 | |
private class AsyncWaiter<T> { | |
var didReceiveValue: Bool = false | |
let value: (T) -> Void | |
let operation: () async throws -> T | |
init(_ value: @escaping (T) -> Void, operation: @escaping () async throws -> T) { | |
self.value = 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
// | |
// Created by Frank Gregor on 19.04.22. | |
// | |
import SwiftUI | |
public struct Platform: OptionSet { | |
public var rawValue: UInt8 | |
public static let iOS: Platform = Platform(rawValue: 1 << 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
// Excerpt from https://github.com/krzyzanowskim/CoreTextWorkshop | |
// Licence BSD-2 clause | |
// Marcin Krzyzanowski [email protected] | |
func getSizeThatFits(_ attributedString: NSAttributedString, maxWidth: CGFloat) -> CGSize { | |
let framesetter = CTFramesetterCreateWithAttributedString(attributedString) | |
let rectPath = CGRect(origin: .zero, size: CGSize(width: maxWidth, height: 50000)) | |
let ctFrame = CTFramesetterCreateFrame(framesetter, CFRange(), CGPath(rect: rectPath, transform: nil), nil) |