It seems to me that Apple is encouraging us to give up using UIViewController
in SwiftUI, but without using view controlelrs, I feel a little bit powerless. What I would like is to be able to implement some sort of ViewModel
which will emit events to View
.
ViewModel:
public protocol LoginViewModel: ViewModel {
var onError: PassthroughSubject<Error, Never> { get }
var onSuccessLogin: PassthroughSubject<Void, Never> { get }
}
View:
public struct LoginView: View {
fileprivate let viewModel: LoginViewModel
public init(viewModel: LoginViewModel) {
self.viewModel = viewModel
}
public var body: some View {
NavigationView {
MasterView()
.onReceive(self.viewModel.onError, perform: self.handleError(_:))
.onReceive(self.viewModel.onSuccessLogin, perform: self.handleSuccessfullLogin)
}
}
func handleSuccessfullLogin() {
//push next screen
}
func handleError(_ error: Error) {
//show alert
}
}
Using SwiftUI, I don't know how to implement the following:
- Push another controller if login is successful
- Show Alert if the error happened
Also, I would appreciate any advice about how to implement what I want in a better way. Thanks.
Update 1: I was able to show an alert, but still cannot find how to push another view in viewModel's callback