Created
April 10, 2015 10:43
-
-
Save zummenix/764c1fadb5ef784b38a1 to your computer and use it in GitHub Desktop.
Simple example with ReactiveCocoa framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ValidationView.h" | |
#import "Validator.h" | |
@interface ValidationView () | |
@property (nonatomic, weak) IBOutlet UITextField *nameTextField; | |
@property (nonatomic, weak) IBOutlet UITextField *passwordTextField; | |
@property (nonatomic, weak) IBOutlet UIActivityIndicatorView *activityView; | |
@property (nonatomic, weak) IBOutlet UILabel *errorLabel; | |
@property (nonatomic, weak) IBOutlet UIButton *validateButton; | |
@property (nonatomic) BOOL validating; | |
@end | |
@implementation ValidationView | |
- (void)awakeFromNib | |
{ | |
[super awakeFromNib]; | |
[self setupUI]; | |
[self setupUILogic]; | |
} | |
- (void)setupUI | |
{ | |
self.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:1.0].CGColor; | |
self.layer.borderWidth = 1.0; | |
self.layer.cornerRadius = 6.0; | |
self.errorLabel.hidden = YES; | |
self.validateButton.layer.borderWidth = 1.0; | |
self.validateButton.layer.cornerRadius = 6.0; | |
self.validateButton.backgroundColor = [UIColor whiteColor]; | |
} | |
- (void)setupUILogic | |
{ | |
@weakify(self); | |
RAC(self.validateButton, enabled, @NO) = [RACSignal combineLatest:@[ | |
self.nameTextField.rac_textSignal, | |
self.passwordTextField.rac_textSignal, | |
RACObserve(self, validating) | |
] reduce:^(NSString *name, NSString *password, NSNumber *validating) { | |
return @(name.length > 0 && password.length > 0 && !validating.boolValue); | |
}]; | |
[[RACObserve(self, validating) deliverOnMainThread] subscribeNext:^(NSNumber *validating) { | |
@strongify(self) | |
self.nameTextField.enabled = !validating.boolValue; | |
self.passwordTextField.enabled = !validating.boolValue; | |
self.activityView.hidden = !validating.boolValue; | |
if (validating.boolValue) { | |
self.errorLabel.hidden = YES; | |
} | |
}]; | |
[[RACObserve(self.validateButton, enabled) deliverOnMainThread] subscribeNext:^(NSNumber *enabled) { | |
@strongify(self) | |
if (enabled.boolValue) { | |
self.validateButton.layer.borderColor = self.validateButton.tintColor.CGColor; | |
} else { | |
self.validateButton.layer.borderColor = [UIColor lightGrayColor].CGColor; | |
} | |
}]; | |
[[[self.validateButton rac_signalForControlEvents:UIControlEventTouchUpInside] deliverOnMainThread] subscribeNext:^(id _) { | |
@strongify(self) | |
self.validating = YES; | |
// Emulation of an async task like a network request. | |
RACSignal *validatorSignal = [Validator validatorSignalForName:self.nameTextField.text password:self.passwordTextField.text]; | |
[[validatorSignal deliverOnMainThread] subscribeError:^(NSError *error) { | |
@strongify(self) | |
[self finishValidationWithSuccess:NO error:error]; | |
} completed:^{ | |
@strongify(self) | |
[self finishValidationWithSuccess:YES error:nil]; | |
}]; | |
}]; | |
} | |
- (void)finishValidationWithSuccess:(BOOL)success error:(NSError *)error | |
{ | |
self.validating = NO; | |
if (success) { | |
if ([self.delegate respondsToSelector:@selector(validationViewDidFinishValidationWithSuccess:)]) { | |
[self.delegate validationViewDidFinishValidationWithSuccess:self]; | |
} | |
} else { | |
self.errorLabel.text = error.localizedDescription; | |
} | |
self.errorLabel.hidden = success; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment