-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSTDebugKitModuleCoreDataViewer.m
More file actions
500 lines (409 loc) · 16.1 KB
/
STDebugKitModuleCoreDataViewer.m
File metadata and controls
500 lines (409 loc) · 16.1 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
//
// STDebugKitModuleCoreDataViewer.m
// STDebugKitDemo
//
// Created by Thomas Dupont on 17/09/13.
// Copyright (c) 2013 iSofTom. All rights reserved.
//
#import "STDebugKitModuleCoreDataViewer.h"
#import <QuartzCore/QuartzCore.h>
@interface STDebugKitModuleCoreDataObjectViewer () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) NSMutableArray* attributes;
@property (nonatomic, strong) NSMutableArray* attributesValues;
@property (nonatomic, strong) NSMutableArray* relationships;
@property (nonatomic, strong) NSMutableArray* relationshipsValues;
@end
@implementation STDebugKitModuleCoreDataObjectViewer
- (id)init
{
self = [super init];
if (self)
{
self.usingContext = [NSManagedObjectContext defaultContext];
self.attributes = [[NSMutableArray alloc] init];
self.attributesValues = [[NSMutableArray alloc] init];
self.relationships = [[NSMutableArray alloc] init];
self.relationshipsValues = [[NSMutableArray alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
#ifdef __IPHONE_7_0
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
#endif
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[self.tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];
[self.view addSubview:self.tableView];
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.attributes removeAllObjects];
[self.attributesValues removeAllObjects];
[self.relationships removeAllObjects];
[self.relationshipsValues removeAllObjects];
NSManagedObject* object = [self.usingContext objectWithID:self.objectId];
NSEntityDescription* entity = object.entity;
NSDictionary* attributes = [entity attributesByName];
for (NSString* attribute in [attributes allKeys])
{
NSObject* value = [object valueForKey:attribute];
if (value)
{
NSString* valueStr = [NSString stringWithFormat:@"%@", value];
if (valueStr)
{
[self.attributes addObject:attribute];
[self.attributesValues addObject:valueStr];
}
}
}
NSDictionary* relationships = [entity relationshipsByName];
for (NSString* relationship in [relationships allKeys])
{
NSObject* value = [object valueForKey:relationship];
[self.relationships addObject:relationship];
if ([value isKindOfClass:[NSManagedObject class]])
{
[self.relationshipsValues addObject:[(NSManagedObject*)value objectID]];
}
else if ([value isKindOfClass:[NSSet class]] || [value isKindOfClass:[NSOrderedSet class]])
{
NSRelationshipDescription* desc = [relationships objectForKey:relationship];
NSEntityDescription* entity = desc.destinationEntity;
NSRelationshipDescription* invert = desc.inverseRelationship;
NSString* entityName = entity.name;
if (entityName && invert)
{
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"%K = %@", invert.name, object];
[self.relationshipsValues addObject:@[entityName, predicate]];
}
else
{
[self.relationshipsValues addObject:[NSNull null]];
}
}
else
{
[self.relationshipsValues addObject:[NSNull null]];
}
}
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return [self.attributes count];
}
else
{
return [self.relationships count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifier = @"identifier";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}
if (indexPath.section == 0)
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
cell.textLabel.text = [self.attributes objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [self.attributesValues objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = [self.relationships objectAtIndex:indexPath.row];
cell.detailTextLabel.text = nil;
if ([[self.relationshipsValues objectAtIndex:indexPath.row] isKindOfClass:[NSManagedObjectID class]])
{
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
else if ([[self.relationshipsValues objectAtIndex:indexPath.row] isKindOfClass:[NSArray class]])
{
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
else
{
if ([[self.relationshipsValues objectAtIndex:indexPath.row] isKindOfClass:[NSManagedObjectID class]])
{
NSManagedObjectID* objectId = [self.relationshipsValues objectAtIndex:indexPath.row];
STDebugKitModuleCoreDataObjectViewer* viewer = [[STDebugKitModuleCoreDataObjectViewer alloc] init];
viewer.usingContext = self.usingContext;
viewer.objectId = objectId;
[self.navigationController pushViewController:viewer animated:YES];
}
else if ([[self.relationshipsValues objectAtIndex:indexPath.row] isKindOfClass:[NSArray class]])
{
NSArray* array = [self.relationshipsValues objectAtIndex:indexPath.row];
NSString* entityName = [array objectAtIndex:0];
NSPredicate* predicate = [array objectAtIndex:1];
STDebugKitModuleCoreDataPredicateViewer* viewer = [[STDebugKitModuleCoreDataPredicateViewer alloc] init];
viewer.usingContext = self.usingContext;
viewer.predicate = predicate;
viewer.entityClass = NSClassFromString(entityName);
[self.navigationController pushViewController:viewer animated:YES];
}
else
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 24;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return @"Attributes";
}
else
{
return @"Relationships";
}
}
@end
@interface STDebugKitModuleCoreDataPredicateViewer () <UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) NSFetchedResultsController* fetchedResultsController;
@property (nonatomic, strong) NSString* sortKey;
@property (nonatomic, strong) UIButton* attributeButton;
@property (nonatomic, strong) UIView* touchCatcherView;
@property (nonatomic, strong) UIPickerView* pickerView;
@property (nonatomic, strong) NSMutableArray* data;
@end
@implementation STDebugKitModuleCoreDataPredicateViewer
- (id)init
{
self = [super init];
if (self)
{
self.usingContext = [NSManagedObjectContext defaultContext];
self.data = [[NSMutableArray alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
#ifdef __IPHONE_7_0
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}
#endif
[self.view setBackgroundColor:[UIColor whiteColor]];
self.attributeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.attributeButton setFrame:CGRectMake(20, 10, self.view.bounds.size.width - 40, 40)];
[self.attributeButton setAutoresizingMask:(UIViewAutoresizingFlexibleWidth)];
[self.attributeButton setTitle:@"Choose Attribute" forState:UIControlStateNormal];
[self.attributeButton addTarget:self action:@selector(handleAttributeButtonTap:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.attributeButton];
NSArray* digits = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
if ([digits count] > 0 && [digits[0] intValue] > 6)
{
[self.attributeButton setBackgroundColor:[UIColor whiteColor]];
[self.attributeButton.layer setCornerRadius:5];
[self.attributeButton.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[self.attributeButton.layer setBorderWidth:1];
}
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height - 60)];
[self.tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];
[self.view addSubview:self.tableView];
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
self.touchCatcherView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.touchCatcherView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];
[self.view addSubview:self.touchCatcherView];
[self.touchCatcherView setBackgroundColor:[UIColor blackColor]];
[self.touchCatcherView setAlpha:0.3f];
[self.touchCatcherView setHidden:YES];
[self.touchCatcherView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchCatcherTap:)]];
self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 216, self.view.bounds.size.width, 216)];
[self.pickerView setBackgroundColor:[UIColor whiteColor]];
[self.pickerView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin)];
[self.view addSubview:self.pickerView];
[self.pickerView setDataSource:self];
[self.pickerView setDelegate:self];
[self.pickerView setHidden:YES];
}
- (void)reloadFetchedResultsController
{
self.fetchedResultsController = nil;
if (self.sortKey)
{
self.title = [NSString stringWithFormat:@"%i object(s)", [self.entityClass countOfEntitiesWithPredicate:self.predicate inContext:self.usingContext]];
self.fetchedResultsController = [self.entityClass fetchAllSortedBy:self.sortKey ascending:YES withPredicate:self.predicate groupBy:nil delegate:self inContext:self.usingContext];
}
[self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.data removeAllObjects];
NSEntityDescription* entity = [NSEntityDescription entityForName: NSStringFromClass(self.entityClass) inManagedObjectContext:self.usingContext];
NSDictionary* attributes = [entity attributesByName];
for (NSString* attribute in [attributes allKeys])
{
[self.data addObject:attribute];
}
if ([self.data count] > 0)
{
[self.pickerView reloadAllComponents];
[self updateAttributeButtonWithTitleAtIndex:0];
}
else
{
[[[UIAlertView alloc] initWithTitle:@"Error" message:@"No attributes found" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil] show];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if ([[self.fetchedResultsController sections] count] > 0)
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
else
{
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifier = @"identifier";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
NSManagedObject* object = [self.fetchedResultsController objectAtIndexPath:indexPath];
id o = [object valueForKey:self.sortKey];
cell.textLabel.text = [NSString stringWithFormat:@"%@", o];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject* object = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (object)
{
NSManagedObjectID* objectId = object.objectID;
STDebugKitModuleCoreDataObjectViewer* viewer = [[STDebugKitModuleCoreDataObjectViewer alloc] init];
viewer.usingContext = self.usingContext;
viewer.objectId = objectId;
[self.navigationController pushViewController:viewer animated:YES];
}
else
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView reloadData];
}
- (void)displayPicker
{
__block CGRect pickerFrame = self.pickerView.frame;
pickerFrame.origin.y = self.view.bounds.size.height;
self.pickerView.frame = pickerFrame;
self.touchCatcherView.alpha = 0.0f;
self.pickerView.hidden = NO;
self.touchCatcherView.hidden = NO;
[UIView animateWithDuration:0.3 animations:^{
pickerFrame.origin.y = self.view.bounds.size.height - pickerFrame.size.height;
self.pickerView.frame = pickerFrame;
self.touchCatcherView.alpha = 0.3;
}];
}
- (void)hidePicker
{
[UIView animateWithDuration:0.3 animations:^{
CGRect pickerFrame = self.pickerView.frame;
pickerFrame.origin.y = self.view.bounds.size.height;
self.pickerView.frame = pickerFrame;
self.touchCatcherView.alpha = 0.0f;
} completion:^(BOOL finished) {
self.pickerView.hidden = YES;
self.touchCatcherView.hidden = YES;
}];
}
- (void)handleAttributeButtonTap:(UIButton*)button
{
[self displayPicker];
}
- (void)handleTouchCatcherTap:(UITapGestureRecognizer*)tap
{
[self hidePicker];
}
- (void)updateAttributeButtonWithTitleAtIndex:(NSInteger)index
{
NSString* key = [self.data objectAtIndex:index];
self.sortKey = key;
[self.attributeButton setTitle:key forState:UIControlStateNormal];
[self reloadFetchedResultsController];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.data count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.data objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[self updateAttributeButtonWithTitleAtIndex:row];
}
@end