FSTLocalViewChanges.mm 2.9 KB

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