-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented validation for EditProfileVC #73
base: main
Are you sure you want to change the base?
Changes from all commits
8862613
3593b70
6c25bd9
01283a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import UIKit | |
class EditProfileViewController: UIViewController { | ||
|
||
// MARK: - Views | ||
|
||
private let cancelButton = UIButton() | ||
private let classTextField = LabeledTextField() | ||
private let classPicker = UIPickerView() | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also want to communicate with @rs929 about having the successful print statements for every network request. I was guilty of doing this before, but I now realize that it clogs up the console and makes it difficult to debug, especially since you can have tons of network requests. If you don't see an error message then you can assume that it succeeded. @tiffany-pann Let me know what you think as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed this notification but I agree, perhaps we can just comment out for now and re-comment if we need to debug networking |
||
return | ||
} | ||
|
||
let firstName = String(nameArray[0]) | ||
let lastName = String(nameArray[1]) | ||
|
||
updateAuthenticatedUserRequest(firstName: firstName, lastName: lastName, grade: grade, phoneNumber: phoneNumber, pronouns: pronouns, prompts: getUserAnswers()) | ||
|
||
|
@@ -691,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] = [] | ||
|
||
|
@@ -793,13 +827,17 @@ 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) | ||
if textField.text?.isEmpty ?? true { | ||
associatedView.hidesLabel(isHidden: true) | ||
} | ||
} | ||
|
||
setSaveButtonColor(disabled: !textFieldsComplete()) | ||
} | ||
|
||
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am really against having these error statements printed out because it clutters the console and is unnecessary as the UI should tell you if it's invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the button color change and updated the PR. Richie said that he would change the custom text fields, so displaying the error would be dealt with later. The print statements are just temporary in place of that for now.