-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWCurrentUser.m
255 lines (128 loc) · 6.15 KB
/
SWCurrentUser.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
//
// SWCurrentUser.m
// StarWorld
//
// Created by Aaron Baker on 9/12/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "SWCurrentUser.h"
#import <extThree20JSON/extThree20JSON.h>
static NSString *const kSWDefaultsKeyUserIsAuthenticated = @"sw_user_is_authenticated";
@interface SWCurrentUser (hidden)
-(void)getStars;
@end
@implementation SWCurrentUser
@synthesize cookie;
@synthesize username;
@synthesize password;
@synthesize request;
@synthesize authenticated;
@synthesize x;
@synthesize y;
@synthesize starredPostIDs;
+ (SWCurrentUser*)currentUserInstance{
static SWCurrentUser *currentUserInstance;
@synchronized(self) {
if(!currentUserInstance) {
currentUserInstance = [[SWCurrentUser alloc] init];
currentUserInstance.starredPostIDs = [[NSMutableArray alloc] init];
}
}
return currentUserInstance;
}
////////////////////////////////////////////////////////////////////////////////
- (void) login {
self.authenticated = YES;
//self.username = @"Aaron";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:kSWDefaultsKeyUserIsAuthenticated];
for (NSHTTPCookie *newCookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
NSLog(@"New Cookie Value: '%@'\n", [newCookie value]);
self.cookie = [newCookie value];
}
[self getStars];
}
////////////////////////////////////////////////////////////////////////////////
- (void) logout {
self.authenticated = NO;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:NO forKey:kSWDefaultsKeyUserIsAuthenticated];
NSLog(@"Trying to Delete Cookies!");
for (NSHTTPCookie *dcookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
NSLog(@"Delete Cookie: %@",dcookie);
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:dcookie];
}
[self.starredPostIDs removeAllObjects];
NSLog(@"LOGOUT!");
}
////////////////////////////////////////////////////////////////////////////////
- (NSString *)description
{
return [NSString stringWithFormat:@"CURRENT USER\nName: %@ \n Current X: %f\nCurrent Y: %f \n AUTH: %d\n",self.username,self.x,self.y,self.authenticated];
}
- (void) dealloc {
[starredPostIDs release];
[super dealloc];
}
////////////////////////////////////////////////////////////////////////////////
- (void) setStarForPostID: (NSNumber*) postID {
//First add the star to the local array
[self.starredPostIDs addObject:postID];
//Then, we add the star to the server
NSString *addStarURL = [NSString stringWithFormat:@"http://pandora.starworlddata.com/posts/add_star/%@",postID];
addStarRequest = [TTURLRequest requestWithURL: addStarURL
delegate: self];
addStarRequest.cachePolicy = NSURLRequestReloadIgnoringCacheData;
addStarRequest.cacheExpirationAge = TT_CACHE_EXPIRATION_AGE_NEVER;
addStarRequest.response = [[[TTURLDataResponse alloc] init] autorelease];
[addStarRequest send];
}
////////////////////////////////////////////////////////////////////////////////
- (void) removeStarForPostID: (NSNumber*) postID {
//First remove the star from the local array
[self.starredPostIDs removeObject:postID];
//Then, remove the star from the server
NSString *removeStarURL = [NSString stringWithFormat:@"http://pandora.starworlddata.com/posts/remove_star/%@",postID];
removeStarRequest = [TTURLRequest requestWithURL: removeStarURL
delegate: self];
removeStarRequest.cachePolicy = NSURLRequestReloadIgnoringCacheData;
removeStarRequest.cacheExpirationAge = TT_CACHE_EXPIRATION_AGE_NEVER;
removeStarRequest.response = [[[TTURLDataResponse alloc] init] autorelease];
[removeStarRequest send];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
- (void) getStars {
getStarRequest = [TTURLRequest requestWithURL: @"http://pandora.starworlddata.com/users/get_stars"
delegate: self];
getStarRequest.cachePolicy = NSURLRequestReloadIgnoringCacheData;
getStarRequest.cacheExpirationAge = TT_CACHE_EXPIRATION_AGE_NEVER;
getStarRequest.response = [[[TTURLJSONResponse alloc] init] autorelease];
[getStarRequest send];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark TTURLRequestDelegate
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)requestDidStartLoad:(TTURLRequest*)request {
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)requestDidFinishLoad:(TTURLRequest*)URLrequest {
//If we are retreving a list of stars, do this:
if ([URLrequest isEqual:getStarRequest]) {
NSLog(@"IT IS SO EQUAl!");
TTURLJSONResponse* response = URLrequest.response;
NSDictionary *starsDict = [response.rootObject objectForKey:@"stars"];
for (NSArray *starKey in starsDict) {
NSString *starPostIDString = [starsDict objectForKey:starKey];
NSNumber *starPostID = [NSNumber numberWithInt:[starPostIDString integerValue]];
[self setStarForPostID:starPostID];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)request:(TTURLRequest*)request didFailLoadWithError:(NSError*)error {
}
@end