UserProfileChangeRequest.swift 4.2 KB

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