From 88626130f720856d2d2a14117663f9515beae71a Mon Sep 17 00:00:00 2001 From: Caitlyn Jin Date: Wed, 25 Oct 2023 18:02:31 -0400 Subject: [PATCH 1/3] Implemented validation for edit profile fields - Cannot save unless all fields are filled and valid --- .../Profile/EditProfileViewController.swift | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Scoop/Controller/Profile/EditProfileViewController.swift b/Scoop/Controller/Profile/EditProfileViewController.swift index 6ffd1e5..0895c6f 100644 --- a/Scoop/Controller/Profile/EditProfileViewController.swift +++ b/Scoop/Controller/Profile/EditProfileViewController.swift @@ -658,19 +658,32 @@ class EditProfileViewController: UIViewController { } @objc private func saveEdit() { - let name = nameTextField.getText()?.split(separator: " ") - let firstName = String(name?[0] ?? "") - let lastName = String(name?[1...].joined(separator: " ") ?? "") - let grade = classTextField.getText() - let pronouns = pronounsTextField.getText() - let phoneNumber = phoneNumTextField.getText() + guard let name = nameTextField.getText(), !name.isEmpty, + let grade = classTextField.getText(), !grade.isEmpty, + let pronouns = pronounsTextField.getText(), !pronouns.isEmpty, + let hometown = hometownTextField.getText(), !hometown.isEmpty, + let snack = snackTextField.getText(), !snack.isEmpty, + let song = songTextField.getText(), !song.isEmpty, + let stop = stopTextField.getText(), !stop.isEmpty else { + print("Error: Please complete all fields") + return + } - hometown = hometownTextField.getText() + let phoneNumber = phoneNumTextField.getText() talkative = talkativeSlider.value music = musicSlider.value - snack = snackTextField.getText() - song = songTextField.getText() - stop = stopTextField.getText() + self.hometown = hometown + self.snack = snack + self.song = song + self.stop = stop + + guard let nameArray = nameTextField.getText()?.split(separator: " "), nameArray.count == 2 else { + print("Error: Name field does not contain first and last name") + return + } + + let firstName = String(nameArray[0]) + let lastName = String(nameArray[1]) updateAuthenticatedUserRequest(firstName: firstName, lastName: lastName, grade: grade, phoneNumber: phoneNumber, pronouns: pronouns, prompts: getUserAnswers()) From 6c25bd916d8d4fcebc9164f40d1127e672e8c331 Mon Sep 17 00:00:00 2001 From: Caitlyn Jin Date: Fri, 3 Nov 2023 02:41:10 -0400 Subject: [PATCH 2/3] Added color change to button depending on text field completion - Created text field completion function that returns whether the fields are filled out and the name field contains both first and last name --- .../Profile/EditProfileViewController.swift | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Scoop/Controller/Profile/EditProfileViewController.swift b/Scoop/Controller/Profile/EditProfileViewController.swift index 52a0df1..fb6d006 100644 --- a/Scoop/Controller/Profile/EditProfileViewController.swift +++ b/Scoop/Controller/Profile/EditProfileViewController.swift @@ -11,7 +11,7 @@ import UIKit class EditProfileViewController: UIViewController { // MARK: - Views - + private let cancelButton = UIButton() private let classTextField = LabeledTextField() private let classPicker = UIPickerView() @@ -704,6 +704,27 @@ class EditProfileViewController: UIViewController { present(imagePicker, animated: true) } + private func setSaveButtonColor(disabled: Bool) { + saveButton.layer.opacity = disabled ? 0.5 : 1 + } + + private func textFieldsComplete() -> Bool { + var complete = true + + [nameTextField, classTextField, pronounsTextField, hometownTextField, snackTextField, songTextField, stopTextField].forEach { textField in + guard let field = textField.getText()?.trimmingCharacters(in: .whitespacesAndNewlines), !field.isEmpty else { + complete = false + return + } + } + + guard let name = nameTextField.getText()?.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: " "), name.count == 2 else { + return false + } + + return complete + } + private func getUserAnswers() -> [UserAnswer] { var userAnswers: [UserAnswer] = [] @@ -806,6 +827,8 @@ extension EditProfileViewController: UITextFieldDelegate { } func textFieldDidEndEditing(_ textField: UITextField) { + textField.text = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines) + if let onboardingTextField = textField as? OnboardingTextField, let associatedView = onboardingTextField.associatedView as? LabeledTextField { associatedView.labeledTextField(isSelected: false) @@ -813,6 +836,13 @@ extension EditProfileViewController: UITextFieldDelegate { associatedView.hidesLabel(isHidden: true) } } + + var fields: [String] = [] + [classTextField, hometownTextField, nameTextField, phoneNumTextField, pronounsTextField, snackTextField, songTextField, stopTextField].forEach { textField in + fields.append(textField.textField.text ?? "") + } + + setSaveButtonColor(disabled: !textFieldsComplete()) } func textFieldShouldReturn(_ textField: UITextField) -> Bool { From 01283a654af53978da74a0a689e5204fe50f453f Mon Sep 17 00:00:00 2001 From: Caitlyn Jin Date: Fri, 3 Nov 2023 03:09:19 -0400 Subject: [PATCH 3/3] Removed unused code --- Scoop/Controller/Profile/EditProfileViewController.swift | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Scoop/Controller/Profile/EditProfileViewController.swift b/Scoop/Controller/Profile/EditProfileViewController.swift index fb6d006..cbd8f72 100644 --- a/Scoop/Controller/Profile/EditProfileViewController.swift +++ b/Scoop/Controller/Profile/EditProfileViewController.swift @@ -837,11 +837,6 @@ extension EditProfileViewController: UITextFieldDelegate { } } - var fields: [String] = [] - [classTextField, hometownTextField, nameTextField, phoneNumTextField, pronounsTextField, snackTextField, songTextField, stopTextField].forEach { textField in - fields.append(textField.textField.text ?? "") - } - setSaveButtonColor(disabled: !textFieldsComplete()) }