@Observable @Bindable?

52 Views Asked by At

Well, iOS17 $ Xcode15 has come along and given us a new way to share data throughout the App by using the @Observable macro set.

I am having so much trouble trying to understand to implement this and make it all work. I have created a Model file in Swift which I want to share detail across all the views my app will eventually have.

Below shows I have defined the class, which I believe is correct:

import Foundation
import SwiftUI
import Observation
import FirebaseAuth
import Firebase


@Observable 
class Authentification {
    
    var userSession: FirebaseAuth.User?
    
    init() {
        self.userSession = Auth.auth().currentUser
    }
    
    func SignIn() {
        print(UserDetail().Email)
    }
    func SignOut() {
        
    }
    func CreateUser() {
        
    }
    func ReturnUser() {
        
    }
    func DeleteAccount() {
        
    }
}

@Observable 
class UserDetail {
    var UserID: String = ""
    var FirstName: String = ""
    var LastName: String = ""
    var Email: String = ""
    var Password: String = ""
    var Location: String = ""
    var Mobile: String = ""
    var Service: String = ""
}

My immediate goal is to have a User log-in using Email & Password, using the following View:

import SwiftUI
import Observation

struct LogInView: View {
    
    var auth = Authentification()
    @Bindable var user: UserDetail
    
    
    var body: some View {
        
        NavigationStack {
            
            ZStack {
                Image("PlanetEarth")
                    .resizable()
                    .ignoresSafeArea()
                
                VStack {
                    ScreenTitle()
                    Spacer()
                    
                    TextField("Email Address", text: $user.Email)
                        .frame(width: UIScreen.main.bounds.width - 100)
                        .textFieldStyle(.roundedBorder)
                        .textInputAutocapitalization(.never)
                        .opacity(0.8)

                    
                    //Divider()
                    
                    
                    SecureField("Password", text: $user.Password)
                        .frame(width: UIScreen.main.bounds.width - 100)
                        .textFieldStyle(.roundedBorder)
                        .opacity(0.8)
                        .padding(.bottom, 50)

                 
                    
                    Button {
                        Task {
                            auth.SignIn()
                        }
                    } label: {
                            HStack {
                                Text("Log-In")
                    .fontWeight(.semibold)
                    Image(systemName: "arrow.right")
                                            }
                                            .foregroundColor(.white)
                                            .frame(width: UIScreen.main.bounds.width - 100, height: 40)
                                            .background(Color(.systemBlue))
                                            .cornerRadius(10.0)
                                            .opacity(0.8)
                    }

When I place a: Print(user.Email) above the Button Task 'auth.SignIn()' I will see in the Debug area the email address print out, so I know this part is doing what it's meant to.

But, when I put the same 'Print' statement in the

func SignIn() {
        print(UserDetail().Email)
    }

I see nothing, the Email Address is not Binding as I thought it should have done.

Where have I gone wrong?

Pete

0

There are 0 best solutions below