StorageTaskSnapshot.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2022 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. /**
  16. * `StorageTaskSnapshot` represents an immutable view of a task.
  17. * A snapshot contains a task, storage reference, metadata (if it exists),
  18. * progress, and an error (if one occurred).
  19. */
  20. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. @objc(FIRStorageTaskSnapshot) open class StorageTaskSnapshot: NSObject {
  22. /**
  23. * The task this snapshot represents.
  24. */
  25. @objc public let task: StorageTask
  26. /**
  27. * Metadata returned by the task, or `nil` if no metadata returned.
  28. */
  29. @objc public let metadata: StorageMetadata?
  30. /**
  31. * The `StorageReference` this task operates on.
  32. */
  33. @objc public let reference: StorageReference
  34. /**
  35. * An object which tracks the progress of an upload or download.
  36. */
  37. @objc public let progress: Progress?
  38. /**
  39. * An error raised during task execution, or `nil` if no error occurred.
  40. */
  41. @objc public let error: Error?
  42. /**
  43. * The status of the task.
  44. */
  45. @objc public let status: StorageTaskStatus
  46. // MARK: NSObject overrides
  47. @objc override public var description: String {
  48. switch status {
  49. case .resume: return "<State: Resume>"
  50. case .progress: return "<State: Progress, Progress: \(String(describing: progress))>"
  51. case .pause: return "<State: Paused>"
  52. case .success: return "<State: Success>"
  53. case .failure: return "<State: Failed, Error: \(String(describing: error))"
  54. case .unknown: return "<State: Unknown>"
  55. }
  56. }
  57. init(task: StorageTask,
  58. state: StorageTaskState,
  59. reference: StorageReference,
  60. progress: Progress,
  61. metadata: StorageMetadata? = nil,
  62. error: Error? = nil) {
  63. self.task = task
  64. self.reference = reference
  65. self.progress = progress
  66. self.error = error
  67. self.metadata = metadata
  68. switch state {
  69. case .queueing, .running, .resuming: status = StorageTaskStatus.resume
  70. case .progress: status = StorageTaskStatus.progress
  71. case .paused, .pausing: status = StorageTaskStatus.pause
  72. case .success, .completing: status = StorageTaskStatus.success
  73. case .cancelled, .failed, .failing: status = .failure
  74. case .unknown: status = .unknown
  75. }
  76. }
  77. }