FSTLocalViewChanges.mm 2.8 KB

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