v2to
v2to

Reputation: 529

SwiftUI: dismiss sheet programmatically

I'm using SwiftUI and I'm trying to implement the auth logic for my app.

I have a LoginView with a Register Button and if I click on it I use a sheet to present the RegisterView. Once the user is registered, the LoginView (on background) goes to HomeView and RegisterView should disappear. The problem is that RegisterView is not disappearing.

@ObservedObject var viewModel = RegisterViewModel()
@EnvironmentObject var authenticatedUser : AuthenticatedUser
@Environment(\.presentationMode) var presentationMode
ButtonWithLoadStateView(title: K.REGISTER, isLoading: self.$vm.isLoading) {
    self.viewModel.isLoading = true
    self.viewModel.register() { user in
        self.authenticatedUser.setLogged(user) // without this IT WORKS!
        self.presentationMode.wrappedValue.dismiss()
   }
}

If I remove the authenticatedUser.setLogged row then it works but it just encapsulate the user and store a token..

Upvotes: 1

Views: 374

Answers (1)

Asperi
Asperi

Reputation: 258461

The provided code is not testable, so just only idea - try the following

ButtonWithLoadStateView(title: K.REGISTER, isLoading: self.$vm.isLoading) {
    self.viewModel.isLoading = true
    self.viewModel.register() { user in
        DispatchQueue.main.async {
           self.authenticatedUser.setLogged(user)
        }
        self.presentationMode.wrappedValue.dismiss()
   }
}

Upvotes: 1

Related Questions