-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWFeedDataSource.m
412 lines (283 loc) · 13.2 KB
/
SWFeedDataSource.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//
// SWDataSource.m
// Starworld Test2
//
// Created by Aaron Baker on 8/13/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "SWFeedDataSource.h"
#import "SWFeedModel.h"
#import "SWPost.h"
#import "SWUnitConverter.h"
#import "SWPostTableCell.h"
#import "SWPostTableItem.h"
#import "SWStarPostTableCell.h"
#import "SWTextTableCell.h"
#import "SWTextTableItem.h"
#import "MapTableItem.h"
#import "MapTableCell.h"
// Three20 Additions
#import <Three20Core/NSDateAdditions.h>
@interface SWFeedDataSource (hidden)
- (NSString*)timeIntervalWithStartDate:(NSDate*)d1 withEndDate:(NSDate*)d2;
- (void) addBonusCellText;
- (void) addMapCell;
- (void) newPostLinkPressed;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation SWFeedDataSource
@synthesize myItems;
@synthesize delegate;
@synthesize searchRemote;
@synthesize searchFeedModel = _searchFeedModel;
///////////////////////////////////////////////////////////////////////////////////////////////////
-(id)initWithStarred:(BOOL)starred {
if ((self = [super init])) {
_searchFeedModel = [[SWFeedModel alloc] initWithStarred:starred];
showStarred = starred;
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
TT_RELEASE_SAFELY(_searchFeedModel);
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id<TTModel>)model {
return _searchFeedModel;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableViewDidLoadModel:(UITableView*)tableView {
myItems = [[NSMutableArray alloc] init];
mySections = [[NSMutableArray alloc] init];
currentUser = [SWCurrentUser currentUserInstance];
NSLog(@"YOU KNOW WE GOT SOEM FRST");
CLLocationDegrees currentX = currentUser.x;
CLLocationDegrees currentY = currentUser.y;
CLLocationDegrees postX;
CLLocationDegrees postY;
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:currentY longitude:currentX];
CLLocation *postLocation;
float distance;
NSString *distanceString;
NSString *sectionsString;
//[sections addObject:@"GOLD"];
float shortestDistance = 10000.0;
for (NSMutableArray* dataSections in _searchFeedModel.posts) {
if ([dataSections count] > 0) {
NSMutableArray* itemListMutable = [[NSMutableArray alloc] initWithCapacity:[dataSections count]];
NSArray* itemList;
//Before going through the posts, we first get the title for the label
float highestDistance = 0.0;
int postCount = 0;
for (SWPost* post in dataSections) {
postCount++;
postX = post.x;
postY = post.y;
postLocation = [[CLLocation alloc] initWithLatitude:postY longitude:postX];
distance = [currentLocation distanceFromLocation:postLocation];
[postLocation release];
if (distance > highestDistance) {
highestDistance = distance;
}
if (distance < shortestDistance) {
shortestDistance = distance;
}
}
distanceString = [SWUnitConverter convertFromMetersRounded:highestDistance];
if (postCount == 1) {
sectionsString = [NSString stringWithFormat:@"%d post within %@",postCount,distanceString];
} else {
sectionsString = [NSString stringWithFormat:@"%d posts within %@",postCount,distanceString];
}
//This line was generating great section strings.
//It's not anymore.
//[mySections addObject:sectionsString];
[mySections addObject:@""];
//Build each post an add them to the item list.
for (SWPost* post in dataSections) {
//TTDPRINT(@"Response text: %@", response.text);
postX = post.x;
postY = post.y;
postLocation = [[CLLocation alloc] initWithLatitude:postY longitude:postX];
distance = [currentLocation distanceFromLocation:postLocation];
[postLocation release];
distanceString = [SWUnitConverter convertFromMeters:distance];
[itemListMutable addObject: [SWPostTableItem itemWithTitle:post.name
caption:distanceString
text:[post.content
stringByReplacingOccurrencesOfString:@"<"
withString:@"<"]
timestamp:post.time
ID:post.ID
starcount:post.starCount
x:postX
y:postY
URL:nil]];
}
itemList = [NSArray arrayWithArray:itemListMutable];
[myItems addObject:itemList];
}
}
// if (!_searchFeedModel.finished) {
// [items addObject:[TTTableMoreButton itemWithText:@"more…"]];
// }
//[self addMapCell];
if (shortestDistance > 310 && self.searchRemote == NO)
[self addBonusCellText];
if (myItems.count < 1)
[self addBonusCellText];
NSLog(@"YOU KNOW WE GOT SOEM ITEMS: %d", myItems.count );
self.items = myItems;
self.sections = mySections;
[delegate populateMapWithItems:myItems];
TT_RELEASE_SAFELY(myItems);
TT_RELEASE_SAFELY(mySections);
}///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) addMapCell {
MapTableItem *mapItem = [MapTableItem mapItemWithItems:myItems];
NSArray *sectionItemHolder = [NSArray arrayWithObject:mapItem];
[mySections insertObject:@"" atIndex:0];
[myItems insertObject:sectionItemHolder atIndex:0];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) addBonusCellText {
// TTTableLongTextItem *bonus = [TTTableLongTextItem itemWithText:@"There are no posts nearby you. Would you like to post one?"
// URL:@"tt://main/newpost"];
if (!showStarred) {
SWTextTableItem *bonus = [SWTextTableItem itemWithText:@"This is Sad. \ue413 There are no posts nearby. Would you like to post one?" delegate:self selector:@selector(newPostLinkPressed)];
NSArray *sectionItemHolder = [NSArray arrayWithObject:bonus];
[mySections insertObject:@"No Posts within 1000 feet" atIndex:0];
[myItems insertObject:sectionItemHolder atIndex:0];
} else {
SWTextTableItem *bonus = [SWTextTableItem itemWithText:@"You can tap the \ue32f to add stars to a post. Starred posts show up here."];
NSArray *sectionItemHolder = [NSArray arrayWithObject:bonus];
[mySections insertObject:@"No Nearby Starred Posts" atIndex:0];
[myItems insertObject:sectionItemHolder atIndex:0];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) newPostLinkPressed {
if (currentUser.authenticated) {
[[TTNavigator navigator] openURLAction:[[TTURLAction actionWithURLPath:@"tt://main/newpost"] applyAnimated:YES]];
} else {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hold On!"
message:@"You need to log in before you can post."
delegate:self
cancelButtonTitle:@"Oh, Nevermind."
otherButtonTitles:@"New Account", @"Login", nil];
[message show];
[message release];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Oh, Nevermind."])
{
NSLog(@"Nevermind was selected.");
}
else if([title isEqualToString:@"New Account"])
{
NSLog(@"New Account was selected.");
[[TTNavigator navigator] openURLAction:[TTURLAction actionWithURLPath:@"tt://main/register"]];
}
else if([title isEqualToString:@"Login"])
{
NSLog(@"Login was selected.");
[[TTNavigator navigator] openURLAction:[TTURLAction actionWithURLPath:@"tt://main/login"]];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (Class)tableView:(UITableView *)tableView cellClassForObject:(id)object {
if([object isKindOfClass:[SWPostTableItem class]]) {
if (showStarred) {
return [SWStarPostTableCell class];
} else {
return [SWPostTableCell class];
}
} else if ([object isKindOfClass:[SWTextTableItem class]]) {
return [SWTextTableCell class];
} else if ([object isKindOfClass:[MapTableItem class]]) {
return [MapTableCell class];
}
else {
return [super tableView:tableView cellClassForObject:object];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)titleForLoading:(BOOL)reloading {
if (reloading) {
return NSLocalizedString(@"Finding More Posts...", @"Starworld feed updating text");
} else {
return NSLocalizedString(@"Finding Nearest Posts...", @"Starworld feed loading text");
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)titleForEmpty {
return NSLocalizedString(@"OH NO! Please try again later.", @"Starworld feed no results");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)subtitleForError:(NSError*)error {
return NSLocalizedString(@"Sorry, there was an error loading the Starworld stream.", @"Starworld Stream Error");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//Constants
#define SECOND 1
#define MINUTE (60 * SECOND)
#define HOUR (60 * MINUTE)
#define DAY (24 * HOUR)
#define MONTH (30 * DAY)
- (NSString*)timeIntervalWithStartDate:(NSDate*)d1 withEndDate:(NSDate*)d2
{
//Calculate the delta in seconds between the two dates
NSTimeInterval delta = [d2 timeIntervalSinceDate:d1];
if (delta < 1 * SECOND)
delta = 1;
if (delta < 1 * MINUTE)
{
return delta == 1 ? @"one second ago" : [NSString stringWithFormat:@"%d seconds", (int)delta];
}
if (delta < 2 * MINUTE)
{
return @"a minute ago";
}
if (delta < 45 * MINUTE)
{
int minutes = floor((double)delta/MINUTE);
return [NSString stringWithFormat:@"%d mins", minutes];
}
if (delta < 90 * MINUTE)
{
return @"1 hour";
}
if (delta < 24 * HOUR)
{
int hours = floor((double)delta/HOUR);
return [NSString stringWithFormat:@"%d hours", hours];
}
if (delta < 48 * HOUR)
{
return @"1 day";
}
if (delta < 30 * DAY)
{
int days = floor((double)delta/DAY);
return [NSString stringWithFormat:@"%d days", days];
}
if (delta < 12 * MONTH)
{
int months = floor((double)delta/MONTH);
return months <= 1 ? @"one month ago" : [NSString stringWithFormat:@"%d months", months];
}
else
{
int years = floor((double)delta/MONTH/12.0);
return years <= 1 ? @"one year ago" : [NSString stringWithFormat:@"%d years", years];
}
}
@end