-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseClient.m
307 lines (283 loc) · 11.2 KB
/
ParseClient.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
//
// ParseClient.m
// unnamed
//
// Created by Bruce Ng on 2/18/15.
// Copyright (c) 2015 com.yahoo. All rights reserved.
//
#import "ParseClient.h"
NSString * const UserDidPostNewSurveyNotification = @"kUserDidPostNewSurveyNotification";
NSString * const UserDidPostUpdateSurveyNotification = @"kUserDidPostUpdateSurveyNotification";
NSInteger const ResultCount = 8;
@implementation ParseClient
//+ (void)getMyAnsweredSurveysOnPage:(NSInteger)page withCompletion:(void(^)(NSArray *surveys, NSError *error))completion {
//
// PFQuery *voteQuery = [Vote query];
// [voteQuery orderByAscending:@"createdAt"];
// [voteQuery whereKey:@"user" equalTo:[PFUser currentUser]];
// [voteQuery includeKey:@"question"];
// voteQuery.skip = page * ResultCount;
// voteQuery.limit = ResultCount;
//
// PFQuery *query = [Question query];
// [query whereKey:@"objectId" matchesKey:@"questionId" inQuery:voteQuery];
// [query includeKey:@"user"];
//
// [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// if (!error) {
// [ParseClient getAnswersForQuestions:objects withResults:[NSMutableArray array] andCompletion:completion];
// } else {
// completion([NSArray array], error);
// }
// }];
//}
+ (void)getUser:(User *)user surveysComplete:(BOOL)complete onPage:(NSInteger)page withCompletion:(void(^)(NSArray *surveys, NSError *error))completion {
if (!user || !user.parseUser) {
completion([NSArray array], [[NSError alloc] initWithDomain:@"No user found" code:909 userInfo:nil]);
return;
}
PFQuery *query = [Question query];
[query orderByDescending:@"createdAt"];
[query whereKey:@"user" equalTo:user.parseUser];
[query includeKey:@"user"];
[query includeKey:@"questionPhotos"];
[query whereKey:@"complete" equalTo:@(complete)];
query.skip = page * ResultCount;
query.limit = ResultCount;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
[ParseClient getMyVotesForQuestions:objects withCompletion:completion];
} else {
completion([NSArray array], error);
}
}];
}
+ (void)getHomeSurveysOnPage:(NSInteger)page withCompletion:(void(^)(NSArray *surveys, NSError *error))completion {
PFQuery *query = [Question query];
[query orderByDescending:@"createdAt"];
[query includeKey:@"user"];
[query includeKey:@"questionPhotos"];
query.skip = page * ResultCount;
query.limit = ResultCount;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
[ParseClient getMyVotesForQuestions:objects withCompletion:completion];
} else {
completion([NSArray array], error);
}
}];
}
+ (void)getMyVotesForQuestions:(NSArray *)questions withCompletion:(void(^)(NSArray *surveys, NSError *error))completion {
NSMutableArray *questionIds = [NSMutableArray array];
for (Question *question in questions) {
[questionIds addObject:question.objectId];
}
PFQuery *query = [Vote query];
[query whereKey:@"questionId" containedIn:questionIds];
[query whereKey:@"user" equalTo:[PFUser currentUser]];
[query includeKey:@"answer"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSMutableDictionary *votesDict = [NSMutableDictionary dictionary];
for (Vote *vote in objects) {
[votesDict setObject:vote forKey:vote.questionId];
}
NSMutableArray *surveys = [NSMutableArray array];
for (Question *question in questions) {
Survey *survey = [[Survey alloc] initWithQuestion:question];
Vote *vote = votesDict[survey.question.objectId];
if (vote) {
survey.voted = YES;
survey.vote = vote;
} else {
survey.voted =NO;
}
[surveys addObject:survey];
}
completion(surveys, nil);
} else {
completion([NSArray array], error);
}
}];
}
+ (void)saveTextSurvey:(Survey *)survey withCompletion:(void(^)(BOOL succeeded, NSError *error))completion{
NSMutableArray *validAnswers = [NSMutableArray array];
for (Answer *answer in survey.answers) {
if ([answer.text length] >= 1) {
[validAnswers addObject:answer.text];
}
}
if ([survey.question.text length] >= 8 && validAnswers.count >= 2) {
//Submit question
survey.question.anonymous = NO;
survey.question.complete = NO;
survey.question.numAnswers = validAnswers.count;
NSMutableArray *counts = [NSMutableArray array];
for (int i = 0; i < validAnswers.count; i++) {
[counts addObject:@(0)];
}
survey.question.answerVoteCounts = counts;
survey.question.answerTexts = [NSArray arrayWithArray:validAnswers];
[survey.question saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
completion(YES, nil);
} else {
NSLog(@"unable to save question");
completion(NO, error);
}
}];
}
}
+ (void)savePhotoSurvey:(Survey *)survey withCompletion:(void(^)(BOOL succeeded, NSError *error))completion{
NSMutableArray *validAnswers = [NSMutableArray array];
for (Answer *answer in survey.answers) {
if (answer.photo) {
[validAnswers addObject:answer.photo];
}
}
if ([survey.question.text length] >= 8 && validAnswers.count >= 2) {
//Submit question
survey.question.anonymous = NO;
survey.question.complete = NO;
survey.question.numAnswers = validAnswers.count;
NSMutableArray *counts = [NSMutableArray array];
for (int i = 0; i < validAnswers.count; i++) {
[counts addObject:@(0)];
}
survey.question.answerVoteCounts = counts;
QuestionPhotos *questionPhotos = [[QuestionPhotos alloc] init];
for (int i = 0; i < validAnswers.count && i < 4; i++) {
NSData *imageData = UIImagePNGRepresentation(validAnswers[i]);
switch (i) {
case 0:
{
PFFile *imageFile = [PFFile fileWithName:@"answer1.png" data:imageData];
questionPhotos.answerPhoto1 = imageFile;
break;
}
case 1:
{
PFFile *imageFile = [PFFile fileWithName:@"answer2.png" data:imageData];
questionPhotos.answerPhoto2 = imageFile;
break;
}
case 2:
{
PFFile *imageFile = [PFFile fileWithName:@"answer3.png" data:imageData];
questionPhotos.answerPhoto3 = imageFile;
break;
}
case 3:
{
PFFile *imageFile = [PFFile fileWithName:@"answer4.png" data:imageData];
questionPhotos.answerPhoto4 = imageFile;
break;
}
default:
break;
}
}
[questionPhotos saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
survey.question.questionPhotos = questionPhotos;
[survey.question saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
completion(YES, nil);
} else {
NSLog(@"unable to save question");
completion(NO, error);
}
}];
} else {
completion(NO, error);
}
}];
} else {
completion(NO, [[NSError alloc] initWithDomain:@"Not enough valid answers for question" code:500 userInfo:nil]);
return;
}
}
+ (void)saveVoteOnSurvey:(Survey *)survey withAnswer:(Answer *)answer withCompletion:(void(^)(Survey *survey, NSError *error))completion {
Vote *vote =[[Vote alloc] init];
NSInteger oldAnswerIndex = -1;
if (survey.vote) {
vote = survey.vote;
oldAnswerIndex = vote.answerIndex;
}
vote.answerIndex = answer.index;
vote.user = [PFUser currentUser];
vote.questionId = survey.question.objectId;
[vote saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[ParseClient updateVoteCountOnSurvey:survey oldAnswerIndex:oldAnswerIndex newVote:vote withCompletion:completion];
} else {
completion(nil, error);
}
}];
}
+ (void)saveComment:(Comment *)comment withCompletion:(void(^)(NSError *error))completion{
//comment already set up
[comment saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if(!error){
completion(nil);
}else{
completion(error);
}
}];
}
+ (void)getCommentsOnSurvey:(Survey *)survey withCompletion:(void(^)(NSArray *comments, NSError *error))completion{
PFQuery *query = [Comment query];
[query orderByDescending:@"createdAt"];
[query includeKey:@"user"];
[query whereKey:@"questionId" equalTo:survey.question.objectId];
//adding to code to select pages to load results from...
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
completion(objects, nil);
} else {
completion(nil, error);
}
}];
}
+ (void)updateVoteCountOnSurvey:(Survey *)survey oldAnswerIndex:(NSInteger)oldIndex newVote:(Vote *)vote withCompletion:(void(^)(Survey *survey, NSError *error))completion{
NSMutableArray *newCounts = [NSMutableArray arrayWithArray:survey.question.answerVoteCounts];
NSInteger count = [newCounts[vote.answerIndex] integerValue];
count++;
newCounts[vote.answerIndex] = @(count);
Answer* newAnswer = survey.answers[vote.answerIndex];
newAnswer.count = count;
survey.totalVotes++;
if (oldIndex >= 0) {
count = [newCounts[oldIndex] integerValue] - 1;
if (count < 0) count = 0;
newCounts[oldIndex] = @(count);
Answer* oldAnswer = survey.answers[oldIndex];
oldAnswer.count = count;
survey.totalVotes--;
}
survey.question.answerVoteCounts = newCounts;
survey.totalVotes = [survey.question getTotalVotes];//Update Total Vote Count
[survey.question saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
survey.voted = YES;
survey.vote = vote;
completion(survey, nil);
} else {
// NEed to delete saved vote;
}
}];
}
+ (void)setImageView:(UIImageView *)iView fromAnswer:(Answer *)answer {
if (answer.photo) {
iView.image = answer.photo;
} else {
[answer.photoFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *newImage = [UIImage imageWithData:imageData];
answer.photo = newImage;
[iView setImage:newImage];
}
}];
}
}
@end