Created
April 15, 2024 18:11
-
-
Save christianselig/2ae2e42d5edc3ffab513f5d7af72d740 to your computer and use it in GitHub Desktop.
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
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
IceCreamView(viewModel: ViewModel(iceCream: "banana")) | |
} | |
} | |
struct IceCreamView: View { | |
@State var viewModel: ViewModel | |
var body: some View { | |
VStack { | |
Text("Flavor: \(viewModel.iceCream)") | |
// This random number changes every 1 second | |
// and ideally it wouldn't | |
Text("Enjoy your ice cream! \(arc4random_uniform(9999999))") | |
} | |
.overlay { | |
// It changes due to this overlay | |
Text("Ordered \(viewModel.secondsSinceCreation) second(s) ago") | |
.padding(.top, 100) | |
} | |
} | |
} | |
@Observable class ViewModel { | |
let iceCream: String | |
var secondsSinceCreation = 0 | |
init(iceCream: String) { | |
self.iceCream = iceCream | |
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in | |
self.secondsSinceCreation += 1 | |
print("Called") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment