UserProfileChangeRequest.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. /** @class UserProfileChangeRequest
  16. @brief Represents an object capable of updating a user's profile data.
  17. @remarks Properties are marked as being part of a profile update when they are set. Setting a
  18. property value to nil is not the same as leaving the property unassigned.
  19. */
  20. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. @objc(FIRUserProfileChangeRequest) public class UserProfileChangeRequest: NSObject {
  22. /** @property displayName
  23. @brief The name of the user.
  24. */
  25. @objc public var displayName: String? {
  26. get { return _displayName }
  27. set(newDisplayName) {
  28. kAuthGlobalWorkQueue.async {
  29. if self.consumed {
  30. fatalError("Internal Auth Error: Invalid call to setDisplayName after commitChanges.")
  31. }
  32. self.displayNameWasSet = true
  33. self._displayName = newDisplayName
  34. }
  35. }
  36. }
  37. private var _displayName: String?
  38. /** @property photoURL
  39. @brief The URL of the user's profile photo.
  40. */
  41. @objc public var photoURL: URL? {
  42. get { return _photoURL }
  43. set(newPhotoURL) {
  44. kAuthGlobalWorkQueue.async {
  45. if self.consumed {
  46. fatalError("Internal Auth Error: Invalid call to setPhotoURL after commitChanges.")
  47. }
  48. self.photoURLWasSet = true
  49. self._photoURL = newPhotoURL
  50. }
  51. }
  52. }
  53. private var _photoURL: URL?
  54. /** @fn commitChangesWithCompletion:
  55. @brief Commits any pending changes.
  56. @remarks This method should only be called once. Once called, property values should not be
  57. changed.
  58. @param completion Optionally; the block invoked when the user profile change has been applied.
  59. Invoked asynchronously on the main thread in the future.
  60. */
  61. @objc public func commitChanges(withCompletion completion: ((Error?) -> Void)? = nil) {
  62. kAuthGlobalWorkQueue.async {
  63. if self.consumed {
  64. fatalError("Internal Auth Error: commitChanges should only be called once.")
  65. }
  66. self.consumed = true
  67. // Return fast if there is nothing to update:
  68. if !self.photoURLWasSet, !self.displayNameWasSet {
  69. User.callInMainThreadWithError(callback: completion, error: nil)
  70. return
  71. }
  72. let displayName = self.displayName
  73. let displayNameWasSet = self.displayNameWasSet
  74. let photoURL = self.photoURL
  75. let photoURLWasSet = self.photoURLWasSet
  76. self.user.executeUserUpdateWithChanges(changeBlock: { user, request in
  77. if photoURLWasSet {
  78. request.photoURL = photoURL
  79. }
  80. if displayNameWasSet {
  81. request.displayName = displayName
  82. }
  83. }) { error in
  84. if let error {
  85. User.callInMainThreadWithError(callback: completion, error: error)
  86. return
  87. }
  88. if displayNameWasSet {
  89. self.user.displayName = displayName
  90. }
  91. if photoURLWasSet {
  92. self.user.photoURL = photoURL
  93. }
  94. if let error = self.user.updateKeychain() {
  95. User.callInMainThreadWithError(callback: completion, error: error)
  96. }
  97. User.callInMainThreadWithError(callback: completion, error: nil)
  98. }
  99. }
  100. }
  101. /** @fn commitChanges
  102. @brief Commits any pending changes.
  103. @remarks This method should only be called once. Once called, property values should not be
  104. changed.
  105. @throws on error.
  106. */
  107. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  108. public func commitChanges() async throws {
  109. return try await withCheckedThrowingContinuation { continuation in
  110. self.commitChanges { error in
  111. if let error {
  112. continuation.resume(throwing: error)
  113. } else {
  114. continuation.resume()
  115. }
  116. }
  117. }
  118. }
  119. init(_ user: User) {
  120. self.user = user
  121. }
  122. private let user: User
  123. private var consumed = false
  124. private var displayNameWasSet = false
  125. private var photoURLWasSet = false
  126. }