Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karigrooms/49dfd30fe95500f1c8eb29a0e1c038bb to your computer and use it in GitHub Desktop.
Save karigrooms/49dfd30fe95500f1c8eb29a0e1c038bb to your computer and use it in GitHub Desktop.
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 }
var pricePublisher: Published<Price?>.Publisher { get }
func fetch()
}
class PricingProvider: PriceRequesting {
@Published private(set) var price: Price?
var priceValue: Published<Price?> {
return _price
}
var pricePublisher: Published<Price?>.Publisher {
return $price
}
init(price initialValue: Price? = nil) {
self.price = initialValue
}
func fetch() {
// TODO: fetch pricing from a service
let price = Int.random(in: 42...150)
self.price = Price(amount: "$\(price)", description: "avg/night")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment