Browse Source

Finished single link reloading after removing

Thomas Buck 11 years ago
parent
commit
3b1de8343d
5 changed files with 66 additions and 12 deletions
  1. 1
    0
      RedditBar/AppDelegate.h
  2. 29
    8
      RedditBar/AppDelegate.m
  3. 1
    0
      RedditBar/Reddit.h
  4. 34
    3
      RedditBar/Reddit.m
  5. 1
    1
      RedditBar/RedditBar-Info.plist

+ 1
- 0
RedditBar/AppDelegate.h View File

65
 -(void)reloadListHasSubredditsCallback:(NSArray *)items;
65
 -(void)reloadListHasSubredditsCallback:(NSArray *)items;
66
 -(void)reloadListHasFrontpageCallback:(NSArray *)items;
66
 -(void)reloadListHasFrontpageCallback:(NSArray *)items;
67
 -(void)readPMsCallback:(NSNumber *)items;
67
 -(void)readPMsCallback:(NSNumber *)items;
68
+-(void)singleItemReloadedCallback:(NSArray *)items;
68
 
69
 
69
 -(void)prefsDidSave;
70
 -(void)prefsDidSave;
70
 
71
 

+ 29
- 8
RedditBar/AppDelegate.m View File

30
 
30
 
31
 @implementation AppDelegate
31
 @implementation AppDelegate
32
 
32
 
33
+NSInteger itemsBeforeLinkList = 2;
34
+NSInteger numberOfStaticMenuItems = 10;
35
+#define MULTIPLIER_PM_INTERVALL_TO_SEC 60
36
+#define RECHECK_PM_AFTER_OPEN 10
37
+#define SUBMENU_INDEX_LINK 0
38
+#define SUBMENU_INDEX_COMMENTS 1
39
+#define SUBMENU_INDEX_BOTH 2
40
+
33
 @synthesize statusMenu, statusItem, statusImage, statusHighlightImage, orangeredImage, orangeredHighlightImage, prefWindow, currentState, application, api, firstMenuItem, menuItems, redditItems, lastFullName, refreshTimer, PMItem, PMSeparator;
41
 @synthesize statusMenu, statusItem, statusImage, statusHighlightImage, orangeredImage, orangeredHighlightImage, prefWindow, currentState, application, api, firstMenuItem, menuItems, redditItems, lastFullName, refreshTimer, PMItem, PMSeparator;
34
 
42
 
35
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
43
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
55
 -(void)recreateRefreshTimer {
63
 -(void)recreateRefreshTimer {
56
     if (refreshTimer != nil)
64
     if (refreshTimer != nil)
57
         [refreshTimer invalidate];
65
         [refreshTimer invalidate];
58
-    refreshTimer = [NSTimer scheduledTimerWithTimeInterval:(currentState.refreshInterval * 60) target:self selector:@selector(refreshTick:) userInfo:nil repeats:YES];
66
+    refreshTimer = [NSTimer scheduledTimerWithTimeInterval:(currentState.refreshInterval * MULTIPLIER_PM_INTERVALL_TO_SEC) target:self selector:@selector(refreshTick:) userInfo:nil repeats:YES];
59
     [refreshTimer fire];
67
     [refreshTimer fire];
60
 }
68
 }
61
 
69
 
116
     }
124
     }
117
 }
125
 }
118
 
126
 
127
+-(void)singleItemReloadedCallback:(NSArray *)items {
128
+    if (items != nil) {
129
+        lastFullName = [items objectAtIndex:[items count] - 1]; // last link fullname is at end of array
130
+        items = [items subarrayWithRange:NSMakeRange(0, [items count] - 1)]; // Remove last item
131
+        NSMutableArray *newMenuItems = [NSMutableArray arrayWithArray:menuItems];
132
+        NSMenuItem *item = [self prepareItemForMenu:[items objectAtIndex:0]];
133
+        [newMenuItems addObject:item];
134
+        [statusMenu insertItem:item atIndex:([statusMenu numberOfItems] - numberOfStaticMenuItems + itemsBeforeLinkList)];
135
+        menuItems = newMenuItems;
136
+        redditItems = [redditItems arrayByAddingObjectsFromArray:items];
137
+    }
138
+}
139
+
119
 -(void)reloadListWithOptions {
140
 -(void)reloadListWithOptions {
120
     if ([currentState.modhash isEqualToString:@""]) {
141
     if ([currentState.modhash isEqualToString:@""]) {
121
         [firstMenuItem setTitle:NSLocalizedString(@"Not logged in!", @"Statusitem when no modhash is stored")];
142
         [firstMenuItem setTitle:NSLocalizedString(@"Not logged in!", @"Statusitem when no modhash is stored")];
173
             removed = TRUE;
194
             removed = TRUE;
174
         }
195
         }
175
         
196
         
176
-        if (removed && ([statusMenu numberOfItems] <= 10)) {
197
+        if (removed && ([statusMenu numberOfItems] <= numberOfStaticMenuItems)) {
177
             [self reloadNextList:nil];
198
             [self reloadNextList:nil];
178
         } else {
199
         } else {
179
             if (removed && currentState.reloadAfterVisit) {
200
             if (removed && currentState.reloadAfterVisit) {
180
-                // TODO load one more item!
201
+                [NSThread detachNewThreadSelector:@selector(readSingleItem:) toTarget:api withObject:self];
181
             }
202
             }
182
         }
203
         }
183
     }
204
     }
197
             
218
             
198
             NSInteger index;
219
             NSInteger index;
199
             if ([title isEqualToString:NSLocalizedString(@"Link...", nil)])
220
             if ([title isEqualToString:NSLocalizedString(@"Link...", nil)])
200
-                index = 0;
221
+                index = SUBMENU_INDEX_LINK;
201
             else if ([title isEqualToString:NSLocalizedString(@"Comments...", nil)])
222
             else if ([title isEqualToString:NSLocalizedString(@"Comments...", nil)])
202
-                index = 1;
223
+                index = SUBMENU_INDEX_COMMENTS;
203
             else
224
             else
204
-                index = 2;
225
+                index = SUBMENU_INDEX_BOTH;
205
             
226
             
206
             if ((submenu != nil) && (sender == [submenu itemAtIndex:index])) {
227
             if ((submenu != nil) && (sender == [submenu itemAtIndex:index])) {
207
                 [self openAndRemoveAndReloadWithIndex:i Comments:isComments Both:isBoth];
228
                 [self openAndRemoveAndReloadWithIndex:i Comments:isComments Both:isBoth];
221
 
242
 
222
 -(IBAction)openUnread:(id)sender {
243
 -(IBAction)openUnread:(id)sender {
223
     [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.reddit.com/message/unread"]];
244
     [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.reddit.com/message/unread"]];
224
-    [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(refreshTick:) userInfo:nil repeats:NO];
245
+    [NSTimer scheduledTimerWithTimeInterval:RECHECK_PM_AFTER_OPEN target:self selector:@selector(refreshTick:) userInfo:nil repeats:NO];
225
 }
246
 }
226
 
247
 
227
 -(void)clearMenuItems {
248
 -(void)clearMenuItems {
240
     for (NSUInteger i = 0; i < [array count]; i++) {
261
     for (NSUInteger i = 0; i < [array count]; i++) {
241
         NSMenuItem *item = [self prepareItemForMenu:[array objectAtIndex:i]];
262
         NSMenuItem *item = [self prepareItemForMenu:[array objectAtIndex:i]];
242
         [items addObject:item];
263
         [items addObject:item];
243
-        [statusMenu insertItem:item atIndex:(i + 2)];
264
+        [statusMenu insertItem:item atIndex:(i + itemsBeforeLinkList)];
244
     }
265
     }
245
     menuItems = items;
266
     menuItems = items;
246
 }
267
 }

+ 1
- 0
RedditBar/Reddit.h View File

53
 -(void)readFrontpage:(id)parent;
53
 -(void)readFrontpage:(id)parent;
54
 -(void)readSubreddits:(id)parent;
54
 -(void)readSubreddits:(id)parent;
55
 -(void)readPMs:(id)parent;
55
 -(void)readPMs:(id)parent;
56
+-(void)readSingleItem:(id)parent;
56
 
57
 
57
 // Internal
58
 // Internal
58
 -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res;
59
 -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res;

+ 34
- 3
RedditBar/Reddit.m View File

102
 }
102
 }
103
 
103
 
104
 -(void)readFrontpage:(id)parent {
104
 -(void)readFrontpage:(id)parent {
105
-    NSHTTPURLResponse *response;
106
-    
107
     NSString *after = ((AppDelegate *)parent).lastFullName;
105
     NSString *after = ((AppDelegate *)parent).lastFullName;
108
     Boolean showSubreddits = ((AppDelegate *)parent).currentState.showSubreddit;
106
     Boolean showSubreddits = ((AppDelegate *)parent).currentState.showSubreddit;
109
     NSString *where = ((AppDelegate *)parent).currentState.filter;
107
     NSString *where = ((AppDelegate *)parent).currentState.filter;
112
         url = [NSString stringWithFormat:@"%@.json?limit=%ld", where, (long)length];
110
         url = [NSString stringWithFormat:@"%@.json?limit=%ld", where, (long)length];
113
     else
111
     else
114
         url = [NSString stringWithFormat:@"%@.json?limit=%ld&after=%@", where, (long)length, after];
112
         url = [NSString stringWithFormat:@"%@.json?limit=%ld&after=%@", where, (long)length, after];
115
-        
113
+
114
+    NSHTTPURLResponse *response;
116
     NSData *data = [self queryAPI:url withResponse:&response];
115
     NSData *data = [self queryAPI:url withResponse:&response];
117
     if ((data == nil) || ([response statusCode] != 200)) {
116
     if ((data == nil) || ([response statusCode] != 200)) {
118
         [parent performSelectorOnMainThread:@selector(reloadListHasFrontpageCallback:) withObject:nil waitUntilDone:false];
117
         [parent performSelectorOnMainThread:@selector(reloadListHasFrontpageCallback:) withObject:nil waitUntilDone:false];
148
     }
147
     }
149
 }
148
 }
150
 
149
 
150
+-(void)readSingleItem:(id)parent {
151
+    NSString *after = ((AppDelegate *)parent).lastFullName;
152
+    Boolean showSubreddits = ((AppDelegate *)parent).currentState.showSubreddit;
153
+    NSString *where = ((AppDelegate *)parent).currentState.filter;
154
+    NSString *url;
155
+    if (((AppDelegate *)parent).currentState.useSubscriptions) {
156
+        if (after == nil)
157
+            url = [NSString stringWithFormat:@"%@.json?limit=1", where];
158
+        else
159
+            url = [NSString stringWithFormat:@"%@.json?limit=1&after=%@", where, after];
160
+    } else {
161
+        NSMutableString *subs = [NSMutableString stringWithString:@"r/"];
162
+        for (NSUInteger i = 0; i < [subreddits count]; i++) {
163
+            [subs appendString:[subreddits objectAtIndex:i]];
164
+            if (i < ([subreddits count] - 1)) {
165
+                [subs appendString:@"+"];
166
+            }
167
+        }
168
+        if (after == nil)
169
+            url = [NSString stringWithFormat:@"%@/%@.json?limit=1", subs, where];
170
+        else
171
+            url = [NSString stringWithFormat:@"%@/%@.json?limit=1&after=%@", subs, where, after];
172
+    }
173
+    NSHTTPURLResponse *response;
174
+    NSData *data = [self queryAPI:url withResponse:&response];
175
+    if ((data == nil) || ([response statusCode] != 200)) {
176
+        [(AppDelegate *)parent performSelectorOnMainThread:@selector(singleItemReloadedCallback:) withObject:nil waitUntilDone:FALSE];
177
+    } else {
178
+        [(AppDelegate *)parent performSelectorOnMainThread:@selector(singleItemReloadedCallback:) withObject:[self convertJSONToItemArray:data ShowSubs:showSubreddits] waitUntilDone:FALSE];
179
+    }
180
+}
181
+
151
 -(NSArray *)convertJSONToItemArray:(NSData *)data ShowSubs:(Boolean)showSubs {
182
 -(NSArray *)convertJSONToItemArray:(NSData *)data ShowSubs:(Boolean)showSubs {
152
     NSError *error;
183
     NSError *error;
153
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
184
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

+ 1
- 1
RedditBar/RedditBar-Info.plist View File

21
 	<key>CFBundleSignature</key>
21
 	<key>CFBundleSignature</key>
22
 	<string>????</string>
22
 	<string>????</string>
23
 	<key>CFBundleVersion</key>
23
 	<key>CFBundleVersion</key>
24
-	<string>298</string>
24
+	<string>301</string>
25
 	<key>LSApplicationCategoryType</key>
25
 	<key>LSApplicationCategoryType</key>
26
 	<string>public.app-category.utilities</string>
26
 	<string>public.app-category.utilities</string>
27
 	<key>LSMinimumSystemVersion</key>
27
 	<key>LSMinimumSystemVersion</key>

Loading…
Cancel
Save