Skip to content

Instantly share code, notes, and snippets.

View karigrooms's full-sized avatar

Kari Grooms karigrooms

View GitHub Profile
@karigrooms
karigrooms / swiftui-card-view-3.swift
Created February 23, 2021 23:46
Blog post: SwiftUI with UICollectionView - 12. Update SwiftUI Card to adapt to its container size
import SwiftUI
protocol CardContent {
var imageName: String { get }
var title: String { get }
var description: String { get }
}
struct Card: View {
@karigrooms
karigrooms / swiftui-card-view-final.swift
Created February 23, 2021 23:50
Blog post: SwiftUI with UICollectionView - 13. Add Spacer() to vertically align to the top of its container
import SwiftUI
protocol CardContent {
var imageName: String { get }
var title: String { get }
var description: String { get }
}
struct Card: View {
@karigrooms
karigrooms / swiftui-card-view-with-state.swift
Created February 24, 2021 00:10
Blog post: SwiftUI with UICollectionView - 14. Do not use @State inside of a SwiftUI View embedded in a UICollectionViewCell
import SwiftUI
struct Card: View {
typealias Content = CardContent
let content: Content
@State var isHearted: Bool = false
@karigrooms
karigrooms / swiftui-card-view-with-environmentobject.swift
Created February 24, 2021 00:21
Blog post: SwiftUI with UICollectionView - 15. Use @EnvironementObject instead of @State when embedding SwiftUI Views inside of UICollectionViewCell
// MARK: Card
protocol CardContent {
var id: String { get }
var imageName: String { get }
var title: String { get }
var description: String { get }
}
struct Card: View {
@karigrooms
karigrooms / swiftui-uicollectionview-4.swift
Last active February 24, 2021 00:45
Blog post: SwiftUI with UICollectionView - 16. Present the property details when a property card is tapped
// MARK: UICollectionViewDelegate
extension CollectionViewExample: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = items[indexPath.row]
let pdp = PropertyDetailsViewController(property: item)
pdp.modalPresentationStyle = .formSheet
present(pdp, animated: true)
}
@karigrooms
karigrooms / swiftui-uicollectionviewcell-with-uibutton.swift
Created February 24, 2021 00:46
Blog post: SwiftUI with UICollectionView - 17. UIButton positioned on top of the SwiftUI View
import SwiftUI
import UIKit
class MyCollectionViewCell: SwiftUICollectionViewCell<Card> {
static var reuseIdentifier = "MyCollectionViewCell"
typealias Content = Card.Content
lazy var heartButton: UIHeartButton = {
@karigrooms
karigrooms / blog--observableobject+protocols--typical-usage.swift
Last active November 15, 2021 16:44
Blog: ObservableObjects and Protocols - Example ObservableObject
// Copyright 2021 Expedia, Inc.
// SPDX-License-Identifier: Apache-2.0
import Combine
struct Price {
let amount: String // "$42"
let description: String // "avg/night"
}
@karigrooms
karigrooms / blog--observableobject+protocols--typical-usage-view.swift
Last active November 15, 2021 16:44
Blog: ObservableObjects and Protocols - Typical usage of ObservableObject in SwiftUI
// Copyright 2021 Expedia, Inc.
// SPDX-License-Identifier: Apache-2.0
import SwiftUI
struct PriceView: View {
@ObservedObject var pricing: PricingProvider
var body: some View {
@karigrooms
karigrooms / blog--observableobject+protocols--protocol-no-published.swift
Last active November 15, 2021 16:43
Blog: ObservableObjects and Protocols - Protocol with @published
// Copyright 2021 Expedia, Inc.
// SPDX-License-Identifier: Apache-2.0
import Combine
protocol PriceRequesting: ObservableObject {
var price: Price? { get }
func fetch()
@karigrooms
karigrooms / blog--observableobject+protocols--protocol-published.swift
Last active November 15, 2021 16:43
Blog: ObservableObjects and Protocols - Protocol with Published properties
// Copyright 2021 Expedia, Inc.
// SPDX-License-Identifier: Apache-2.0
import Combine
protocol PriceRequesting: ObservableObject {
var price: Price? { get }
var priceValue: Published<Price?> { get }