|
@@ -7,6 +7,7 @@
|
7
|
7
|
//
|
8
|
8
|
|
9
|
9
|
#import "Reddit.h"
|
|
10
|
+#import "AppDelegate.h"
|
10
|
11
|
|
11
|
12
|
@implementation Reddit
|
12
|
13
|
|
|
@@ -14,14 +15,18 @@ NSString *version = @"1.0.0";
|
14
|
15
|
NSString *author = @"xythobuz";
|
15
|
16
|
NSString *appName = @"RedditBar";
|
16
|
17
|
|
17
|
|
-@synthesize username, modhash, password;
|
|
18
|
+NSInteger maxTitleLength = 50;
|
|
19
|
+NSString *replaceTextForTitle = @"...";
|
18
|
20
|
|
19
|
|
--(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash {
|
|
21
|
+@synthesize username, modhash, password, length, subreddits;
|
|
22
|
+
|
|
23
|
+-(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash Length:(NSInteger)leng {
|
20
|
24
|
self = [super init];
|
21
|
25
|
if (self) {
|
22
|
26
|
username = name;
|
23
|
27
|
modhash = hash;
|
24
|
28
|
password = nil;
|
|
29
|
+ length = leng;
|
25
|
30
|
}
|
26
|
31
|
return self;
|
27
|
32
|
}
|
|
@@ -36,28 +41,6 @@ NSString *appName = @"RedditBar";
|
36
|
41
|
return self;
|
37
|
42
|
}
|
38
|
43
|
|
39
|
|
--(BOOL)isAuthenticatedNewModhash:(NSString **)newModHash {
|
40
|
|
- NSHTTPURLResponse *response;
|
41
|
|
- NSData *data = [self queryAPI:@"api/me.json" withResponse:&response];
|
42
|
|
- if ((data != nil) && ([response statusCode] == 200)) {
|
43
|
|
- NSError *error;
|
44
|
|
- NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
45
|
|
- NSDictionary *data = [json valueForKey:@"data"];
|
46
|
|
- if (data == nil)
|
47
|
|
- return FALSE;
|
48
|
|
- NSString *newHash = [data valueForKey:@"modhash"];
|
49
|
|
- if ((newHash == nil) || ([newHash isEqualToString:@""]))
|
50
|
|
- return FALSE;
|
51
|
|
- if (![newHash isEqualToString:modhash]) {
|
52
|
|
- modhash = newHash;
|
53
|
|
- *newModHash = newHash;
|
54
|
|
- }
|
55
|
|
- return TRUE;
|
56
|
|
-
|
57
|
|
- }
|
58
|
|
- return FALSE;
|
59
|
|
-}
|
60
|
|
-
|
61
|
44
|
-(NSString *)queryModhash {
|
62
|
45
|
NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
|
63
|
46
|
[stringData appendFormat:@"&user=%@", [self urlencode: username]];
|
|
@@ -92,13 +75,7 @@ NSString *appName = @"RedditBar";
|
92
|
75
|
}
|
93
|
76
|
}
|
94
|
77
|
|
95
|
|
--(NSArray *)readFrontpageLength:(NSInteger)length {
|
96
|
|
- NSHTTPURLResponse *response;
|
97
|
|
- NSString *url = [NSString stringWithFormat:@"hot.json?limit=%ld", (long)length];
|
98
|
|
- NSData *data = [self queryAPI:url withResponse:&response];
|
99
|
|
- if ((data == nil) || ([response statusCode] != 200)) {
|
100
|
|
- return nil;
|
101
|
|
- }
|
|
78
|
+-(NSArray *)convertJSONToItemArray:(NSData *)data {
|
102
|
79
|
NSError *error;
|
103
|
80
|
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
104
|
81
|
NSDictionary *dat = [json valueForKey:@"data"];
|
|
@@ -120,21 +97,61 @@ NSString *appName = @"RedditBar";
|
120
|
97
|
if (!isSelf) {
|
121
|
98
|
comments = [NSString stringWithFormat:@"http://www.reddit.com%@", [current valueForKey:@"permalink"]];
|
122
|
99
|
}
|
|
100
|
+ if ([name length] > maxTitleLength) {
|
|
101
|
+ name = [NSString stringWithFormat:@"%@%@", [name substringToIndex:(maxTitleLength - [replaceTextForTitle length])], replaceTextForTitle];
|
|
102
|
+ }
|
123
|
103
|
RedditItem *r = [RedditItem itemWithName:name Link:link Comments:comments Self:isSelf];
|
124
|
104
|
[array insertObject:r atIndex:i];
|
125
|
105
|
}
|
126
|
106
|
return array;
|
127
|
107
|
}
|
128
|
108
|
|
129
|
|
--(NSArray *)readSubreddits:(NSArray *)source Length:(NSInteger)length {
|
|
109
|
+-(void)readFrontpage:(id)parent {
|
|
110
|
+ NSHTTPURLResponse *response;
|
|
111
|
+ NSString *url = [NSString stringWithFormat:@"hot.json?limit=%ld", (long)length];
|
|
112
|
+ NSData *data = [self queryAPI:url withResponse:&response];
|
|
113
|
+ if ((data == nil) || ([response statusCode] != 200)) {
|
|
114
|
+ [parent performSelectorOnMainThread:@selector(reloadListHasFrontpageCallback:) withObject:nil waitUntilDone:false];
|
|
115
|
+ } else {
|
|
116
|
+ [parent performSelectorOnMainThread:@selector(reloadListHasFrontpageCallback:) withObject:[self convertJSONToItemArray:data] waitUntilDone:false];
|
|
117
|
+ }
|
|
118
|
+}
|
|
119
|
+
|
|
120
|
+-(void)readSubreddits:(id)parent {
|
130
|
121
|
// TODO read subreddits (as multireddit?)
|
131
|
|
- return nil;
|
|
122
|
+ // TODO if implemented, enable checkbox in prefs
|
|
123
|
+}
|
|
124
|
+
|
|
125
|
+-(void)isAuthenticatedNewModhash:(id)parent {
|
|
126
|
+ NSHTTPURLResponse *response;
|
|
127
|
+ NSData *data = [self queryAPI:@"api/me.json" withResponse:&response];
|
|
128
|
+ if ((data != nil) && ([response statusCode] == 200)) {
|
|
129
|
+ NSError *error;
|
|
130
|
+ NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
|
131
|
+ NSDictionary *data = [json valueForKey:@"data"];
|
|
132
|
+ if (data == nil) {
|
|
133
|
+ [parent performSelectorOnMainThread:@selector(reloadListNotAuthenticatedCallback) withObject:nil waitUntilDone:false];
|
|
134
|
+ return;
|
|
135
|
+ }
|
|
136
|
+ NSString *newHash = [data valueForKey:@"modhash"];
|
|
137
|
+ if ((newHash == nil) || ([newHash isEqualToString:@""])) {
|
|
138
|
+ [parent performSelectorOnMainThread:@selector(reloadListNotAuthenticatedCallback) withObject:nil waitUntilDone:false];
|
|
139
|
+ return;
|
|
140
|
+ }
|
|
141
|
+ if (![newHash isEqualToString:modhash]) {
|
|
142
|
+ modhash = newHash;
|
|
143
|
+ }
|
|
144
|
+ [parent performSelectorOnMainThread:@selector(reloadListIsAuthenticatedCallback) withObject:nil waitUntilDone:false];
|
|
145
|
+ return;
|
|
146
|
+
|
|
147
|
+ }
|
|
148
|
+ [parent performSelectorOnMainThread:@selector(reloadListNotAuthenticatedCallback) withObject:nil waitUntilDone:false];
|
132
|
149
|
}
|
133
|
150
|
|
134
|
151
|
-(NSData *)queryAPI:(NSString *)api withResponse:(NSHTTPURLResponse **)res {
|
135
|
152
|
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self getAPIPoint:api]];
|
136
|
153
|
[request setTimeoutInterval:5.0];
|
137
|
|
- [request setCachePolicy:NSURLCacheStorageNotAllowed];
|
|
154
|
+ [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
|
138
|
155
|
[request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
|
139
|
156
|
[request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
|
140
|
157
|
if ((modhash != nil) && (![modhash isEqualToString:@""]))
|
|
@@ -153,7 +170,7 @@ NSString *appName = @"RedditBar";
|
153
|
170
|
NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
|
154
|
171
|
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self getAPIPoint:api]];
|
155
|
172
|
[request setTimeoutInterval:5.0];
|
156
|
|
- [request setCachePolicy:NSURLCacheStorageNotAllowed];
|
|
173
|
+ [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
|
157
|
174
|
[request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
|
158
|
175
|
[request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
|
159
|
176
|
[request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
|
|
@@ -170,8 +187,7 @@ NSString *appName = @"RedditBar";
|
170
|
187
|
}
|
171
|
188
|
|
172
|
189
|
-(NSURL *)getAPIPoint:(NSString *)where {
|
173
|
|
- NSString *url = @"https://ssl.reddit.com";
|
174
|
|
- return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, where]];
|
|
190
|
+ return [NSURL URLWithString:[NSString stringWithFormat:@"https://ssl.reddit.com/%@", where]];
|
175
|
191
|
}
|
176
|
192
|
|
177
|
193
|
-(NSString *)urlencode:(NSString *)string {
|