mirror of
https://github.com/zhigang1992/FunctionalReactivePixels.git
synced 2026-01-12 22:47:31 +08:00
Votes for photo once logged in as a consequence of trying to vote for the photo when logged out.
This commit is contained in:
@@ -12,6 +12,4 @@
|
||||
|
||||
@property (strong, nonatomic) UIWindow *window;
|
||||
|
||||
@property (nonatomic, readonly) PXAPIHelper *apiHelper;
|
||||
|
||||
@end
|
||||
|
||||
@@ -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]];
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"]];
|
||||
|
||||
Reference in New Issue
Block a user