-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectTableViewController.m
443 lines (391 loc) · 20.7 KB
/
ObjectTableViewController.m
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
//
// ObjectTableViewController.m
// CommonViewControllers
//
// Created by Lessica <[email protected]> on 2022/1/20.
// Copyright © 2022 Zheng Wu. All rights reserved.
//
#import "ObjectTableViewController.h"
#import "ObjectNode.h"
#import "ObjectNode-Private.h"
#import "ObjectCell.h"
@interface ObjectTableViewController () <UISearchResultsUpdating>
@property (nonatomic, strong) ObjectNode *rootNode;
@property (nonatomic, strong) ObjectNode *rootNodeForSearch;
@property (nonatomic, strong) UISearchController *searchController;
@end
@implementation ObjectTableViewController
+ (NSString *)viewerName {
return NSLocalizedString(@"Object Viewer", @"ObjectTableViewController");
}
+ (id)objectWithContentsOfPath:(NSString *)path {
NSError *readError = nil;
NSData *data = [NSData dataWithContentsOfFile:path options:kNilOptions error:&readError];
if (!data) return nil;
id object = nil;
object = [NSPropertyListSerialization propertyListWithData:data options:kNilOptions format:nil error:&readError];
if (object) return object;
object = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&readError];
if (object) return object;
return nil;
}
- (instancetype)initWithPath:(NSString *)path {
if (self = [super init]) {
_indentationWidth = 14.0;
_entryPath = path;
[self setupWithPath];
}
return self;
}
- (instancetype)initWithObject:(id)object {
if (self = [super init]) {
_indentationWidth = 14.0;
_object = object;
[self setupWithObject];
}
return self;
}
- (void)setupWithPath {
_object = [ObjectTableViewController objectWithContentsOfPath:_entryPath];
[self setupWithObject];
}
- (void)setupWithObject {
_rootNode = [[ObjectNode alloc] initWithPropertyList:_object];
[_rootNode _sortUsingDescriptors:@[
[NSSortDescriptor sortDescriptorWithKey:@"key" ascending:YES],
]];
if (self.initialRootExpanded) {
[_rootNode setExpanded:YES recursively:NO];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
if (self.title.length == 0) {
if (self.entryPath) {
NSString *entryName = [self.entryPath lastPathComponent];
self.title = entryName;
} else {
self.title = [[self class] viewerName];
}
}
self.view.backgroundColor = [UIColor systemBackgroundColor];
self.searchController = ({
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.obscuresBackgroundDuringPresentation = NO;
searchController.hidesNavigationBarDuringPresentation = YES;
searchController;
});
if (self.pullToReload && self.entryPath) {
self.refreshControl = ({
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(reloadDataFromEntry:) forControlEvents:UIControlEventValueChanged];
refreshControl;
});
}
if (self.allowSearch) {
self.navigationItem.hidesSearchBarWhenScrolling = YES;
self.navigationItem.searchController = self.searchController;
}
UINavigationBarAppearance *newNavBarAppearance = [[UINavigationBarAppearance alloc] init];
[newNavBarAppearance configureWithOpaqueBackground];
[self.navigationController.navigationBar setScrollEdgeAppearance:newNavBarAppearance];
if (@available(iOS 15.0, *)) {
[self.navigationController.navigationBar setCompactScrollEdgeAppearance:newNavBarAppearance];
}
[self.tableView registerClass:[ObjectCell class] forCellReuseIdentifier:@"ObjectCell"];
if (self.initialRootExpanded) {
[self.rootNode setExpanded:YES recursively:NO];
}
}
- (void)reloadDataFromEntry:(UIRefreshControl *)sender {
if (self.searchController.isActive) {
return;
}
[self loadDataFromEntry];
if ([sender isRefreshing]) {
[sender endRefreshing];
}
}
- (void)loadDataFromEntry {
[self setupWithPath];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.searchController.isActive ? self.rootNodeForSearch.numberOfDescendants : self.rootNode.numberOfDescendants;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [UIView new];
}
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
ObjectNode *cellNode = [(self.searchController.isActive ? self.rootNodeForSearch : self.rootNode) descendantNodeAtIndex:indexPath.row];
return cellNode.levelOfDescendants;
}
- (nullable UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point {
if (self.pressToCopy) {
ObjectNode *rootNode = self.searchController.isActive ? self.rootNodeForSearch : self.rootNode;
ObjectNode *cellNode = [rootNode descendantNodeAtIndex:indexPath.row];
NSMutableArray <UIAction *> *cellActions = [NSMutableArray array];
if (cellNode.isLeafNode) {
NSString *strValue = [NSString stringWithFormat:@"%@", [ObjectNode stringValueOfNode:cellNode]];
if (cellNode != rootNode && !cellNode.isNodeOfArray) {
NSString *strKey = [NSString stringWithFormat:@"%@", [ObjectNode stringKeyOfNode:cellNode]];
[cellActions addObject:[UIAction actionWithTitle:NSLocalizedString(@"Copy Key", @"ObjectTableViewController") image:[UIImage systemImageNamed:@"doc.on.doc"] identifier:nil handler:^(__kindof UIAction *_Nonnull action) {
[[UIPasteboard generalPasteboard] setString:strKey];
}]];
}
[cellActions addObject:[UIAction actionWithTitle:NSLocalizedString(@"Copy Value", @"ObjectTableViewController") image:[UIImage systemImageNamed:@"doc.on.doc.fill"] identifier:nil handler:^(__kindof UIAction *_Nonnull action) {
[[UIPasteboard generalPasteboard] setString:strValue];
}]];
} else {
if (cellNode != rootNode) {
NSString *strKey = [NSString stringWithFormat:@"%@", [ObjectNode stringKeyOfNode:cellNode]];
[cellActions addObject:[UIAction actionWithTitle:NSLocalizedString(@"Copy Key", @"ObjectTableViewController") image:[UIImage systemImageNamed:@"doc.on.doc"] identifier:nil handler:^(__kindof UIAction *_Nonnull action) {
[[UIPasteboard generalPasteboard] setString:strKey];
}]];
}
if (!self.searchController.isActive) {
if (cellNode.isExpanded) {
[cellActions addObject:[UIAction actionWithTitle:NSLocalizedString(@"Collapse Recursively", @"ObjectTableViewController") image:[UIImage systemImageNamed:@"arrow.down.right.and.arrow.up.left"] identifier:nil handler:^(__kindof UIAction *_Nonnull action) {
[self tableView:tableView triggerNodeAtIndexPath:indexPath recursively:YES];
}]];
} else {
[cellActions addObject:[UIAction actionWithTitle:NSLocalizedString(@"Expand Recursively", @"ObjectTableViewController") image:[UIImage systemImageNamed:@"arrow.up.left.and.arrow.down.right"] identifier:nil handler:^(__kindof UIAction *_Nonnull action) {
[self tableView:tableView triggerNodeAtIndexPath:indexPath recursively:YES];
}]];
}
}
}
if (self.searchController.isActive) {
[cellActions addObject:[UIAction actionWithTitle:NSLocalizedString(@"Focus", @"ObjectTableViewController") image:[UIImage systemImageNamed:@"scope"] identifier:nil handler:^(__kindof UIAction *_Nonnull action) {
ObjectNode *targetNode = [self.rootNode descendantNodeForVisibleKey:cellNode.visibleKey];
if (targetNode) {
[self.rootNode expandToDescendantNode:targetNode];
NSInteger targetIndex = [self.rootNode indexOfDescendantNode:targetNode];
NSIndexPath *targetPath = [NSIndexPath indexPathForRow:targetIndex inSection:0];
if (targetIndex != NSNotFound) {
[self.searchController.searchBar setText:@""];
[self.searchController dismissViewControllerAnimated:YES completion:^{
[tableView reloadData];
[tableView selectRowAtIndexPath:targetPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}];
}
}
}]];
}
return [UIContextMenuConfiguration configurationWithIdentifier:nil previewProvider:nil actionProvider:^UIMenu *_Nullable (NSArray<UIMenuElement *> *_Nonnull suggestedActions) {
UIMenu *menu = [UIMenu menuWithTitle:(self.showTypeHint ? [NSString stringWithFormat:NSLocalizedString(@"<%@: %p>", @"ObjectTableViewController"), NSStringFromClass([cellNode.propertyList class]), cellNode.propertyList] : @"") children:cellActions];
return menu;
}];
}
return nil;
}
- (void)tableView:(UITableView *)tableView triggerNodeAtIndexPath:(NSIndexPath *)indexPath recursively:(BOOL)recursively {
if (self.searchController.isActive) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
return;
}
ObjectNode *cellNode = [(self.searchController.isActive ? self.rootNodeForSearch : self.rootNode) descendantNodeAtIndex:indexPath.row];
if (cellNode.isLeafNode) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} else {
NSInteger beforeCount = cellNode.numberOfDescendants;
if (cellNode.isExpanded) {
[cellNode setExpanded:NO recursively:recursively];
} else {
[cellNode setExpanded:YES recursively:recursively];
}
[tableView beginUpdates];
NSInteger afterCount = cellNode.numberOfDescendants;
if (afterCount != beforeCount) {
if (afterCount > beforeCount) {
// add
NSInteger changeCount = afterCount - beforeCount;
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:changeCount];
for (NSInteger i = 0; i < changeCount; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:indexPath.row + 1 + i inSection:0]];
}
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
} else {
// delete
NSInteger changeCount = beforeCount - afterCount;
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:changeCount];
for (NSInteger i = 0; i < changeCount; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:indexPath.row + 1 + i inSection:0]];
}
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
}
}
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self tableView:tableView triggerNodeAtIndexPath:indexPath recursively:NO];
}
- (UIColor *)cachedCellBackgroundColorForIndentationLevel:(NSInteger)level referenceColor:(UIColor *)refColor {
static NSMutableDictionary <NSNumber *, UIColor *> *cachedColors = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cachedColors = [[NSMutableDictionary alloc] init];
});
NSNumber *cachedKey = @(level);
if (cachedColors[cachedKey]) {
return cachedColors[cachedKey];
}
CGFloat brightnessDelta = level * 0.025;
CGFloat lightHue, lightSaturation, lightBrightness, lightAlpha;
CGFloat darkHue, darkSaturation, darkBrightness, darkAlpha;
UIColor *resolvedLight = [refColor resolvedColorWithTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]];
UIColor *updatedLight = nil;
{
BOOL converted = [resolvedLight getHue:&lightHue saturation:&lightSaturation brightness:&lightBrightness alpha:&lightAlpha];
if (converted) {
lightBrightness -= brightnessDelta;
updatedLight = [UIColor colorWithHue:lightHue saturation:lightSaturation brightness:lightBrightness alpha:lightAlpha];
}
}
UIColor *resolvedDark = [refColor resolvedColorWithTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark]];
UIColor *updatedDark = nil;
{
BOOL converted = [resolvedDark getHue:&darkHue saturation:&darkSaturation brightness:&darkBrightness alpha:&darkAlpha];
if (converted) {
darkBrightness += brightnessDelta;
updatedDark = [UIColor colorWithHue:darkHue saturation:darkSaturation brightness:darkBrightness alpha:darkAlpha];
}
}
if (!updatedDark || !updatedLight) {
return nil;
}
UIColor *color = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
return updatedDark;
} else {
return updatedLight;
}
}];
cachedColors[cachedKey] = color;
return color;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ObjectCell *cell = (ObjectCell *)[tableView dequeueReusableCellWithIdentifier:@"ObjectCell" forIndexPath:indexPath];
cell.indentationWidth = self.indentationWidth;
ObjectNode *rootNode = self.searchController.isActive ? self.rootNodeForSearch : self.rootNode;
ObjectNode *cellNode = [rootNode descendantNodeAtIndex:indexPath.row];
if (self.coloredIndentation) {
UIColor *bgColor = [self cachedCellBackgroundColorForIndentationLevel:cellNode.levelOfDescendants referenceColor:tableView.backgroundColor];
if (bgColor) {
if (@available(iOS 14.0, *)) {
UIBackgroundConfiguration *bgConf = [UIBackgroundConfiguration listPlainCellConfiguration];
[bgConf setBackgroundColor:bgColor];
[cell setBackgroundConfiguration:bgConf];
} else {
[cell setBackgroundColor:bgColor];
}
}
}
NSString *cellKey;
BOOL isRootNode;
if (cellNode == rootNode) {
cellKey = NSLocalizedString(@"Root", @"ObjectTableViewController");
isRootNode = YES;
} else {
cellKey = [ObjectNode stringKeyOfNode:cellNode];
isRootNode = NO;
}
{
NSMutableAttributedString *attrKey = nil;
NSString *strKey = nil;
if ([cellNode isContainerNode]) {
strKey = [NSString stringWithFormat:@"%@ %@", ([cellNode isExpanded] ? @"▼" : @"▶"), cellKey];
attrKey = [[NSMutableAttributedString alloc] initWithString:strKey attributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:[UIFont systemFontSize]],
NSForegroundColorAttributeName: cellNode.isNodeOfArray ? [UIColor linkColor] : [UIColor labelColor],
}];
} else {
strKey = [NSString stringWithFormat:@"%@", cellKey];
attrKey = [[NSMutableAttributedString alloc] initWithString:strKey attributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:[UIFont systemFontSize]],
NSForegroundColorAttributeName: cellNode.isNodeOfArray ? [UIColor linkColor] : [UIColor labelColor],
}];
}
if (!isRootNode && !cellNode.isNodeOfArray && self.searchController.isActive) {
NSString *searchContent = self.searchController.searchBar.text;
NSRange searchRange = [strKey rangeOfString:searchContent options:NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch range:NSMakeRange(0, strKey.length)];
if (searchRange.location != NSNotFound) {
[attrKey addAttributes:@{
NSForegroundColorAttributeName: [UIColor colorWithDynamicProvider:^UIColor *_Nonnull (UITraitCollection *_Nonnull traitCollection) {
if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
return [UIColor systemBackgroundColor];
} else {
return [UIColor labelColor];
}
}],
NSBackgroundColorAttributeName: [UIColor colorWithRed:253.0/255.0 green:247.0/255.0 blue:148.0/255.0 alpha:1.0],
} range:searchRange];
}
}
[cell.textLabel setAttributedText:attrKey];
}
{
NSMutableAttributedString *attrValue = nil;
NSString *strVal = [NSString stringWithFormat:@"%@", [ObjectNode stringValueOfNode:cellNode]];
if ([cellNode isLeafNode]) {
attrValue = [[NSMutableAttributedString alloc] initWithString:strVal attributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:[UIFont systemFontSize]],
NSForegroundColorAttributeName: [UIColor secondaryLabelColor],
}];
} else {
attrValue = [[NSMutableAttributedString alloc] initWithString:strVal attributes:@{
NSFontAttributeName: [UIFont italicSystemFontOfSize:[UIFont systemFontSize]],
NSForegroundColorAttributeName: [UIColor secondaryLabelColor],
}];
}
if (cellNode.isLeafNode && self.searchController.isActive) {
NSString *searchContent = self.searchController.searchBar.text;
NSRange searchRange = [strVal rangeOfString:searchContent options:NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch range:NSMakeRange(0, strVal.length)];
if (searchRange.location != NSNotFound) {
[attrValue addAttributes:@{
NSForegroundColorAttributeName: [UIColor colorWithDynamicProvider:^UIColor *_Nonnull (UITraitCollection *_Nonnull traitCollection) {
if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
return [UIColor systemBackgroundColor];
} else {
return [UIColor labelColor];
}
}],
NSBackgroundColorAttributeName: [UIColor colorWithRed:253.0/255.0 green:247.0/255.0 blue:148.0/255.0 alpha:1.0],
} range:searchRange];
}
}
[cell.detailTextLabel setAttributedText:attrValue];
}
return cell;
}
#pragma mark - Lazy Getters
- (ObjectNode *)rootNodeForSearch {
if (!_rootNodeForSearch) {
_rootNodeForSearch = [_rootNode copy];
[_rootNodeForSearch setExpanded:YES recursively:YES];
}
return _rootNodeForSearch;
}
#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *text = self.searchController.searchBar.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", text];
if (predicate) {
[self.rootNodeForSearch updateVisibleStateWithPredicate:predicate];
}
[self.tableView reloadData];
}
#pragma mark -
- (void)dealloc {
#if DEBUG
NSLog(@"- [%@ dealloc]", [self class]);
#endif
}
@end