Votes for photo once logged in as a consequence of trying to vote for the photo when logged out.

This commit is contained in:
Ash Furrow
2013-10-20 17:28:14 -04:00
parent 8f623e70af
commit 755f0d4d4b
4 changed files with 31 additions and 8 deletions

View File

@@ -12,6 +12,4 @@
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, readonly) PXAPIHelper *apiHelper;
@end

View File

@@ -23,7 +23,6 @@
NSString *consumerKey = @"DC2To2BS0ic1ChKDK15d44M42YHf9gbUJgdFoF0m";
NSString *consumerSecret = @"i8WL4chWoZ4kw9fh3jzHK7XzTer1y5tUNvsTFNnB";
self.apiHelper = [[PXAPIHelper alloc] initWithHost:nil consumerKey:consumerKey consumerSecret:consumerSecret];
[PXRequest setConsumerKey:consumerKey consumerSecret:consumerSecret];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

View File

@@ -15,12 +15,16 @@
// Utilities
#import "FRPPhotoImporter.h"
#import <SVProgressHUD/SVProgressHUD.h>
@interface FRPPhotoDetailViewController ()
// Private assignment
@property (nonatomic, strong) FRPPhotoModel *photoModel;
// Private properties
@property (nonatomic, assign) BOOL needsVotingOnceReappeared;
@end
@implementation FRPPhotoDetailViewController
@@ -85,19 +89,22 @@
UIButton *voteButton = [UIButton buttonWithType:UIButtonTypeCustom];
voteButton.frame = CGRectMake(20, CGRectGetHeight(self.view.bounds) - 44 - 20, CGRectGetWidth(self.view.bounds) - 40, 44);
voteButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[RACObserve(self.photoModel, isVotedFor) subscribeNext:^(id x) {
// Note: can't use getter keypath
[RACObserve(self.photoModel, votedFor) subscribeNext:^(id x) {
if ([x boolValue]) {
[voteButton setTitle:@"Voted For!" forState:UIControlStateNormal];
} else {
[voteButton setTitle:@"Vote" forState:UIControlStateNormal];
}
}];
voteButton.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
voteButton.rac_command = [[RACCommand alloc] initWithEnabled:[RACObserve(self.photoModel, isVotedFor) not] signalBlock:^RACSignal *(id input) {
if ([[PXRequest apiHelper] authMode] == PXAPIHelperModeNoAuth) {
// Not logged in
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
@strongify(self);
self.needsVotingOnceReappeared = YES;
FRPLoginViewController *viewController = [[FRPLoginViewController alloc] initWithNibName:@"FRPLoginViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
@@ -111,6 +118,21 @@
return [FRPPhotoImporter voteForPhoto:self.photoModel];
}
}];
[voteButton.rac_command.errors subscribeNext:^(id x) {
[x subscribeNext:^(NSError *error) {
[SVProgressHUD showErrorWithStatus:[error localizedDescription]];
}];
}];
[self.view addSubview:voteButton];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.needsVotingOnceReappeared) {
self.needsVotingOnceReappeared = NO;
[[FRPPhotoImporter voteForPhoto:self.photoModel] replay];
}
}
@end

View File

@@ -12,11 +12,11 @@
@implementation FRPPhotoImporter
+(NSURLRequest *)popularURLRequest {
return [AppDelegate.apiHelper urlRequestForPhotoFeature:PXAPIHelperPhotoFeaturePopular resultsPerPage:100 page:0 photoSizes:PXPhotoModelSizeThumbnail sortOrder:PXAPIHelperSortOrderRating except:PXPhotoModelCategoryNude];
return [[PXRequest apiHelper] urlRequestForPhotoFeature:PXAPIHelperPhotoFeaturePopular resultsPerPage:100 page:0 photoSizes:PXPhotoModelSizeThumbnail sortOrder:PXAPIHelperSortOrderRating except:PXPhotoModelCategoryNude];
}
+(NSURLRequest *)photoURLRequest:(FRPPhotoModel *)photoModel {
return [AppDelegate.apiHelper urlRequestForPhotoID:photoModel.identifier.integerValue];
return [[PXRequest apiHelper] urlRequestForPhotoID:photoModel.identifier.integerValue];
}
+(RACSignal *)importPhotos {
@@ -97,6 +97,10 @@
photoModel.thumbnailURL = [self urlForImageSize:3 inDictionary:dictionary[@"images"]];
if (dictionary[@"voted"]) {
photoModel.votedFor = [dictionary[@"voted"] boolValue];
}
// Extended attributes fetched with subsequent request
if (dictionary[@"comments_count"]) {
photoModel.fullsizedURL = [self urlForImageSize:4 inDictionary:dictionary[@"images"]];