Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karigrooms/733bcf6ccf0fe6cd7c05f98bd204a4f4 to your computer and use it in GitHub Desktop.
Save karigrooms/733bcf6ccf0fe6cd7c05f98bd204a4f4 to your computer and use it in GitHub Desktop.
Blog: ObservableObjects and Protocols - Protocol in SwiftUI
// Copyright 2021 Expedia, Inc.
// SPDX-License-Identifier: Apache-2.0
import SwiftUI
struct PriceView: View {
@State private var price: Price?
let pricing: PriceRequesting
var body: some View {
Group {
if let price = price {
Text(price.amount).bold() + Text(" \(price.description)").font(.system(size: 12))
} else {
Text("Price not available")
.italic()
.foregroundColor(.gray)
}
}
.font(.system(size: 16))
.onReceive(pricing.pricePublisher, perform: { price in
self.price = price
})
}
}
struct Demo: View {
@StateObject private var pricing = PricingProvider()
var body: some View {
VStack(alignment: .center, spacing: 8) {
PriceView(pricing: pricing)
Button("Fetch price", action: {
pricing.fetch()
})
.buttonStyle(PrimaryButtonStyle())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment