Skip to content

Instantly share code, notes, and snippets.

View hmlongco's full-sized avatar

Michael Long hmlongco

View GitHub Profile
@hmlongco
hmlongco / WithTableView.swift
Created July 31, 2021 15:07
With in tableviews
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch section {
case 0:
return tableView.dequeueCell(CELLID, for: indexPath) { (_ cell: MyTableViewCell) in
cell.configure(viewModel.config((for: indexPath.section))
}
case 1:
...
}
}
@hmlongco
hmlongco / WithTableViewExtension.swift
Last active August 1, 2021 18:15
The tableview extension
extension UITableView {
func dequeueCell<Cell:UITableViewCell>(_ id: String, for indexPath: IndexPath, configure: (_ cell: Cell) -> Void) -> UITableViewCell {
let cell = dequeueReusableCell(withIdentifier: id, for: indexPath)
if let cell = cell as? Cell {
configure(cell)
}
return cell
}
@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):
@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 / 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 / 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 / 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 / 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 / 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 / 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