FSTLocalViewChanges.mm 2.8 KB

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