Created
January 22, 2013 19:02
-
-
Save jlee42/4597321 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "TTActivityTypesViewController.h" | |
#import "TTConstants.h" | |
#import "TTActivityType.h" | |
#import "TTCustomer.h" | |
static void TTShowAlertWithError(NSError *error) | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" | |
message:[error localizedDescription] | |
delegate:nil | |
cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alert show]; | |
} | |
@interface TTActivityTypesViewController () <NSFetchedResultsControllerDelegate> | |
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath; | |
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController; | |
@end | |
@implementation TTActivityTypesViewController | |
- (void)awakeFromNib | |
{ | |
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { | |
self.clearsSelectionOnViewWillAppear = NO; | |
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); | |
} | |
[super awakeFromNib]; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
self.view.backgroundColor = [UIColor clearColor]; | |
self.title = @"Activity Types"; | |
[self loadData]; | |
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ActivityType"]; | |
[fetchRequest setReturnsObjectsAsFaults:NO]; | |
NSSortDescriptor *customerDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"customer.name" ascending:YES]; | |
NSSortDescriptor *activityTypeDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; | |
fetchRequest.sortDescriptors = @[customerDescriptor, activityTypeDescriptor]; | |
NSError *error = nil; | |
// Setup fetched results | |
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest | |
managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext | |
sectionNameKeyPath:@"customer" | |
cacheName:nil]; | |
[self.fetchedResultsController setDelegate:self]; | |
BOOL fetchSuccessful = [self.fetchedResultsController performFetch:&error]; | |
if (! fetchSuccessful) { | |
TTShowAlertWithError(error); | |
} | |
[self.refreshControl addTarget:self | |
action:@selector(refreshView:) | |
forControlEvents:UIControlEventValueChanged]; | |
} | |
-(void)refreshView:(UIRefreshControl *)refresh { | |
int64_t delayInSeconds = 2.0; | |
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); | |
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ | |
// Reload data | |
[self loadData]; | |
[self.tableView reloadData]; | |
[self.refreshControl endRefreshing]; | |
}); | |
} | |
- (void)loadData | |
{ | |
// Load the object model via RestKit | |
[[RKObjectManager sharedManager] getObjectsAtPath:@"/activity_types" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { | |
RKLogInfo(@"Load complete: Table should refresh..."); | |
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"ActivityTypesLastUpdatedAt"]; | |
[[NSUserDefaults standardUserDefaults] synchronize]; | |
} failure:^(RKObjectRequestOperation *operation, NSError *error) { | |
RKLogError(@"Load failed with error: %@", error); | |
TTShowAlertWithError(error); | |
}]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
#pragma mark - Table View | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
return [[self.fetchedResultsController sections] count]; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section]; | |
return [sectionInfo numberOfObjects]; | |
} | |
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section | |
{ | |
TTCustomer *customer = [[self.fetchedResultsController sections] objectAtIndex:section]; | |
NSLog(@"customer name: %@", [customer name]); | |
return [customer name]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; | |
[self configureCell:cell atIndexPath:indexPath]; | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { | |
} | |
} | |
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath | |
{ | |
TTActivityType *activityType = [self.fetchedResultsController objectAtIndexPath:indexPath]; | |
cell.textLabel.text = [[activityType valueForKey:@"name"] description]; | |
} | |
#pragma mark NSFetchedResultsControllerDelegate methods | |
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller | |
{ | |
[self.tableView reloadData]; | |
// Stop refresh control | |
[self.refreshControl endRefreshing]; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment