#import "ATimerViewController.h"
@implementation ATimerViewController
@synthesize timePicker;
@synthesize label;
@synthesize label2;
@synthesize label3;
@synthesize view1;
@synthesize view2;
NSInteger seconds = 0;
NSInteger minutes = 0;
// MINUTES (2) ----------------MINUTES-----------------
- (IBAction) echoMinute:(id)sender
{
NSDate * time = timePicker.date;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH:MM:SS"];
NSLog(@"date:%@", [dateFormatter stringFromDate:time]);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:time];
NSInteger hour = [comps hour];
NSInteger minute = [comps minute];
NSLog(@"Hour:%i", hour);
NSLog(@"minute:%i", minute);
NSInteger secs =hour * 60 * 60 + minute * 60;
NSInteger mins = secs / 60;
NSNumber *elapsedMinutes = [NSNumber numberWithInt:mins];
NSDictionary *myMinute = [NSDictionary dictionaryWithObject:elapsedMinutes forKey:@"TotalMinutes"];
[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(echoIt2:) userInfo:myMinute repeats:YES];
label.text = [NSString stringWithFormat:@"Hours: %i, Min: %i", hour, minute]; // TIME CHOSEN (1st Label)
}
- (void) echoIt2:(NSTimer *)timer // MINUTES METHOD
{
NSNumber *num = (NSNumber *) [[timer userInfo] valueForKey:@"TotalMinutes"]; //elapsed
minutes++;
NSInteger min = [num integerValue] - minutes; //remaining
NSLog(@"elapsed: %i, remaining: %i", minutes, min);
if (min <=0)
{
[timer invalidate];
}
label2.text = [NSString stringWithFormat:@"Minutes remaining: %i", min]; // (2nd Label)
}
// SECONDS (3rd Label) ----------------SECONDS-----------------
- (IBAction) echoTime:(id)sender
{
NSDate * time = timePicker.date;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH:MM:SS"];
NSLog(@"date:%@", [dateFormatter stringFromDate:time]);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:time];
NSInteger hour = [comps hour];
NSInteger minute = [comps minute];
NSLog(@"Hour:%i", hour);
NSLog(@"minute:%i", minute);
NSInteger secs =hour * 60 * 60 + minute * 60;
NSNumber *elapsedSeconds = [NSNumber numberWithInt:secs];
NSDictionary *myDict = [NSDictionary dictionaryWithObject:elapsedSeconds forKey:@"TotalSeconds"];
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(echoIt:) userInfo:myDict repeats:YES];
}
- (void) echoIt:(NSTimer *)timer // SECONDS METHOD (3)
{
NSNumber *num = (NSNumber *) [[timer userInfo] valueForKey:@"TotalSeconds"]; //elapsed
seconds++;
NSInteger secs = [num integerValue] - seconds; //remaining
NSLog(@"elapsed: %i, remaining: %i", seconds, secs);
if (secs <= 0)
{
[timer invalidate];
timer = nil;
}
label3.text = [NSString stringWithFormat:@"Seconds remaining: %i", secs]; // (3rd Label)
}
//NEW TIMER ---------------- NEW TIMER -----------------
- (IBAction) newActionTimer: (id)sender
{
NSDate * time = timePicker.date;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH:MM:SS"];
NSLog(@"date:%@", [dateFormatter stringFromDate:time]);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:time];
NSInteger hour = [comps hour];
NSInteger minute = [comps minute];
NSLog(@"Hour:%i", hour);
NSLog(@"minute:%i", minute);
NSInteger secs =hour * 60 * 60 + minute * 60;
NSInteger mins = secs / 60;
NSNumber *elapsedMinutes = [NSNumber numberWithInt:mins];
NSDictionary *myMinute = [NSDictionary dictionaryWithObject:elapsedMinutes forKey:@"TotalMinutes"];
newTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(echoNewTimer:) userInfo:myMinute repeats:NO];
}
- (void) echoNewTimer: (NSTimer *)timer2
{
[myTimer invalidate];
myTimer = nil;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Invalidate"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[view1 setHidden:NO];
[view2 setHidden:YES];
// is my newTimer On right now ??
}
//NEXT PAGE ---------------- NEXT PAGE -----------------
- (void) nextPage:(id)sender
{
[view1 setHidden:YES];
[view2 setHidden:NO];
}
- (void) dealloc {
[view1 release];
[view2 release];
[timePicker release];
[super dealloc];
}
@end