Browse Source

Finished single link reloading after removing

Thomas Buck 10 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,6 +65,7 @@
65 65
 -(void)reloadListHasSubredditsCallback:(NSArray *)items;
66 66
 -(void)reloadListHasFrontpageCallback:(NSArray *)items;
67 67
 -(void)readPMsCallback:(NSNumber *)items;
68
+-(void)singleItemReloadedCallback:(NSArray *)items;
68 69
 
69 70
 -(void)prefsDidSave;
70 71
 

+ 29
- 8
RedditBar/AppDelegate.m View File

@@ -30,6 +30,14 @@
30 30
 
31 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 41
 @synthesize statusMenu, statusItem, statusImage, statusHighlightImage, orangeredImage, orangeredHighlightImage, prefWindow, currentState, application, api, firstMenuItem, menuItems, redditItems, lastFullName, refreshTimer, PMItem, PMSeparator;
34 42
 
35 43
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
@@ -55,7 +63,7 @@
55 63
 -(void)recreateRefreshTimer {
56 64
     if (refreshTimer != nil)
57 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 67
     [refreshTimer fire];
60 68
 }
61 69
 
@@ -116,6 +124,19 @@
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 140
 -(void)reloadListWithOptions {
120 141
     if ([currentState.modhash isEqualToString:@""]) {
121 142
         [firstMenuItem setTitle:NSLocalizedString(@"Not logged in!", @"Statusitem when no modhash is stored")];
@@ -173,11 +194,11 @@
173 194
             removed = TRUE;
174 195
         }
175 196
         
176
-        if (removed && ([statusMenu numberOfItems] <= 10)) {
197
+        if (removed && ([statusMenu numberOfItems] <= numberOfStaticMenuItems)) {
177 198
             [self reloadNextList:nil];
178 199
         } else {
179 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,11 +218,11 @@
197 218
             
198 219
             NSInteger index;
199 220
             if ([title isEqualToString:NSLocalizedString(@"Link...", nil)])
200
-                index = 0;
221
+                index = SUBMENU_INDEX_LINK;
201 222
             else if ([title isEqualToString:NSLocalizedString(@"Comments...", nil)])
202
-                index = 1;
223
+                index = SUBMENU_INDEX_COMMENTS;
203 224
             else
204
-                index = 2;
225
+                index = SUBMENU_INDEX_BOTH;
205 226
             
206 227
             if ((submenu != nil) && (sender == [submenu itemAtIndex:index])) {
207 228
                 [self openAndRemoveAndReloadWithIndex:i Comments:isComments Both:isBoth];
@@ -221,7 +242,7 @@
221 242
 
222 243
 -(IBAction)openUnread:(id)sender {
223 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 248
 -(void)clearMenuItems {
@@ -240,7 +261,7 @@
240 261
     for (NSUInteger i = 0; i < [array count]; i++) {
241 262
         NSMenuItem *item = [self prepareItemForMenu:[array objectAtIndex:i]];
242 263
         [items addObject:item];
243
-        [statusMenu insertItem:item atIndex:(i + 2)];
264
+        [statusMenu insertItem:item atIndex:(i + itemsBeforeLinkList)];
244 265
     }
245 266
     menuItems = items;
246 267
 }

+ 1
- 0
RedditBar/Reddit.h View File

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

+ 34
- 3
RedditBar/Reddit.m View File

@@ -102,8 +102,6 @@ NSString *subredditFormat = @" [r/%@]";
102 102
 }
103 103
 
104 104
 -(void)readFrontpage:(id)parent {
105
-    NSHTTPURLResponse *response;
106
-    
107 105
     NSString *after = ((AppDelegate *)parent).lastFullName;
108 106
     Boolean showSubreddits = ((AppDelegate *)parent).currentState.showSubreddit;
109 107
     NSString *where = ((AppDelegate *)parent).currentState.filter;
@@ -112,7 +110,8 @@ NSString *subredditFormat = @" [r/%@]";
112 110
         url = [NSString stringWithFormat:@"%@.json?limit=%ld", where, (long)length];
113 111
     else
114 112
         url = [NSString stringWithFormat:@"%@.json?limit=%ld&after=%@", where, (long)length, after];
115
-        
113
+
114
+    NSHTTPURLResponse *response;
116 115
     NSData *data = [self queryAPI:url withResponse:&response];
117 116
     if ((data == nil) || ([response statusCode] != 200)) {
118 117
         [parent performSelectorOnMainThread:@selector(reloadListHasFrontpageCallback:) withObject:nil waitUntilDone:false];
@@ -148,6 +147,38 @@ NSString *subredditFormat = @" [r/%@]";
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 182
 -(NSArray *)convertJSONToItemArray:(NSData *)data ShowSubs:(Boolean)showSubs {
152 183
     NSError *error;
153 184
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

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

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

Loading…
Cancel
Save