Skip to content

Instantly share code, notes, and snippets.

View hmlongco's full-sized avatar

Michael Long hmlongco

View GitHub Profile
@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
}
}
}
@hmlongco
hmlongco / Filtered.swift
Created October 18, 2022 15:16
Filtered Example
let tableViewDataSource = users
.filter { $0.age > 21 && $0.name != "Simon" && $0.phone.contains("iPhone") }
.map { ViewModel(cellTitle: $0.name, cellSubtitle: $0.phone) }
@hmlongco
hmlongco / CoreDataExtensions.swift
Last active October 12, 2022 18:05
Core Data Extensions for SwiftUI
class RingEditViewModel: ObservableObject {
@NestedObservableObject var ring: RingData
private let isEditing: Bool
private let temporaryContext: NSManagedObjectContext
init(context: NSManagedObjectContext, ring: RingData? = nil) {
self.temporaryContext = context.child()
if let ring = temporaryContext.copy(of: ring) {
@hmlongco
hmlongco / Subscriptions.swift
Created August 27, 2022 18:58
Subscriptions addition to Combine
//
// Combine+Extensions.swift
// Common
//
// Created by Michael Long on 8/27/22.
//
import Foundation
import Combine
@hmlongco
hmlongco / ExternalProperties.swift
Last active August 5, 2022 00:44
External Properties
struct ToDo: Codable, Identifiable {
let id: Int
let userId: Int
var title: String
var completed: Bool
}
class SelectionManager<Item: Identifiable>: ObservableObject {
@Published var status: [Item.ID:Bool] = [:]
func isSelected(_ item: Item) -> Bool {