forked from danielctull/DataCenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCTManagedObjectViewController.m
261 lines (181 loc) · 7.91 KB
/
DCTManagedObjectViewController.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
//
// DCTManagedObjectViewController.m
// DCTCoreDataBrowser
//
// Created by Daniel Tull on 23.02.2011.
// Copyright 2011 Daniel Tull. All rights reserved.
//
NSInteger const DCTManagedObjectViewControllerAttributeSection = 1;
NSInteger const DCTManagedObjectViewControllerRelationshipSection = 2;
#import "DCTManagedObjectViewController.h"
#import "DCTDatePickerViewController.h"
#import "DCTManagedObjectRelationshipsViewController.h"
#import "NSManagedObject+DCTNiceDescription.h"
@interface DCTManagedObjectViewController ()
@end
@implementation DCTManagedObjectViewController
@synthesize managedObject;
#pragma mark - NSObject
- (id)init {
return [self initWithStyle:UITableViewStyleGrouped];
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
#pragma mark - DCTManagedObjectViewController
- (void)setManagedObject:(NSManagedObject *)mo {
NSManagedObject *oldManagedObject = managedObject;
managedObject = [mo retain];
[oldManagedObject release];
NSEntityDescription *entity = [managedObject entity];
self.title = [entity name];
[relationships release];
relationships = [[[entity relationshipsByName] allKeys] retain];
[attributes release];
attributes = [[[entity attributesByName] allKeys] retain];
if ([self isViewLoaded])
[self.tableView reloadData];
}
#pragma mark - Editing
@synthesize textField = _textField;
- (UITextField *)textField;
{
if (!_textField)
{
_textField = [[UITextField alloc] init];
_textField.borderStyle = UITextBorderStyleNone;
_textField.returnKeyType = UIReturnKeyDone;
_textField.delegate = self;
}
return _textField;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *attrName = [attributes objectAtIndex:textField.tag];
[self.managedObject setValue:textField.text forKey:attrName];
[self.tableView reloadData];
if ([self.managedObject.managedObjectContext save:NULL])
{
[textField removeFromSuperview];
}
return NO;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == DCTManagedObjectViewControllerAttributeSection)
return [attributes count];
if (section == DCTManagedObjectViewControllerRelationshipSection)
return [relationships count];
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == DCTManagedObjectViewControllerAttributeSection)
return @"Attributes";
if (section == DCTManagedObjectViewControllerRelationshipSection)
return @"Relationships";
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *AttributeIdentifier = @"AttributeIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AttributeIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:AttributeIdentifier] autorelease];
if ((NSInteger)indexPath.section == DCTManagedObjectViewControllerAttributeSection) {
NSString *attributeName = [attributes objectAtIndex:indexPath.row];
cell.textLabel.text = attributeName;
cell.detailTextLabel.text = [[managedObject valueForKey:attributeName] description];
BOOL isDate = [[[managedObject.entity attributesByName] objectForKey:attributeName] attributeType] == NSDateAttributeType;
cell.selectionStyle = (isDate ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone);
cell.accessoryType = (isDate ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone);
}
if ((NSInteger)indexPath.section == DCTManagedObjectViewControllerRelationshipSection) {
NSString *relationshipName = [relationships objectAtIndex:indexPath.row];
cell.textLabel.text = relationshipName;
NSRelationshipDescription *relationship = [[[self.managedObject entity] relationshipsByName] objectForKey:relationshipName];
if ([relationship isToMany])
cell.detailTextLabel.text = [NSString stringWithFormat:@"Many %@s", [[relationship destinationEntity] name]];
else
cell.detailTextLabel.text = [[managedObject valueForKey:relationshipName] dct_niceDescription];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ((NSInteger)indexPath.section == DCTManagedObjectViewControllerRelationshipSection) {
NSString *relationshipName = [relationships objectAtIndex:indexPath.row];
NSRelationshipDescription *relationship = [[[self.managedObject entity] relationshipsByName] objectForKey:relationshipName];
if (![relationship isToMany]) {
NSManagedObject *mo = [managedObject valueForKey:relationshipName];
DCTManagedObjectViewController *vc = [[DCTManagedObjectViewController alloc] init];
vc.managedObject = mo;
[self.navigationController pushViewController:vc animated:YES];
[vc release];
} else {
DCTManagedObjectRelationshipsViewController *vc = [[DCTManagedObjectRelationshipsViewController alloc] init];
vc.managedObject = self.managedObject;
vc.relationship = relationship;
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
}
else if (indexPath.section == DCTManagedObjectViewControllerAttributeSection)
{
NSString *attrName = [attributes objectAtIndex:indexPath.row];
NSAttributeDescription *attribute = [[[self.managedObject entity] attributesByName] objectForKey:attrName];
switch (attribute.attributeType)
{
case NSStringAttributeType:
{
UILabel *label = [[tv cellForRowAtIndexPath:indexPath] detailTextLabel];
UITextField *textField = self.textField;
textField.backgroundColor = label.backgroundColor;
textField.text = label.text;
textField.font = label.font;
textField.textColor = label.textColor;
CGRect frame = label.frame;
frame.size.width = label.superview.bounds.size.width - frame.origin.x;
textField.frame = frame;
[label.superview addSubview:textField];
textField.tag = indexPath.row;
[textField becomeFirstResponder];
break;
}
case NSDateAttributeType:
{
DCTDatePickerViewController *detail = [[DCTDatePickerViewController alloc] init];
NSDate *date = [self.managedObject valueForKey:attrName];
if (!date) date = [NSDate date];
detail.date = date;
UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveDate)];
detail.navigationItem.rightBarButtonItem = save;
[save release];
[self.navigationController pushViewController:detail animated:YES];
[detail release];
_editingDateRow = indexPath.row;
}
}
}
}
- (void)saveDate;
{
NSString *attrName = [attributes objectAtIndex:_editingDateRow];
[self.managedObject setValue:[(DCTDatePickerViewController *)self.navigationController.topViewController date]
forKey:attrName];
[self.tableView reloadData];
}
#pragma mark - Lifecycle
- (void)dealloc {
[attributes release], attributes = nil;
[relationships release], relationships = nil;
[managedObject release], managedObject = nil;
[_textField release];
[super dealloc];
}
@end