Skip to content

Instantly share code, notes, and snippets.

View codeactual's full-sized avatar

David Smith codeactual

  • Found Apparatus
  • Portland, OR
View GitHub Profile
@codeactual
codeactual / BetterTapGesture.swift
Created August 12, 2022 13:37 — forked from fluidpixel/BetterTapGesture.swift
Using SwiftUI DragGesture as a tap, as TapGesture doesn't give location (as of beta6)
struct ContentView: View {
@State var moved: CGFloat = 0
@State var startTime: Date?
var body: some View {
//0 means that it acts like a press
//coordinateSpace local means local to the view its added to
let tap = DragGesture(minimumDistance: 0, coordinateSpace: .local)
.onChanged { value in
//store distance the touch has moved as a sum of all movements
@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
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)