Skip to content

Instantly share code, notes, and snippets.

View hmlongco's full-sized avatar

Michael Long hmlongco

View GitHub Profile
@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)
@hmlongco
hmlongco / ClearSO.swift
Created February 25, 2024 15:54
Clearing StateObject with ID
struct ChildStateDemo: View {
@State var account = 1
var body: some View {
VStack(spacing: 20) {
SubView(id: account)
.id(account) // required to change subview state
Button("Account 1") {
account = 1
}
@hmlongco
hmlongco / IDState.swift
Last active February 24, 2024 18:34
ID and State
struct ParentView: View {
@State var id = UUID().uuidString
var body: some View {
VStack(spacing: 20) {
VStack {
Text("Parent View")
Text(id).font(.footnote)
}
ChildView(name: "Child Maintains State On Update")
ChildView(name: "Child Loses State On Update")
@hmlongco
hmlongco / AnyViewTest.swift
Created September 4, 2023 00:22
AnyViewTest code for AnyView article on Medium.
import SwiftUI
class ContentModel: ObservableObject {
@Published var count: Int = 0
let items10 = (0 ..< 10).map { Item(id: $0 + 1) }
let items100 = (0 ..< 100).map { Item(id: $0 + 1) }
let items10K = (0 ..< 10_000).map { Item(id: $0 + 1) }
let items100K = (0 ..< 100_000).map { Item(id: $0 + 1) }
}
@hmlongco
hmlongco / ShopViewModel.swift
Created July 14, 2023 00:24
ShopViewModel.swift
// Classic
class ShopViewModel: ObservableObject {
@Published private(set) var products: [Products]
init(products: [Products] = []) {
self.products = products
}
public func add(product) {
products.append(product)
}
@hmlongco
hmlongco / DeinitTracking.swift
Created January 9, 2023 22:57
DeinitTracking
class DeinitTracker {}
class Test {
init() {}
#if DEBUG
lazy var deinitTracker: DeinitTracker? = DeinitTracker()
#endif
}
@hmlongco
hmlongco / Binding+defaultValue.swift
Created November 10, 2022 19:11
Binding+defaultValue
extension Binding {
public func defaultValue<T>(_ value: T) -> Binding<T> where Value == Optional<T> {
Binding<T> {
wrappedValue ?? value
} set: {
wrappedValue = $0
}
}
}
@hmlongco
hmlongco / Binding+variable.swift
Created November 10, 2022 19:09
Binding+variable
extension Binding {
public static func variable(_ value: Value) -> Binding<Value> {
var state = value
return Binding<Value> {
state
} set: {
state = $0
}
}
}