FSTLocalViewChanges.mm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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)initWithQuery:(FSTQuery *)query
  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. DocumentKeySet addedKeys;
  33. DocumentKeySet removedKeys;
  34. for (FSTDocumentViewChange *docChange in viewSnapshot.documentChanges) {
  35. switch (docChange.type) {
  36. case FSTDocumentViewChangeTypeAdded:
  37. addedKeys = addedKeys.insert(docChange.document.key);
  38. break;
  39. case FSTDocumentViewChangeTypeRemoved:
  40. removedKeys = removedKeys.insert(docChange.document.key);
  41. break;
  42. default:
  43. // Do nothing.
  44. break;
  45. }
  46. }
  47. return [self changesForQuery:viewSnapshot.query
  48. addedKeys:std::move(addedKeys)
  49. removedKeys:std::move(removedKeys)];
  50. }
  51. + (instancetype)changesForQuery:(FSTQuery *)query
  52. addedKeys:(DocumentKeySet)addedKeys
  53. removedKeys:(DocumentKeySet)removedKeys {
  54. return [[FSTLocalViewChanges alloc] initWithQuery:query
  55. addedKeys:std::move(addedKeys)
  56. removedKeys:std::move(removedKeys)];
  57. }
  58. - (instancetype)initWithQuery:(FSTQuery *)query
  59. addedKeys:(DocumentKeySet)addedKeys
  60. removedKeys:(DocumentKeySet)removedKeys {
  61. self = [super init];
  62. if (self) {
  63. _query = query;
  64. _addedKeys = std::move(addedKeys);
  65. _removedKeys = std::move(removedKeys);
  66. }
  67. return self;
  68. }
  69. - (const DocumentKeySet &)addedKeys {
  70. return _addedKeys;
  71. }
  72. - (const DocumentKeySet &)removedKeys {
  73. return _removedKeys;
  74. }
  75. @end
  76. NS_ASSUME_NONNULL_END