-
Notifications
You must be signed in to change notification settings - Fork 25
/
LogInViewController.m
224 lines (201 loc) · 8.07 KB
/
LogInViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* * Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#import "LogInViewController.h"
@interface LogInViewController ()
@property UITextField *userNameField;
@property UITextField *passwordField;
@property UILabel *statusMessageLabel;
@property UIButton *actionButton;
@end
static NSString *const USERNAME = @"admin";
static NSString *const PASSWORD = @"password";
static NSString *const USERNAME_FIELD_PLACEHOLDER = @"Username";
static NSString *const PASSWORD_FIELD_PLACEHOLDER = @"Password";
static NSString *const SUCCESS_MESSAGE = @"Logged in as admin";
static NSString *const FAIL_MESSAGE = @"PERMISSION DENIED";
static NSString *const LOGIN_BUTTON_TITLE = @"Login";
static NSString *const SUCCESS_BUTTON_TITLE = @"log out";
static NSString *const FAIL_BUTTON_TITLE = @"try again";
/**
* The controller for the login view
*/
@implementation LogInViewController
#pragma mark - Controller Methods
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeMainTextFields];
[self initializeActionButton];
[self initializeAltScreen];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillHideNotification object:nil];
[self displayMainView];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[self reDisplayMainView];
}
#pragma mark - Initialization Methods
/**
* Creates the action button
*/
-(void)initializeActionButton {
//Setting up the button size
CGSize actionButtonSize = CGSizeMake([self getWidthMinusLargePadding], [self getLargeHeightPadding]);
_actionButton = [[UIButton alloc] initWithFrame:[self frameFromCGPoint:CGPointZero AndCGSize:actionButtonSize]];
//placing the button (center at bottom)
[self putViewAtBottom:_actionButton withPadding:[self getMicroHeightPadding]];
[self centerViewByWidth:_actionButton];
}
/**
* Creates the second screen
*/
-(void)initializeAltScreen{
_statusMessageLabel = [[UILabel alloc] initWithFrame: [self frameFromCGPoint:CGPointZero AndCGSize:CGSizeMake([self getWidthMinusSmallPadding], 0)]];
[self fillScreenWithView:_statusMessageLabel];
_statusMessageLabel.font = [UIFont largeFont];
_statusMessageLabel.textAlignment = NSTextAlignmentCenter;
}
/**
* Creates the text field inputs
*/
-(void)initializeMainTextFields {
CGSize inputFieldSize = CGSizeMake([self getWidthMinusLargePadding], [self getDefaultTextfieldHeight]);
CGPoint inputFieldPoint = CGPointMake(0, [self getTopPosition] + [self getMicroHeightPadding]);
_userNameField = [[UITextField alloc] initWithFrame:[self frameFromCGPoint:inputFieldPoint AndCGSize:inputFieldSize]];
[self centerViewByWidth:_userNameField];
[self configureTextFieldProperties:_userNameField withPlaceHolder:USERNAME_FIELD_PLACEHOLDER];
_passwordField = [[UITextField alloc] initWithFrame:[self frameFromCGPoint:inputFieldPoint AndCGSize:inputFieldSize]];
[self centerViewByWidth:_passwordField];
[self putView:_passwordField belowView:_userNameField withPadding:[self getMicroHeightPadding]];
[self configureTextFieldProperties:_passwordField withPlaceHolder:PASSWORD_FIELD_PLACEHOLDER];
_passwordField.secureTextEntry = YES;
}
#pragma mark - Display Methods For Screens
/**
* Displays the main view
*/
-(void)displayMainView{
[self displayActionButtonWithTitle:LOGIN_BUTTON_TITLE andAction:@selector(loginButtonPush:) andColor:[UIColor flatMidnightBlue]];
[self.view addSubview:_passwordField];
[self.view addSubview:_userNameField];
[_userNameField becomeFirstResponder];
}
/**
* Reinitializes the main view
*/
-(void)reDisplayMainView {
[self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
_userNameField.text = @"";
_passwordField.text = @"";
[self displayMainView];
}
/**
* Creates the login success screen
*/
-(void)buildSuccessScreen{
[self displayAltScreenWithButtonTitle:SUCCESS_BUTTON_TITLE andStatusMessage:SUCCESS_MESSAGE andButtonColor:[UIColor flatMidnightBlue]];
}
/**
* Creates the login fail screen
*/
-(void)buildFailScreen{
[self displayAltScreenWithButtonTitle:FAIL_BUTTON_TITLE andStatusMessage:FAIL_MESSAGE andButtonColor:[UIColor flatMidnightBlue]];
}
#pragma mark - Display Methods For Components
/**
* Displays the secondary screen with a specific button title, message, and color
*
* @param buttonTitle the button title
* @param status the message
* @param color the button color
*/
-(void)displayAltScreenWithButtonTitle:(NSString*)buttonTitle andStatusMessage:(NSString*)status andButtonColor:(UIColor*)color{
_statusMessageLabel.text = status;
_actionButton.backgroundColor = color;
[self displayActionButtonWithTitle:buttonTitle andAction:@selector(logoutButtonPressed:) andColor:color];
[self.view addSubview:_statusMessageLabel];
}
/**
* Displays the action button with a specific title
*
* @param title the buttons title
* @param action the action of the button
* @param color the buttons color
*/
-(void)displayActionButtonWithTitle:(NSString*)title andAction:(SEL)action andColor:(UIColor*)color{
[self initializeActionButton];
[_actionButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
_actionButton.backgroundColor = color;
[_actionButton setTitle:title forState:UIControlStateNormal];
[_actionButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_actionButton];
}
#pragma mark - Configure Methods
/**
* Configures the text inputs within the app
*
* @param field the field to configure
* @param placeholder the field's placeholder text
*/
-(void)configureTextFieldProperties:(UITextField*)field withPlaceHolder:(NSString*)placeholder{
field.borderStyle = UITextBorderStyleRoundedRect;
field.font = [UIFont mediumFont];
field.placeholder = placeholder;
field.autocorrectionType = NO;
field.keyboardType = UIKeyboardAppearanceDefault;
field.clearButtonMode = UITextFieldViewModeWhileEditing;
field.returnKeyType = UIReturnKeyDone;
field.backgroundColor = [UIColor whiteColor];
field.autocorrectionType = UITextAutocorrectionTypeNo;
field.autocapitalizationType = UITextAutocapitalizationTypeNone;
[field addTarget:self action:@selector(loginButtonPush:) forControlEvents:UIControlEventEditingDidEndOnExit];
}
#pragma mark - Event Actions
/**
* Event when the keyboard is about to be show
*
* Used to reposition the button
*
* @param notification <#notification description#>
*/
-(void)keyboardWillShow: (NSNotification *) notification {
CGFloat keyboardSize = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGRect newButtonFrame = _actionButton.frame;
newButtonFrame.origin.y -= keyboardSize;
_actionButton.frame = newButtonFrame;
}
/**
* Event when the login button is pushed
*
* @param sender the login button
*/
-(void)loginButtonPush:(UIButton*)sender{
[_actionButton removeFromSuperview];
[_passwordField removeFromSuperview];
[_userNameField removeFromSuperview];
if ([_passwordField.text isEqualToString:PASSWORD] && [_userNameField.text isEqualToString:USERNAME]) {
[self buildSuccessScreen];
} else {
[self buildFailScreen];
}
}
/**
* Event when the logout/tryagain button is pressed
*
* @param sender the logout/tryagain button
*/
-(void)logoutButtonPressed: (UIButton *) sender{
[self reDisplayMainView];
}
@end