-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectNode.m
More file actions
574 lines (492 loc) · 16.6 KB
/
ObjectNode.m
File metadata and controls
574 lines (492 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//
// ObjectNode.m
// CommonViewControllers
//
// Created by Lessica <82flex@gmail.com> on 2022/1/20.
// Copyright © 2022 Zheng Wu. All rights reserved.
//
#import "ObjectNode-Private.h"
@implementation ObjectNode
+ (BOOL)supportsSecureCoding {
return YES;
}
+ (NSNumberFormatter *)_numberFormatter {
static NSNumberFormatter *__numberFormatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__numberFormatter = [NSNumberFormatter new];
__numberFormatter.numberStyle = NSNumberFormatterNoStyle;
});
return __numberFormatter;
}
+ (NSDateFormatter *)_dateFormatter {
static NSDateFormatter *__dateFormatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__dateFormatter = ({
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
[dateFormatter setCalendar:[NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian]];
dateFormatter;
});
});
return __dateFormatter;
}
+ (ObjectNodeType)_typeForObject:(id)obj {
if ([obj isKindOfClass:[NSNull class]]) {
return ObjectNodeTypeNull;
}
if ([obj isKindOfClass:[NSArray class]]) {
return ObjectNodeTypeArray;
}
if ([obj isKindOfClass:[NSDictionary class]]) {
return ObjectNodeTypeDictionary;
}
if ([obj isKindOfClass:[NSString class]]) {
return ObjectNodeTypeString;
}
if ([obj isKindOfClass:[NSDate class]]) {
return ObjectNodeTypeDate;
}
if ([obj isKindOfClass:[NSData class]]) {
return ObjectNodeTypeData;
}
if ([obj isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {
return ObjectNodeTypeBoolean;
}
if ([obj isKindOfClass:[NSNumber class]]) {
return ObjectNodeTypeNumber;
}
return ObjectNodeTypeUnknown;
}
+ (NSString *)stringForType:(ObjectNodeType)type {
switch (type) {
case ObjectNodeTypeUnknown:
return nil;
case ObjectNodeTypeNull:
return @"Null";
case ObjectNodeTypeArray:
return @"Array";
case ObjectNodeTypeDictionary:
return @"Dictionary";
case ObjectNodeTypeBoolean:
return @"Boolean";
case ObjectNodeTypeDate:
return @"Date";
case ObjectNodeTypeData:
return @"Data";
case ObjectNodeTypeNumber:
return @"Number";
case ObjectNodeTypeString:
return @"String";
}
}
+ (ObjectNodeType)typeForString:(NSString *)str {
if ([str isEqualToString:@"Null"]) {
return ObjectNodeTypeNull;
}
if ([str isEqualToString:@"Array"]) {
return ObjectNodeTypeArray;
}
if ([str isEqualToString:@"Dictionary"]) {
return ObjectNodeTypeDictionary;
}
if ([str isEqualToString:@"String"]) {
return ObjectNodeTypeString;
}
if ([str isEqualToString:@"Date"]) {
return ObjectNodeTypeDate;
}
if ([str isEqualToString:@"Data"]) {
return ObjectNodeTypeData;
}
if ([str isEqualToString:@"Boolean"]) {
return ObjectNodeTypeBoolean;
}
if ([str isEqualToString:@"Number"]) {
return ObjectNodeTypeNumber;
}
return ObjectNodeTypeUnknown;
}
+ (id)defaultValueForType:(ObjectNodeType)type {
switch (type) {
case ObjectNodeTypeUnknown:
return nil;
case ObjectNodeTypeNull:
return [NSNull null];
case ObjectNodeTypeArray:
case ObjectNodeTypeDictionary:
return nil;
case ObjectNodeTypeBoolean:
return @NO;
case ObjectNodeTypeDate:
return [NSDate date];
case ObjectNodeTypeData:
return [NSData data];
case ObjectNodeTypeNumber:
return @0;
case ObjectNodeTypeString:
return @"";
}
}
+ (id)convertString:(NSString *)str toObjectOfType:(ObjectNodeType)type {
switch (type) {
case ObjectNodeTypeUnknown:
return nil;
case ObjectNodeTypeNull:
return nil;
case ObjectNodeTypeArray:
return nil;
case ObjectNodeTypeDictionary:
return nil;
case ObjectNodeTypeBoolean:
return [[str uppercaseString] isEqualToString:@"YES"] || [[str lowercaseString] isEqualToString:@"true"] ? @YES : @NO;
case ObjectNodeTypeDate:
return nil;
case ObjectNodeTypeData:
return nil;
case ObjectNodeTypeNumber:
return [ObjectNode._numberFormatter numberFromString:str];
case ObjectNodeTypeString:
return str;
}
}
+ (NSString *)stringValueOfNode:(ObjectNode *)node {
id valueToTranslate = node._cachedDisplayValue ?: node.value;
NSArray *childrenToTranslate = [node._cachedDisplayValue isKindOfClass:[NSArray class]] ? node._cachedDisplayValue : [node._cachedDisplayValue isKindOfClass:[NSDictionary class]] ? [node._cachedDisplayValue allKeys] : node.children;
ObjectNodeType typeToTranslate = node._cachedDisplayValue ? [self _typeForObject:node._cachedDisplayValue] : node.type;
switch (typeToTranslate) {
case ObjectNodeTypeUnknown:
return nil;
case ObjectNodeTypeNull:
return NSLocalizedString(@"(null)", @"");
case ObjectNodeTypeArray:
case ObjectNodeTypeDictionary:
return [NSString stringWithFormat:NSLocalizedString(@"(%lu items)", @""), [childrenToTranslate count]];
case ObjectNodeTypeBoolean:
return [valueToTranslate boolValue] ? NSLocalizedString(@"YES", @"") : NSLocalizedString(@"NO", @"");
case ObjectNodeTypeDate:
return [ObjectNode._dateFormatter stringFromDate:valueToTranslate];
case ObjectNodeTypeData:
return [NSString stringWithFormat:NSLocalizedString(@"(%lu bytes)", @""), [(NSData *)valueToTranslate length]];
case ObjectNodeTypeNumber:
return [ObjectNode._numberFormatter stringFromNumber:valueToTranslate];
case ObjectNodeTypeString:
return valueToTranslate;
}
}
+ (NSString *)stringKeyOfNode:(ObjectNode *)node {
switch (node.parent.type) {
case ObjectNodeTypeArray:
return [NSString stringWithFormat:NSLocalizedString(@"Item #%lu", @""), [node.parent.children indexOfObject:node]];
default:
return node.key;
}
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self._visible = YES;
self.key = [aDecoder decodeObjectForKey:@"key"];
self.type = [[aDecoder decodeObjectForKey:@"type"] unsignedIntegerValue];
self.value = [aDecoder decodeObjectForKey:@"value"];
self.children = [aDecoder decodeObjectForKey:@"children"];
[self.children enumerateObjectsUsingBlock:^(ObjectNode *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
obj.parent = self;
}];
}
return self;
}
- (instancetype)initWithDictionary:(NSDictionary <NSString *, id> *)dictionary {
self = [super init];
if (self) {
self._visible = YES;
self.type = ObjectNodeTypeDictionary;
self.children = [NSMutableArray new];
[dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull key, id _Nonnull obj, BOOL *_Nonnull stop) {
ObjectNode *childNode = [[ObjectNode alloc] initWithObject:obj];
childNode.key = key;
childNode.parent = self;
[self.children addObject:childNode];
}];
}
return self;
}
- (instancetype)initWithArray:(NSArray <id> *)array {
self = [super init];
if (self) {
self._visible = YES;
self.type = ObjectNodeTypeArray;
self.children = [NSMutableArray new];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
ObjectNode *childNode = [[ObjectNode alloc] initWithObject:obj];
childNode.parent = self;
[self.children addObject:childNode];
}];
}
return self;
}
- (instancetype)initWithObject:(id)obj {
if (obj == nil) {
return nil;
}
ObjectNodeType type = [ObjectNode _typeForObject:obj];
if (type == ObjectNodeTypeDictionary) {
return [self initWithDictionary:obj];
}
if (type == ObjectNodeTypeArray) {
return [self initWithArray:obj];
}
self = [super init];
if (self) {
self._visible = YES;
self.type = type;
self.value = obj;
}
return self;
}
- (instancetype)initWithPropertyList:(id)obj {
return [self initWithObject:obj];
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.key forKey:@"key"];
[aCoder encodeObject:@(self.type) forKey:@"type"];
[aCoder encodeObject:self.value forKey:@"value"];
[aCoder encodeObject:self.children forKey:@"children"];
}
- (NSString *)description {
NSMutableString *builder = [NSMutableString stringWithFormat:@"<%@ %p", NSStringFromClass([self class]), self];
if (self.key != nil) {
[builder appendFormat:@" key: “%@”", self.key];
}
[builder appendFormat:@" type: “%@”", [ObjectNode stringForType:self.type]];
if (self.type == ObjectNodeTypeDictionary || self.type == ObjectNodeTypeArray) {
[builder appendFormat:@" children: “%lu items”", self.children.count];
}else {
[builder appendFormat:@" value: “%@”", [self.value description]];
}
[builder appendString:@">"];
return builder;
}
- (NSDictionary *)_dictionaryObject {
NSMutableDictionary *rv = [NSMutableDictionary new];
[self.children enumerateObjectsUsingBlock:^(ObjectNode *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
rv[obj.key] = obj.propertyList;
}];
return rv.copy;
}
- (NSArray *)_arrayObject {
NSMutableArray *rv = [NSMutableArray new];
[self.children enumerateObjectsUsingBlock:^(ObjectNode *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
[rv addObject:obj.propertyList];
}];
return rv.copy;
}
- (id)propertyList {
if (self.type == ObjectNodeTypeDictionary) {
return self._dictionaryObject;
}
if (self.type == ObjectNodeTypeArray) {
return self._arrayObject;
}
return self.value;
}
- (ObjectNode *)childNodeContainingDescendantNode:(ObjectNode *)descendantNode;
{
ObjectNode *parent = descendantNode;
while (parent != nil && [self.children containsObject:parent] == NO) {
parent = parent.parent;
}
return parent;
}
- (ObjectNode *)childNodeForKey:(NSString *)key {
return [self.children filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"key == %@", key]].firstObject;
}
- (ObjectNode *)childNodeForVisibleKey:(NSString *)key {
for (ObjectNode *childNode in self.children) {
if ([childNode.visibleKey isEqualToString:key]) {
return childNode;
}
}
return nil;
}
- (void)_sortUsingDescriptors:(NSArray <NSSortDescriptor *> *)descriptors {
if (self.type != ObjectNodeTypeDictionary) {
return;
}
[self.children sortUsingDescriptors:descriptors];
for (ObjectNode *child in self.children) {
[child _sortUsingDescriptors:descriptors];
}
}
- (BOOL)isContainerNode {
return self.type == ObjectNodeTypeArray || self.type == ObjectNodeTypeDictionary;
}
- (BOOL)isLeafNode {
return !self.isContainerNode;
}
- (BOOL)isNodeOfArray {
return self.parent.type == ObjectNodeTypeArray;
}
- (BOOL)isVisible {
return self._visible;
}
- (NSString *)_visibleKey {
if (!__visibleKey) {
__visibleKey = [[NSUUID UUID] UUIDString];
}
return __visibleKey;
}
- (NSString *)visibleKey {
return self._visibleKey;
}
- (NSInteger)levelOfDescendants {
return self._levelOfDescendants;
}
- (void)setExpanded:(BOOL)expanded {
[self setExpanded:expanded recursively:NO];
}
- (void)setExpanded:(BOOL)expanded recursively:(BOOL)recursively {
if (![self isContainerNode]) {
return;
}
if (_expanded != expanded || recursively) {
_expanded = expanded;
ObjectNode *parent = self;
while ((parent = parent.parent)) {
parent._hasCachedNumberOfDescendants = NO;
}
[self _invalidateNumberOfDescendantsRecursively:!recursively];
if (expanded || recursively) {
for (ObjectNode *childNode in self.children) {
if (expanded) {
childNode._levelOfDescendants = self.levelOfDescendants + 1;
}
if (recursively) {
[childNode setExpanded:expanded recursively:recursively];
}
}
}
}
}
- (NSInteger)numberOfDescendants {
if (self._hasCachedNumberOfDescendants) {
return self._cachedNumberOfDescendants;
}
NSInteger num = self.isVisible ? 1 : 0;
if (self.isVisible && self.isExpanded && [self isContainerNode]) {
for (ObjectNode *childNode in self.children) {
num += childNode.numberOfDescendants;
}
}
self._cachedNumberOfDescendants = num;
self._hasCachedNumberOfDescendants = YES;
return num;
}
- (void)_invalidateNumberOfDescendants {
[self _invalidateNumberOfDescendantsRecursively:NO];
}
- (void)_invalidateNumberOfDescendantsRecursively:(BOOL)recursively {
self._hasCachedNumberOfDescendants = NO;
if (recursively) {
for (ObjectNode *childNode in self.children) {
[childNode _invalidateNumberOfDescendantsRecursively:recursively];
}
}
}
- (ObjectNode *)descendantNodeAtIndex:(NSInteger)index {
if (index == 0) {
return self;
}
NSInteger lowerBound = 1;
for (ObjectNode *childNode in self.children) {
NSInteger upperBound = lowerBound + childNode.numberOfDescendants;
if (index >= lowerBound && index < upperBound) {
return [childNode descendantNodeAtIndex:index - lowerBound];
}
lowerBound = upperBound;
}
NSAssert(NO, @"out of range");
return nil;
}
- (NSInteger)indexOfDescendantNode:(ObjectNode *)descendantNode {
if (descendantNode == self) {
return 0;
}
NSInteger offset = 1;
for (ObjectNode *childNode in self.children) {
NSInteger indexFound = [childNode indexOfDescendantNode:descendantNode];
if (indexFound != NSNotFound) {
return offset + indexFound;
} else {
offset += childNode.numberOfDescendants;
}
}
return NSNotFound;
}
- (ObjectNode *)descendantNodeForVisibleKey:(NSString *)visibleKey {
if ([self._visibleKey isEqualToString:visibleKey]) {
return self;
}
for (ObjectNode *childNode in self.children) {
ObjectNode *targetNode = [childNode descendantNodeForVisibleKey:visibleKey];
if (targetNode) {
return targetNode;
}
}
return nil;
}
- (void)updateVisibleStateWithPredicate:(NSPredicate *)predicate {
BOOL matched = NO;
if (!matched && !self.isNodeOfArray) {
matched = [predicate evaluateWithObject:[NSString stringWithFormat:@"%@", [ObjectNode stringKeyOfNode:self]]];
}
if (!matched && self.isLeafNode) {
matched = [predicate evaluateWithObject:[NSString stringWithFormat:@"%@", [ObjectNode stringValueOfNode:self]]];
}
self._visible = matched;
if (matched) {
ObjectNode *parent = self;
while ((parent = parent.parent)) {
parent._visible = matched;
}
}
[self _invalidateNumberOfDescendantsRecursively:NO];
for (ObjectNode *childNode in self.children) {
[childNode updateVisibleStateWithPredicate:predicate];
}
}
- (void)expandToDescendantNode:(ObjectNode *)descendantNode {
NSMutableArray <ObjectNode *> *nodesToExpand = [NSMutableArray array];
ObjectNode *parent = descendantNode;
while (parent != nil && parent != self) {
parent = parent.parent;
[nodesToExpand addObject:parent];
}
[nodesToExpand addObject:parent];
for (ObjectNode *node in [nodesToExpand reverseObjectEnumerator]) {
[node setExpanded:YES];
}
}
- (instancetype)copyWithZone:(NSZone *)zone {
// Objective-C no longer uses zone.
ObjectNode *newNode = [[ObjectNode alloc] init];
newNode.key = [self.key copy];
newNode.value = [self.value copy];
newNode.type = self.type;
// Expand state is not copied.
// Visible state is copied.
newNode._visible = self._visible;
newNode._visibleKey = [self._visibleKey copy];
NSMutableArray *children = [[NSMutableArray alloc] initWithCapacity:self.children.count];
for (ObjectNode *childNode in self.children) {
ObjectNode *newChildNode = [childNode copy];
newChildNode.parent = newNode;
[children addObject:newChildNode];
}
newNode.children = [children copy];
return newNode;
}
@end