UserProfileTableViewController+AvatarSetup.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 {
  7. func avatarHeaderView() -> UIView? {
  8. let headerView = AvatarEditView()
  9. headerView.delegate = self
  10. headerView.avatarImageView?.image = NCAPIController.sharedInstance().userProfileImage(for: account, with: self.traitCollection.userInterfaceStyle)
  11. headerView.nameLabel?.text = account.userDisplayName
  12. headerView.nameLabel?.isHidden = self.isEditable
  13. let avatarScopeImage = self.imageForScope(scope: account.avatarScope)?.applyingSymbolConfiguration(iconHeaderConfiguration)
  14. headerView.scopeButton?.tag = kAvatarScopeButtonTag
  15. headerView.scopeButton?.setImage(avatarScopeImage, for: .normal)
  16. headerView.scopeButton?.addTarget(self, action: #selector(showScopeSelectionDialog(_:)), for: .touchUpInside)
  17. headerView.scopeButton?.isHidden = !(isEditable && showScopes)
  18. headerView.editView?.isHidden = !(isEditable && NCDatabaseManager.sharedInstance().serverHasTalkCapability(kCapabilityTempUserAvatarAPI, forAccountId: account.accountId))
  19. // Avatar emojis are not allowed for now
  20. headerView.emojiButton.isHidden = true
  21. // Removal is only allowed for custom avatars
  22. headerView.trashButton.isHidden = !account.hasCustomAvatar
  23. // Need to have an explicit size here for the header view
  24. let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
  25. headerView.frame = CGRect(origin: .zero, size: size)
  26. return headerView
  27. }
  28. func checkAndPresentCamera() {
  29. // https://stackoverflow.com/a/20464727/2512312
  30. let mediaType: String = AVMediaType.video.rawValue
  31. let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType(mediaType))
  32. if authStatus == AVAuthorizationStatus.authorized {
  33. self.presentCamera()
  34. return
  35. } else if authStatus == AVAuthorizationStatus.notDetermined {
  36. AVCaptureDevice.requestAccess(for: AVMediaType(mediaType)) { granted in
  37. if granted {
  38. self.presentCamera()
  39. }
  40. }
  41. return
  42. }
  43. let alertTitle = NSLocalizedString("Could not access camera", comment: "")
  44. let alertMessage = NSLocalizedString("Camera access is not allowed. Check your settings.", comment: "")
  45. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
  46. let okButton = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil)
  47. alert.addAction(okButton)
  48. NCUserInterfaceController.sharedInstance().presentAlertViewController(alert)
  49. }
  50. func presentCamera() {
  51. DispatchQueue.main.async {
  52. self.imagePicker = UIImagePickerController()
  53. if let imagePicker = self.imagePicker {
  54. imagePicker.sourceType = .camera
  55. imagePicker.delegate = self
  56. self.present(imagePicker, animated: true)
  57. }
  58. }
  59. }
  60. func presentPhotoLibrary() {
  61. DispatchQueue.main.async {
  62. self.imagePicker = UIImagePickerController()
  63. if let imagePicker = self.imagePicker {
  64. imagePicker.sourceType = .photoLibrary
  65. imagePicker.delegate = self
  66. self.present(imagePicker, animated: true)
  67. }
  68. }
  69. }
  70. func sendUserProfileImage(image: UIImage) {
  71. NCAPIController.sharedInstance().setUserProfileImage(image, for: account) { error, _ in
  72. if error == nil {
  73. self.refreshUserProfile()
  74. } else {
  75. self.showProfileImageError(NSLocalizedString("An error occurred setting profile image", comment: ""))
  76. print("Error removing profile image: \(error.debugDescription)")
  77. }
  78. }
  79. }
  80. func removeUserProfileImage() {
  81. NCAPIController.sharedInstance().removeUserProfileImage(for: account) { error, _ in
  82. if error == nil {
  83. self.refreshUserProfile()
  84. } else {
  85. self.showProfileImageError(NSLocalizedString("An error occurred removing profile image", comment: ""))
  86. print("Error removing profile image: ", error.debugDescription)
  87. }
  88. }
  89. }
  90. func showProfileImageError(_ reason: String) {
  91. let errorDialog = UIAlertController(title: reason, message: nil, preferredStyle: .alert)
  92. let okAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil)
  93. errorDialog.addAction(okAction)
  94. self.present(errorDialog, animated: true, completion: nil)
  95. }
  96. }