Last active
October 5, 2023 20:25
-
-
Save mbrandonw/65210ea93273c18165e1d62b9b67121d 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 | |
@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() | |
} |
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 | |
@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