-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWFeedModel.m
216 lines (126 loc) · 6.09 KB
/
SWFeedModel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// SWFeedModel.m
// Starworld Test2
//
// Created by Aaron Baker on 7/24/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "SWFeedModel.h"
#import "SWPost.h"
#import <extThree20JSON/extThree20JSON.h>
static NSString* kSWBaseURL = @"http://pandora.starworlddata.com/posts";
static NSString* kSWFeedPath = @"posts_json";
static NSString* kSWStarPath = @"posts_starred_json";
static NSString* kSWRemotePath = @"mapquery_json";
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation SWFeedModel
@synthesize posts = _posts;
@synthesize resultsPerPage = _resultsPerPage;
@synthesize finished = _finished;
@synthesize page = _page;
@synthesize searchRemote;
@synthesize locationTop;
@synthesize locationBottom;
@synthesize locationLeft;
@synthesize locationRight;
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithStarred:(BOOL)starred {
if ((self = [super init])) {
currentUser = [SWCurrentUser currentUserInstance];
_resultsPerPage = 50;
_page = 1;
_posts = [[NSMutableArray array] retain];
showStarred = starred;
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) dealloc {
TT_RELEASE_SAFELY(_posts);
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more {
if (!self.isLoading) { //Something possible important was removed from here.
if (more) {
_page++;
}
else {
_page = 1;
_finished = NO;
[_posts removeAllObjects];
}
NSString* dataPath = kSWFeedPath;
NSString* url;
if (showStarred)
dataPath = kSWStarPath;
if (searchRemote) {
dataPath = kSWRemotePath;
url = [NSString stringWithFormat:@"%@/%@/%f/%f/%f/%f",kSWBaseURL,dataPath,locationLeft,locationRight,locationTop,locationBottom];
} else {
url = [NSString stringWithFormat:@"%@/%@/%f/%f",kSWBaseURL,dataPath,currentUser.y,currentUser.x];
}
NSLog(@"URL: %@",url);
TTURLRequest* request = [TTURLRequest
requestWithURL: url
delegate: self];
request.cachePolicy = NSURLRequestReloadIgnoringCacheData;
request.cacheExpirationAge = TT_CACHE_EXPIRATION_AGE_NEVER;
TTURLJSONResponse* response = [[TTURLJSONResponse alloc] init];
request.response = response;
TT_RELEASE_SAFELY(response);
[request send];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)requestDidFinishLoad:(TTURLRequest*)request {
TTURLJSONResponse* response = request.response;
// NSLog(@"JSON RESPONSE COOKIES");
// for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
// {
// NSLog(@"name: '%@'\n", [cookie name]);
// NSLog(@"value: '%@'\n", [cookie value]);
// NSLog(@"domain: '%@'\n", [cookie domain]);
// NSLog(@"path: '%@'\n", [cookie path]);
// }
//TTDASSERT([response.rootObject isKindOfClass:[NSDictionary class]]);
NSArray* responseSections = [response.rootObject objectForKey:@"posts"];
//NSLog(@"ENTRIES: %@",entries);
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSMutableArray* sections = [NSMutableArray arrayWithCapacity:[responseSections count]];
for (NSDictionary* entries in responseSections) {
NSMutableArray* posts = [NSMutableArray arrayWithCapacity:[entries count]];
for (NSDictionary* entry in entries) {
NSDictionary *entryPost = [entry objectForKey:@"Post"];
NSDictionary *entryUser = [entry objectForKey:@"User"];
NSDate* date = [dateFormatter dateFromString:[entryPost objectForKey:@"created"]];
NSString *username = [entryUser objectForKey:@"username"];
float entryX = [[entryPost objectForKey:@"x"] floatValue];
float entryY = [[entryPost objectForKey:@"y"] floatValue];
NSInteger postID = [[entryPost objectForKey:@"id"] intValue];
NSInteger postStarCount = [[entryPost objectForKey:@"star_count"] intValue];
SWPost* post = [[SWPost alloc]initWithName:username
time:date
x:entryX
y:entryY
ID:postID
starCount:postStarCount
content:[entryPost objectForKey:@"body"]];
//NSLog(@"Body: %@",[entryPost objectForKey:@"body"]);
//NSLog(@"POST ID: %d",postID);
[posts addObject:post];
TT_RELEASE_SAFELY(post);
}
[sections addObject:posts];
}
//_finished = posts.count < _resultsPerPage;
_finished = YES;
[_posts addObjectsFromArray: sections];
TT_RELEASE_SAFELY(dateFormatter);
[super requestDidFinishLoad:request];
}
@end