UserProfileTableViewController+DelegateMethods.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import Foundation
  6. extension UserProfileTableViewController: UINavigationControllerDelegate, UITextFieldDelegate, UIGestureRecognizerDelegate, UIImagePickerControllerDelegate, AvatarEditViewDelegate {
  7. // MARK: - DetailedOptionSelector Delegate
  8. func detailedOptionsSelector(_ viewController: DetailedOptionsSelectorTableViewController!, didSelectOptionWithIdentifier option: DetailedOption!) {
  9. self.dismiss(animated: true) {
  10. if !option.selected {
  11. self.setUserProfileField(viewController.senderId, scopeValue: option.identifier)
  12. }
  13. }
  14. }
  15. func detailedOptionsSelectorWasCancelled(_ viewController: DetailedOptionsSelectorTableViewController!) {
  16. self.dismiss(animated: true, completion: nil)
  17. }
  18. // MARK: - UIImagePickerController Delegate
  19. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
  20. let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String
  21. if mediaType == "public.image" {
  22. let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
  23. self.dismiss(animated: true) {
  24. if let image = image {
  25. let cropViewController = TOCropViewController(croppingStyle: TOCropViewCroppingStyle.circular, image: image)
  26. cropViewController.delegate = self
  27. self.present(cropViewController, animated: true)
  28. }
  29. }
  30. }
  31. }
  32. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  33. self.dismiss(animated: true, completion: nil)
  34. }
  35. // MARK: - TOCROPViewControllerDelegate
  36. func cropViewController(_ cropViewController: TOCropViewController, didCropTo image: UIImage, with cropRect: CGRect, angle: Int) {
  37. self.sendUserProfileImage(image: image)
  38. // Fixes weird iOS 13 bug: https://github.com/TimOliver/TOCropViewController/issues/365
  39. cropViewController.transitioningDelegate = nil
  40. cropViewController.dismiss(animated: true)
  41. }
  42. func cropViewController(_ cropViewController: TOCropViewController, didFinishCancelled cancelled: Bool) {
  43. // Fixes weird iOS 13 bug: https://github.com/TimOliver/TOCropViewController/issues/365
  44. cropViewController.transitioningDelegate = nil
  45. cropViewController.dismiss(animated: true)
  46. }
  47. // MARK: - UITextField delegate
  48. func textFieldDidBeginEditing(_ textField: UITextField) {
  49. activeTextField = textField
  50. }
  51. func textFieldDidEndEditing(_ textField: UITextField) {
  52. var field: String?
  53. var currentValue: String?
  54. let newValue = textField.text!.trimmingCharacters(in: CharacterSet.whitespaces)
  55. let tag = textField.tag
  56. let waitForModification = self.waitingForModification
  57. self.waitingForModification = false
  58. activeTextField = nil
  59. if tag == kNameTextFieldTag {
  60. field = kUserProfileDisplayName
  61. currentValue = account.userDisplayName
  62. } else if tag == kEmailTextFieldTag {
  63. field = kUserProfileEmail
  64. currentValue = account.email
  65. } else if tag == kPhoneTextFieldTag {
  66. return
  67. } else if tag == kAddressTextFieldTag {
  68. field = kUserProfileAddress
  69. currentValue = account.address
  70. } else if tag == kWebsiteTextFieldTag {
  71. field = kUserProfileWebsite
  72. currentValue = account.website
  73. } else if tag == kTwitterTextFieldTag {
  74. field = kUserProfileTwitter
  75. currentValue = account.twitter
  76. }
  77. textField.text = newValue
  78. self.setModifyingProfileUI()
  79. if newValue != currentValue {
  80. NCAPIController.sharedInstance().setUserProfileField(field, withValue: newValue, for: account) { error, _ in
  81. if error != nil {
  82. self.showProfileModificationErrorForField(inTextField: tag, textField: textField)
  83. } else {
  84. if waitForModification {
  85. self.editButtonPressed()
  86. }
  87. self.refreshUserProfile()
  88. }
  89. self.removeModifyingProfileUI()
  90. }
  91. } else {
  92. if waitForModification {
  93. self.editButtonPressed()
  94. }
  95. self.removeModifyingProfileUI()
  96. }
  97. }
  98. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  99. if textField.tag == kPhoneTextFieldTag {
  100. let inputPhoneNumber = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
  101. let phoneNumber = try? phoneUtil.parse(inputPhoneNumber, defaultRegion: nil)
  102. setPhoneAction.isEnabled = phoneUtil.isValidNumber(phoneNumber) && account.phone != inputPhoneNumber
  103. }
  104. return true
  105. }
  106. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  107. textField.resignFirstResponder()
  108. return true
  109. }
  110. // MARK: - AvatarEditView Delegate
  111. func avatarEditViewPresentCamera(_ controller: AvatarEditView?) {
  112. self.checkAndPresentCamera()
  113. }
  114. func avatarEditViewPresentPhotoLibrary(_ controller: AvatarEditView?) {
  115. self.presentPhotoLibrary()
  116. }
  117. func avatarEditViewRemoveAvatar(_ controller: AvatarEditView?) {
  118. self.removeUserProfileImage()
  119. }
  120. }