Skip to content

Instantly share code, notes, and snippets.

View juanarzola's full-sized avatar

Juan Arzola juanarzola

View GitHub Profile
import SwiftData
import CoreSpotlight
import FetchDescriptorObserver
import OSLog
import AppIntents
private let logger = Logger.spotlight
// Initialize from a SwiftData PersistentModel
protocol FromPersistentModel {
@juanarzola
juanarzola / FetchDescriptor+model.swift
Last active November 16, 2025 02:55
An extension that normally works, except from shortcuts, where uuid isn't found in the schema
extension FetchDescriptor {
public static func model<Model: PersistentModel & UUIDString>(
withUUIDString uuidString: String?
) -> FetchDescriptor<Model> {
guard let uuidString else { return .init(predicate: .false) }
let pred = #Predicate<Model> { $0.uuid == uuidString }
var desc = FetchDescriptor<Model>(predicate: pred)
desc.fetchLimit = 1
return desc
}
TARGET="[email protected]"; APP="/path/to/DerivedData/.../MyApp.app"; APP_NAME=$(basename "$APP"); rsync -avE --delete "$APP" "$TARGET:/Applications/" && ssh "$TARGET" "echo Remote host: \$(hostname); codesign --force --deep -s - /Applications/$APP_NAME && open -n /Applications/$APP_NAME"
public extension Task where Success == Void , Failure == Never {
// Suspends until the task is cancelled
static func cancellation() async -> Void {
let s = AsyncStream<Void>{ continuation in }
for await _ in s {
}
}
// EMITS INFINITE ELEMENTS CPU 100%
@juanarzola
juanarzola / EnvironmentDimmedTintColorViewModifier.swift
Created March 17, 2025 21:03
Exposes tintColor in the dimmed state to SwiftUI. Use this to adapt any non-accentColor color to the dimmed state.
//
// EnvironmentDimmedTintColorViewModifier.swift
// Learn
//
// Created by Juan Arzola on 3/17/25.
// Copyright © 2025 Juan Arzola. All rights reserved.
//
import SwiftUI
import UIKit
@juanarzola
juanarzola / KeyboardShortcutButtons.swift
Last active March 17, 2025 21:11
Overlay with buttons used just for triggering keyboard shortcuts
//
// KeyboardShortcutButtons.swift
// Learn
//
// Created by Juan Arzola on 3/15/25.
// Copyright © 2025 Juan Arzola. All rights reserved.
//
import SwiftUI
import UIKit
@juanarzola
juanarzola / PersistentIdentifier+shortDescription.swift
Created December 19, 2024 09:45
A super simple hack to get a short description of SwiftData's PersistentIdentifier suitable for debugging. Format: `{ModelName}/p{Number}`
import SwiftData
extension PersistentIdentifier {
var shortDebugDescription: String {
let description = "\(self)"
let entityNameRange = description.firstMatch(of: self.entityName)?.range
let lastParenthesisRange = description.firstMatch(of: ")")?.range
if let entityNameRange, let lastParenthesisRange,
entityNameRange.lowerBound < lastParenthesisRange.upperBound {
return String(description[entityNameRange.lowerBound..<lastParenthesisRange.lowerBound])
@juanarzola
juanarzola / ModelContainer+runNonisolated.swift
Last active August 20, 2024 23:37
Convenience extension for running ModelActor code in the background
extension ModelContainer {
// non-throwing version
nonisolated func runNonisolated<ResultType, ActorType: InitWithModelContainer>(
action: @Sendable (_ actor: ActorType) async -> ResultType
) async -> ResultType {
let actor = ActorType(modelContainer: self)
let result = await action(actor)
return result
}
// throwing version
@juanarzola
juanarzola / swift-data-request.swift
Last active June 18, 2024 06:17
Proposed query API for SwiftData notifications (stream based)
// Our one super simple SwiftData Model
@Model
class Item {
...
}
// MARK: - TodayView
@MainActor
@juanarzola
juanarzola / updateLoop.swift
Last active June 8, 2024 17:31
ViewModel (or controller(!)) updateLoop pattern that keeps the model up-to-date in a task.
// MARK: - TodayView
@MainActor
struct TodayView: View {
@State private var viewModel = TodayViewModel()
@Environment(\.modelContext) private var modelContext
var body: some View {
HStack {
switch viewModel.content {