FIRLoadBundleTask.mm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. namespace api = firebase::firestore::api;
  24. namespace util = firebase::firestore::util;
  25. using firebase::firestore::util::ThrowInvalidArgument;
  26. } // namespace
  27. @implementation FIRLoadBundleTaskProgress {
  28. }
  29. - (instancetype)initWithInternal:(api::LoadBundleTaskProgress)progress {
  30. if (self = [super init]) {
  31. _bytesLoaded = progress.bytes_loaded();
  32. _documentsLoaded = progress.documents_loaded();
  33. _totalBytes = progress.total_bytes();
  34. _totalDocuments = progress.total_documents();
  35. switch (progress.state()) {
  36. case api::LoadBundleTaskState::kInProgress:
  37. _state = FIRLoadBundleTaskStateInProgress;
  38. break;
  39. case api::LoadBundleTaskState::kSuccess:
  40. _state = FIRLoadBundleTaskStateSuccess;
  41. break;
  42. case api::LoadBundleTaskState::kError:
  43. _state = FIRLoadBundleTaskStateError;
  44. break;
  45. }
  46. }
  47. return self;
  48. }
  49. - (BOOL)isEqual:(id)other {
  50. if (self == other) {
  51. return YES;
  52. } else if (![other isKindOfClass:[FIRLoadBundleTaskProgress class]]) {
  53. return NO;
  54. }
  55. FIRLoadBundleTaskProgress *otherProgress = (FIRLoadBundleTaskProgress *)other;
  56. return self.documentsLoaded == otherProgress.documentsLoaded &&
  57. self.totalDocuments == otherProgress.totalDocuments &&
  58. self.bytesLoaded == otherProgress.bytesLoaded &&
  59. self.totalBytes == otherProgress.totalBytes && self.state == otherProgress.state;
  60. }
  61. @end
  62. @implementation FIRLoadBundleTask {
  63. std::shared_ptr<api::LoadBundleTask> _task;
  64. }
  65. - (instancetype)initWithTask:(std::shared_ptr<api::LoadBundleTask>)task {
  66. if (self = [super init]) {
  67. _task = std::move(task);
  68. }
  69. return self;
  70. }
  71. - (FIRLoadBundleObserverHandle)addObserver:(void (^)(FIRLoadBundleTaskProgress *progress))observer {
  72. if (!observer) {
  73. ThrowInvalidArgument("Handler cannot be nil");
  74. }
  75. api::LoadBundleTask::ProgressObserver core_observer =
  76. [observer](api::LoadBundleTaskProgress internal_progress) {
  77. observer([[FIRLoadBundleTaskProgress alloc] initWithInternal:internal_progress]);
  78. };
  79. return _task->Observe(std::move(core_observer));
  80. }
  81. - (void)removeObserverWithHandle:(FIRLoadBundleObserverHandle)handle {
  82. _task->RemoveObserver(handle);
  83. }
  84. - (void)removeAllObservers {
  85. _task->RemoveAllObservers();
  86. }
  87. @end
  88. NS_ASSUME_NONNULL_END