FSTLocalViewChanges.mm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2017 Google
  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 "Firestore/Source/Local/FSTLocalViewChanges.h"
  17. #include <utility>
  18. #import "Firestore/Source/Core/FSTViewSnapshot.h"
  19. #import "Firestore/Source/Model/FSTDocument.h"
  20. using firebase::firestore::core::DocumentViewChangeType;
  21. using firebase::firestore::model::DocumentKeySet;
  22. using firebase::firestore::model::TargetId;
  23. NS_ASSUME_NONNULL_BEGIN
  24. @interface FSTLocalViewChanges ()
  25. - (instancetype)initWithTarget:(TargetId)targetID
  26. addedKeys:(DocumentKeySet)addedKeys
  27. removedKeys:(DocumentKeySet)removedKeys NS_DESIGNATED_INITIALIZER;
  28. @end
  29. @implementation FSTLocalViewChanges {
  30. DocumentKeySet _addedKeys;
  31. DocumentKeySet _removedKeys;
  32. }
  33. + (instancetype)changesForViewSnapshot:(FSTViewSnapshot *)viewSnapshot
  34. withTargetID:(TargetId)targetID {
  35. DocumentKeySet addedKeys;
  36. DocumentKeySet removedKeys;
  37. for (FSTDocumentViewChange *docChange in viewSnapshot.documentChanges) {
  38. switch (docChange.type) {
  39. case DocumentViewChangeType::kAdded:
  40. addedKeys = addedKeys.insert(docChange.document.key);
  41. break;
  42. case DocumentViewChangeType::kRemoved:
  43. removedKeys = removedKeys.insert(docChange.document.key);
  44. break;
  45. default:
  46. // Do nothing.
  47. break;
  48. }
  49. }
  50. return [self changesForTarget:targetID
  51. addedKeys:std::move(addedKeys)
  52. removedKeys:std::move(removedKeys)];
  53. }
  54. + (instancetype)changesForTarget:(TargetId)targetID
  55. addedKeys:(DocumentKeySet)addedKeys
  56. removedKeys:(DocumentKeySet)removedKeys {
  57. return [[FSTLocalViewChanges alloc] initWithTarget:targetID
  58. addedKeys:std::move(addedKeys)
  59. removedKeys:std::move(removedKeys)];
  60. }
  61. - (instancetype)initWithTarget:(TargetId)targetID
  62. addedKeys:(DocumentKeySet)addedKeys
  63. removedKeys:(DocumentKeySet)removedKeys {
  64. self = [super init];
  65. if (self) {
  66. _targetID = targetID;
  67. _addedKeys = std::move(addedKeys);
  68. _removedKeys = std::move(removedKeys);
  69. }
  70. return self;
  71. }
  72. - (const DocumentKeySet &)addedKeys {
  73. return _addedKeys;
  74. }
  75. - (const DocumentKeySet &)removedKeys {
  76. return _removedKeys;
  77. }
  78. @end
  79. NS_ASSUME_NONNULL_END