Skip to content

Instantly share code, notes, and snippets.

View hmlongco's full-sized avatar

Michael Long hmlongco

View GitHub Profile
@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 {
@hmlongco
hmlongco / WrappedCodable.swift
Created August 4, 2022 20:00
WrappedCodable.swift
struct ToDoStore: Codable {
let userId: Int
let id: Int
var title: String
var completed: Bool
}
struct ToDo {
private var toDo: ToDoStore
@hmlongco
hmlongco / NameValueView.swift
Created June 19, 2022 21:08
Builder: NameValueView
struct NameValueView: ViewBuilder {
let name: String?
let value: String?
var body: View {
HStackView {
LabelView(name)
.color(.secondaryLabel)
SpacerView()
@hmlongco
hmlongco / LabeledPhotoView.swift
Created June 19, 2022 21:07
Builder: LabeledPhotoView
struct LabeledPhotoView: ViewBuilder {
let photo: Observable<UIImage?>
let name: String
var body: View {
ZStackView {
ImageView(photo)
.contentMode(.scaleAspectFill)
.clipsToBounds(true)
@hmlongco
hmlongco / DetailCardView.swift
Created June 19, 2022 21:05
Builder: DetailCardView
struct DetailCardView: ViewBuilder {
@Injected var viewModel: DetailViewModel
init(user: User) {
viewModel.configure(user)
}
var body: View {
StandardCardView {
@hmlongco
hmlongco / Themes.swift
Last active January 10, 2023 06:54
Themes for SwiftUI
struct Colors {
let primary: Color
}
struct Fonts {
let body: Font
}
class Theme: ObservableObject {
let color: Colors
@hmlongco
hmlongco / LoadingView.swift
Last active December 20, 2021 17:15
LoadingView state dispatch
struct LoadingView: View {
@StateObject var viewModel = ViewModel()
var body: some View {
switch viewModel.state {
case .initial, .loading:
ProgressView()
.onAppear(perform: viewModel.load)
case .empty(let message):