Skip to content

Instantly share code, notes, and snippets.

View codeactual's full-sized avatar

David Smith codeactual

  • Found Apparatus
View GitHub Profile
struct OverflowLayout: Layout {
var spacing = CGFloat(10)
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
let containerWidth = proposal.replacingUnspecifiedDimensions().width
let sizes = subviews.map { $0.sizeThatFits(.unspecified) }
return layout(sizes: sizes, containerWidth: containerWidth).size
}
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
//
// SFSymbolImage.swift
// SFSymbolVariableValueAnimationWrong
//
// Created by Matthew Young on 12/22/22.
//
import SwiftUI
struct AnimatableVariableValueModifier: Animatable, ViewModifier {
@bjhomer
bjhomer / cross-view-lines.swift
Last active November 5, 2022 05:31
Creating cross-view lines in SwiftUI
//
// ContentView.swift
// SwiftUIPlayground
//
// Created by BJ Homer on 4/26/21.
//
import SwiftUI
import UIKit
class ViewController: UIViewController {
let textViewWrapper = TextViewWrapper()
override func viewDidLoad() {
super.viewDidLoad()
// The problem view! I want to add this one to my set of Auto Layout constraints,
// even though it's not using Auto Layout internally.
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()
})
}
@manmal
manmal / TaskAutoCancellation.swift
Last active July 27, 2022 20:52
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 {
@manmal
manmal / AsyncSequenceExtensions.swift
Created July 6, 2022 16:57
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 Foundation
#if canImport(Cocoa)
import Cocoa
#elseif canImport(UIKit)
import UIKit
#endif
public struct EdgeInsets {
var top, bottom, leading, trailing: CGFloat
@KaneCheshire
KaneCheshire / AnyTask.swift
Last active August 22, 2024 18:31
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.
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),