summaryrefslogtreecommitdiff
path: root/Volta/Views/InfographicViewController.m
blob: 0eabb92a87da040ac5817951520b3b3fe13ec105 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
//  InfographicViewController.m
//  Volta
//
//  Created by Ronny Fenrich on 2013-06-18.
//  Copyright (c) 2013 Decoder. All rights reserved.
//

#import "InfographicViewController.h"
#import "StatsTabBarController.h"
#import "VoltaReading.h"


@interface InfographicViewController ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation InfographicViewController


- (void)viewDidLoad
{
    [super viewDidLoad];


    NSString *resourcePath = [[[[NSBundle mainBundle] resourcePath] stringByReplacingOccurrencesOfString:@"/" withString:@"//"]
                              stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSString *markup = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]
                                                 encoding:NSUTF8StringEncoding
                                                    error:nil];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.webView loadHTMLString:markup baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//", resourcePath]]];
    });

    self.webView.alpha = 0.0;
}


- (void)viewDidUnload {
    [self setWebView:nil];
    [super viewDidUnload];
}


- (void)viewWillDisappear
{
    if (self.webView.loading) {
        [self.webView stopLoading];
    }
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self hideGradientBackground:self.webView];

    // calc totals...
    StatsTabBarController *parentTabBarController = (StatsTabBarController *)self.parentViewController;
    float totalkWh = 0.0f;
    float totalCost = 0.0f;

    for (VoltaReading *reading in parentTabBarController.data.allValues)
    {
        float kWh = reading.usage / 1000.0f;
        float cost = reading.cost / 100000.0f;

        totalkWh += kWh;
        totalCost += cost;
    }

    double delayInSeconds = 0.5;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"updateData(%.2f, %.2f)", totalkWh, totalCost]];
        [self.webView fadeIn];
    });
}


#pragma mark - Web View

- (void)hideGradientBackground:(UIView*)theView
{
    for (UIView * subview in theView.subviews)
    {
        if ([subview isKindOfClass:[UIImageView class]])
            subview.hidden = YES;

        [self hideGradientBackground:subview];
    }
}

- (void)webView:(UIWebView *)theWebView didFailLoadWithError:(NSError *)error
{
    // If the URLLoadingSystem cancelled the load don't show anything.
    NSString *errorUrl = [error.userInfo objectForKey:@"NSErrorFailingURLStringKey"];
    if (([error code] != NSURLErrorCancelled ) && ([[errorUrl uppercaseString] startsWith:@"HTTP"]))
    {
        BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Connection Error" message:@"We couldn't load the web page, please check your internet connection and try again."];
        [alert setCancelButtonWithTitle:@"Ok" block:nil];
        [alert show];
    }
}


- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)theRequest navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
    // scroll to the top
    [self.webView.scrollView setContentOffset:CGPointMake(0, 0)];
}


- (void)webViewDidStartLoad:(UIWebView *)theWebView
{
}



@end