UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Observation, @Observable & @Bindable ??

Forums > SwiftUI

Hi All,

As we know, iOS17 and the latest SwiftUI, we now have additional functionality in the form of: Observation, @Observable & @Bindable just to name a few.

For the past two weeks I am been trying to work out how to implement these and it's been a very steep learning curve. I now have a simple App (ServiceME) that has the main App file (ServiceMEApp) and a single view called LoginView.

The app also contains two Models (User & ViewModel).

I am unsure if the data flow is correct or I have just stumbled over an alternate way to do it.

I am hoping that someone who has the know to have a look at my Code and tell me if this is correct please. The following snippets contain the code.

Any help will be greatly appreciated.

Pete

ServiceMeApp

import SwiftUI

@main
struct ServiceMEApp: App {
    @State private var user = User()

    var body: some Scene {
        WindowGroup {
            LoginView()
                .environment(user)
        }
    }
}

User Model

import Foundation
import Observation

@Observable class User {
    var EmailAddress = ""
    var Password = ""
}

ViewModel

import Foundation
import Observation

@Observable
class Authentification {

    var user: User

    init(user: User) {
        self.user = user
    }

    func SignIn() {
        print(user.EmailAddress)
    }
}

LogInView

import SwiftUI
import Observation

@Observable class ViewData {
    var emailAddress: String = ""
    var passWord: String = ""
}

struct LoginView: View {

    @Bindable var viewData = ViewData()
    @Environment(User.self) private var user

    var auth = Authentification(user: User())

    var body: some View {
        NavigationStack {
            ZStack {
                BackGround()
                VStack {
                    Title()
                    Spacer()

                    TextField("Email Address", text: $viewData.emailAddress)
                                            .frame(width: UIScreen.main.bounds.width - 100)
                                            .textFieldStyle(.roundedBorder)
                                            .textInputAutocapitalization(.never)
                                            .opacity(/*@START_MENU_TOKEN@*/0.8/*@END_MENU_TOKEN@*/)

                    SecureField("Password", text: $viewData.passWord)
                                            .frame(width: UIScreen.main.bounds.width - 100)
                                            .textFieldStyle(.roundedBorder)
                                            .opacity(/*@START_MENU_TOKEN@*/0.8/*@END_MENU_TOKEN@*/)
                                            .padding(.bottom, 50)
                    Divider()

                    Button {
                        auth.user.EmailAddress = self.viewData.emailAddress
                        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(/*@START_MENU_TOKEN@*/0.8/*@END_MENU_TOKEN@*/)
                                   }

                    Spacer()
                }
            }
        }
    }
}

#Preview {
    LoginView()
        .environment(User())
}

struct BackGround: View {
    var body: some View {
        Image("PlanetEarth")
            .resizable()
            .ignoresSafeArea()
    }
}
struct Title: View {
    var body: some View {
        Text("Login")
            .font(.system(size: 40, weight: .regular))
            .foregroundColor(.yellow)
    }
}

   

Have you completed the iExpense project offered on this terrific learning site?

   

No I not yet @Obelix, but i will.

Cheers.

Pete

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.