Skip to content

Instantly share code, notes, and snippets.

@Zammy
Last active December 25, 2015 06:39
Show Gist options
  • Save Zammy/6933546 to your computer and use it in GitHub Desktop.
Save Zammy/6933546 to your computer and use it in GitHub Desktop.
#import "TaxiArrivingAnnotation.h"
#define SECONDS_IN_A_MINUTE 60
@interface TaxiArrivingAnnotation ()
@property (nonatomic) NSTimer * timer;
@property (nonatomic) NSDate * timeOfArrival;
@property (nonatomic, weak) id token1;
@property (nonatomic, weak) id token2;
@end
@implementation TaxiArrivingAnnotation
- (id)init
{
self = [super init];
if (self) {
__weak TaxiArrivingAnnotation * this = self;
self.token1 = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note)
{
NSLog(@"DID BECOME ACTIVE");
NSTimeInterval secondsLeft = [this.timeOfArrival timeIntervalSinceNow];
if (secondsLeft < 0) {
self.minutesToTaxiArrival = 0;
return;
}
this.minutesToTaxiArrival = secondsLeft / SECONDS_IN_A_MINUTE;
[this startTimer];
}];
self.token2 = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillResignActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note)
{
NSLog(@"WILL RESIGN ACTIVE");
[this.timer invalidate];
this.timer = nil;
}];
// [[NSNotificationCenter defaultCenter]addObserver:self
// selector:@selector(didBecomeActive)
// name:UIApplicationDidBecomeActiveNotification
// object:nil];
//
// [[NSNotificationCenter defaultCenter]addObserver:self
// selector:@selector(willResignActive)
// name:UIApplicationWillResignActiveNotification
// object:nil];
}
return self;
}
-(void) setMinutesToTaxiArrival:(int)newMinutes {
self->_minutesToTaxiArrival = newMinutes;
self->_timeOfArrival = [NSDate dateWithTimeIntervalSinceNow:SECONDS_IN_A_MINUTE * newMinutes];
if (newMinutes < 0) {
[self.timer invalidate];
}
}
//-(void) didBecomeActive {
// NSTimeInterval secondsLeft = [self.timeOfArrival timeIntervalSinceNow];
// if (secondsLeft < 0) {
// self.minutesToTaxiArrival = 0;
// return;
// }
//
// self.minutesToTaxiArrival = secondsLeft / SECONDS_IN_A_MINUTE;
//
// [self startTimer];
//}
//-(void) willResignActive {
// [self.timer invalidate];
// self.timer = nil;
//}
-(void) startTimer {
self.timer = [NSTimer timerWithTimeInterval:SECONDS_IN_A_MINUTE target:self selector:@selector(aMinutedPassed) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
-(void) aMinutedPassed {
self.minutesToTaxiArrival--;
}
-(void) dealloc {
NSLog(@"DEALLOC");
if (self.timer != nil && [self.timer isValid])
[self.timer invalidate];
// [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
// [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self.token1];
[[NSNotificationCenter defaultCenter] removeObserver:self.token2];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment