var imagePicker = UIImagePickerController()
var imagePicked = false
@IBAction func updateProfilePressed(_ sender: Any) {
let email = emailTextField.text ?? ""
let phone = phoneNumberTextField.text ?? ""
if nameTextField.text == "" {
self.showToast(message: "Please enter name")
return
} else if !isValidPhone(phone) {
self.showToast(message: "Please enter a valid phone number")
return
}
if imagePicked {
DB.getImageUrl(profileImageView) { imageUrl in
let data = [
"profileUrl" : imageUrl,
"name" : self.nameTextField.text ?? "",
"phoneNumber": self.phoneNumberTextField.text ?? "",
]
self.saveData(data)
}
} else {
let data = [
"name" : self.nameTextField.text ?? "",
"phoneNumber": self.phoneNumberTextField.text ?? "",
]
self.saveData(data)
}
}
func saveData(_ data : [String:Any]) {
let userId = Auth.auth().currentUser?.uid ?? ""
DB.updateDocument("ServiceProviders", userId, data, merge: true) { success in
if success {
self.showToast(message: "Profile Updated!")
}
}
}
// MARK: - Image Picker
extension ProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func enableImageViewTapGesture()
{
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(profileImageTapped(tapGesturerecognizer:)))
profileImageView.isUserInteractionEnabled = true
profileImageView.addGestureRecognizer(tapGestureRecognizer)
}
@objc func profileImageTapped(tapGesturerecognizer: UITapGestureRecognizer){
if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
print("Button capture")
imagePicker.delegate = self
imagePicker.sourceType = .savedPhotosAlbum
imagePicker.allowsEditing = true
SVProgressHUD.show()
present(imagePicker, animated: true) {
SVProgressHUD.dismiss()
}
}
else {
print("No")
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
imagePicked = true
profileImageView.layer.cornerRadius = profileImageView.bounds.width/2
profileImageView.image = pickedImage
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion:nil)
}
}
Comments