-
-
Save DenTelezhkin/ca98f057cd0c62c9a9f309274f3302a9 to your computer and use it in GitHub Desktop.
import SwiftUI | |
protocol ViewModelContainable: View { | |
associatedtype ViewModel : ObservableObject | |
init(model: ViewModel) | |
} | |
// This struct is a direct MVVM alternative to @StateObject in iOS 14 and Mac OS Big Sur. | |
struct StateWrapped<U: ViewModelContainable, T> : View | |
where U.ViewModel == T | |
{ | |
@State var model: T | |
var body : some View { | |
U(model: model) | |
} | |
} |
Hi Denys!
I gave your gist a try in the following way:
struct FooView: ViewModelContainable {
@ObservedObject var model: FooViewModel
init(model: FooViewModel) {
self.model = model
}
var body: some View {
ZStack {
model.color.edgesIgnoringSafeArea(.all)
Button("Show sheet") {
model.isShowingSheet = true
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
model.color = .yellow
}
}
}
.sheet(isPresented: $model.isShowingSheet) {
StateWrapped<BarView, BarViewModel>(model: .init())
}
}
}
class FooViewModel: ObservableObject {
@Published var color = Color.red
@Published var isShowingSheet = false {
didSet {
if !isShowingSheet {
color = .red
}
}
}
}
struct BarView: ViewModelContainable {
@ObservedObject var model: BarViewModel
init(model: BarViewModel) {
self.model = model
}
var body: some View {
model.color.edgesIgnoringSafeArea(.all)
}
}
class BarViewModel: ObservableObject {
init() {
print("New BarViewModel")
}
@Published var color = Color.green
}
Unfortunately, I still see "New BarViewModel"
twice, which doesn't happen using @StateObject
... Am I missing something?
@cicerocamargo Yep, that's unfortunate :( This trick seems to work only for root object in view hierarchy. SwiftUI does not understand that BarViewModel
does not need to be recreated when using this approach.
The only workaround that comes in my mind is that you can create BarViewModel inside FooViewModel and push it down to BarView when it's needed:
.sheet(isPresented: $model.isShowingSheet) {
BarView(model: model.barViewModel)
}
That's not pretty, and will lead to immediate creation of BarViewModel
, instead of delaying it to actual sheet presentation, but I don't see another option here. Seems that we need StateObject
to do better.
If you just want a propertyWrapper that simply replaces @StateObject: https://gist.github.com/Amzd/8f0d4d94fcbb6c9548e7cf0c1493eaff
Thanks! Relevant discussion: https://twitter.com/dentelezhkin/status/1308774256050941954?s=21