forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwfulNewPMNotifierAgent.m
67 lines (52 loc) · 1.89 KB
/
AwfulNewPMNotifierAgent.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// AwfulNewPMNotifierAgent.m
// Awful
//
// Created by me on 1/26/13.
// Copyright (c) 2013 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulNewPMNotifierAgent.h"
#import "AwfulHTTPClient.h"
#import "AwfulPrivateMessage.h"
@interface AwfulNewPMNotifierAgent ()
@property (nonatomic) NSDate *lastCheckDate;
@end
@implementation AwfulNewPMNotifierAgent
+ (instancetype)agent
{
static AwfulNewPMNotifierAgent *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [self new];
});
return instance;
}
- (NSDate *)lastCheckDate
{
return [[NSUserDefaults standardUserDefaults] objectForKey:kLastMessageCheckDate];
}
- (void)setLastCheckDate:(NSDate *)lastCheckDate
{
[[NSUserDefaults standardUserDefaults] setObject:lastCheckDate forKey:kLastMessageCheckDate];
[[NSUserDefaults standardUserDefaults] synchronize];
}
static NSString * const kLastMessageCheckDate = @"com.awfulapp.Awful.LastMessageCheckDate";
- (void)checkForNewMessages
{
[[AwfulHTTPClient client] listPrivateMessagesAndThen:^(NSError *error, NSArray *messages)
{
if (error) {
NSLog(@"error checking for new private messages: %@", error);
return;
}
self.lastCheckDate = [NSDate date];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"seen = NO"];
NSArray *unseen = [messages filteredArrayUsingPredicate:predicate];
NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
[noteCenter postNotificationName:AwfulNewPrivateMessagesNotification object:self
userInfo:@{ AwfulNewPrivateMessageCountKey: @([unseen count]) }];
}];
}
@end
NSString * const AwfulNewPrivateMessagesNotification = @"AwfulNewPrivateMessagesNotification";
NSString * const AwfulNewPrivateMessageCountKey = @"AwfulNewPrivateMessageCountKey";