Skip to content

Commit

Permalink
Fix ProfileViewController. Update lists with new questions when posted
Browse files Browse the repository at this point in the history
  • Loading branch information
brung committed Feb 21, 2015
1 parent c87789b commit 5a67022
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 137 deletions.
2 changes: 2 additions & 0 deletions unnamed/ComposeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ - (void)onSubmitButton {
survey.user = [User currentUser];
[ParseClient saveSurvey:survey withCompletion:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSDictionary *dict = [NSDictionary dictionaryWithObject:survey forKey:@"survey"];
[[NSNotificationCenter defaultCenter] postNotificationName:UserDidPostNewSurveyNotification object:nil userInfo:dict];
[self resetForm];
} else {
[[[UIAlertView alloc] initWithTitle:@"Save Failed" message:@"Unable to save at this time. Please try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
Expand Down
28 changes: 28 additions & 0 deletions unnamed/HomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ @interface HomeViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *surveys;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, assign) BOOL isUpdating;
@property (nonatomic, assign) BOOL isInsertingNewPost;

- (NSInteger)getTotalFromAnswers:(NSArray *)answers;

Expand Down Expand Up @@ -62,6 +63,9 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

- (void)viewDidLoad {
[super viewDidLoad];
//Setup Notification listener
self.isInsertingNewPost = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNewPost:) name:UserDidPostNewSurveyNotification object:nil];

// Setup Objects
self.pageIndex = 0;
Expand All @@ -81,6 +85,18 @@ - (void)viewDidLoad {
[self onTableRefresh];
}

- (void)viewWillAppear:(BOOL)animated {
if (self.isInsertingNewPost) {
self.isInsertingNewPost = NO;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[UIView animateWithDuration:1 animations:^{
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
}];
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
Expand Down Expand Up @@ -139,6 +155,7 @@ - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(
return UITableViewAutomaticDimension;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
SurveyViewController *vc = [[SurveyViewController alloc] init];
Expand All @@ -155,4 +172,15 @@ - (NSInteger)getTotalFromAnswers:(NSArray *)answers {
return total;
}

- (void)onNewPost:(NSNotification *)notification {
Survey *survey = notification.userInfo[@"survey"];
if (survey) {
NSMutableArray *newSurveys = [NSMutableArray arrayWithObject:survey];
[newSurveys addObjectsFromArray:self.surveys];
self.surveys = newSurveys;
self.isInsertingNewPost = YES;
}

}

@end
1 change: 1 addition & 0 deletions unnamed/ParseClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "Answer.h"
#import "Vote.h"

extern NSString * const UserDidPostNewSurveyNotification;
extern NSInteger const ResultCount;

@interface ParseClient : Parse
Expand Down
5 changes: 3 additions & 2 deletions unnamed/ParseClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

#import "ParseClient.h"

NSInteger const ResultCount = 20;
NSString * const UserDidPostNewSurveyNotification = @"kUserDidPostNewSurveyNotification";
NSInteger const ResultCount = 8;

@implementation ParseClient
+ (void)getMyAnsweredSurveysOnPage:(NSInteger)page withCompletion:(void(^)(NSArray *surveys, NSError *error))completion {
Expand Down Expand Up @@ -149,7 +150,7 @@ + (void)saveSurvey:(Survey *)survey withCompletion:(void(^)(BOOL succeeded, NSEr

+ (void)saveAnswerFromNSMutableArray:(NSMutableArray *)answers withCompletion:(void(^)(BOOL succeeded, NSError *error))completion {
if (answers.count < 1) {
completion(YES, nil);
completion(YES, nil);
return;
}

Expand Down
Loading

0 comments on commit 5a67022

Please sign in to comment.