// // StatsTabBarController.m // Volta // // Created by Ronny Fenrich on 2013-06-14. // Copyright (c) 2013 Decoder. All rights reserved. // #import "StatsTabBarController.h" #import "VoltaReading.h" @interface StatsTabBarController () @property (strong, nonatomic) NSDateFormatter *dateFormatter; @end @implementation StatsTabBarController - (void)viewDidLoad { [super viewDidLoad]; // start loading data... [self updateData]; } - (NSDateFormatter *)dateFormatter { if (!_dateFormatter) { _dateFormatter = [[NSDateFormatter alloc] init]; [_dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; NSCalendar* calendar = [NSCalendar currentCalendar]; [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; [_dateFormatter setCalendar:calendar]; } return _dateFormatter; } // -------------------------------------------------------------------------------------- - (void)updateData { self.data = nil; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; // load data AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:HOST]]; [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; [httpClient setDefaultHeader:@"Accept" value:@"application/json"]; [httpClient setParameterEncoding:AFJSONParameterEncoding]; NSString *startDateUTC = [self.dateFormatter stringFromDate:self.startDate]; NSString *endDateUTC = [self.dateFormatter stringFromDate:self.endDate]; NSError *error; NSString *token = [SSKeychain passwordForService:KEYCHAIN_API_TOKEN account:KEYCHAIN_ACCOUNT error:&error]; NSMutableURLRequest *request; request = [httpClient requestWithMethod:@"GET" path:[NSString stringWithFormat:URL_READINGS, token, startDateUTC, endDateUTC] parameters:nil]; NSLog(@"GET: %@", request); AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; NSLog(@"%@", JSON); if (!self.data) { self.data = [[MGOrderedDictionary alloc] init]; } for (NSDictionary *data in JSON) { VoltaReading *aReading = [VoltaReading initFromJSON:data]; NSString *dictKey = aReading.dictionaryKey; // we now group data by day (calc totals for days usage and cost) if (![self.data objectForKey:dictKey]) { [self.data insertObject:aReading forKey:dictKey atIndex:self.data.count]; } else { VoltaReading *readingsForData = [self.data objectForKey:dictKey]; aReading.cost += readingsForData.cost; aReading.usage += readingsForData.usage; [self.data setObject:aReading forKey:dictKey]; } } [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_UPDATED_STATS_DATA object:nil]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_UPDATED_STATS_DATA object:nil]; BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Error" message:@"Failed to retrieve reading data. Please try again later."]; [alert setCancelButtonWithTitle:@"Ok" block:nil]; [alert show]; }]; [operation start]; } @end