• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


27 Mar, 2025

Updated at 18 May, 2025

How to make Model didset value trigger when i change textfield value in SwiftUI

I need isProfileDataEdited = true when i edit/add any one of the textfield in my screen

I have 30 textfields.. so i cant check every individual textfield value is being changed or not.. so i try to check by using Model value

i am using this viewModel.profileModel?.profile? in every textfield.. so if any value changed here then i need isProfileDataEdited = true.. but how?? please guide me

@MainActor class ProfileViewModel: ObservableObject, UserAuthenticatable {

@Published var isProfileDataEdited: Bool = false
@Published var profileModel: MyProfileModel?{
    didSet{
        isProfileDataEdited = true
    }
}

func fetchProfileDetails(isFromParent: Bool = false, showHud: Bool = true, _ completion: @escaping StatusCompletion) {
    
    if APIManager.shared.isConnectedToInternet {
        
        APIManager.shared.callDecodableApiRequest(with: url, method: .get, showHud: showHud) { (statusCode, error, model: MyProfileModel?) in
            
            if statusCode.isSuccess {
                self.profile = model?.profile
                self.profileModel = model
                completion(true)
            } else {
                showErrorToast(message: K.Msgs.error)
                completion(false)
            }
        }
    } else {
        showInfoToast(message: K.Msgs.noInternet)
        completion(false)
    }
}
}

and this is view ParentEditProfile: here i have 30 textfield.. so if i change any one for eg. if i change Contact Email ID then how do i get isProfileDataEdited = true

struct ParentEditProfile: View {
 var body: some View {

VStack(spacing: 0) {
    
    ScrollView {
        VStack(alignment: .leading, spacing: 15) {
            
            firstSet()
            
            HStack{
                Spacer()
                Button {
                    sendRequest()
                } label: {
                    Text("Send Request")
                        .font(.calibriRegular(with: 20))
                    
                }
                .buttonStyle(AppButtonStyle())
                .frame(width: 170)
                Spacer()
            }
            Spacer()
        }
        .padding()
    }
}
.background(
    Color.hexF4F4FC
        .ignoresSafeArea()
)

.onAppear {
    viewModel.fetchProfileDetails { status in
        
    }
}

}

func sendRequest() {

let studentProfile: [String: Any] = [
    "aadhaarNumber": viewModel.profileModel?.profile?.aadhaarNumber ?? "",
    "address": viewModel.profileModel?.profile?.address ?? "",
    "birthPlace": viewModel.profileModel?.profile?.studentProfile?.birthPlace ?? "",
    "city": viewModel.profileModel?.profile?.studentProfile?.city ?? "",
    "contactEmailID": viewModel.profileModel?.profile?.studentProfile?.contactEmailID ?? "",
    .....so on
]

viewModel.updateParentProfile() { statusCode, error in
    
}
}

@ViewBuilder func firstSet() -> some View {

VStack(alignment: .leading, spacing: 15) {
    
    
    VStack(alignment: .leading) {
        Text("Mobile")
            .font(.calibriBold(with: 16))
        
        TextField("Mobile Number", text: Binding(
            get: { viewModel.profileModel?.profile?.studentProfile?.contactMobile ?? "" },
            set: { newValue in
                viewModel.profileModel?.profile?.studentProfile?.contactMobile = newValue.isEmpty ? nil : newValue
            }
        ))
        .font(.calibriRegular(with: 16))
        
    }
    
    VStack(alignment: .leading) {
        Text("Student Place of birth")
            .font(.calibriBold(with: 16))
        
        TextField("Student Place of birth", text: Binding(
            get: { viewModel.profileModel?.profile?.studentProfile?.birthPlace ?? "" },
            set: { newValue in
                viewModel.profileModel?.profile?.studentProfile?.birthPlace = newValue.isEmpty ? nil : newValue
            }
        ))
        
    }
    VStack(alignment: .leading) {
        Text("Contact Email ID")
            .font(.calibriBold(with: 16))
        
        TextField("Contact Email ID", text: Binding(
            get: { viewModel.profileModel?.profile?.studentProfile?.contactEmailID ?? "" },
            set: { newValue in
                viewModel.profileModel?.profile?.studentProfile?.contactEmailID = newValue.isEmpty ? nil : newValue
            }
        ))
        
    }
    
}

} }