Can @StateObject be injected using Resolver?
I have the following:
struct FooView: View {
@StateObject private var viewModel: FooViewModel
some code
}
protocol FooViewModel: ObservableObject {
var someValue: String { get }
func someRequest()
}
class FooViewModelImpl {
some code
}
I would like to inject FooViewModel into FooView using Resolver but have been struggling as Resolver wants to use the @Inject annotation and of course, I need the @StateObject annotation but I cannot seem to use both. Are @StateObject not able to be injected using some Dependency Injection framework like Resolver? I have not found any examples where developers have used DI in this approach.
The latest version of Resolver supports @InjectedObject
property wrapper for ObservableObjects. This wrapper is meant for use in SwiftUI Views and exposes bindable objects similar to that of SwiftUI @ObservedObject and @EnvironmentObject.
I am using it a lot now and its very cool feature.
eg:
class AuthService: ObservableObject { @Published var isValidated = false } class LoginViewModel: ObservableObject { @InjectedObject var authService: AuthService }
Note: Dependent service must be of type ObservableObject. Updating object state will trigger view update.