ios – Altering the property of observableObject from one other observableObject

[ad_1]

There’s a downside of the next nature: it’s essential to create an authorization window for the appliance, probably the most logical resolution I discovered the next implementation (I had to do that as a result of the mainView has a tabView which behaves incorrectly whether it is in a navigationView)

struct ContentView: View {

@EnvironmentObject var vm: AppSettings

var physique: some View {
    if vm.isLogin {
        MainView()
    } else {
        LoginView()
    }
}

AppSettings seems to be like this:

class AppSettings: ObservableObject {
    @Printed var isLogin = false
}

By default, the person shall be introduced with an authorization window that appears like this:

struct LoginView: View {
    @StateObject var vm = LoginViewModel()
    var physique: some View {
        NavigationView {
            VStack {
                TextField("E mail", textual content: $vm.login)
                TextField("Password", textual content: $vm.password)
                
                Button {
                    vm.auth()
                } label: {
                    Textual content("SignIn")
                }
            }
        }
    }
}

And eventually the loginViewModel seems to be like this:

class LoginViewModel: ObservableObject {
    
    @Printed var login = ""
    @Printed var password = ""
    
  //@Printed var appSettings = AppSettings() -- error on the primary screenshot
    //or
  //@EnvironmentObject var appSettings: AppSettings -- error on the second screenshot

    func auth() {
        UserAPI().Auth(req: LoginRequest(electronic mail: login, password: password)) { response, error in
            if let err = error {
                // Error Processing
            } else if let response = response {
                Defaults.accessToken = response.tokens.accessToken
                Defaults.refreshToken = response.tokens.refreshToken
                self.appSettings.isLogin = true
            }
        }
    }
}

1 error – Accessing StateObject’s object with out being put in on a View. It will create a brand new occasion every time

2 error – No ObservableObject of sort AppSettings discovered. A View.environmentObject(_:) for AppSettings could also be lacking as an ancestor of this view

I ask for assist, I simply cannot discover a manner for the interplay of two observableObject. I needed to insert all of the logic into the motion of the button to implement such performance

Along with this performance, it’s deliberate to implement an exit from the account by altering the isLogin variable to false in varied circumstances or use different setting variables to simply implement different features

The instance is intentionally simplified for a simple clarification of the scenario


[ad_2]

Leave a Reply