diff options
| author | Ronny Fenrich <Fenrich@Gmail.com> | 2013-06-14 11:42:17 -0600 |
|---|---|---|
| committer | Ronny Fenrich <Fenrich@Gmail.com> | 2013-06-14 11:42:17 -0600 |
| commit | c721c485f56cc2034f499a61c02117f31cc73da4 (patch) | |
| tree | 1989be521806bc34591f8ecc7ea2993c5f6afe93 /Volta/Models | |
| parent | 0d40c8c8a1850559a9204b717585ba42ad4f2e9f (diff) | |
Finished Table view display of usage data
Diffstat (limited to 'Volta/Models')
| -rw-r--r-- | Volta/Models/VoltaReading.h | 23 | ||||
| -rw-r--r-- | Volta/Models/VoltaReading.m | 55 |
2 files changed, 78 insertions, 0 deletions
diff --git a/Volta/Models/VoltaReading.h b/Volta/Models/VoltaReading.h new file mode 100644 index 0000000..325a4e4 --- /dev/null +++ b/Volta/Models/VoltaReading.h @@ -0,0 +1,23 @@ +// +// VoltaReading.h +// Volta +// +// Created by Ronny Fenrich on 2013-06-14. +// Copyright (c) 2013 Decoder. All rights reserved. +// + +#import <Foundation/Foundation.h> + +@interface VoltaReading : NSObject + ++ (VoltaReading *)initFromJSON:(NSDictionary *)jsonData; + +@property (nonatomic) NSInteger id; +@property (nonatomic) NSInteger usage; +@property (nonatomic) NSInteger duration; +@property (nonatomic) NSInteger cost; +@property (strong, nonatomic) NSDate *startDate; + +@property (strong, nonatomic) NSString *dictionaryKey; // Date of reading + +@end diff --git a/Volta/Models/VoltaReading.m b/Volta/Models/VoltaReading.m new file mode 100644 index 0000000..bd2d5fd --- /dev/null +++ b/Volta/Models/VoltaReading.m @@ -0,0 +1,55 @@ +// +// VoltaReading.m +// Volta +// +// Created by Ronny Fenrich on 2013-06-14. +// Copyright (c) 2013 Decoder. All rights reserved. +// + +#import "VoltaReading.h" + +@implementation VoltaReading + + ++ (VoltaReading *)initFromJSON:(NSDictionary *)jsonData; +{ + VoltaReading *result = [[VoltaReading alloc] init]; + + result.id = [[jsonData objectForKey:@"id"] integerValue]; + result.usage = [[jsonData objectForKey:@"usage"] integerValue]; + result.duration = [[jsonData objectForKey:@"duration"] integerValue]; + result.cost = [[jsonData objectForKey:@"cost"] integerValue]; + + // Dates + NSDateFormatter *inputDateParser = [[NSDateFormatter alloc] init]; + [inputDateParser setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; + NSString *startDateString = [jsonData objectForKey:@"start"] == [NSNull null] ? nil : [jsonData objectForKey:@"start"]; + if (!startDateString) + { + result.startDate = nil; + } + else + { + result.startDate = [inputDateParser dateFromString:startDateString]; + } + + return result; +} + + +- (NSString *)dictionaryKey +{ + if (self.startDate) + { + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + [dateFormatter setDateStyle:NSDateFormatterFullStyle]; + [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; + return [dateFormatter stringFromDate:self.startDate]; + } + else + { + return @"unknown date"; + } +} + +@end |
