This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 { | |
| @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 |
This file contains hidden or 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 MobileCoreServices; | |
| @import AVFoundation; | |
| @import AssetsLibrary; | |
| // ... | |
| - (void)cropVideoAtURL:(NSURL *)videoURL toSquareWithSide:(CGFloat)sideLength completion:(void(^)(NSURL *resultURL, NSError *error))completionHander { | |
| /* asset */ |
This file contains hidden or 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
| // | |
| // ContentView.swift | |
| // SwiftUIPlayground | |
| // | |
| // Created by BJ Homer on 4/26/21. | |
| // | |
| import SwiftUI | |
This file contains hidden or 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() | |
| }) | |
| } |
This file contains hidden or 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
| // | |
| // UIScrollViewWrapper.swift | |
| // lingq-5 | |
| // | |
| // Created by Timothy Costa on 2019/07/05. | |
| // Copyright © 2019 timothycosta.com. All rights reserved. | |
| // | |
| import SwiftUI |