UserProfileChangeRequest.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /// Represents an object capable of updating a user's profile data.
  16. ///
  17. /// 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. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  20. @objc(FIRUserProfileChangeRequest) open class UserProfileChangeRequest: NSObject,
  21. @unchecked Sendable /* TODO: sendable */ {
  22. /// The name of the user.
  23. @objc open var displayName: String? {
  24. get { return _displayName }
  25. set(newDisplayName) {
  26. kAuthGlobalWorkQueue.async {
  27. if self.consumed {
  28. fatalError("Internal Auth Error: Invalid call to setDisplayName after commitChanges.")
  29. }
  30. self.displayNameWasSet = true
  31. self._displayName = newDisplayName
  32. }
  33. }
  34. }
  35. private var _displayName: String?
  36. /// The URL of the user's profile photo.
  37. @objc open var photoURL: URL? {
  38. get { return _photoURL }
  39. set(newPhotoURL) {
  40. kAuthGlobalWorkQueue.async {
  41. if self.consumed {
  42. fatalError("Internal Auth Error: Invalid call to setPhotoURL after commitChanges.")
  43. }
  44. self.photoURLWasSet = true
  45. self._photoURL = newPhotoURL
  46. }
  47. }
  48. }
  49. private var _photoURL: URL?
  50. /// Commits any pending changes.
  51. ///
  52. /// Invoked asynchronously on the main thread in the future.
  53. ///
  54. /// This method should only be called once.Once called, property values should not be changed.
  55. /// - Parameter completion: Optionally; the block invoked when the user profile change has been
  56. /// applied.
  57. @objc open func commitChanges(completion: (@Sendable (Error?) -> Void)? = nil) {
  58. kAuthGlobalWorkQueue.async {
  59. if self.consumed {
  60. fatalError("Internal Auth Error: commitChanges should only be called once.")
  61. }
  62. self.consumed = true
  63. // Return fast if there is nothing to update:
  64. if !self.photoURLWasSet, !self.displayNameWasSet {
  65. User.callInMainThreadWithError(callback: completion, error: nil)
  66. return
  67. }
  68. let displayName = self.displayName
  69. let displayNameWasSet = self.displayNameWasSet
  70. let photoURL = self.photoURL
  71. let photoURLWasSet = self.photoURLWasSet
  72. self.user.executeUserUpdateWithChanges(changeBlock: { user, request in
  73. if photoURLWasSet {
  74. request.photoURL = photoURL
  75. }
  76. if displayNameWasSet {
  77. request.displayName = displayName
  78. }
  79. }) { error in
  80. if let error {
  81. User.callInMainThreadWithError(callback: completion, error: error)
  82. return
  83. }
  84. if displayNameWasSet {
  85. self.user.displayName = displayName
  86. }
  87. if photoURLWasSet {
  88. self.user.photoURL = photoURL
  89. }
  90. if let error = self.user.updateKeychain() {
  91. User.callInMainThreadWithError(callback: completion, error: error)
  92. }
  93. User.callInMainThreadWithError(callback: completion, error: nil)
  94. }
  95. }
  96. }
  97. /// Commits any pending changes.
  98. ///
  99. /// This method should only be called once. Once called, property values should not be changed.
  100. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  101. open func commitChanges() async throws {
  102. return try await withCheckedThrowingContinuation { continuation in
  103. self.commitChanges { error in
  104. if let error {
  105. continuation.resume(throwing: error)
  106. } else {
  107. continuation.resume()
  108. }
  109. }
  110. }
  111. }
  112. init(_ user: User) {
  113. self.user = user
  114. }
  115. private let user: User
  116. private var consumed = false
  117. private var displayNameWasSet = false
  118. private var photoURLWasSet = false
  119. }