Skip to content

Instantly share code, notes, and snippets.

View hmlongco's full-sized avatar

Michael Long hmlongco

View GitHub Profile
@hmlongco
hmlongco / Aligned.swift
Last active June 21, 2025 22:04
Aligned Releases
// present
@available(iOS 26.0, macOS 26.0, tvOS 26.0, watchOS 26.0, visionOS 26.0, *)
#available(iOS 26.0, macOS 26.0, tvOS 26.0, watchOS 26.0, visionOS 26.0, *)
// past
@available(iOS 18.0, macOS 15.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
#available(iOS 18.0, macOS 15.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, visionOS 1.0, *)
#available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, visionOS 1.0, *)
// demonstrate advantages of moving task from view to loading state
struct FeedView: View {
@Environment(BlueSkyClient.self) private var client
@Environment(AppTheme.self) private var theme
enum ViewState {
case loading
case error(String)
case loaded([Post])
@hmlongco
hmlongco / applyIf.swift
Created May 28, 2025 13:54
Conditional Views Bad Demonstration
//
// ContentView.swift
// ConditionalViews
//
// Created by Michael Long on 5/28/25.
//
import SwiftUI
@MainActor
class Test {
// can do this
func someTask1() {
let _ = ""
Task {
let _ = ""
let data = await doBackgroundTask()
let _ = data // Update UI with data
}
@hmlongco
hmlongco / gist:57181651ecd6a8415d3af3d07182ee21
Created March 29, 2025 18:17
ViewModel called from task
class MyViewModel: ObservableObject {
@Published var items: [Item] = []
@MainActor
func loadData() async {
items = await fetchData()
}
}
struct MyView: View {
@hmlongco
hmlongco / cancellation.swift
Created February 24, 2025 20:10
Task cancellation.swift
// original code, intermediate variable
class SomeViewModel1: ObservableObject {
@Published var searchResults: [String] = []
private var currentSearchTask: Task<Void, Never>?
@MainActor
func search(_ query: String) {
currentSearchTask?.cancel()
let newTask = Task {
do {
try await Task.sleep(for: .milliseconds(500))
@hmlongco
hmlongco / DecodingError.swift
Last active March 10, 2025 18:58
DecodingError.swift
// instead of "nested" try catches...
public func load() async throws -> [FeedItem] {
do {
let (data, response) = try await client.get(from: url)
guard let httpResponse = response as? HTTPURLResponse else {
throw Error.invalidData
}
guard httpResponse.statusCode == 200 else {
@hmlongco
hmlongco / GPT4.swift
Last active April 9, 2024 00:28
GTP4 Form
struct User {
var firstName: String
var lastName: String
var email: String
var phoneNumber: String
var street: String
var city: String
var state: String
var zipCode: String
}
struct Account: Identifiable {
let id = UUID().uuidString
}
struct SheetLaunchingDemoView: View {
@State var showSheet: Account?
var body: some View {
NavigationStack {
Button("Show Sheet") {
showSheet = Account()
@hmlongco
hmlongco / InterruptedTaskView.swift
Created February 25, 2024 23:48
Interrupting a Button
class InterruptedTaskViewModel: ObservableObject {
@Published var name = "Michael"
init() {
print("INIT")
}
@MainActor
func load() {
Task {
print("LOADING")
let _ = try? await Task.sleep(nanoseconds: 3 * NSEC_PER_SEC)