forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDIManagedTableViewController.m
326 lines (234 loc) · 9.26 KB
/
CDIManagedTableViewController.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
//
// CDIManagedTableViewController.m
// Cheddar for iOS
//
// Created by Sam Soffes on 4/10/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//
#import "CDIManagedTableViewController.h"
#import "CDITableViewCell.h"
#import "UIColor+CheddariOSAdditions.h"
#import "CDILoadingView.h"
@implementation CDIManagedTableViewController {
UITapGestureRecognizer *_tableViewTapGestureRecognizer;
BOOL _allowScrolling;
}
@synthesize pullToRefreshView = _pullToRefreshView;
@synthesize editingIndexPath = _editingIndexPath;
@synthesize keyboardRect = _keyboardRect;
@synthesize coverView = _coverView;
- (UIView *)coverView {
if (!_coverView) {
CGRect frame = self.tableView.bounds;
frame.origin.y += [CDITableViewCell cellHeight];
frame.size.height -= [CDITableViewCell cellHeight];
_coverView = [[UIView alloc] initWithFrame:frame];
_coverView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
_coverView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.9f];
_coverView.alpha = 0.0f;
[_coverView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverViewTapped:)]];
[self.tableView addSubview:_coverView];
}
return _coverView;
}
#pragma mark - NSObject
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
_pullToRefreshView.delegate = nil;
[_pullToRefreshView removeFromSuperview];
_pullToRefreshView = nil;
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = [CDITableViewCell cellHeight];
UIView *background = [[UIView alloc] initWithFrame:CGRectZero];
background.backgroundColor = [UIColor cheddarArchesColor];
self.tableView.backgroundView = background;
SSGradientView *footer = [[SSGradientView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 3.0f)];
footer.backgroundColor = [UIColor clearColor];
footer.colors = [NSArray arrayWithObjects:
[UIColor colorWithWhite:0.937f alpha:1.0f],
[UIColor colorWithWhite:0.937f alpha:0.0f],
nil];
self.tableView.tableFooterView = footer;
_tableViewTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endCellTextEditing)];
_tableViewTapGestureRecognizer.enabled = NO;
_tableViewTapGestureRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:_tableViewTapGestureRecognizer];
_pullToRefreshView = [[CDIPullToRefreshView alloc] initWithScrollView:self.tableView delegate:self];
self.loadingView = [[CDILoadingView alloc] initWithFrame:self.view.bounds];
self.loadingView.userInteractionEnabled = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh:) name:kCDKCurrentUserChangedNotificationName object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
_tableViewTapGestureRecognizer.enabled = editing;
if (!editing) {
[self endCellTextEditing];
}
}
- (void)viewWillAppear:(BOOL)animated {
[self refresh:nil];
[super viewWillAppear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
}
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
#pragma mark - SSManagedViewController
- (void)setLoading:(BOOL)loading animated:(BOOL)animated {
[super setLoading:loading animated:animated];
if (self.loading) {
[self.pullToRefreshView startLoading];
} else {
[self.pullToRefreshView finishLoading];
}
}
- (void)showLoadingView:(BOOL)animated {
if (!self.loadingView || self.loadingView.superview) {
return;
}
self.loadingView.alpha = 0.0f;
self.loadingView.frame = self.view.bounds;
[self.tableView addSubview:self.loadingView];
void (^change)(void) = ^{
self.loadingView.alpha = 1.0f;
};
if (animated) {
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:change completion:nil];
} else {
change();
}
}
- (void)showNoContentView:(BOOL)animated {
if (!self.noContentView || self.noContentView.superview) {
return;
}
self.noContentView.alpha = 0.0f;
self.noContentView.frame = self.view.bounds;
[self.tableView addSubview:self.noContentView];
void (^change)(void) = ^{
self.noContentView.alpha = 1.0f;
};
if (animated) {
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:change completion:nil];
} else {
change();
}
}
#pragma mark - Actions
- (void)refresh:(id)sender {
// Subclasses should override this
}
#pragma mark - Editing
- (void)toggleEditMode:(id)sender {
[self setEditing:!self.editing animated:YES];
}
- (void)editRow:(UIGestureRecognizer *)editingGestureRecognizer {
CDITableViewCell *cell = (CDITableViewCell *)[self.tableView cellForRowAtIndexPath:_editingIndexPath];
cell.editingText = NO;
cell.textField.delegate = nil;
cell = (CDITableViewCell *)editingGestureRecognizer.view;
cell.editingText = YES;
cell.textField.delegate = self;
_editingIndexPath = [self.tableView indexPathForCell:cell];
}
#pragma mark - Keyboard
- (void)updateTableViewOffsets {
CGFloat offset = self.tableView.contentOffset.y;
CGFloat top = fminf(0.0f, offset);
CGFloat bottom = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? self.keyboardRect.size.height : 0.0f;
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(top, 0.0f, bottom, 0.0f);
self.pullToRefreshView.defaultContentInset = UIEdgeInsetsMake(0.0f, 0.0f, bottom, 0.0f);
}
- (void)keyboardDidShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
_keyboardRect = [self.view convertRect:[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
_allowScrolling = YES;
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self updateTableViewOffsets];
// TODO: Once there are flexible row heights, change this to use better calculations
if (_editingIndexPath && _editingIndexPath.row > 2) {
CGRect cellRect = [self.tableView rectForRowAtIndexPath:_editingIndexPath];
CGPoint offset = cellRect.origin;
offset.y -= 51.0f;
[self.tableView setContentOffset:offset animated:NO];
}
} completion:^(BOOL finished) {
_allowScrolling = NO;
}];
}
- (void)keyboardDidHide:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
_keyboardRect = CGRectZero;
[UIView beginAnimations:@"hideKeyboard" context:NULL];
[UIView setAnimationDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
[UIView setAnimationCurve:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]];
[self updateTableViewOffsets];
[UIView commitAnimations];
}
- (void)reachabilityChanged:(NSNotification *)notification {
if ([notification.object isReachable]) {
[self refresh:nil];
}
}
#pragma mark - Private
- (void)endCellTextEditing {
CDITableViewCell *cell = (CDITableViewCell *)[self.tableView cellForRowAtIndexPath:_editingIndexPath];
cell.editingText = NO;
cell.textField.delegate = nil;
_editingIndexPath = nil;
}
- (void)showCoverView {
UIView *coverView = self.coverView;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
coverView.alpha = 1.0f;
} completion:nil];
}
- (BOOL)showingCoverView {
return _coverView != nil;
}
- (void)hideCoverView {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
_coverView.alpha = 0.0f;
} completion:^(BOOL finished) {
[_coverView removeFromSuperview];
_coverView = nil;
}];
}
- (void)coverViewTapped:(id)sender {
// Subclasses should override this method
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self updateTableViewOffsets];
if (_allowScrolling) {
return;
}
[self endCellTextEditing];
}
#pragma mark - SSPullToRefreshViewDelegate
- (void)pullToRefreshViewDidStartLoading:(SSPullToRefreshView *)view {
[self refresh:view];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return NO;
}
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[super controllerDidChangeContent:controller];
if (self.editing && !self.hasContent) {
[self setEditing:NO animated:YES];
}
}
@end