-
-
Save zontan/c9d0e38e109f697894cd858c99947425 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func didTapSave(_ sender: Any) { | |
guard let user = Auth.auth().currentUser else { | |
showErrorAlert(error: nil) | |
return | |
} | |
let newName = nameTextField.text | |
let changeRequest = user.createProfileChangeRequest() | |
changeRequest.displayName = newName | |
changeRequest.commitChanges { (error) in | |
if let error = error { | |
self.showErrorAlert(error: error) | |
} else { | |
let ref = Database.database().reference() | |
ref.child("users/\(user.uid)/username").setValue(newName?.lowercased()) | |
ref.child("users/\(user.uid)/displayname").setValue(newName) | |
let alert = UIAlertController(title: "Success", message: "Your changes have been saved.", preferredStyle: .alert) | |
self.present(alert, animated: true) | |
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: { | |
alert.dismiss(animated: true, completion: { | |
self.navigationController?.popViewController(animated: true) | |
}) | |
}) | |
} | |
} | |
} | |
func showErrorAlert(error: Error?) { | |
if let error = error { | |
print(error.localizedDescription) | |
} | |
let alert = UIAlertController(title: "Error", message: "Something went wrong. Please try again.", preferredStyle: .alert) | |
self.present(alert, animated: true) | |
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: { | |
alert.dismiss(animated: true) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment