diff options
| author | Ronny Fenrich <Fenrich@Gmail.com> | 2013-06-18 13:08:09 -0600 |
|---|---|---|
| committer | Ronny Fenrich <Fenrich@Gmail.com> | 2013-06-18 13:08:09 -0600 |
| commit | 27f0ec52de63124f034dbcaae3680a8c59e3daf4 (patch) | |
| tree | 44031091e2967661daef5f917c125ea8d74de892 /Volta/Views/InfographicViewController.m | |
| parent | e7052db8483ae59f5a4bc7c76b52321480759ccf (diff) | |
added Infographic
Diffstat (limited to 'Volta/Views/InfographicViewController.m')
| -rw-r--r-- | Volta/Views/InfographicViewController.m | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/Volta/Views/InfographicViewController.m b/Volta/Views/InfographicViewController.m new file mode 100644 index 0000000..a247597 --- /dev/null +++ b/Volta/Views/InfographicViewController.m @@ -0,0 +1,125 @@ +// +// 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]]]; + }); + +} + + +- (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]]; + }); +} + + +#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 |
