From f3111faf32986797e4c4e15abe70be4c4d4c14d0 Mon Sep 17 00:00:00 2001 From: Bruce Ng Date: Wed, 25 Feb 2015 16:00:12 -0800 Subject: [PATCH] Fix inserting new surveys on home and profile screen. --- unnamed/AnswerView.m | 3 +++ unnamed/ComposeViewController.m | 18 ++++++++++++------ unnamed/ParseClient.m | 2 -- unnamed/Question.h | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/unnamed/AnswerView.m b/unnamed/AnswerView.m index bd2e93a..f243952 100644 --- a/unnamed/AnswerView.m +++ b/unnamed/AnswerView.m @@ -63,6 +63,9 @@ - (id)initWithCoder:(NSCoder *)aDecoder { - (void)setAnswer:(Answer *)answer { _answer = answer; + if (!answer.text) { + return; + } self.answerLabel.text = [NSString stringWithFormat:@"%ld. %@", self.index, answer.text]; CGFloat percentage = self.total <= 0 ? 0.0 : 1.0f * answer.count / self.total; self.percentLabel.text = [NSString stringWithFormat:@"%.1f%%", percentage * 100.0]; diff --git a/unnamed/ComposeViewController.m b/unnamed/ComposeViewController.m index 98e7882..ec81516 100644 --- a/unnamed/ComposeViewController.m +++ b/unnamed/ComposeViewController.m @@ -202,7 +202,7 @@ - (void)onSubmitButton { for (Answer *answer in self.answers) { if ((self.isShowingTextAnswers && [answer.text length] >= 1) || (!self.isShowingTextAnswers && answer.photo)) { - [validAnswers addObject:answer]; + [validAnswers addObject:answer]; } } if ([self.questionText.text length] >= 8 && validAnswers.count >= 2) { @@ -215,10 +215,13 @@ - (void)onSubmitButton { survey.question.isTextSurvey = self.isShowingTextAnswers; if (self.isShowingTextAnswers) { + survey.question.isTextSurvey = YES; + survey.question.createdAt = [NSDate date] ; [ParseClient saveTextSurvey:survey withCompletion:^(BOOL succeeded, NSError *error) { [self onPostSurvey:survey completionStatus:succeeded error:error]; }]; } else { + survey.question.isTextSurvey = NO; [ParseClient savePhotoSurvey:survey withCompletion:^(BOOL succeeded, NSError *error) { [self onPostSurvey:survey completionStatus:succeeded error:error]; }]; @@ -242,17 +245,18 @@ - (void)onPostSurvey:(Survey *)survey completionStatus:(BOOL) succeeded error:(N - (void)onSuccessfulPostSurvey:(Survey *)survey { NSDictionary *dict = [NSDictionary dictionaryWithObject:survey forKey:@"survey"]; [[NSNotificationCenter defaultCenter] postNotificationName:UserDidPostNewSurveyNotification object:nil userInfo:dict]; - [self onCancelButton]; + [self resetForm]; } - (void)onErrorPost { [[[UIAlertView alloc] initWithTitle:@"Save Failed" message:@"Unable to save at this time. Please try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; } -- (void)onCancelButton { - [self resetForm]; - [self.navigationController dismissViewControllerAnimated:YES completion:nil]; -} +//- (void)onCancelButton { +// [self resetForm]; +// [self dismissViewControllerAnimated:YES completion:nil]; +//// [self.navigationController dismissViewControllerAnimated:YES completion:nil]; +//} - (void)resetForm { self.answers = [NSMutableArray arrayWithObject:[[Answer alloc] init]]; @@ -268,6 +272,8 @@ - (void)resetForm { self.questionText.text = @""; self.questionTextCountLabel.text = [NSString stringWithFormat:@"%ld", maxCount]; self.questionText.alpha = 1; + [self.navigationController popViewControllerAnimated:YES]; +// [self dismissViewControllerAnimated:YES completion:nil]; }]; } diff --git a/unnamed/ParseClient.m b/unnamed/ParseClient.m index 481cf2b..d558acb 100644 --- a/unnamed/ParseClient.m +++ b/unnamed/ParseClient.m @@ -123,7 +123,6 @@ + (void)saveTextSurvey:(Survey *)survey withCompletion:(void(^)(BOOL succeeded, survey.question.anonymous = NO; survey.question.complete = NO; survey.question.numAnswers = validAnswers.count; - survey.answers = validAnswers; NSMutableArray *counts = [NSMutableArray array]; for (int i = 0; i < validAnswers.count; i++) { [counts addObject:@(0)]; @@ -153,7 +152,6 @@ + (void)savePhotoSurvey:(Survey *)survey withCompletion:(void(^)(BOOL succeeded, survey.question.anonymous = NO; survey.question.complete = NO; survey.question.numAnswers = validAnswers.count; - survey.answers = validAnswers; NSMutableArray *counts = [NSMutableArray array]; for (int i = 0; i < validAnswers.count; i++) { [counts addObject:@(0)]; diff --git a/unnamed/Question.h b/unnamed/Question.h index eba73c4..6f519f6 100644 --- a/unnamed/Question.h +++ b/unnamed/Question.h @@ -19,7 +19,7 @@ @property (nonatomic, strong) NSArray *answerVoteCounts; @property (nonatomic, assign) BOOL anonymous; @property (nonatomic, assign) BOOL complete; -@property (nonatomic, readonly) NSDate *createdAt; +@property (nonatomic, assign) NSDate *createdAt; @property (nonatomic, assign) BOOL isTextSurvey; @property (nonatomic, strong) QuestionPhotos *questionPhotos;