Created
December 24, 2013 06:18
-
-
Save ColinEberhardt/8109474 to your computer and use it in GitHub Desktop.
After applying the style guide
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
// | |
// RWViewController.m | |
// RWReactivePlayground | |
// | |
// Created by Colin Eberhardt on 18/12/2013. | |
// Copyright (c) 2013 Colin Eberhardt. All rights reserved. | |
// | |
#import "RWViewController.h" | |
#import "RWDummySignInService.h" | |
@interface RWViewController () | |
@property (weak, nonatomic) IBOutlet UITextField *usernameTextField; | |
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField; | |
@property (weak, nonatomic) IBOutlet UIButton *signInButton; | |
@property (weak, nonatomic) IBOutlet UILabel *signInFailureText; | |
@property (nonatomic) BOOL passwordIsValid; | |
@property (nonatomic) BOOL usernameIsValid; | |
@property (strong, nonatomic) RWDummySignInService *signInService; | |
@end | |
@implementation RWViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
[self updateUIState]; | |
_signInService = [RWDummySignInService new]; | |
// handle text changes for both text fields | |
[self.usernameTextField addTarget:self action:@selector(usernameTextFieldChanged) forControlEvents:UIControlEventEditingChanged]; | |
[self.passwordTextField addTarget:self action:@selector(passwordTextFieldChanged) forControlEvents:UIControlEventEditingChanged]; | |
// initially hide the failure message | |
self.signInFailureText.hidden = YES; | |
} | |
- (BOOL)isValidUsername:(NSString *)username { | |
return username.length > 3; | |
} | |
- (BOOL)isValidPassword:(NSString *)password { | |
return password.length > 3; | |
} | |
- (IBAction)signInButtonTouched:(id)sender { | |
// disable all UI controls | |
[self enableUIControls:NO]; | |
// sign in | |
[self.signInService signInWithUsername:self.usernameTextField.text | |
password:self.passwordTextField.text | |
complete:^(BOOL success) { | |
[self enableUIControls:YES]; | |
self.signInFailureText.hidden = success; | |
if (success) { | |
[self performSegueWithIdentifier:@"signInSuccess" sender:self]; | |
} | |
}]; | |
} | |
- (void)enableUIControls: (BOOL)enabled { | |
self.usernameTextField.enabled = enabled; | |
self.passwordTextField.enabled = enabled; | |
self.signInButton.enabled = enabled; | |
} | |
// updates the enabled state and style of the text fields based on whether the current username | |
// and password combo is valid | |
- (void)updateUIState { | |
self.usernameTextField.backgroundColor = self.usernameIsValid ? [UIColor clearColor] : [UIColor yellowColor]; | |
self.passwordTextField.backgroundColor = self.passwordIsValid ? [UIColor clearColor] : [UIColor yellowColor]; | |
self.signInButton.enabled = self.usernameIsValid && self.passwordIsValid; | |
self.signInFailureText.hidden = YES; | |
} | |
- (void)usernameTextFieldChanged { | |
self.usernameIsValid = [self isValidUsername:self.usernameTextField.text]; | |
[self updateUIState]; | |
} | |
- (void)passwordTextFieldChanged { | |
self.passwordIsValid = [self isValidPassword:self.passwordTextField.text]; | |
[self updateUIState]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment