I need isProfileDataEdited = true
when I edit/add any one of the textfield in my screen.
I have 30 textfields, so I can't 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. How can I achieve this?
@MainActor class ProfileViewModel: ObservableObject {
@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
}
))
}
}
}
}
EDIT: Model
struct MyProfileModel: Codable, Equatable {
let status: String?
let errorCode: Int?
let message: String?
var profile: ProfileData?
}
// MARK: - Profile
struct ProfileData: Codable, Hashable {
let lName, designation, fatherHusbandMob, address: String?
let fatherHusbandName, emailID, qualification, alternateEmailID: String?
let paNNumber: String?
.....so on