forked from ewindso/ios-parse-titanium-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseSingleton.m
611 lines (464 loc) · 21.7 KB
/
ParseSingleton.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//
// ParseSingleton.m
// parsemodule
//
// Created by Elijah Windsor on 12/10/12.
//
//
#import "ParseSingleton.h"
@implementation ParseSingleton
@synthesize facebook;
@synthesize fbDialogCallback;
static ParseSingleton *sharedSingleton;
+(ParseSingleton *)sharedParseSingleton {
if(!sharedSingleton) {
sharedSingleton = [[ParseSingleton alloc]init];
}
return sharedSingleton;
}
-(void)setApplicationId:(NSString *)appId clientKey:(NSString *)clientKey {
[Parse setApplicationId:appId clientKey:clientKey];
// TEST
/* PFObject *game = [PFObject objectWithClassName:@"TestGame"];
[game saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
PFObject *move = [PFObject objectWithClassName:@"Move"];
[move saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
PFRelation *relation = [game relationforKey:@"moves"];
[relation addObject:move];
[game save];
}];
}];*/
}
-(void)createObjectWithClassName:(NSString *)className andProperties:(NSDictionary *)properties andCallback:(void(^)(NSDictionary *obj, NSError *))callbackBlock {
PFObject *obj = [PFObject objectWithClassName:className];
// check if we need to create PFObjects for some of the properties
NSMutableDictionary *finalProperties = [NSMutableDictionary dictionaryWithDictionary:properties];
NSArray *keys = [finalProperties allKeys];
for(NSInteger i = 0; i < [keys count]; i++) {
id obj = [finalProperties objectForKey:[keys objectAtIndex:i]];
/* if([obj isKindOfClass:[NSDictionary class]]) {
// check if it needs to be a PFObject
NSString *className = [obj objectForKey:@"_className"];
NSString *objectId = [obj objectForKey:@"_objectId"];
if(className && objectId) { // let's make it a PFObject
PFObject *pfObj = [PFObject objectWithoutDataWithClassName:className objectId:objectId];
[finalProperties setValue:pfObj forKey:[keys objectAtIndex:i]]; // assign it
}
}*/
[finalProperties setValue:[self convertToPFObjectIfNeededWithObject:obj] forKey:[keys objectAtIndex:i]];
}
[obj setValuesForKeysWithDictionary:finalProperties];
[obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if(succeeded) { // callback with objectId
callbackBlock([self convertPFObjectToNSDictionary:obj], error);
} else {
callbackBlock(nil, error);
}
}];
}
-(id)convertToPFObjectIfNeededWithObject:(id)obj {
if([obj isKindOfClass:[NSDictionary class]]) {
// check if it needs to be a PFObject
NSString *className = [obj objectForKey:@"_className"];
NSString *objectId = [obj objectForKey:@"_objectId"];
if(className && objectId) { // let's make it a PFObject
PFObject *pfObj = [PFObject objectWithoutDataWithClassName:className objectId:objectId];
// go through and assign keys
NSArray *keys = [obj allKeys];
NSEnumerator *e = [keys objectEnumerator];
id key;
while (key = [e nextObject]) {
id curValue = [obj objectForKey:key];
if([key isEqualToString:@"_className"] ||
[key isEqualToString:@"_objectId"] ||
[key isEqualToString:@"_createdAt"] ||
[key isEqualToString:@"_updatedAt"]) {
continue;
}
// ignore ACL as we don't have a format set up to conver to JS yet
if([curValue isKindOfClass:[NSString class]] ||
[curValue isKindOfClass:[NSNumber class]] ||
[curValue isKindOfClass:[NSArray class]]) {
[pfObj setObject:curValue forKey:key];
} else if([curValue isKindOfClass:[NSDictionary class]]) {
NSString *className = [curValue objectForKey:@"_className"];
if(className) { // it's a type of PFObject
if([className isEqualToString:@"_File"]) { // ignore PFFiles, as we can't recreate them
continue;
}
[pfObj setObject:[self convertToPFObjectIfNeededWithObject:curValue] forKey:key];
} else { // just an NSDictionary
[pfObj setObject:curValue forKey:key];
}
}
}
return pfObj;
}
}
return obj;
}
-(void)fetchObjectOfClassName:(NSString *)className andObjectId:(NSString *)objectId andCallback:(void(^)(NSDictionary *, NSError *))callbackBlock; {
PFObject *obj = [PFObject objectWithoutDataWithClassName:className objectId:objectId];
[obj fetchIfNeededInBackgroundWithBlock:^(PFObject *pfObject, NSError *error) {
NSDictionary *dic = nil;
if(pfObject) {
dic = [self convertPFObjectToNSDictionary:pfObject];
}
callbackBlock(dic, error);
}];
}
-(void)findObjectsOfClassName:(NSString *)className withCriteria:(NSArray *)criteria andCallback:(void(^)(NSArray *, NSError *))callbackBlock {
PFQuery *query = [PFQuery queryWithClassName:className];
for(NSInteger i = 0; i < [criteria count]; i++) {
NSDictionary *dic = [criteria objectAtIndex:i];
NSString *key = [dic objectForKey:@"key"];
NSString *condition = [dic objectForKey:@"condition"];
// if it should be a PFObject, let it be so in query
id value = [self convertToPFObjectIfNeededWithObject:[dic objectForKey:@"value"]];
if([condition isEqualToString:@"=="]) {
[query whereKey:key equalTo:value];
} else if([condition isEqualToString:@">"]) {
[query whereKey:key greaterThan:value];
} else if([condition isEqualToString:@">="]) {
[query whereKey:key greaterThanOrEqualTo:value];
} else if([condition isEqualToString:@"<"]) {
[query whereKey:key lessThan:value];
} else if([condition isEqualToString:@"<="]) {
[query whereKey:key lessThanOrEqualTo:value];
} else if([condition isEqualToString:@"!="]) {
[query whereKey:key notEqualTo:value];
} else if([condition isEqualToString:@"in"]) {
[query whereKey:key containedIn:value];
} else if([condition isEqualToString:@"not in"]) {
[query whereKey:key notContainedIn:value];
} else if([condition isEqualToString:@"orderby"] && [value isEqualToString:@"asc"]) {
[query orderByAscending:key];
} else if([condition isEqualToString:@"orderby"] && [value isEqualToString:@"desc"]) {
[query orderByDescending:key];
}
}
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// convert PFObjects to NSDictionaries
NSMutableArray *responseArray = [NSMutableArray arrayWithCapacity:[objects count]];
for(NSInteger i = 0; i < [objects count]; i++) {
PFObject *curObj = [objects objectAtIndex:i];
NSDictionary *dic = [self convertPFObjectToNSDictionary:curObj];
[responseArray setObject:dic atIndexedSubscript:i];
}
callbackBlock(responseArray, error);
}];
}
-(NSDictionary *)convertPFObjectToNSDictionary:(PFObject *)curObj {
NSArray *keys = [curObj allKeys];
NSEnumerator *e = [keys objectEnumerator];
id object;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
while (object = [e nextObject]) {
id curValue = [curObj objectForKey:object];
// ignore ACL as we don't have a format set up to conver to JS yet
if([curValue isKindOfClass:[PFACL class]]) {
continue;
}
if([curValue isKindOfClass:[PFFile class]]) {
PFFile *curFile = curValue;
curValue = [NSDictionary dictionaryWithObjectsAndKeys:curFile.name, @"name", curFile.url, @"url", @"_File", @"_className", nil];
} else if([curValue isKindOfClass:[PFObject class]]) {
PFObject *curPFObj = curValue;
NSString *className = [curPFObj className];
NSString *objectId = [curPFObj objectId];
curValue = [NSDictionary dictionaryWithObjectsAndKeys:className, @"_className", objectId, @"_objectId", nil];
}
[dict setValue:curValue forKey:object];
}
// assign id
[dict setValue:curObj.objectId forKey:@"_objectId"];
// remember className
[dict setValue:curObj.className forKey:@"_className"];
// assign createdAt, updatedAt
[dict setValue:curObj.createdAt forKey:@"_createdAt"];
[dict setValue:curObj.updatedAt forKey:@"_updatedAt"];
return [dict autorelease];
}
-(NSDictionary *)convertPFFileToNSDictionary:(PFFile *)curFile {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
// assign id
[dict setValue:curFile.name forKey:@"name"];
[dict setValue:curFile.url forKey:@"url"];
// remember className
[dict setValue:@"File" forKey:@"_className"];
return [dict autorelease];
}
-(void)updateObject:(NSDictionary *)object withCallback:(void(^)(BOOL, NSError *))callbackBlock {
PFObject *obj = [self convertToPFObjectIfNeededWithObject:object];
[obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
callbackBlock(succeeded, error);
}];
}
-(void)deleteObjectWithClassName:(NSString *)className andObjectId:(NSString *)objectId andCallback:(void(^)(BOOL, NSError *))callbackBlock {
PFObject *obj = [PFObject objectWithoutDataWithClassName:className objectId:objectId];
[obj deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
callbackBlock(succeeded, error);
}];
}
-(void)deleteObject:(NSDictionary *)object withCallback:(void(^)(BOOL, NSError *))callbackBlock {
NSMutableDictionary *newObj = [NSMutableDictionary dictionaryWithDictionary:object];
NSString *className = [newObj objectForKey:@"_className"];
NSString *objectId = [newObj objectForKey:@"_objectId"];
[newObj removeObjectForKey:@"_className"];
[newObj removeObjectForKey:@"_objectId"];
[self deleteObjectWithClassName:className andObjectId:objectId andCallback:callbackBlock];
}
-(void)saveAllObjects:(NSArray *)objects withCallback:(void(^)(BOOL, NSError *))callbackBlock {
NSMutableArray *objectArray = [[NSMutableArray alloc]init];
for(NSInteger i = 0; i < [objects count]; i++) {
PFObject *curObject = [self convertToPFObjectIfNeededWithObject:[objects objectAtIndex:i]];
[objectArray addObject:curObject];
}
[PFObject saveAllInBackground:objectArray block:^(BOOL succeeded, NSError *error) {
callbackBlock(succeeded, error);
[objectArray release];
}];
}
#pragma mark -
#pragma mark PFFile
-(void)createFileWithName:(NSString *)name andData:(NSData *)data andAttachmentInfo:(NSDictionary *)attachmentInfo withCallback:(void(^)(NSDictionary *, NSError *))callbackBlock {
PFFile *fileObj = [PFFile fileWithName:name data:data];
[fileObj saveInBackgroundWithBlock:^(BOOL success, NSError *error) {
if(success) {
// use attachment info to attach to PFObject
NSDictionary *attachToDic = [attachmentInfo objectForKey:@"object"];
NSString *attachForKey = [attachmentInfo objectForKey:@"key"];
PFObject *attachToObj = [PFObject objectWithoutDataWithClassName:[attachToDic objectForKey:@"_className"] objectId:[attachToDic objectForKey:@"_objectId"]];
[attachToObj setValue:fileObj forKey:attachForKey];
[attachToObj saveInBackgroundWithBlock:^(BOOL success, NSError *error) {
if(success) {
callbackBlock([self convertPFObjectToNSDictionary:attachToObj], error);
} else {
callbackBlock(nil, error);
}
}];
} else {
callbackBlock(nil, error);
}
}];
}
#pragma mark -
#pragma mark PFUser
-(void)signupUserWithUsername:(NSString *)username andPassword:(NSString *)password andEmail:(NSString *)email andCallback:(void(^)(NSDictionary *, NSError *))callbackBlock {
PFUser *user = [PFUser user];
user.username = username;
user.password = password;
if(email) {
user.email = email;
}
[user signUpInBackgroundWithBlock:^(BOOL success, NSError *error) {
if(success) {
callbackBlock([self convertPFObjectToNSDictionary:user], error);
} else {
callbackBlock(nil, error);
}
}];
}
-(void)signupUserWithUsername:(NSString *)username andPassword:(NSString *)password andCallback:(void(^)(NSDictionary *, NSError *))callbackBlock {
[self signupUserWithUsername:username andPassword:password andEmail:nil andCallback:callbackBlock];
}
-(void)loginWithUsername:(NSString *)username andPassword:(NSString *)password andCallback:(void(^)(NSDictionary *, NSError *))callbackBlock {
[PFUser logInWithUsernameInBackground:username password:password block:^(PFUser *user, NSError *error) {
if(user) {
callbackBlock([self convertPFObjectToNSDictionary:user], error);
} else {
callbackBlock(nil, error);
}
}];
}
-(void)requestPasswordResetForEmail:(NSString *)email {
[PFUser requestPasswordResetForEmail:email];
}
-(void)logout {
[PFUser logOut];
}
-(NSDictionary *)currentUser {
if([PFUser currentUser] == nil) {
return nil;
}
return [self convertPFObjectToNSDictionary:[PFUser currentUser]];
}
-(void)refreshCurrentUser {
[[PFUser currentUser]refresh];
}
#pragma mark -
#pragma mark PFCloud
-(void)callCloudFunction:(NSString *)functionName withParameters:(NSDictionary *)parameters andCallback:(void(^)(id object, NSError *error))callbackBlock {
//TODO: not yet implemented in Parse SDK!
// convert JSON to PFObjects if need-be
/* NSMutableDictionary *finalParameters = [NSMutableDictionary dictionaryWithDictionary:parameters];
NSArray *keys = [finalParameters allKeys];
for(NSInteger i = 0; i < [keys count]; i++) {
id obj = [finalParameters objectForKey:[keys objectAtIndex:i]];
[finalParameters setValue:[self convertToPFObjectIfNeededWithObject:obj] forKey:[keys objectAtIndex:i]];
}*/
[PFCloud callFunctionInBackground:functionName withParameters:parameters block:^(id object, NSError *error) {
if([object isKindOfClass:[PFObject class]]) {
object = [self convertPFObjectToNSDictionary:object];
}
callbackBlock(object, error);
}];
}
#pragma mark -
#pragma mark PFFacebookUtils
-(void)setupFacebookWithAppId:(NSString *)appId {
[PFFacebookUtils initializeWithApplicationId:appId];
}
-(void)facebookLoginWithPermissions:(NSArray *)permissions andCallback:(CallbackBlock)callbackBlock {
[PFFacebookUtils logInWithPermissions:permissions block:^(PFUser *user, NSError *error) {
if(error) {
callbackBlock(nil, error);
} else {
if (!user) {
callbackBlock(nil, nil);
return;
}
NSDictionary *userDic = [self convertPFObjectToNSDictionary:user];
if(user.isNew) {
callbackBlock(userDic, nil);
} else {
callbackBlock(userDic, nil);
}
}
}];
}
-(void)facebookLinkWithUser:(NSDictionary *)user andPermissions:(NSArray *)permissions andCallback:(CallbackBlock)callbackBlock {
PFUser *curUser = [self convertToPFObjectIfNeededWithObject:user];
[PFFacebookUtils linkUser:curUser permissions:permissions block:^(BOOL succeeded, NSError *error) {
if(error) {
callbackBlock(nil, error);
} else {
if (!user) {
callbackBlock(nil, nil);
return;
}
NSDictionary *userDic = [self convertPFObjectToNSDictionary:[PFUser currentUser]];
callbackBlock(userDic, nil);
}
}];
}
-(void)doFbRequestWithPath:(NSString *)path andCallback:(CallbackBlock)callbackBlock {
NSLog(@"Going to do it");
PF_FBRequest *request = [PF_FBRequest requestForGraphPath:path];
NSLog(@"About to start with path: %@", path);
[request startWithCompletionHandler:^(PF_FBRequestConnection *connection, id result, NSError *error) {
NSLog(@"Hey there");
if (!error) {
NSDictionary *userData = (NSDictionary *)result; // The result is a dictionary
callbackBlock(userData, nil);
} else {
callbackBlock(nil, error);
}
}];
NSLog(@"It started");
}
-(void)handleOpenURL:(NSURL *)url {
[PFFacebookUtils handleOpenURL:url];
}
-(NSString *)getFbAccessToken {
PF_FBSession *session = [PFFacebookUtils session];
return session.accessToken;
}
-(void)closeFbSession {
[[PFFacebookUtils session]closeAndClearTokenInformation];
}
-(void)showFacebookDialog:(NSString *)dialog withParams:(NSDictionary *)params andCallback:(SimpleCallbackBlock)callbackBlock {
PF_FBSession *session = [PFFacebookUtils session];
facebook = [[PF_Facebook alloc] initWithAppId:session.appID
andDelegate:nil];
fbDialogCallback = [callbackBlock copy];
// Store the Facebook session information
facebook.accessToken = session.accessToken;
facebook.expirationDate = session.expirationDate;
NSMutableDictionary *finalParams = [NSMutableDictionary dictionaryWithDictionary:params];
[finalParams setObject:session.appID forKey:@"appid"];
[facebook dialog:dialog andParams:finalParams andDelegate:self];
}
#pragma mark -
#pragma mark PFTwitterUtils
-(void)setupTwitterWithConsumerKey:(NSString *)consumerKey andConsumerSecret:(NSString *)consumerSecret {
[PFTwitterUtils initializeWithConsumerKey:consumerKey consumerSecret:consumerSecret];
}
-(void)twitterLoginWithCallback:(CallbackBlock)callbackBlock {
[PFTwitterUtils logInWithBlock:^(PFUser *user, NSError *error) {
if(error) {
callbackBlock(nil, error);
} else {
if (!user) {
callbackBlock(nil, nil);
return;
}
NSDictionary *userDic = [self convertPFObjectToNSDictionary:user];
if(user.isNew) {
callbackBlock(userDic, nil);
} else {
callbackBlock(userDic, nil);
}
}
}];
}
-(void)twitterApiWithUrlString:(NSString *)urlString andMethod:(NSString *)method andData:(NSDictionary *)data withCallback:(CallbackBlockWithExtra)callbackBlock {
if(data && [[method uppercaseString] isEqualToString:@"GET"]) {
// go through nsdictionary
NSArray *keys = [data allKeys];
for(NSInteger i = 0; i < [keys count]; i++) {
NSString *curKey = [keys objectAtIndex:i];
NSString *curVal = [data objectForKey:curKey];
NSString *appendPrefix = @"&";
if(i == 0) {
appendPrefix = @"?";
}
urlString = [NSString stringWithFormat:@"%@%@%@=%@", urlString, appendPrefix, curKey, curVal];
}
}
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:method];
[[PFTwitterUtils twitter] signRequest:request];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *dataString = [NSString stringWithUTF8String:[data bytes]];
callbackBlock(response, dataString, error);
}];
}
- (void)registerForPushWithDeviceToken:(NSString *)deviceToken andSubscribeToChannel:(NSString *)channel withCallback:(void(^)(BOOL, NSError *))callbackBlock {
// Store the deviceToken in the current Installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation addUniqueObject:channel forKey:@"channels"];
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
callbackBlock(succeeded, error);
}];
}
- (void)unsubscribeFromPushChannel:(NSString *)channel withCallback:(void(^)(BOOL, NSError *))callbackBlock {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation removeObject:channel forKey:@"channels"];
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
callbackBlock(succeeded, error);
}];
}
#pragma mark PF_FBDialogDelegate methods
- (void)dialogCompleteWithUrl:(NSURL *)url {
BOOL completed = YES;
// Fix for FB-bug where they hit the Cancel button, but it still says "did complete"
if([url.absoluteString rangeOfString:@"request"].location == NSNotFound) {
completed = NO;
}
if(fbDialogCallback) {
fbDialogCallback(completed);
}
[fbDialogCallback release];
[facebook release];
}
- (void)dialogDidNotComplete:(PF_FBDialog *)dialog {
if(fbDialogCallback) {
fbDialogCallback(false);
}
[fbDialogCallback release];
[facebook release];
}
@end