|
@@ -101,6 +101,51 @@ NSString *subredditFormat = @" [r/%@]";
|
101
|
101
|
}
|
102
|
102
|
}
|
103
|
103
|
|
|
104
|
+-(void)readFrontpage:(id)parent {
|
|
105
|
+ NSHTTPURLResponse *response;
|
|
106
|
+
|
|
107
|
+ NSString *after = ((AppDelegate *)parent).lastFullName;
|
|
108
|
+ Boolean showSubreddits = ((AppDelegate *)parent).currentState.showSubreddit;
|
|
109
|
+ NSString *url;
|
|
110
|
+ if (after == nil)
|
|
111
|
+ url = [NSString stringWithFormat:@"hot.json?limit=%ld", (long)length];
|
|
112
|
+ else
|
|
113
|
+ url = [NSString stringWithFormat:@"hot.json?limit=%ld&after=%@", (long)length, after];
|
|
114
|
+
|
|
115
|
+ NSData *data = [self queryAPI:url withResponse:&response];
|
|
116
|
+ if ((data == nil) || ([response statusCode] != 200)) {
|
|
117
|
+ [parent performSelectorOnMainThread:@selector(reloadListHasFrontpageCallback:) withObject:nil waitUntilDone:false];
|
|
118
|
+ } else {
|
|
119
|
+ [parent performSelectorOnMainThread:@selector(reloadListHasFrontpageCallback:) withObject:[self convertJSONToItemArray:data ShowSubs:showSubreddits] waitUntilDone:false];
|
|
120
|
+ }
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+-(void)readSubreddits:(id)parent {
|
|
124
|
+ NSMutableString *subs = [NSMutableString stringWithString:@"r/"];
|
|
125
|
+ for (NSUInteger i = 0; i < [subreddits count]; i++) {
|
|
126
|
+ [subs appendString:[subreddits objectAtIndex:i]];
|
|
127
|
+ if (i < ([subreddits count] - 1)) {
|
|
128
|
+ [subs appendString:@"+"];
|
|
129
|
+ }
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ NSString *after = ((AppDelegate *)parent).lastFullName;
|
|
133
|
+ Boolean showSubreddits = ((AppDelegate *)parent).currentState.showSubreddit;
|
|
134
|
+ NSString *url;
|
|
135
|
+ if (after == nil)
|
|
136
|
+ url = [NSString stringWithFormat:@"%@/hot.json?limit=%ld", subs, (long)length];
|
|
137
|
+ else
|
|
138
|
+ url = [NSString stringWithFormat:@"%@/hot.json?limit=%ld&after=%@", subs, (long)length, after];
|
|
139
|
+
|
|
140
|
+ NSHTTPURLResponse *response;
|
|
141
|
+ NSData *data = [self queryAPI:url withResponse:&response];
|
|
142
|
+ if ((data == nil) || ([response statusCode] != 200)) {
|
|
143
|
+ [(AppDelegate *)parent performSelectorOnMainThread:@selector(reloadListHasSubredditsCallback:) withObject:nil waitUntilDone:FALSE];
|
|
144
|
+ } else {
|
|
145
|
+ [(AppDelegate *)parent performSelectorOnMainThread:@selector(reloadListHasSubredditsCallback:) withObject:[self convertJSONToItemArray:data ShowSubs:showSubreddits] waitUntilDone:FALSE];
|
|
146
|
+ }
|
|
147
|
+}
|
|
148
|
+
|
104
|
149
|
-(NSArray *)convertJSONToItemArray:(NSData *)data ShowSubs:(Boolean)showSubs {
|
105
|
150
|
NSError *error;
|
106
|
151
|
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
|
@@ -108,7 +153,7 @@ NSString *subredditFormat = @" [r/%@]";
|
108
|
153
|
if (dat == nil)
|
109
|
154
|
return nil;
|
110
|
155
|
NSArray *children = [dat valueForKey:@"children"];
|
111
|
|
- if (children == nil)
|
|
156
|
+ if ((children == nil) || ([children count] == 0))
|
112
|
157
|
return nil;
|
113
|
158
|
|
114
|
159
|
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[children count]];
|
|
@@ -126,17 +171,17 @@ NSString *subredditFormat = @" [r/%@]";
|
126
|
171
|
NSString *subreddit = [NSString stringWithFormat:subredditFormat, [current valueForKey:@"subreddit"]];
|
127
|
172
|
NSInteger maxLen = titleLength;
|
128
|
173
|
if ([subreddit length] >= titleLength)
|
129
|
|
- showSubs = FALSE;
|
|
174
|
+ showSubs = FALSE;
|
130
|
175
|
if (showSubs)
|
131
|
|
- maxLen -= [subreddit length];
|
|
176
|
+ maxLen -= [subreddit length];
|
132
|
177
|
if ([name length] > maxLen)
|
133
|
|
- name = [NSString stringWithFormat:@"%@%@", [name substringToIndex:(maxLen - [replaceTextForTitle length])], replaceTextForTitle];
|
|
178
|
+ name = [NSString stringWithFormat:@"%@%@", [name substringToIndex:(maxLen - [replaceTextForTitle length])], replaceTextForTitle];
|
134
|
179
|
if (showSubs)
|
135
|
|
- name = [NSString stringWithFormat:@"%@%@", name, subreddit];
|
|
180
|
+ name = [NSString stringWithFormat:@"%@%@", name, subreddit];
|
136
|
181
|
RedditItem *r = [RedditItem itemWithName:name Link:link Comments:comments Self:isSelf];
|
137
|
182
|
NSString *fullName = [current valueForKey:@"title"];
|
138
|
183
|
if (showSubs)
|
139
|
|
- fullName = [NSString stringWithFormat:@"%@%@", fullName, subreddit];
|
|
184
|
+ fullName = [NSString stringWithFormat:@"%@%@", fullName, subreddit];
|
140
|
185
|
[r setFullName:fullName];
|
141
|
186
|
[array insertObject:r atIndex:i];
|
142
|
187
|
|
|
@@ -148,57 +193,29 @@ NSString *subredditFormat = @" [r/%@]";
|
148
|
193
|
return array;
|
149
|
194
|
}
|
150
|
195
|
|
151
|
|
--(void)readFrontpage:(id)parent {
|
152
|
|
- NSHTTPURLResponse *response;
|
153
|
|
-
|
154
|
|
- NSString *after = ((AppDelegate *)parent).lastFullName;
|
155
|
|
- Boolean showSubreddits = ((AppDelegate *)parent).currentState.showSubreddit;
|
156
|
|
- NSString *url;
|
157
|
|
- if (after == nil)
|
158
|
|
- url = [NSString stringWithFormat:@"hot.json?limit=%ld", (long)length];
|
159
|
|
- else
|
160
|
|
- url = [NSString stringWithFormat:@"hot.json?limit=%ld&after=%@", (long)length, after];
|
161
|
|
-
|
162
|
|
- NSData *data = [self queryAPI:url withResponse:&response];
|
163
|
|
- if ((data == nil) || ([response statusCode] != 200)) {
|
164
|
|
- [parent performSelectorOnMainThread:@selector(reloadListHasFrontpageCallback:) withObject:nil waitUntilDone:false];
|
165
|
|
- } else {
|
166
|
|
- [parent performSelectorOnMainThread:@selector(reloadListHasFrontpageCallback:) withObject:[self convertJSONToItemArray:data ShowSubs:showSubreddits] waitUntilDone:false];
|
167
|
|
- }
|
|
196
|
+-(NSNumber *)messagesCount:(NSData *)data {
|
|
197
|
+ NSError *error;
|
|
198
|
+ NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
|
199
|
+ NSDictionary *dat = [json valueForKey:@"data"];
|
|
200
|
+ if (dat == nil)
|
|
201
|
+ return nil;
|
|
202
|
+ NSArray *children = [dat valueForKey:@"children"];
|
|
203
|
+ if (children == nil)
|
|
204
|
+ return nil;
|
|
205
|
+ return [NSNumber numberWithInteger:[children count]];
|
168
|
206
|
}
|
169
|
207
|
|
170
|
|
--(void)readSubreddits:(id)parent {
|
171
|
|
- NSMutableString *subs = [NSMutableString stringWithString:@"r/"];
|
172
|
|
- for (NSUInteger i = 0; i < [subreddits count]; i++) {
|
173
|
|
- [subs appendString:[subreddits objectAtIndex:i]];
|
174
|
|
- if (i < ([subreddits count] - 1)) {
|
175
|
|
- [subs appendString:@"+"];
|
176
|
|
- }
|
177
|
|
- }
|
178
|
|
-
|
179
|
|
- NSString *after = ((AppDelegate *)parent).lastFullName;
|
180
|
|
- Boolean showSubreddits = ((AppDelegate *)parent).currentState.showSubreddit;
|
181
|
|
- NSString *url;
|
182
|
|
- if (after == nil)
|
183
|
|
- url = [NSString stringWithFormat:@"%@/hot.json?limit=%ld", subs, (long)length];
|
184
|
|
- else
|
185
|
|
- url = [NSString stringWithFormat:@"%@/hot.json?limit=%ld&after=%@", subs, (long)length, after];
|
186
|
|
-
|
|
208
|
+-(void)readPMs:(id)parent {
|
|
209
|
+ NSString *url = @"message/unread.json";
|
187
|
210
|
NSHTTPURLResponse *response;
|
188
|
211
|
NSData *data = [self queryAPI:url withResponse:&response];
|
189
|
212
|
if ((data == nil) || ([response statusCode] != 200)) {
|
190
|
|
- [(AppDelegate *)parent performSelectorOnMainThread:@selector(reloadListHasSubredditsCallback:) withObject:nil waitUntilDone:FALSE];
|
|
213
|
+ [(AppDelegate *)parent performSelectorOnMainThread:@selector(readPMsCallback:) withObject:nil waitUntilDone:FALSE];
|
191
|
214
|
} else {
|
192
|
|
- [(AppDelegate *)parent performSelectorOnMainThread:@selector(reloadListHasSubredditsCallback:) withObject:[self convertJSONToItemArray:data ShowSubs:showSubreddits] waitUntilDone:FALSE];
|
|
215
|
+ [(AppDelegate *)parent performSelectorOnMainThread:@selector(readPMsCallback:) withObject:[self messagesCount:data] waitUntilDone:FALSE];
|
193
|
216
|
}
|
194
|
217
|
}
|
195
|
218
|
|
196
|
|
--(void)readPMs:(id)parent {
|
197
|
|
- // TODO read PMs
|
198
|
|
- // Fill NSArray with RedditItems for each PM, or return nil if none
|
199
|
|
- [(AppDelegate *)parent performSelectorOnMainThread:@selector(readPMsCallback:) withObject:nil waitUntilDone:FALSE];
|
200
|
|
-}
|
201
|
|
-
|
202
|
219
|
-(void)isAuthenticatedNewModhash:(id)parent {
|
203
|
220
|
NSHTTPURLResponse *response;
|
204
|
221
|
NSData *dat = [self queryAPI:@"api/me.json" withResponse:&response];
|