Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 49 additions & 11 deletions Scoop/Controller/Profile/EditProfileViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit
class EditProfileViewController: UIViewController {

// MARK: - Views

private let cancelButton = UIButton()
private let classTextField = LabeledTextField()
private let classPicker = UIPickerView()
Expand Down Expand Up @@ -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")
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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())

Expand All @@ -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] = []

Expand Down Expand Up @@ -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 {
Expand Down