Skip to content

Instantly share code, notes, and snippets.

View BrentMifsud's full-sized avatar

Brent Mifsud BrentMifsud

View GitHub Profile
@BrentMifsud
BrentMifsud / AsyncSequence+AsyncStreamInitializer.swift
Last active July 24, 2023 20:34
Convenience methods for converting any async sequence into an async stream
fileprivate extension AsyncStream {
init<Base: AsyncSequence>(from sequence: Base, file: StaticString = #filePath, line: UInt = #line) where Element == Base.Element {
var iterator = sequence.makeAsyncIterator()
// FIXME: In later swift versions, AsyncSequence protocol will likely have an associated error type.
// FIXME: For now, produce an assertionFailure to let developer know to use an AsyncThrowingStream instead.
self.init {
do {
return try await iterator.next()
} catch {
assertionFailure("warning: Base AsyncSequence threw an error. Use AsyncThrowingStream instead", file: file, line: line)
@BrentMifsud
BrentMifsud / Flow.swift
Last active December 18, 2023 22:19
A view that renders its children in a flow layout
import SwiftUI
/// A layout that presents its children in a flow layout
/// Thanks to [objc.io](https://talk.objc.io/episodes/S01E308-the-layout-protocol?t=489) for the starting point
/// Added some additional changes here to support view spacing and resizing of subviews that are larger than the container.
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
public struct FlowLayout: Layout {
private let spacing: CGFloat
public init(spacing: CGFloat = 8) {
@BrentMifsud
BrentMifsud / Image+Data.swift
Last active March 14, 2024 16:19
SwiftUI Image from data
import Foundation
import SwiftUI
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif
extension Image {
/// Initializes a SwiftUI `Image` from data.