summaryrefslogtreecommitdiff
path: root/Volta/Views/DataTableViewController.m
blob: 15667f360e6315892967cf4c9b0070be6fb1b148 (plain)
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
//
//  DataTableViewController.m
//  Volta
//
//  Created by Ronny Fenrich on 2013-06-14.
//  Copyright (c) 2013 Decoder. All rights reserved.
//

#import "DataTableViewController.h"
#import "VoltaReading.h"
#import "MGOrderedDictionary.h"
#import "StatsTabBarController.h"

@interface DataTableViewController ()

@property (strong, nonatomic) MGOrderedDictionary *data;    // VoltaReading objects


@end

@implementation DataTableViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(updatedDataNotification)
												 name:NOTIFICATION_UPDATED_STATS_DATA
											   object:nil];

}

- (void)updatedDataNotification
{
    [self.tableView reloadData];
}

- (MGOrderedDictionary *)data
{
    // get data from parent tabbar controller
    StatsTabBarController *parentTabBarController = (StatsTabBarController *)self.parentViewController;
    return parentTabBarController.data;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!self.data)
    {
        return 1;   // Loading
    }
    else if (self.data.allKeys.count == 0)
    {
        return 1;   // no data
    }
    else
    {
        return self.data.allKeys.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.data)
    {
        // still loading data
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoadingCell"];
        return cell;
    }
    else if (self.data.count == 0)
    {
        // no results
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoResultsCell"];
        return cell;
    }

    // Configure the data cell...
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReadingCell"];

    NSString *dataKey = [self.data.allKeys objectAtIndex:indexPath.row];
    VoltaReading *reading = [self.data objectForKey:dataKey];

    UILabel *dateLabel = (UILabel *)[cell viewWithTag:1];
    dateLabel.text = dataKey;
    UILabel *kWhLabel = (UILabel *)[cell viewWithTag:2];
    float kWh = reading.usage / 1000.0f;
    kWhLabel.text = [NSString stringWithFormat:@"%.2f kWh", kWh];
    UILabel *costLabel = (UILabel *)[cell viewWithTag:3];
    float cost = reading.cost / 100000.0f;
    costLabel.text = [NSString stringWithFormat:@"%.2f $", cost];

    return cell;
}



@end