Skip to content

Instantly share code, notes, and snippets.

View codeactual's full-sized avatar

David Smith codeactual

  • Found Apparatus
View GitHub Profile
@codeactual
codeactual / AnyTask.swift
Created July 27, 2022 20:53 — forked from KaneCheshire/AnyTask.swift
Type-erased Swift Task that cancels itself on deinit
/// 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.
@codeactual
codeactual / TaskAutoCancellation.swift
Created July 27, 2022 20:52 — forked from manmal/TaskAutoCancellation.swift
Swift Structured Concurrency - Task Auto Cancellation
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 {
@codeactual
codeactual / AsyncSequenceExtensions.swift
Created July 26, 2022 20:24 — forked from manmal/AsyncSequenceExtensions.swift
AsyncSequence.eraseToAsyncStream()
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 {
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),
import Foundation
#if canImport(Cocoa)
import Cocoa
#elseif canImport(UIKit)
import UIKit
#endif
public struct EdgeInsets {
var top, bottom, leading, trailing: CGFloat
import SwiftUI
extension CGPoint {
static func *(lhs: Self, rhs: CGFloat) -> Self {
.init(x: lhs.x * rhs, y: lhs.y * rhs)
}
}
// Idea: https://www.framer.com/showcase/project/lo2Qka8jtPXrjzZaPZdB/
//
// 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?
// }
//
@codeactual
codeactual / AsyncWaiter.swift
Created May 16, 2022 16:48 — forked from krzyzanowskim/AsyncWaiter.swift
Synchronously (well) wait for async Task value update
/// 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
@codeactual
codeactual / PlatformVisibility.swift
Created May 3, 2022 20:31 — forked from phranck/PlatformVisibility.swift
A Swift view modifier to handle visibility of views for specific platforms
//
// 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)
@codeactual
codeactual / StringGetSizeThatFits.swift
Created April 10, 2022 19:43 — forked from krzyzanowskim/StringGetSizeThatFits.swift
Calculate frame of String, that fits given width
// Excerpt from https://github.com/krzyzanowskim/CoreTextWorkshop
// Licence BSD-2 clause
// Marcin Krzyzanowski marcin@krzyzanowskim.com
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)