Last active
November 15, 2021 16:43
-
-
Save karigrooms/49dfd30fe95500f1c8eb29a0e1c038bb to your computer and use it in GitHub Desktop.
Blog: ObservableObjects and Protocols - Protocol with Published properties
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 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