FIRLoadBundleTask.mm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRLoadBundleTask.h"
  17. #include <memory>
  18. #import "Firestore/Source/API/FIRLoadBundleTask+Internal.h"
  19. #include "Firestore/core/src/api/load_bundle_task.h"
  20. #include "Firestore/core/src/util/exception.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. namespace {
  23. using firebase::firestore::util::ThrowInvalidArgument;
  24. } // namespace
  25. @implementation FIRLoadBundleTaskProgress {
  26. }
  27. - (instancetype)initWithInternal:(api::LoadBundleTaskProgress)progress {
  28. if (self = [super init]) {
  29. _bytesLoaded = (NSInteger)progress.bytes_loaded();
  30. _documentsLoaded = progress.documents_loaded();
  31. _totalBytes = (NSInteger)progress.total_bytes();
  32. _totalDocuments = progress.total_documents();
  33. switch (progress.state()) {
  34. case api::LoadBundleTaskState::kInProgress:
  35. _state = FIRLoadBundleTaskStateInProgress;
  36. break;
  37. case api::LoadBundleTaskState::kSuccess:
  38. _state = FIRLoadBundleTaskStateSuccess;
  39. break;
  40. case api::LoadBundleTaskState::kError:
  41. _state = FIRLoadBundleTaskStateError;
  42. break;
  43. }
  44. }
  45. return self;
  46. }
  47. - (BOOL)isEqual:(id)other {
  48. if (self == other) {
  49. return YES;
  50. } else if (![other isKindOfClass:[FIRLoadBundleTaskProgress class]]) {
  51. return NO;
  52. }
  53. FIRLoadBundleTaskProgress *otherProgress = (FIRLoadBundleTaskProgress *)other;
  54. return self.documentsLoaded == otherProgress.documentsLoaded &&
  55. self.totalDocuments == otherProgress.totalDocuments &&
  56. self.bytesLoaded == otherProgress.bytesLoaded &&
  57. self.totalBytes == otherProgress.totalBytes && self.state == otherProgress.state;
  58. }
  59. @end
  60. @implementation FIRLoadBundleTask {
  61. std::shared_ptr<api::LoadBundleTask> _task;
  62. }
  63. - (instancetype)initWithTask:(std::shared_ptr<api::LoadBundleTask>)task {
  64. if (self = [super init]) {
  65. _task = std::move(task);
  66. }
  67. return self;
  68. }
  69. - (FIRLoadBundleObserverHandle)addObserver:(void (^)(FIRLoadBundleTaskProgress *progress))observer {
  70. if (!observer) {
  71. ThrowInvalidArgument("Handler cannot be nil");
  72. }
  73. api::LoadBundleTask::ProgressObserver core_observer =
  74. [observer](api::LoadBundleTaskProgress internal_progress) {
  75. observer([[FIRLoadBundleTaskProgress alloc] initWithInternal:internal_progress]);
  76. };
  77. return (FIRLoadBundleObserverHandle)_task->Observe(std::move(core_observer));
  78. }
  79. - (void)removeObserverWithHandle:(FIRLoadBundleObserverHandle)handle {
  80. _task->RemoveObserver(handle);
  81. }
  82. - (void)removeAllObservers {
  83. _task->RemoveAllObservers();
  84. }
  85. @end
  86. NS_ASSUME_NONNULL_END