Last active
March 14, 2021 09:21
-
-
Save waterlou/a1cb0cde00080b86fdf6d5ced049b29c to your computer and use it in GitHub Desktop.
002-stockpricetext
This file contains hidden or 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
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