Navigation Menu

Skip to content

Commit

Permalink
fixed bug that cause the app to crash when switching between tabs.
Browse files Browse the repository at this point in the history
  • Loading branch information
nbanerje committed Jan 14, 2011
1 parent 95e86a0 commit 3e29dce
Show file tree
Hide file tree
Showing 61 changed files with 2,794 additions and 890 deletions.
29 changes: 25 additions & 4 deletions Classes/HistoryData.m
Expand Up @@ -138,17 +138,38 @@ -(NSString*)getDatePercentStateFromIndex:(int)index {

//Returns the percentage (range: 0-1)
-(NSNumber*)getPercentFromIndex:(int)index {
NSDictionary *entry = [history objectForKey:[sortedKeyArray objectAtIndex:index]];
return (NSNumber*)[entry objectForKey:[dictKeys objectAtIndex:0]];
if(index < [sortedKeyArray count]) {
NSDictionary *entry = [history objectForKey:[sortedKeyArray objectAtIndex:index]];
return (NSNumber*)[entry objectForKey:[dictKeys objectAtIndex:0]];

}
else {
NSLog(@"HistoryData::getPercentFromIndex index greater than count -1");
return nil;
}


}
//Date is formatted in short form
-(NSString*)getDateFromIndex:(int)index {
return [formatter2 stringFromDate:[formatter dateFromString:[sortedKeyArray objectAtIndex:index]]];
if(index < [sortedKeyArray count]) {
return [formatter2 stringFromDate:[formatter dateFromString:[sortedKeyArray objectAtIndex:index]]];
}
else {
NSLog(@"HistoryData::getDateFromIndex index greater than count -1");
return nil;
}

}
//NSNumber Date
-(NSNumber*)getNSNumberDateFromIndex:(int)index {
return [NSNumber numberWithDouble:[[formatter dateFromString:[sortedKeyArray objectAtIndex:index]] timeIntervalSinceReferenceDate]];
if(index < [sortedKeyArray count]) {
return [NSNumber numberWithDouble:[[formatter dateFromString:[sortedKeyArray objectAtIndex:index]] timeIntervalSinceReferenceDate]];
}
else {
NSLog(@"HistoryData::getNSNumberDateFromIndex index greater than count -1");
return nil;
}
}

//First save data then return the data
Expand Down
6 changes: 3 additions & 3 deletions Classes/MainViewController.h
Expand Up @@ -29,6 +29,7 @@ typedef enum {
SHOW_BATTERY_STATE } BatteryStates;

@interface MainViewController : UIViewController <UITextFieldDelegate> {
aBatteryAppDelegate *delegate;

NSMutableDictionary *settingsDictionary;

Expand Down Expand Up @@ -66,7 +67,7 @@ typedef enum {
NSTimer *updateTimer;




BatteryStates currentState;
BatteryStates nextState;
Expand All @@ -78,7 +79,7 @@ typedef enum {
BOOL loadedState;
}

//@property (nonatomic, retain) IBOutlet UIView *allInfoView;
@property (nonatomic, retain) aBatteryAppDelegate *delegate;

@property (nonatomic, retain) IBOutlet UILabel *talk2G;
@property (nonatomic, retain) IBOutlet UILabel *talk3G;
Expand Down Expand Up @@ -125,5 +126,4 @@ typedef enum {
-(void) showAllTimes;
-(void) hideAllTimes;

- (UIImage *)reflectedImageRepresentationWithHeight:(NSUInteger)height;
@end
184 changes: 22 additions & 162 deletions Classes/MainViewController.m
Expand Up @@ -27,6 +27,8 @@

@implementation MainViewController

@synthesize delegate;

@synthesize batteryLabel;
@synthesize batteryStateLabel;
@synthesize batteryTimeLabel;
Expand Down Expand Up @@ -64,16 +66,18 @@ @implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
delegate = (aBatteryAppDelegate *)[[UIApplication sharedApplication]delegate];
// Custom initialization
self.tabBarItem.image = [UIImage imageNamed:@"battery_tab3.png"];
self.tabBarItem.title = @"Battery";



//Initalize battery object
battery = [[Battery alloc] init];
//Connect battery to app delegate
battery = delegate.battery;
//Turn on notifications
[battery on];

batteryHistory = delegate.batteryHistory;

//Setup notifications to update the display on a currentDevice change


Expand All @@ -98,7 +102,6 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
object: [UIDevice currentDevice]];
//Initalize the batteryView

//batteryView = [[UIView alloc] init];
batteryLabel = [[UILabel alloc] init];
batteryTimeLabel = [[UILabel alloc] init];
batteryStateLabel = [[UILabel alloc] init];
Expand All @@ -109,10 +112,9 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
batteryInternetWifiTimeLabel = [[UILabel alloc] init];
batteryVideoTimeLabel = [[UILabel alloc] init];
batteryAudioTimeLabel = [[UILabel alloc] init];
settingsDictionary = [[NSMutableDictionary alloc] initWithCapacity:10];
switchKeys = [[NSArray alloc] initWithObjects:@"Display Times", @"Disable Sleep", @"20 Sample Max", nil];
batteryHistory = [[HistoryData alloc] init];
loadedState = NO;
settingsDictionary = delegate.settingsDictionary;
switchKeys = delegate.switchKeys;
loadedState = NO;

}
//
Expand Down Expand Up @@ -143,7 +145,7 @@ - (void)viewDidLoad {
//[tmp release];


[self batteryViewSetup];
//[self batteryViewSetup];
[self setUserPrefs];

[self setFrames];
Expand Down Expand Up @@ -241,39 +243,20 @@ - (void) batteryViewSetup {
[batteryView insertSubview:batteryInternetWifiTimeLabel aboveSubview:batteryView];
[batteryView insertSubview:batteryVideoTimeLabel aboveSubview:batteryView];
[batteryView insertSubview:batteryAudioTimeLabel aboveSubview:batteryView];

/*
CGRect viewRect = CGRectMake(100, 10, 320, 480);
// create the reflection view
CGRect reflectionRect=viewRect;
// the reflection is a fraction of the size of the view being reflected
reflectionRect.size.height=reflectionRect.size.height*reflectionFraction;
// and is offset to be at the bottom of the view being reflected
reflectionRect=CGRectOffset(reflectionRect,0,viewRect.size.height);
UIImageView *localReflectionImageView = [[UIImageView alloc] initWithFrame:reflectionRect];
// determine the size of the reflection to create
NSUInteger reflectionHeight=batteryView.bounds.size.height*reflectionFraction;
self.reflectionView = localReflectionImageView;
// create the reflection image, assign it to the UIImageView and add the image view to the containerView
reflectionView.image=[self reflectedImageRepresentationWithHeight:reflectionHeight];
reflectionView.alpha=reflectionOpacity;
[self.view addSubview:reflectionView];
*/


[self.view addSubview:batteryView];
}

- (void)viewDidDisappear:(BOOL)animated {
[batteryView removeFromSuperview];
}

-(void) viewWillAppear:(BOOL)animated {
//NSLog(@"MainViewController::View Will Appear");
//[self batteryViewSetup];
[self batteryViewSetup];
[self setFrames];
[self setUserPrefs];

}

-(void) setUserPrefs {
Expand Down Expand Up @@ -982,141 +965,18 @@ -(void) hideAllTimes {

}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

//save currentState
//save max accelerometer values

}

- (void) saveUserDefaults {
[settingsDictionary setObject:[NSNumber numberWithInt:currentState] forKey:@"currentState"];
[SaveData writeApplicationPlist:settingsDictionary toFile:@"Data.plist"];
[batteryHistory saveDataToDisk];
}

CGImageRef AEViewCreateGradientImage (int pixelsWide,
int pixelsHigh)
{
CGImageRef theCGImage = NULL;
CGContextRef gradientBitmapContext = NULL;
CGColorSpaceRef colorSpace;
CGGradientRef grayScaleGradient;
CGPoint gradientStartPoint, gradientEndPoint;

// Our gradient is always black-white and the mask
// must be in the gray colorspace
colorSpace = CGColorSpaceCreateDeviceGray();

// create the bitmap context
gradientBitmapContext = CGBitmapContextCreate (NULL, pixelsWide, pixelsHigh,
8, 0, colorSpace, kCGImageAlphaNone);

if (gradientBitmapContext != NULL) {
// define the start and end grayscale values (with the alpha, even though
// our bitmap context doesn't support alpha the gradient requires it)
CGFloat colors[] = {0.0, 1.0,1.0, 1.0,};

// create the CGGradient and then release the gray color space
grayScaleGradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);

// create the start and end points for the gradient vector (straight down)
gradientStartPoint = CGPointZero;
gradientEndPoint = CGPointMake(0,pixelsHigh);

// draw the gradient into the gray bitmap context
CGContextDrawLinearGradient (gradientBitmapContext, grayScaleGradient, gradientStartPoint, gradientEndPoint, kCGGradientDrawsAfterEndLocation);

// clean up the gradient
CGGradientRelease(grayScaleGradient);

// convert the context into a CGImageRef and release the
// context
theCGImage=CGBitmapContextCreateImage(gradientBitmapContext);
CGContextRelease(gradientBitmapContext);

}

// clean up the colorspace
CGColorSpaceRelease(colorSpace);

// return the imageref containing the gradient
return theCGImage;
}

- (UIImage *)reflectedImageRepresentationWithHeight:(NSUInteger)height
{
CGContextRef mainViewContentContext;
CGColorSpaceRef colorSpace;

colorSpace = CGColorSpaceCreateDeviceRGB();

// create a bitmap graphics context the size of the image
mainViewContentContext = CGBitmapContextCreate (NULL, batteryView.bounds.size.width,height, 8,0, colorSpace, kCGImageAlphaPremultipliedLast);

// free the rgb colorspace
CGColorSpaceRelease(colorSpace);

if (mainViewContentContext==NULL)
return NULL;

// offset the context. This is necessary because, by default, the layer created by a view for
// caching its content is flipped. But when you actually access the layer content and have
// it rendered it is inverted. Since we're only creating a context the size of our
// reflection view (a fraction of the size of the main view) we have to translate the context the
// delta in size, render it, and then translate back (we could have saved/restored the graphics
// state

CGFloat translateVertical=batteryView.bounds.size.height-height;
CGContextTranslateCTM(mainViewContentContext,0,-translateVertical);

// render the layer into the bitmap context
[batteryView.layer renderInContext:mainViewContentContext];

// translate the context back
CGContextTranslateCTM(mainViewContentContext,0,translateVertical);

// Create CGImageRef of the main view bitmap content, and then
// release that bitmap context
CGImageRef mainViewContentBitmapContext=CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);

// create a 2 bit CGImage containing a gradient that will be used for masking the
// main view content to create the 'fade' of the reflection. The CGImageCreateWithMask
// function will stretch the bitmap image as required, so we can create a 1 pixel wide
// gradient
CGImageRef gradientMaskImage=AEViewCreateGradientImage(1,height);

// Create an image by masking the bitmap of the mainView content with the gradient view
// then release the pre-masked content bitmap and the gradient bitmap
CGImageRef reflectionImage=CGImageCreateWithMask(mainViewContentBitmapContext,gradientMaskImage);
CGImageRelease(mainViewContentBitmapContext);
CGImageRelease(gradientMaskImage);

// convert the finished reflection image to a UIImage
UIImage *theImage=[UIImage imageWithCGImage:reflectionImage];

// image is retained by the property setting above, so we can
// release the original
CGImageRelease(reflectionImage);

// return the image
return theImage;
}


- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[battery release];
[batteryView release];
[batteryLabel release];
[batteryTimeLabel release];
[batteryStateLabel release];
[settingsDictionary release];
[switchKeys release];
[batteryHistory release];
[super dealloc];
}

Expand Down
14 changes: 12 additions & 2 deletions Classes/aBatteryAppDelegate.h
Expand Up @@ -5,17 +5,27 @@
// Created by Neel Banerjee on 3/26/09.
// Copyright Om Design LLC 2009. All rights reserved.
//
#import "Battery.h"
#import "HistoryData.h"

@class MainViewController;

@interface aBatteryAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
//MainViewController *mainViewController;
UITabBarController *tabController;
UITabBarController *tabController;

NSMutableDictionary *settingsDictionary;
Battery *battery;
NSArray *switchKeys;
HistoryData *batteryHistory;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITabBarController *tabController;
@property (nonatomic, retain) NSMutableDictionary *settingsDictionary;
@property (nonatomic, retain) Battery *battery;
@property (nonatomic, retain) NSArray *switchKeys;
@property (nonatomic, retain) HistoryData *batteryHistory;

@end

0 comments on commit 3e29dce

Please sign in to comment.