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
|
//
// 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
|