Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Last active October 5, 2023 20:25
Show Gist options
  • Save mbrandonw/65210ea93273c18165e1d62b9b67121d to your computer and use it in GitHub Desktop.
Save mbrandonw/65210ea93273c18165e1d62b9b67121d to your computer and use it in GitHub Desktop.
import SwiftUI
@Observable
class CounterModel {
var count = 0
}
struct CounterView: View {
let model: CounterModel
var body: some View {
Text(self.model.count.description)
Button("Increment") { self.model.count += 1 }
}
}
enum Enum {
case counter(CounterModel)
}
@Observable
class ContentModel {
var state = Enum.counter(CounterModel())
}
struct ContentView: View {
let model = ContentModel()
var body: some View {
Form {
switch self.model.state {
case let .counter(counterModel):
CounterView(model: counterModel)
}
Button("Reset") {
self.model.state = .counter(CounterModel())
}
}
}
}
#Preview {
ContentView()
}
import SwiftUI
@Observable
class CounterModel {
var count = 0
}
struct CounterView: View {
let model: CounterModel
var body: some View {
Text(self.model.count.description)
Button("Increment") { self.model.count += 1 }
}
}
enum Enum {
case counter(CounterModel)
}
struct ContentView: View {
@State var state = Enum.counter(CounterModel())
var body: some View {
Form {
switch self.state {
case let .counter(counterModel):
CounterView(model: counterModel)
}
Button("Reset") {
self.state = .counter(CounterModel())
}
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment