123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- //
- // SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
- // SPDX-License-Identifier: GPL-3.0-or-later
- //
- import Foundation
- extension UserProfileTableViewController: UINavigationControllerDelegate, UITextFieldDelegate, UIGestureRecognizerDelegate, UIImagePickerControllerDelegate, AvatarEditViewDelegate {
- // MARK: - DetailedOptionSelector Delegate
- func detailedOptionsSelector(_ viewController: DetailedOptionsSelectorTableViewController!, didSelectOptionWithIdentifier option: DetailedOption!) {
- self.dismiss(animated: true) {
- if !option.selected {
- self.setUserProfileField(viewController.senderId, scopeValue: option.identifier)
- }
- }
- }
- func detailedOptionsSelectorWasCancelled(_ viewController: DetailedOptionsSelectorTableViewController!) {
- self.dismiss(animated: true, completion: nil)
- }
- // MARK: - UIImagePickerController Delegate
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
- let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String
- if mediaType == "public.image" {
- let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
- self.dismiss(animated: true) {
- if let image = image {
- let cropViewController = TOCropViewController(croppingStyle: TOCropViewCroppingStyle.circular, image: image)
- cropViewController.delegate = self
- self.present(cropViewController, animated: true)
- }
- }
- }
- }
- func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
- self.dismiss(animated: true, completion: nil)
- }
- // MARK: - TOCROPViewControllerDelegate
- func cropViewController(_ cropViewController: TOCropViewController, didCropTo image: UIImage, with cropRect: CGRect, angle: Int) {
- self.sendUserProfileImage(image: image)
- // Fixes weird iOS 13 bug: https://github.com/TimOliver/TOCropViewController/issues/365
- cropViewController.transitioningDelegate = nil
- cropViewController.dismiss(animated: true)
- }
- func cropViewController(_ cropViewController: TOCropViewController, didFinishCancelled cancelled: Bool) {
- // Fixes weird iOS 13 bug: https://github.com/TimOliver/TOCropViewController/issues/365
- cropViewController.transitioningDelegate = nil
- cropViewController.dismiss(animated: true)
- }
- // MARK: - UITextField delegate
- func textFieldDidBeginEditing(_ textField: UITextField) {
- activeTextField = textField
- }
- func textFieldDidEndEditing(_ textField: UITextField) {
- var field: String?
- var currentValue: String?
- let newValue = textField.text!.trimmingCharacters(in: CharacterSet.whitespaces)
- let tag = textField.tag
- let waitForModification = self.waitingForModification
- self.waitingForModification = false
- activeTextField = nil
- if tag == kNameTextFieldTag {
- field = kUserProfileDisplayName
- currentValue = account.userDisplayName
- } else if tag == kEmailTextFieldTag {
- field = kUserProfileEmail
- currentValue = account.email
- } else if tag == kPhoneTextFieldTag {
- return
- } else if tag == kAddressTextFieldTag {
- field = kUserProfileAddress
- currentValue = account.address
- } else if tag == kWebsiteTextFieldTag {
- field = kUserProfileWebsite
- currentValue = account.website
- } else if tag == kTwitterTextFieldTag {
- field = kUserProfileTwitter
- currentValue = account.twitter
- }
- textField.text = newValue
- self.setModifyingProfileUI()
- if newValue != currentValue {
- NCAPIController.sharedInstance().setUserProfileField(field, withValue: newValue, for: account) { error, _ in
- if error != nil {
- self.showProfileModificationErrorForField(inTextField: tag, textField: textField)
- } else {
- if waitForModification {
- self.editButtonPressed()
- }
- self.refreshUserProfile()
- }
- self.removeModifyingProfileUI()
- }
- } else {
- if waitForModification {
- self.editButtonPressed()
- }
- self.removeModifyingProfileUI()
- }
- }
- func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
- if textField.tag == kPhoneTextFieldTag {
- let inputPhoneNumber = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
- let phoneNumber = try? phoneUtil.parse(inputPhoneNumber, defaultRegion: nil)
- setPhoneAction.isEnabled = phoneUtil.isValidNumber(phoneNumber) && account.phone != inputPhoneNumber
- }
- return true
- }
- func textFieldShouldReturn(_ textField: UITextField) -> Bool {
- textField.resignFirstResponder()
- return true
- }
- // MARK: - AvatarEditView Delegate
- func avatarEditViewPresentCamera(_ controller: AvatarEditView?) {
- self.checkAndPresentCamera()
- }
- func avatarEditViewPresentPhotoLibrary(_ controller: AvatarEditView?) {
- self.presentPhotoLibrary()
- }
- func avatarEditViewRemoveAvatar(_ controller: AvatarEditView?) {
- self.removeUserProfileImage()
- }
- }
|