Created
January 23, 2013 17:45
-
-
Save jlee42/4610890 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 "TTConstants.h" | |
#import "TTActivitiesViewController.h" | |
#import "TTDetailViewController.h" | |
#import "TTSignInCell.h" | |
#import "TTActivity.h" | |
#import "TTUser.h" | |
#import "TTActivityType.h" | |
#import "TTCustomer.h" | |
#import <SDWebImage/UIImageView+WebCache.h> | |
static void TTShowAlertWithError(NSError *error) | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" | |
message:[error localizedDescription] | |
delegate:nil | |
cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alert show]; | |
} | |
@interface TTActivitiesViewController () <NSFetchedResultsControllerDelegate> | |
- (void)configureSignInCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath; | |
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController; | |
@end | |
@implementation TTActivitiesViewController | |
- (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 = @"Activities"; | |
self.detailViewController = (TTDetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController]; | |
[self.refreshControl addTarget:self | |
action:@selector(refreshView:) | |
forControlEvents:UIControlEventValueChanged]; | |
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Activity"]; | |
NSPredicate *filter = [NSPredicate predicateWithFormat:@"activityTypeID = %@", [[NSUserDefaults standardUserDefaults] valueForKey:@"activity_type_id"]]; | |
[fetchRequest setPredicate:filter]; | |
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"updatedAt" ascending:NO]; | |
fetchRequest.sortDescriptors = @[descriptor]; | |
NSError *error = nil; | |
// Setup fetched results | |
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest | |
managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext | |
sectionNameKeyPath:nil | |
cacheName:nil]; | |
[self.fetchedResultsController setDelegate:self]; | |
BOOL fetchSuccessful = [self.fetchedResultsController performFetch:&error]; | |
if (! fetchSuccessful) { | |
TTShowAlertWithError(error); | |
} | |
[self loadData]; | |
NSLog(@"viewDidLoad called"); | |
} | |
-(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]; | |
NSError *error = nil; | |
BOOL fetchSuccessful = [self.fetchedResultsController performFetch:&error]; | |
if (! fetchSuccessful) { | |
TTShowAlertWithError(error); | |
} | |
[self.refreshControl endRefreshing]; | |
}); | |
} | |
- (void)loadData | |
{ | |
NSLog(@"loadData called"); | |
// Load the object model via RestKit | |
NSString *activitiesPath = [NSString stringWithFormat:@"/api/v2/json/%@/activity_types/%@/activities", | |
[[NSUserDefaults standardUserDefaults] valueForKey:@"authentication_token"], | |
[[NSUserDefaults standardUserDefaults] valueForKey:@"activity_type_id"]]; | |
[[RKObjectManager sharedManager] getObjectsAtPath:activitiesPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { | |
RKLogInfo(@"Load complete: Table should refresh..."); | |
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastUpdatedAt"]; | |
[[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 | |
{ | |
NSDate *lastUpdatedAt = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastUpdatedAt"]; | |
NSString *dateString = [NSDateFormatter localizedStringFromDate:lastUpdatedAt dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterMediumStyle]; | |
if (nil == dateString) { | |
dateString = @"Never"; | |
} | |
return [NSString stringWithFormat:@"Last Load: %@", dateString]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
TTSignInCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; | |
[self configureSignInCell:cell atIndexPath:indexPath]; | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { | |
TTActivity *activity = [[self fetchedResultsController] objectAtIndexPath:indexPath]; | |
self.detailViewController.detailItem = activity; | |
} | |
} | |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
{ | |
if ([[segue identifier] isEqualToString:@"showDetail"]) { | |
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; | |
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath]; | |
[[segue destinationViewController] setDetailItem:object]; | |
} | |
} | |
- (void)configureSignInCell:(TTSignInCell *)cell atIndexPath:(NSIndexPath *)indexPath | |
{ | |
TTActivity *activity = [self.fetchedResultsController objectAtIndexPath:indexPath]; | |
TTUser *user = (TTUser *)activity.user; | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; | |
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"hh:mm a" options:0 locale:[NSLocale currentLocale]]]; | |
NSString *timeIn = [dateFormatter stringFromDate:[activity valueForKey:@"timeIn"]]; | |
NSString *timeOut = [dateFormatter stringFromDate:[activity valueForKey:@"timeOut"]]; | |
NSString *photoURLString = [NSString stringWithFormat:@"https://domain.com/api/v2/jpg/guest/photos/lsuid/%@?style=small", [user valueForKey:@"photoHash"]]; | |
[cell.userPhotoImage setImageWithURL:[NSURL URLWithString:photoURLString] | |
placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; | |
cell.nameLabel.text = [[user valueForKey:@"displayName"] description]; | |
cell.timeInLabel.text = timeIn; | |
cell.timeOutLabel.text = timeOut; | |
} | |
#pragma mark NSFetchedResultsControllerDelegate methods | |
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { | |
NSLog(@"controllerDidChangeContent called"); | |
[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