FSTLocalViewChanges.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #import "Firestore/Source/Core/FSTViewSnapshot.h"
  18. #import "Firestore/Source/Model/FSTDocument.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @interface FSTLocalViewChanges ()
  21. - (instancetype)initWithQuery:(FSTQuery *)query
  22. addedKeys:(FSTDocumentKeySet *)addedKeys
  23. removedKeys:(FSTDocumentKeySet *)removedKeys NS_DESIGNATED_INITIALIZER;
  24. @end
  25. @implementation FSTLocalViewChanges
  26. + (instancetype)changesForViewSnapshot:(FSTViewSnapshot *)viewSnapshot {
  27. FSTDocumentKeySet *addedKeys = [FSTDocumentKeySet keySet];
  28. FSTDocumentKeySet *removedKeys = [FSTDocumentKeySet keySet];
  29. for (FSTDocumentViewChange *docChange in viewSnapshot.documentChanges) {
  30. switch (docChange.type) {
  31. case FSTDocumentViewChangeTypeAdded:
  32. addedKeys = [addedKeys setByAddingObject:docChange.document.key];
  33. break;
  34. case FSTDocumentViewChangeTypeRemoved:
  35. removedKeys = [removedKeys setByAddingObject:docChange.document.key];
  36. break;
  37. default:
  38. // Do nothing.
  39. break;
  40. }
  41. }
  42. return [self changesForQuery:viewSnapshot.query addedKeys:addedKeys removedKeys:removedKeys];
  43. }
  44. + (instancetype)changesForQuery:(FSTQuery *)query
  45. addedKeys:(FSTDocumentKeySet *)addedKeys
  46. removedKeys:(FSTDocumentKeySet *)removedKeys {
  47. return
  48. [[FSTLocalViewChanges alloc] initWithQuery:query addedKeys:addedKeys removedKeys:removedKeys];
  49. }
  50. - (instancetype)initWithQuery:(FSTQuery *)query
  51. addedKeys:(FSTDocumentKeySet *)addedKeys
  52. removedKeys:(FSTDocumentKeySet *)removedKeys {
  53. self = [super init];
  54. if (self) {
  55. _query = query;
  56. _addedKeys = addedKeys;
  57. _removedKeys = removedKeys;
  58. }
  59. return self;
  60. }
  61. @end
  62. NS_ASSUME_NONNULL_END