forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDIListsViewController.m
532 lines (402 loc) · 17.4 KB
/
CDIListsViewController.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
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
//
// CDIListsViewController.m
// Cheddar for iOS
//
// Created by Sam Soffes on 3/30/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//
#import "CDIListsViewController.h"
#import "CDIListTableViewCell.h"
#import "CDIListViewController.h"
#import "CDICreateListViewController.h"
#import "CDISettingsViewController.h"
#import "CDISplitViewController.h"
#import "UIColor+CheddariOSAdditions.h"
#import "CDIUpgradeViewController.h"
#import "CDINoListsView.h"
#import "CDIAddListTableViewCell.h"
#import "SMTEDelegateController.h"
#import <SSToolkit/UIScrollView+SSToolkitAdditions.h>
#ifdef CHEDDAR_USE_PASSWORD_FLOW
#import "CDISignUpViewController.h"
#else
#import "CDIWebSignInViewController.h"
#endif
NSString *const kCDISelectedListKey = @"CDISelectedListKey";
@interface CDIListsViewController ()
@property (nonatomic, strong) SMTEDelegateController *textExpander;
- (void)_listUpdated:(NSNotification *)notification;
- (void)_currentUserDidChange:(NSNotification *)notification;
- (void)_createList:(id)sender;
- (void)_cancelAddingList:(id)sender;
- (void)_selectListAtIndexPath:(NSIndexPath *)indexPath newList:(BOOL)newList;
- (void)_checkUser;
@end
@implementation CDIListsViewController {
CDKList *_selectedList;
BOOL _adding;
BOOL _checkForOneList;
}
@synthesize textExpander = _textExpander;
#pragma mark - NSObject
- (void)dealloc {
if (self.textExpander) {
[[NSNotificationCenter defaultCenter] removeObserver:self.textExpander];
}
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *title = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nav-title.png"]];
title.frame = CGRectMake(0.0f, 0.0f, 116.0f, 21.0f);
self.navigationItem.titleView = title;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Lists" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"plus.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(createList:)];
[self setEditing:NO animated:NO];
self.noContentView = [[CDINoListsView alloc] initWithFrame:CGRectZero];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_listUpdated:) name:kCDKListDidUpdateNotificationName object:nil];
}
_checkForOneList = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_currentUserDidChange:) name:kCDKCurrentUserChangedNotificationName object:nil];
if ([SMTEDelegateController isTextExpanderTouchInstalled]) {
self.textExpander = [[SMTEDelegateController alloc] init];
self.textExpander.nextDelegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self.textExpander selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self _checkUser];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[self _checkUser];
}
[SSRateLimit executeBlock:^{
[self refresh:nil];
} name:@"refresh-lists" limit:30.0];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEditMode:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered target:self action:@selector(showSettings:)];
} else {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEditMode:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"plus.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(createList:)];
}
if (!editing && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[self.tableView selectRowAtIndexPath:[self.fetchedResultsController indexPathForObject:_selectedList] animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
}
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
#pragma mark - SSManagedViewController
- (Class)entityClass {
return [CDKList class];
}
- (NSPredicate *)predicate {
return [NSPredicate predicateWithFormat:@"archivedAt = nil && user = %@", [CDKUser currentUser]];
}
#pragma mark - SSManagedTableViewController
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
CDKList *list = [self objectForViewIndexPath:indexPath];
[(CDIListTableViewCell *)cell setList:list];
}
- (NSIndexPath *)viewIndexPathForFetchedIndexPath:(NSIndexPath *)fetchedIndexPath {
if (_adding) {
return [NSIndexPath indexPathForRow:fetchedIndexPath.row + 1 inSection:fetchedIndexPath.section];
}
return fetchedIndexPath;
}
- (NSIndexPath *)fetchedIndexPathForViewIndexPath:(NSIndexPath *)viewIndexPath {
if (_adding) {
return [NSIndexPath indexPathForRow:viewIndexPath.row - 1 inSection:viewIndexPath.section];
}
return viewIndexPath;
}
#pragma mark - CDIManagedTableViewController
- (void)coverViewTapped:(id)sender {
[self _cancelAddingList:sender];
}
#pragma mark - Actions
- (void)refresh:(id)sender {
if (self.loading || ![CDKUser currentUser]) {
return;
}
self.loading = YES;
[[CDKHTTPClient sharedClient] getListsWithSuccess:^(AFJSONRequestOperation *operation, id responseObject) {
dispatch_async(dispatch_get_main_queue(), ^{
self.loading = NO;
});
} failure:^(AFJSONRequestOperation *operation, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[SSRateLimit resetLimitForName:@"refresh-lists"];
self.loading = NO;
});
}];
// Also update their user incase push for updates failed
[[CDKHTTPClient sharedClient] updateCurrentUserWithSuccess:nil failure:nil];
}
- (void)showSettings:(id)sender {
CDISettingsViewController *viewController = [[CDISettingsViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self.navigationController presentModalViewController:navigationController animated:YES];
}
- (void)createList:(id)sender {
if (self.fetchedResultsController.fetchedObjects.count >= 2 && [[CDKUser currentUser] hasPlus].boolValue == NO) {
UIViewController *viewController = [[CDIUpgradeViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self.navigationController presentModalViewController:navigationController animated:YES];
return;
}
[self hideNoContentView:YES];
UIView *coverView = self.coverView;
coverView.frame = self.view.bounds;
[self setEditing:NO animated:YES];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(_cancelAddingList:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Create" style:UIBarButtonItemStyleDone target:self action:@selector(_createList:)];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self.tableView scrollToTopAnimated:NO];
coverView.alpha = 1.0f;
_adding = YES;
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
coverView.frame = CGRectMake(0.0f, [CDIListTableViewCell cellHeight], self.tableView.bounds.size.width, self.tableView.bounds.size.height - [CDIListTableViewCell cellHeight]);
} completion:nil];
return;
}
#pragma mark - Private
- (void)_listUpdated:(NSNotification *)notification {
if ([notification.object isEqual:_selectedList.remoteID] == NO) {
return;
}
if (_selectedList.archivedAt != nil) {
[CDISplitViewController sharedSplitViewController].listViewController.managedObject = nil;
_selectedList = nil;
}
}
- (void)_currentUserDidChange:(NSNotification *)notification {
self.fetchedResultsController = nil;
_checkForOneList = YES;
[self.tableView reloadData];
}
- (void)_createList:(id)sender {
CDIAddListTableViewCell *cell = (CDIAddListTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UITextField *textField = cell.textField;
if (textField.text.length == 0) {
[self _cancelAddingList:nil];
return;
}
SSHUDView *hud = [[SSHUDView alloc] initWithTitle:@"Creating..." loading:YES];
[hud show];
CDKList *list = [[CDKList alloc] init];
list.title = textField.text;
list.position = [NSNumber numberWithInteger:INT32_MAX];
list.user = [CDKUser currentUser];
[list createWithSuccess:^{
dispatch_async(dispatch_get_main_queue(), ^{
[hud completeAndDismissWithTitle:@"Created!"];
[self _cancelAddingList:nil];
textField.text = nil;
NSIndexPath *indexPath = [self.fetchedResultsController indexPathForObject:list];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self _selectListAtIndexPath:indexPath newList:YES];
});
} failure:^(AFJSONRequestOperation *remoteOperation, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *responseObject = remoteOperation.responseJSON;
if ([responseObject isKindOfClass:[NSDictionary class]] && [[responseObject objectForKey:@"error"] isEqualToString:@"plus_required"]) {
[hud dismiss];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Plus Required" message:@"You need Cheddar Plus to create more than 2 lists. Please upgrade to continue." delegate:self cancelButtonTitle:@"Later" otherButtonTitles:@"Upgrade", nil];
[alert show];
} else {
[hud failAndDismissWithTitle:@"Failed"];
[textField becomeFirstResponder];
}
});
}];
}
- (void)_cancelAddingList:(id)sender {
if (!_adding) {
return;
}
_adding = NO;
CDIAddListTableViewCell *cell = (CDIAddListTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
if ([cell.textField isFirstResponder]) {
[cell.textField resignFirstResponder];
}
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"plus.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(createList:)];
[self setEditing:NO animated:NO];
[self hideCoverView];
[self updatePlaceholderViews:YES];
}
- (void)_selectListAtIndexPath:(NSIndexPath *)indexPath newList:(BOOL)newList {
if (_adding) {
return;
}
if ([[self.tableView indexPathForSelectedRow] isEqual:indexPath] == NO) {
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
CDKList *list = [self objectForViewIndexPath:indexPath];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:list.remoteID forKey:kCDISelectedListKey];
[userDefaults synchronize];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[CDISplitViewController sharedSplitViewController].listViewController.managedObject = list;
_selectedList = list;
} else {
CDIListViewController *viewController = [[CDIListViewController alloc] init];
viewController.managedObject = list;
viewController.focusKeyboard = newList;
[self.navigationController pushViewController:viewController animated:YES];
}
}
- (void)_checkUser {
if (![CDKUser currentUser]) {
#ifdef CHEDDAR_USE_PASSWORD_FLOW
UIViewController *viewController = [[CDISignUpViewController alloc] init];
#else
UIViewController *viewController = [[CDIWebSignInViewController alloc] init];
#endif
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[self.splitViewController presentModalViewController:navigationController animated:YES];
} else {
[self.navigationController presentModalViewController:navigationController animated:NO];
}
return;
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rows = [super tableView:tableView numberOfRowsInSection:section];
if (_adding) {
return rows + 1;
}
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *const cellIdentifier = @"cellIdentifier";
static NSString *const addCellIdentifier = @"addCellIdentifier";
if (_adding && indexPath.row == 0) {
CDIAddListTableViewCell *cell = (CDIAddListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:addCellIdentifier];
if (!cell) {
cell = [[CDIAddListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addCellIdentifier];
if (self.textExpander) {
cell.textField.delegate = self.textExpander;
} else {
cell.textField.delegate = self;
}
[cell.closeButton addTarget:self action:@selector(_cancelAddingList:) forControlEvents:UIControlEventTouchUpInside];
}
[cell.textField becomeFirstResponder];
return cell;
}
CDIListTableViewCell *cell = (CDIListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CDIListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
[cell.editingTapGestureRecognizer addTarget:self action:@selector(editRow:)];
}
cell.list = [self objectForViewIndexPath:indexPath];
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self _selectListAtIndexPath:indexPath newList:NO];
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
if (sourceIndexPath.row == destinationIndexPath.row) {
return;
}
self.ignoreChange = YES;
NSMutableArray *lists = [self.fetchedResultsController.fetchedObjects mutableCopy];
CDKList *list = [self objectForViewIndexPath:sourceIndexPath];
[lists removeObject:list];
[lists insertObject:list atIndex:destinationIndexPath.row];
NSInteger i = 0;
for (list in lists) {
list.position = [NSNumber numberWithInteger:i++];
}
[self.managedObjectContext save:nil];
self.ignoreChange = NO;
[CDKList sortWithObjects:lists];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"Archive";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle != UITableViewCellEditingStyleDelete) {
return;
}
CDKList *list = [self objectForViewIndexPath:indexPath];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CDIListViewController *listViewController = [CDISplitViewController sharedSplitViewController].listViewController;
if ([listViewController.managedObject isEqual:list]) {
listViewController.managedObject = nil;
}
}
list.archivedAt = [NSDate date];
[list save];
[list update];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (_adding) {
[self _cancelAddingList:scrollView];
}
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (_adding) {
[self _createList:textField];
return NO;
}
CDKList *list = [self objectForViewIndexPath:self.editingIndexPath];
list.title = textField.text;
[list save];
[list update];
[self endCellTextEditing];
return NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (_adding) {
[self _cancelAddingList:textField];
}
}
#pragma mark - NSFetchedResultsController
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[super controllerDidChangeContent:controller];
if (_checkForOneList) {
NSNumber *selectedList = [[NSUserDefaults standardUserDefaults] objectForKey:kCDISelectedListKey];
if (selectedList) {
CDKList *list = [CDKList objectWithRemoteID:selectedList];
NSIndexPath *fIndexPath = [self.fetchedResultsController indexPathForObject:list];
if (!fIndexPath) {
_checkForOneList = NO;
return;
}
NSIndexPath *selectedIndexPath = [self viewIndexPathForFetchedIndexPath:fIndexPath];
[self _selectListAtIndexPath:selectedIndexPath newList:NO];
}
if (self.fetchedResultsController.fetchedObjects.count == 1) {
[self _selectListAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] newList:NO];
}
_checkForOneList = NO;
}
}
@end