ios – Altering the property of observableObject from one other observableObject
[ad_1]
There’s a drawback of the next nature: it’s essential to create an authorization window for the applying, essentially 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 appears like this:
class AppSettings: ObservableObject {
@Printed var isLogin = false
}
By default, the person will probably be offered with an authorization window that appears like this:
struct LoginView: View {
@StateObject var vm = LoginViewModel()
var physique: some View {
NavigationView {
VStack {
TextField("Electronic mail", textual content: $vm.login)
TextField("Password", textual content: $vm.password)
Button {
vm.auth()
} label: {
Textual content("SignIn")
}
}
}
}
}
And eventually the loginViewModel appears 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
}
}
}
}
I ask for assist, I simply can’t discover a method 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 numerous 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]