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
import Foundation
#if canImport(Cocoa)
import Cocoa
#elseif canImport(UIKit)
import UIKit
#endif
public struct EdgeInsets {
var top, bottom, leading, trailing: CGFloat
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),
@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 {
@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 / 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 / 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 / CropVideo.m
Created August 24, 2022 15:57 — forked from zrxq/CropVideo.m
Crop video to square with a specified side respecting the original video orientation
@import MobileCoreServices;
@import AVFoundation;
@import AssetsLibrary;
// ...
- (void)cropVideoAtURL:(NSURL *)videoURL toSquareWithSide:(CGFloat)sideLength completion:(void(^)(NSURL *resultURL, NSError *error))completionHander {
/* asset */
@codeactual
codeactual / cross-view-lines.swift
Created September 15, 2022 15:15 — forked from bjhomer/cross-view-lines.swift
Creating cross-view lines in SwiftUI
//
// ContentView.swift
// SwiftUIPlayground
//
// Created by BJ Homer on 4/26/21.
//
import SwiftUI
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()
})
}
@codeactual
codeactual / UIScrollViewWrapper.swift
Created November 11, 2022 20:32 — forked from timothycosta/UIScrollViewWrapper.swift
UIScrollView wrapped for SwiftUI
//
// UIScrollViewWrapper.swift
// lingq-5
//
// Created by Timothy Costa on 2019/07/05.
// Copyright © 2019 timothycosta.com. All rights reserved.
//
import SwiftUI