瀏覽代碼

Implemented Unread PM Notifications.

Thomas Buck 11 年之前
父節點
當前提交
67371acf4f

+ 2
- 2
RedditBar/AppDelegate.h 查看文件

@@ -31,7 +31,7 @@
31 31
 #import "StateModel.h"
32 32
 #import "Reddit.h"
33 33
 
34
-@interface AppDelegate : NSObject <NSApplicationDelegate>
34
+@interface AppDelegate : NSObject <NSApplicationDelegate, NSUserNotificationCenterDelegate>
35 35
 
36 36
 @property (atomic, retain) IBOutlet NSMenu *statusMenu;
37 37
 @property (atomic, retain) IBOutlet NSApplication *application;
@@ -64,7 +64,7 @@
64 64
 -(void)reloadListNotAuthenticatedCallback;
65 65
 -(void)reloadListHasSubredditsCallback:(NSArray *)items;
66 66
 -(void)reloadListHasFrontpageCallback:(NSArray *)items;
67
--(void)readPMsCallback:(NSNumber *)items;
67
+-(void)readPMsCallback:(NSArray *)items;
68 68
 -(void)singleItemReloadedCallback:(NSArray *)items;
69 69
 
70 70
 -(void)prefsDidSave;

+ 25
- 4
RedditBar/AppDelegate.m 查看文件

@@ -32,8 +32,9 @@
32 32
 
33 33
 NSInteger itemsBeforeLinkList = 2;
34 34
 NSInteger numberOfStaticMenuItems = 10;
35
+
35 36
 #define MULTIPLIER_PM_INTERVALL_TO_SEC 60
36
-#define RECHECK_PM_AFTER_OPEN 10
37
+#define RECHECK_PM_AFTER_OPEN 7
37 38
 #define SUBMENU_INDEX_LINK 0
38 39
 #define SUBMENU_INDEX_COMMENTS 1
39 40
 #define SUBMENU_INDEX_BOTH 2
@@ -58,6 +59,15 @@ NSInteger numberOfStaticMenuItems = 10;
58 59
     lastFullName = nil;
59 60
     [self reloadListWithOptions];
60 61
     [self recreateRefreshTimer];
62
+    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
63
+}
64
+
65
+-(BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
66
+    return YES;
67
+}
68
+
69
+-(void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
70
+    [self openUnread:nil];
61 71
 }
62 72
 
63 73
 -(void)recreateRefreshTimer {
@@ -71,8 +81,8 @@ NSInteger numberOfStaticMenuItems = 10;
71 81
     [NSThread detachNewThreadSelector:@selector(readPMs:) toTarget:api withObject:self];
72 82
 }
73 83
 
74
--(void)readPMsCallback:(NSNumber *)items {
75
-    if ((items == nil) || ([items integerValue] == 0)) {
84
+-(void)readPMsCallback:(NSArray *)items {
85
+    if ((items == nil) || ([items count] < 1) || (((NSNumber *)[items objectAtIndex:0]).integerValue == 0)) {
76 86
         [statusItem setImage:statusImage];
77 87
         [statusItem setAlternateImage:statusHighlightImage];
78 88
         [PMItem setHidden:TRUE];
@@ -80,9 +90,20 @@ NSInteger numberOfStaticMenuItems = 10;
80 90
     } else {
81 91
         [statusItem setImage:orangeredImage];
82 92
         [statusItem setAlternateImage:orangeredHighlightImage];
83
-        [PMItem setTitle:[NSString stringWithFormat:NSLocalizedString(@"You've got %ld unread PMs!", @"PM message"), (long)items.integerValue]];
93
+        [PMItem setTitle:[NSString stringWithFormat:NSLocalizedString(@"You've got %ld unread PMs.", @"PM message"), (long)((NSNumber *)[items objectAtIndex:0]).integerValue]];
84 94
         [PMItem setHidden:FALSE];
85 95
         [PMSeparator setHidden:FALSE];
96
+
97
+        if ([items count] >= 2) {
98
+            if (![currentState.lastNotifiedPM isEqualToString:[items objectAtIndex:1]]) {
99
+                currentState.lastNotifiedPM = [items objectAtIndex:1];
100
+                [currentState savePreferences];
101
+                NSUserNotification *notification = [[NSUserNotification alloc] init];
102
+                notification.title = NSLocalizedString(@"New Reddit PM!", @"Notification Title");
103
+                notification.informativeText = [NSString stringWithFormat:NSLocalizedString(@"You've got %ld unread PMs.", nil), (long)((NSNumber *)[items objectAtIndex:0]).integerValue];
104
+                [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
105
+            }
106
+        }
86 107
     }
87 108
 }
88 109
 

+ 9
- 3
RedditBar/Reddit.m 查看文件

@@ -228,7 +228,7 @@ NSString *subredditFormat = @" [r/%@]";
228 228
     return array;
229 229
 }
230 230
 
231
--(NSNumber *)messagesCount:(NSData *)data {
231
+-(NSArray *)messagesData:(NSData *)data {
232 232
     NSError *error;
233 233
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
234 234
     NSDictionary *dat = [json valueForKey:@"data"];
@@ -237,7 +237,13 @@ NSString *subredditFormat = @" [r/%@]";
237 237
     NSArray *children = [dat valueForKey:@"children"];
238 238
     if (children == nil)
239 239
         return nil;
240
-    return [NSNumber numberWithInteger:[children count]];
240
+    NSString *newest_name = nil;
241
+    if (children.count > 0) {
242
+        NSDictionary *child = [children objectAtIndex:0];
243
+        NSDictionary *current = [child valueForKey:@"data"];
244
+        newest_name = [current valueForKey:@"name"];
245
+    }
246
+    return [NSArray arrayWithObjects:[NSNumber numberWithInteger:[children count]], newest_name, nil];
241 247
 }
242 248
 
243 249
 -(void)readPMs:(id)parent {
@@ -247,7 +253,7 @@ NSString *subredditFormat = @" [r/%@]";
247 253
     if ((data == nil) || ([response statusCode] != 200)) {
248 254
         [(AppDelegate *)parent performSelectorOnMainThread:@selector(readPMsCallback:) withObject:nil waitUntilDone:FALSE];
249 255
     } else {
250
-        [(AppDelegate *)parent performSelectorOnMainThread:@selector(readPMsCallback:) withObject:[self messagesCount:data] waitUntilDone:FALSE];
256
+        [(AppDelegate *)parent performSelectorOnMainThread:@selector(readPMsCallback:) withObject:[self messagesData:data] waitUntilDone:FALSE];
251 257
     }
252 258
 }
253 259
 

+ 2
- 2
RedditBar/RedditBar-Info.plist 查看文件

@@ -17,11 +17,11 @@
17 17
 	<key>CFBundlePackageType</key>
18 18
 	<string>APPL</string>
19 19
 	<key>CFBundleShortVersionString</key>
20
-	<string>1.3.1</string>
20
+	<string>1.4.0</string>
21 21
 	<key>CFBundleSignature</key>
22 22
 	<string>????</string>
23 23
 	<key>CFBundleVersion</key>
24
-	<string>301</string>
24
+	<string>312</string>
25 25
 	<key>LSApplicationCategoryType</key>
26 26
 	<string>public.app-category.utilities</string>
27 27
 	<key>LSMinimumSystemVersion</key>

+ 1
- 0
RedditBar/StateModel.h 查看文件

@@ -41,6 +41,7 @@
41 41
 @property (atomic, retain) NSString *filter;
42 42
 @property (atomic) Boolean removeVisited;
43 43
 @property (atomic) Boolean reloadAfterVisit;
44
+@property (atomic, retain) NSString *lastNotifiedPM;
44 45
 
45 46
 -(void)registerDefaultPreferences;
46 47
 -(void)savePreferences;

+ 5
- 1
RedditBar/StateModel.m 查看文件

@@ -30,7 +30,7 @@
30 30
 
31 31
 @implementation StateModel
32 32
 
33
-@synthesize username, modhash, useSubscriptions, subreddits, length, showSubreddit, titleLength, refreshInterval, filter, removeVisited, reloadAfterVisit;
33
+@synthesize username, modhash, useSubscriptions, subreddits, length, showSubreddit, titleLength, refreshInterval, filter, removeVisited, reloadAfterVisit, lastNotifiedPM;
34 34
 
35 35
 NSString *s_username = @"username";
36 36
 NSString *s_modhash = @"modhash";
@@ -43,6 +43,7 @@ NSString *s_refresh = @"refreshInterval";
43 43
 NSString *s_filter = @"filter";
44 44
 NSString *s_remove = @"remove";
45 45
 NSString *s_reload = @"reload";
46
+NSString *s_lastPM = @"lastNotifiedPM";
46 47
 
47 48
 -(void)registerDefaultPreferences {
48 49
     NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
@@ -56,6 +57,7 @@ NSString *s_reload = @"reload";
56 57
     [appDefaults setValue:@"hot" forKey:s_filter];
57 58
     [appDefaults setValue:[NSNumber numberWithBool:YES] forKey:s_remove];
58 59
     [appDefaults setValue:[NSNumber numberWithBool:YES] forKey:s_reload];
60
+    [appDefaults setValue:@"" forKey:s_lastPM];
59 61
     [store registerDefaults:appDefaults];
60 62
 }
61 63
 
@@ -72,6 +74,7 @@ NSString *s_reload = @"reload";
72 74
     [store setObject:filter forKey:s_filter];
73 75
     [store setBool:removeVisited forKey:s_remove];
74 76
     [store setBool:reloadAfterVisit forKey:s_reload];
77
+    [store setObject:lastNotifiedPM forKey:s_lastPM];
75 78
     [store synchronize];
76 79
 }
77 80
 
@@ -89,6 +92,7 @@ NSString *s_reload = @"reload";
89 92
     filter = [store stringForKey:s_filter];
90 93
     removeVisited = [store boolForKey:s_remove];
91 94
     reloadAfterVisit = [store boolForKey:s_reload];
95
+    lastNotifiedPM = [store stringForKey:s_lastPM];
92 96
 }
93 97
 
94 98
 @end

+ 3
- 0
RedditBar/de.lproj/Localizable.strings 查看文件

@@ -22,6 +22,9 @@
22 22
 /* Statusitem when API is not authenticated */
23 23
 "Login Error!" = "Fehler beim Login!";
24 24
 
25
+/* Notification Title */
26
+"New Reddit PM!" = "Neue Reddit PM!";
27
+
25 28
 /* Statusitem when no modhash is stored */
26 29
 "Not logged in!" = "Nicht eingeloggt!";
27 30
 

二進制
RedditBar/en.lproj/Localizable.strings 查看文件


Loading…
取消
儲存