|
| 1 | +// |
| 2 | +// KPKSynchronizationChangesStore.m |
| 3 | +// KeePassKit |
| 4 | +// |
| 5 | +// Created by Michael Starke on 02.10.22. |
| 6 | +// Copyright © 2022 HicknHack Software GmbH. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "KPKSynchronizationChangesStore.h" |
| 10 | +#import "KPKSynchronizationChangesStore_Private.h" |
| 11 | +#import "KPKSynchronizationChangesItem.h" |
| 12 | + |
| 13 | +@implementation KPKSynchronizationChangesStore |
| 14 | + |
| 15 | +@dynamic addedNodes; |
| 16 | +@dynamic removedNodes; |
| 17 | +@dynamic allChanges; |
| 18 | + |
| 19 | +- (instancetype)init { |
| 20 | + self = [super init]; |
| 21 | + if(self) { |
| 22 | + self.mutableChanges = [[NSMutableArray alloc] init]; |
| 23 | + self.recordingChanges = NO; |
| 24 | + } |
| 25 | + return self; |
| 26 | +} |
| 27 | + |
| 28 | +- (void)_beginRecordingChanges { |
| 29 | + [self.mutableChanges removeAllObjects]; |
| 30 | + self.recordingChanges = YES; |
| 31 | +} |
| 32 | + |
| 33 | +- (void)_endRecordingChanges { |
| 34 | + self.recordingChanges = NO; |
| 35 | +} |
| 36 | + |
| 37 | +- (void)_addChange:(KPKSynchronizationChangesItem *)changeItem { |
| 38 | + NSAssert(self.isRecordingChanges, @"Change recording is only possible while in active recording"); |
| 39 | + [self.mutableChanges addObject:changeItem]; |
| 40 | +} |
| 41 | + |
| 42 | +- (NSArray<KPKSynchronizationChangesItem *> *)allChanges { |
| 43 | + NSAssert(!self.isRecordingChanges, @"Cannot access changes while actively recording them"); |
| 44 | + return [self.mutableChanges copy]; |
| 45 | +} |
| 46 | + |
| 47 | +- (NSArray<KPKSynchronizationChangesItem *> *)addedNodes { |
| 48 | + NSAssert(!self.isRecordingChanges, @"Cannot access changes while actively recording them"); |
| 49 | + NSPredicate *addedNodesPredicate = [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) { |
| 50 | + KPKSynchronizationChangesItem *item = (KPKSynchronizationChangesItem *)evaluatedObject; |
| 51 | + return (item.changeType == KPKSynchronizationChangeTypeAddedNode); |
| 52 | + |
| 53 | + }]; |
| 54 | + return [self.mutableChanges filteredArrayUsingPredicate:addedNodesPredicate]; |
| 55 | +} |
| 56 | + |
| 57 | +- (NSArray<KPKSynchronizationChangesItem *> *)removedNodes { |
| 58 | + NSAssert(!self.isRecordingChanges, @"Cannot access changes while actively recording them"); |
| 59 | + NSPredicate *addedNodesPredicate = [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) { |
| 60 | + KPKSynchronizationChangesItem *item = (KPKSynchronizationChangesItem *)evaluatedObject; |
| 61 | + return (item.changeType == KPKSynchronizationChangeTypeRemovedNode); |
| 62 | + |
| 63 | + }]; |
| 64 | + return [self.mutableChanges filteredArrayUsingPredicate:addedNodesPredicate]; |
| 65 | +} |
| 66 | + |
| 67 | +- (NSArray<KPKSynchronizationChangesItem *> *)changesForUUID:(NSUUID *)uuid { |
| 68 | + NSAssert(!self.isRecordingChanges, @"Cannot access changes while actively recording them"); |
| 69 | + NSPredicate *addedNodesPredicate = [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) { |
| 70 | + KPKSynchronizationChangesItem *item = (KPKSynchronizationChangesItem *)evaluatedObject; |
| 71 | + return ([item.uuid isEqual:uuid]); |
| 72 | + }]; |
| 73 | + return [self.mutableChanges filteredArrayUsingPredicate:addedNodesPredicate]; |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +@end |
0 commit comments