Skip to content

Instantly share code, notes, and snippets.

@waterlou
Last active March 14, 2021 09:21
Show Gist options
  • Save waterlou/a1cb0cde00080b86fdf6d5ced049b29c to your computer and use it in GitHub Desktop.
Save waterlou/a1cb0cde00080b86fdf6d5ced049b29c to your computer and use it in GitHub Desktop.
002-stockpricetext
struct StockPriceText: View {
class Context: ObservableObject {
var lastPrice: Double?
@Published var color: Color = Color.black
}
var price: Double
@StateObject var context = Context()
var body: some View {
if let lastPrice = context.lastPrice {
if price > lastPrice {
context.color = .green
}
else if price < lastPrice {
context.color = .red
}
}
context.lastPrice = price
return Text(String(price))
.foregroundColor(context.color)
}
init(price: Double) {
self.price = price
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment