Skip to content
Browse files

Notifications continue to be sent after laptop sleeps/wakes up

[Fixes #1]
  • Loading branch information...
1 parent 6d1c21d commit 9e59339cd967549baeb9c370d558a47ff5b79ef0 @alindeman committed
Showing with 33 additions and 10 deletions.
  1. +17 −2 Reflecticle Activity Feed/AppDelegate.m
  2. +16 −8 Reflecticle Activity Feed/ReflecticleClient.m
View
19 Reflecticle Activity Feed/AppDelegate.m
@@ -17,7 +17,11 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
client = [[ReflecticleClient alloc] initWithApiKeyFromDotFile];
feed = [[ReflecticleActivityFeed alloc] initWithClient:client];
- feedTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:feed selector:@selector(notifyOfActivitySinceLastCheck:) userInfo:nil repeats:YES];
+ // Stop timer upon sleep; start it again upon waking
+ [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(stopFeedTimer:) name:NSWorkspaceWillSleepNotification object:nil];
+ [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(startFeedTimer:) name:NSWorkspaceDidWakeNotification object:nil];
+
+ [self startFeedTimer:self];
}
-(NSDictionary *)registrationDictionaryForGrowl {
@@ -33,4 +37,15 @@ -(void)growlNotificationWasClicked:(id)clickContext {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.reflecticle.com"]];
}
-@end
+-(void)startFeedTimer:(id)sender {
+ [self stopFeedTimer:sender]; // sanity check to make sure there really is no other feed timer
+
+ feedTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:feed selector:@selector(notifyOfActivitySinceLastCheck:) userInfo:nil repeats:YES];
+}
+
+-(void)stopFeedTimer:(id)sender {
+ [feedTimer invalidate];
+ feedTimer = nil;
+}
+
+@end
View
24 Reflecticle Activity Feed/ReflecticleClient.m
@@ -40,17 +40,25 @@ -(id)initWithApiKey:(NSString *)key {
}
-(NSArray*)activitySince:(NSDate *)since {
- NSString *dateString = [[since descriptionWithCalendarFormat:nil timeZone:nil locale:nil] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
+ NSMutableArray *activities;
- NSError *e;
- NSArray *activityJsonArray = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[self authenticatedUrl:@"activities.json" withQuery:[NSString stringWithFormat:@"newer_than=%@", dateString]]] options:0 error:&e];
+ NSString *dateString = [[since descriptionWithCalendarFormat:nil timeZone:nil locale:nil] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
+ NSData *jsonData = [NSData dataWithContentsOfURL:[self authenticatedUrl:@"activities.json" withQuery:[NSString stringWithFormat:@"newer_than=%@", dateString]]];
- NSMutableArray *activities = [NSMutableArray arrayWithCapacity:[activityJsonArray count]];
- for (uint i = 0; i < [activityJsonArray count]; i++) {
- NSDictionary *activityJsonObject = [activityJsonArray objectAtIndex:i];
- ReflecticleActivity *activity = [[ReflecticleActivity alloc] initWithJSON:activityJsonObject];
+ if (jsonData) {
+ NSError *e;
+ NSArray *activityJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&e];
- [activities addObject:activity];
+ activities = [NSMutableArray arrayWithCapacity:[activityJsonArray count]];
+ for (uint i = 0; i < [activityJsonArray count]; i++) {
+ NSDictionary *activityJsonObject = [activityJsonArray objectAtIndex:i];
+ ReflecticleActivity *activity = [[ReflecticleActivity alloc] initWithJSON:activityJsonObject];
+
+ [activities addObject:activity];
+ }
+ } else {
+ // Transient failure in Reflecticle API? For now just say there are no activities.
+ activities = [NSArray array];
}
return activities;

0 comments on commit 9e59339

Please sign in to comment.
Something went wrong with that request. Please try again.