Last active
November 15, 2021 16:44
-
-
Save karigrooms/af72e7d37c3daf4e2a6b088a41cb575e to your computer and use it in GitHub Desktop.
Blog: ObservableObjects and Protocols - Typical usage of ObservableObject in SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2021 Expedia, Inc. | |
// SPDX-License-Identifier: Apache-2.0 | |
import SwiftUI | |
struct PriceView: View { | |
@ObservedObject var pricing: PricingProvider | |
var body: some View { | |
Group { | |
if let price = pricing.price { | |
Text(price.amount).bold() + Text(" \(price.description)").font(.system(size: 12)) | |
} else { | |
Text("Price not available") | |
.italic() | |
.foregroundColor(.gray) | |
} | |
} | |
.font(.system(size: 16)) | |
} | |
} | |
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