Last active
January 1, 2016 07:09
-
-
Save ColinEberhardt/8109483 to your computer and use it in GitHub Desktop.
Before 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; | |
@end | |
@implementation RWViewController { | |
BOOL _passwordIsValid; | |
BOOL _usernameIsValid; | |
RWDummySignInService *_signInService; | |
} | |
- (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 | |
[_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 = _usernameIsValid ? [UIColor clearColor] : [UIColor yellowColor]; | |
self.passwordTextField.backgroundColor = _passwordIsValid ? [UIColor clearColor] : [UIColor yellowColor]; | |
self.signInButton.enabled = _usernameIsValid && _passwordIsValid; | |
self.signInFailureText.hidden = YES; | |
} | |
- (void)usernameTextFieldChanged { | |
_usernameIsValid = [self isValidUsername:self.usernameTextField.text]; | |
[self updateUIState]; | |
} | |
- (void)passwordTextFieldChanged { | |
_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