Skip to content

Commit

Permalink
Refactor Parse client models
Browse files Browse the repository at this point in the history
  • Loading branch information
brung committed Feb 21, 2015
1 parent 017f1aa commit 1e11794
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 178 deletions.
8 changes: 3 additions & 5 deletions unnamed/Answer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
// Copyright (c) 2015 com.yahoo. All rights reserved.
//

#import <Parse/PFObject+Subclass.h>
#import <Foundation/Foundation.h>

@interface Answer : PFObject<PFSubclassing>
+ (NSString *)parseClassName;
@property (nonatomic, strong) NSString *objectId;
@property (nonatomic, strong) NSString *questionId;
@interface Answer : NSObject
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, assign) NSInteger count;
@end
12 changes: 0 additions & 12 deletions unnamed/Answer.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,5 @@
#import "Answer.h"

@implementation Answer
@dynamic objectId;
@dynamic questionId;
@dynamic text;
@dynamic count;

+ (void)load {
[self registerSubclass];
}

+ (NSString *)parseClassName {
return @"Answer";
}

@end
4 changes: 1 addition & 3 deletions unnamed/AnswerCollectionView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@
#import <UIKit/UIKit.h>

@interface AnswerCollectionView : UIView

@property (nonatomic, strong) NSArray *answers;

- (void) setAnswers:(NSArray *)answers andTotal:(NSInteger)total;
@end
17 changes: 3 additions & 14 deletions unnamed/AnswerCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@
#import "Answer.h"

@interface AnswerCollectionView ()

@property (nonatomic, strong) NSArray *answers;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, strong) NSMutableArray *answerViews;

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

@end

@implementation AnswerCollectionView
Expand Down Expand Up @@ -50,10 +47,9 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
return self;
}

- (void)setAnswers:(NSArray *)answers {
- (void)setAnswers:(NSArray *)answers andTotal:(NSInteger)total {
_answers = answers;
_total = [self getTotalFromAnswers:answers];

_total = total;
for (AnswerView *view in self.answerViews) {
[view removeFromSuperview];
}
Expand Down Expand Up @@ -90,12 +86,5 @@ - (void)layoutSubviews {
}

#pragma mark - Private Methods
- (NSInteger)getTotalFromAnswers:(NSArray *)answers {
NSInteger total = 0;
for (Answer *ans in answers) {
total += ans.count;
}
return total;
}

@end
56 changes: 26 additions & 30 deletions unnamed/HomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "ProfileViewController.h"
#import "SurveyViewCell.h"
#import "ParseClient.h"
#import "UIColor+AppBgColor.h"

NSString * const kSurveyViewCell = @"SurveyViewCell";

Expand All @@ -29,28 +30,28 @@ - (NSInteger)getTotalFromAnswers:(NSArray *)answers;
@implementation HomeViewController

- (void) setupTestData {
for (int i=0; i<20; i++) {
Survey *survey = [[Survey alloc] init];
survey.question = [[Question alloc] init];
survey.question.text = [NSString stringWithFormat:@"Question %d?", i];
survey.question.anonymous = (i%2==0)?YES:NO;
survey.question.complete = (i%2==0)?NO:YES;

survey.user = [[User alloc] init];
survey.user.name = [NSString stringWithFormat:@"Name %d", i];

NSMutableArray *answers = [[NSMutableArray alloc] init];
for (int j=0; j < ((i % 4) + 2); j++) {
Answer *answer = [[Answer alloc] init];
answer.count = i * j;
answer.text = [NSString stringWithFormat:@"Answer %d - %d", i, j];
[answers addObject:answer];
}
survey.answers = [NSArray arrayWithArray:answers];
survey.voted = (i%2 == 0)?YES:NO;

[self.surveys addObject:survey];
}
// for (int i=0; i<20; i++) {
// Survey *survey = [[Survey alloc] init];
// survey.question = [[Question alloc] init];
// survey.question.text = [NSString stringWithFormat:@"Question %d?", i];
// survey.question.anonymous = (i%2==0)?YES:NO;
// survey.question.complete = (i%2==0)?NO:YES;
//
// survey.user = [[User alloc] init];
// survey.user.name = [NSString stringWithFormat:@"Name %d", i];
//
// NSMutableArray *answers = [[NSMutableArray alloc] init];
// for (int j=0; j < ((i % 4) + 2); j++) {
// Answer *answer = [[Answer alloc] init];
// answer.count = i * j;
// answer.text = [NSString stringWithFormat:@"Answer %d - %d", i, j];
// [answers addObject:answer];
// }
// survey.answers = [NSArray arrayWithArray:answers];
// survey.voted = (i%2 == 0)?YES:NO;
//
// [self.surveys addObject:survey];
// }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
Expand All @@ -64,6 +65,8 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor appBgColor];

//Setup Notification listener
self.isInsertingNewPost = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNewPost:) name:UserDidPostNewSurveyNotification object:nil];
Expand All @@ -82,6 +85,7 @@ - (void)viewDidLoad {
self.tableView.delegate = self;
[self.tableView registerNib:[UINib nibWithNibName:kSurveyViewCell bundle:nil] forCellReuseIdentifier:kSurveyViewCell];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.backgroundColor = [UIColor appBgColor];

[self onTableRefresh];
}
Expand Down Expand Up @@ -177,14 +181,6 @@ - (void)surveyViewCell:(SurveyViewCell *)cell didClickOnUser:(User *)user {
}

#pragma mark - Private Methods
- (NSInteger)getTotalFromAnswers:(NSArray *)answers {
NSInteger total = 0;
for (Answer *ans in answers) {
total += ans.count;
}
return total;
}

- (void)onNewPost:(NSNotification *)notification {
Survey *survey = notification.userInfo[@"survey"];
if (survey) {
Expand Down
6 changes: 2 additions & 4 deletions unnamed/Model/Survey.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@
#import <Foundation/Foundation.h>
#import "Question.h"
#import "User.h"
#import "Answer.h"
#import "Vote.h"
#import "Answer.h"

@interface Survey : NSObject
@property (nonatomic, strong) Question *question;
@property (nonatomic, strong) User *user;
@property (nonatomic, strong) NSArray *answers;
@property (nonatomic, assign) BOOL voted;
@property (nonatomic, strong) Vote *vote;
@property (nonatomic, assign) NSInteger totalVotes;

- (BOOL)isCurrentVoteAnswer:(Answer *)answer;
- (id)initWithQuestion:(Question *)question;
- (id)initWithQuestion:(Question *)question andUser:(User *)user;
- (id)initWithQuestion:(Question *)question andPFUser:(PFUser *)user;

@end
24 changes: 4 additions & 20 deletions unnamed/Model/Survey.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,16 @@

@implementation Survey
- (BOOL)isCurrentVoteAnswer:(Answer *)answer {
return [self.vote.answer.objectId isEqualToString:answer.objectId];
return self.vote.answerIndex == answer.index;
}

- (id)initWithQuestion:(Question *)question {
self = [super init];
if (self) {
self.question = question;
}
return self;
}

- (id)initWithQuestion:(Question *)question andPFUser:(PFUser *)user {
self = [super init];
if (self) {
self.question = question;
self.user = [[User alloc] initWithPFUser:user];
}
return self;
}


- (id)initWithQuestion:(Question *)question andUser:(User *)user {
self = [super init];
if (self) {
self.question = question;
self.user = user;
self.user = [[User alloc] initWithPFUser:question.user];
self.answers = [question createAnswersFromQuestion];
self.totalVotes = [question getTotalVotes];
}
return self;
}
Expand Down
5 changes: 2 additions & 3 deletions unnamed/ParseClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#import <Parse/Parse.h>
#import "Survey.h"
#import "Question.h"
#import "Answer.h"
#import "Vote.h"
#import "Answer.h"
#import "User.h"

extern NSString * const UserDidPostNewSurveyNotification;
Expand All @@ -19,8 +19,7 @@ extern NSInteger const ResultCount;
@interface ParseClient : Parse
+ (void)getHomeSurveysOnPage:(NSInteger)page withCompletion:(void(^)(NSArray *surveys, NSError *error))completion;
+ (void)getUser:(User *)user surveysComplete:(BOOL)complete onPage:(NSInteger)page withCompletion:(void(^)(NSArray *surveys, NSError *error))completion;
+ (void)getMyAnsweredSurveysOnPage:(NSInteger)page withCompletion:(void(^)(NSArray *surveys, NSError *error))completion;
//+ (void)getMyAnsweredSurveysOnPage:(NSInteger)page withCompletion:(void(^)(NSArray *surveys, NSError *error))completion;
+ (void)saveSurvey:(Survey *)survey withCompletion:(void(^)(BOOL succeeded, NSError *error))completion;
+ (void)saveAnswerFromNSMutableArray:(NSMutableArray *)answers withCompletion:(void(^)(BOOL succeeded, NSError *error))completion;
+ (void)saveVoteOnSurvey:(Survey *)survey withAnswer:(Answer *)answer withCompletion:(void(^)(Vote *vote, NSError *error))completion;
@end
Loading

0 comments on commit 1e11794

Please sign in to comment.