Skip to content

Instantly share code, notes, and snippets.

View samsonjs's full-sized avatar

Sami Samhuri samsonjs

View GitHub Profile
@samsonjs
samsonjs / AsyncMonitorTask.swift
Created April 19, 2025 22:04
Cool exploration but it doesn't work unless everything is sendable, and AsyncSequence and AsyncStream don't seem to be sendable
import Foundation
public final class AsyncMonitorTask: Hashable {
let task: Task<Void, Never>
init<Element: Sendable>(
sequence: sending any AsyncSequence<Element, Never>,
performing block: @escaping @MainActor (Element) async -> Void
) {
self.task = Task {
@samsonjs
samsonjs / gist:d2973f1c61014671601b442ca7eb8d97
Created March 8, 2025 18:17
Dynamic SwiftUI view that pins a header above a scroll view's contents, except at larger dynamic type sizes where it makes the header part of the scrolling content.
import SwiftUI
/// Dynamic SwiftUI view that pins a header above a scroll view's contents, except at larger dynamic type sizes where it makes the header part of the scrolling content.
struct DynamicTypePinnedHeaderView<Header: View, Footer: View, Content: View>: View {
let inlineThreshold: DynamicTypeSize
let header: Header
let footer: Footer
@samsonjs
samsonjs / NavigationFreeze.swift
Created December 1, 2024 22:22
SwiftUI navigation freeze when defining openURL and using a property or function to build a view
//
// ContentView.swift
// NavigationFreeze
//
// Created by Sami Samhuri on 2024-12-01.
//
import SwiftUI
struct ContentView: View {
@samsonjs
samsonjs / log.txt
Created September 27, 2024 17:41
AVFoundation error on spatial videos
Failed to append audio sample buffer CMSampleBuffer 0x1360948c0 retainCount: 3 allocator: 0x1fc343380
invalid = NO
dataReady = YES
makeDataReadyCallback = 0x0
makeDataReadyRefcon = 0x0
formatDescription = <CMAudioFormatDescription 0x30353c420 [0x1fc343380]> {
mediaType:'soun'
mediaSubType:'lpcm'
mediaSpecific: {
ASBD: {
@samsonjs
samsonjs / ContentView.swift
Created September 17, 2024 02:25
FB15150266: PhotosPicker label closure issue with Xcode 16.0
import PhotosUI
import SwiftUI
enum SFSymbol: String {
case ant
}
extension Label where Title == Text, Icon == Image {
init(_ titleKey: LocalizedStringKey, systemSymbol: SFSymbol) {
self.init(titleKey, systemImage: systemSymbol.rawValue)
@samsonjs
samsonjs / gross.swift
Last active September 14, 2024 20:27
Work-around for nonisolated PhotosPicker initializer in Xcode 16.0 RC
import PhotosUI
import SwiftUI
private final class SendableWrapper<T>: @unchecked Sendable {
private var unsafeValue: T
private let lock = NSLock()
var value: T {
get {
@samsonjs
samsonjs / SampleWriter.swift
Created July 11, 2024 06:27
Task cancellation in a dispatch queue world of AVAssetWriterInputs
actor SampleWriter {
// ...
var isCancelled = false
// ...
func cancel() async {
isCancelled = true
}
private func encodeAudioTracks() async {
@samsonjs
samsonjs / ExportSession.swift
Created July 8, 2024 15:54
Attempting to send non-Sendable AVFoundation types in a safe way
public import AVFoundation
struct UniqueRef<Value>: ~Copyable, @unchecked Sendable {
private let lock = NSLock()
private var unsafeValue: Value?
init(value: sending Value) {
self.unsafeValue = value
}
@samsonjs
samsonjs / ContentView.swift
Created July 2, 2024 21:22
How to fix a Swift 6 warning: "Sending main actor-isolated value of type 'WebAuthenticationSession' with later accesses to nonisolated context risks causing data races; this is an error in the Swift 6 language mode"
// https://iosdev.space/@andy/112714337582288785
import AuthenticationServices
import SwiftUI
struct ContentView: View {
@State var url = URL(string: "https://example.net")!
@Environment(\.webAuthenticationSession) var webAuthenticationSession
@samsonjs
samsonjs / VFAExportSession.swift
Last active June 29, 2024 05:10
Exploring possible modern APIs for something like AVAssetExportSession
// Old way
let session = VFAExportSession(asset: asset)
session.timeRange = CMTimeRange(start: .seconds(1), duration: .seconds(3))
session.audioMix = audioMix
session.audioOutputConfiguration = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVNumberOfChannelsKey: NSNumber(value: 2),
AVSampleRateKey: NSNumber(value: 44_100.0),
]
session.videoComposition = videoComposition