UserProfileChangeRequest.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. if consumed {
  26. fatalError("Internal Auth Error: Invalid call to setDisplayName after commitChanges.")
  27. }
  28. displayNameWasSet = true
  29. _displayName = newDisplayName
  30. }
  31. }
  32. private var _displayName: String?
  33. /// The URL of the user's profile photo.
  34. @objc open var photoURL: URL? {
  35. get { return _photoURL }
  36. set(newPhotoURL) {
  37. if consumed {
  38. fatalError("Internal Auth Error: Invalid call to setPhotoURL after commitChanges.")
  39. }
  40. photoURLWasSet = true
  41. _photoURL = newPhotoURL
  42. }
  43. }
  44. private var _photoURL: URL?
  45. /// Commits any pending changes.
  46. ///
  47. /// Invoked asynchronously on the main thread in the future.
  48. ///
  49. /// This method should only be called once. Once called, property values should not be changed.
  50. /// - Parameter completion: Optionally; the block invoked when the user profile change has been
  51. /// applied.
  52. @objc open func commitChanges(completion: ((Error?) -> Void)? = nil) {
  53. Task {
  54. do {
  55. try await self.commitChanges()
  56. await MainActor.run {
  57. completion?(nil)
  58. }
  59. } catch {
  60. await MainActor.run {
  61. completion?(error)
  62. }
  63. }
  64. }
  65. }
  66. /// Commits any pending changes.
  67. ///
  68. /// This method should only be called once. Once called, property values should not be changed.
  69. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  70. open func commitChanges() async throws {
  71. try await user.auth.authWorker.commitChanges(changeRequest: self)
  72. }
  73. init(_ user: User) {
  74. self.user = user
  75. }
  76. let user: User
  77. var consumed = false
  78. var displayNameWasSet = false
  79. var photoURLWasSet = false
  80. }