Permalink
Browse files

SmartTimer update

  • Loading branch information...
1 parent d903bd7 commit 09e484e1719fff26c71f79ba8347da20333be15e Ugo Landini committed Mar 25, 2011
Showing with 34 additions and 4 deletions.
  1. +4 −0 Pomodoro/src/SmartTimer.h
  2. +13 −4 Pomodoro/src/SmartTimer.m
  3. +17 −0 PomodoroTests/SmartTimerTests.m
@@ -32,16 +32,20 @@
@optional
- (void) repeating:(SmartTimer*) aTimer;
- (void) oneShot:(SmartTimer*) aTimer;
+- (void) willSleep;
+- (void) didWakeUp:(NSTimeInterval) interval;
@end
@interface SmartTimer : NSObject {
+ NSDate* lastTimeWhenWentToSleep;
NSTimer* internalTimer;
id delegate;
}
+@property (nonatomic, retain) NSDate* lastTimeWhenWentToSleep;
@property (nonatomic, retain) id delegate;
@property (nonatomic, retain) NSTimer* internalTimer;
View
@@ -27,14 +27,14 @@
@implementation SmartTimer
-@synthesize delegate, internalTimer;
+@synthesize lastTimeWhenWentToSleep, delegate, internalTimer;
+ (SmartTimer*) createAndStartRepeatingTimerFor:(NSInteger)seconds withDelegate:(id)delegate {
- return [SmartTimer createAndStartRepeatingTimerFor:seconds withDelegate:delegate inRealTime:NO];
+ return [SmartTimer createAndStartRepeatingTimerFor:seconds withDelegate:delegate inRealTime:YES];
}
+ (SmartTimer*) createAndStartOneShotTimerAfter:(NSInteger)seconds withDelegate:(id)delegate {
- return [SmartTimer createAndStartOneShotTimerAfter:seconds withDelegate:delegate inRealTime:NO];
+ return [SmartTimer createAndStartOneShotTimerAfter:seconds withDelegate:delegate inRealTime:YES ];
}
+ (SmartTimer*) createAndStartRepeatingTimerFor:(NSInteger)seconds withDelegate:(id)delegate inRealTime:(BOOL)real {
@@ -98,10 +98,19 @@ - (void) reset {
- (void) receiveSleepNote: (NSNotification*) note {
NSLog(@"receiveSleepNote: %@", [note name]);
+ lastTimeWhenWentToSleep = [NSDate date];
+ if ([delegate respondsToSelector:@selector(willSleep)]) {
+ [delegate willSleep];
+ }
}
- (void) receiveWakeNote: (NSNotification*) note {
- NSLog(@"receiveSleepNote: %@", [note name]);
+ NSDate* now = [NSDate date];
+ NSTimeInterval secondsPassed = [now timeIntervalSinceDate:lastTimeWhenWentToSleep];
+ NSLog(@"receiveWakeNote: %@", [note name]);
+ if ([delegate respondsToSelector:@selector(didWakeUp:)]) {
+ [delegate didWakeUp:secondsPassed];
+ }
}
- (void) setRealTime {
@@ -49,6 +49,15 @@ - (void) oneShot:(SmartTimer*) timer {
oneShot = YES;
}
+- (void) willSleep {
+ NSLog(@"Will sleep");
+}
+
+- (void) didWakeUp:(NSTimeInterval) interval {
+ NSLog(@"Did wake up %f", interval);
+
+}
+
- (void)testOneShotSmartTimer {
SmartTimer* smart = [SmartTimer createAndStartOneShotTimerAfter:1 withDelegate:self];
@@ -72,6 +81,14 @@ - (void)testRepeatingSmartTimer {
}
+- (void)testSleepSmartTimer {
+
+ SmartTimer* smart = [SmartTimer createAndStartRepeatingTimerFor:1 withDelegate:self];
+
+ [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
+ [smart reset];
+
+}
@end

0 comments on commit 09e484e

Please sign in to comment.