// // 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 () @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