-
Notifications
You must be signed in to change notification settings - Fork 30
/
LoginViewController.m
297 lines (258 loc) · 10.6 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//
// LoginViewController.m
// piwigo
//
// Created by Spencer Baker on 1/17/15.
// Copyright (c) 2015 bakercrew. All rights reserved.
//
#import "LoginViewController.h"
#import "LoginViewController_iPhone.h"
#import "LoginViewController_iPad.h"
#import "KeychainAccess.h"
#import "Model.h"
#import "SessionService.h"
#import "AppDelegate.h"
@interface LoginViewController () <UITextFieldDelegate>
@end
@implementation LoginViewController
-(instancetype)init
{
self = [super init];
if(self)
{
self.view.backgroundColor = [UIColor piwigoGray];
self.piwigoLogo = [UIImageView new];
self.piwigoLogo.translatesAutoresizingMaskIntoConstraints = NO;
self.piwigoLogo.image = [UIImage imageNamed:@"piwigoLogo"];
self.piwigoLogo.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:self.piwigoLogo];
self.serverTextField = [ServerField new];
self.serverTextField.translatesAutoresizingMaskIntoConstraints = NO;
self.serverTextField.textField.placeholder = NSLocalizedString(@"login_serverPlaceholder", @"Server");
self.serverTextField.textField.text = [Model sharedInstance].serverName;
self.serverTextField.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.serverTextField.textField.autocorrectionType = UITextAutocorrectionTypeNo;
self.serverTextField.textField.keyboardType = UIKeyboardTypeURL;
self.serverTextField.textField.returnKeyType = UIReturnKeyNext;
self.serverTextField.textField.delegate = self;
[self.view addSubview:self.serverTextField];
self.userTextField = [PiwigoTextField new];
self.userTextField.translatesAutoresizingMaskIntoConstraints = NO;
self.userTextField.placeholder = NSLocalizedString(@"login_userPlaceholder", @"Username (optional)");
self.userTextField.text = [KeychainAccess getLoginUser];
self.userTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.userTextField.autocorrectionType = UITextAutocorrectionTypeNo;
self.userTextField.returnKeyType = UIReturnKeyNext;
self.userTextField.delegate = self;
[self.view addSubview:self.userTextField];
self.passwordTextField = [PiwigoTextField new];
self.passwordTextField.translatesAutoresizingMaskIntoConstraints = NO;
self.passwordTextField.placeholder = NSLocalizedString(@"login_passwordPlaceholder", @"Password (optional)");
self.passwordTextField.secureTextEntry = YES;
self.passwordTextField.text = [KeychainAccess getLoginPassword];
self.passwordTextField.returnKeyType = UIReturnKeyGo;
self.passwordTextField.delegate = self;
[self.view addSubview:self.passwordTextField];
self.loginButton = [PiwigoButton new];
self.loginButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.loginButton setTitle:NSLocalizedString(@"login", @"Login") forState:UIControlStateNormal];
[self.loginButton addTarget:self action:@selector(performLogin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.loginButton];
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]];
self.loadingView = [UIView new];
self.loadingView.translatesAutoresizingMaskIntoConstraints = NO;
self.loadingView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
self.loadingView.hidden = YES;
[self.view addSubview:self.loadingView];
self.loggingInLabel = [UILabel new];
self.loggingInLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.loggingInLabel.text = NSLocalizedString(@"login_loggingIn", @"Logging In...");
self.loggingInLabel.font = [UIFont piwigoFontNormal];
self.loggingInLabel.textColor = [UIColor whiteColor];
[self.loadingView addSubview:self.loggingInLabel];
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.spinner.translatesAutoresizingMaskIntoConstraints = NO;
[self.loadingView addSubview:self.spinner];
[self performSelector:@selector(setupAutoLayout) withObject:nil]; // now located in child VC, thus import .h files
}
return self;
}
-(void)moveTextFieldsBy:(NSInteger)amount
{
self.logoTopConstraint.constant = amount;
[UIView animateWithDuration:0.3 animations:^{
[self.view layoutIfNeeded];
}];
}
-(void)dismissKeyboard
{
[self moveTextFieldsBy:self.topConstraintAmount];
[self.view endEditing:YES];
}
-(void)performLogin
{
[self.view endEditing:YES];
[self showLoading];
if(self.serverTextField.textField.text.length <= 0)
{
[UIAlertView showWithTitle:NSLocalizedString(@"loginEmptyServer_title", @"Enter a Web Address")
message:NSLocalizedString(@"loginEmptyServer_message", @"Please select a protocol and enter a Piwigo web address in order to proceed.")
cancelButtonTitle:NSLocalizedString(@"alertOkButton", @"OK")
otherButtonTitles:nil
tapBlock:nil];
[self hideLoading];
return;
}
NSString *cleanServerString = [self cleanServerString:self.serverTextField.textField.text];
self.serverTextField.textField.text = cleanServerString;
[Model sharedInstance].serverName = cleanServerString;
[Model sharedInstance].serverProtocol = [self.serverTextField getProtocolString];
[[Model sharedInstance] saveToDisk];
if(self.userTextField.text.length > 0) // Perform Login if username exists
{
[SessionService performLoginWithUser:self.userTextField.text
andPassword:self.passwordTextField.text
onCompletion:^(BOOL result, id response) {
if(result)
{
[Model sharedInstance].hadOpenedSession = YES;
[self getSessionStatus];
if([Model sharedInstance].hasAdminRights) {
[self getSessionPluginsList]; // To determine if VideoJS is active
}
}
else
{
[Model sharedInstance].hadOpenedSession = NO;
[self hideLoading];
[self showLoginFail];
}
} onFailure:^(NSURLSessionTask *task, NSError *error) {
[self hideLoading];
NSLog(@"Error %ld: %@", (long)error.code, error.localizedDescription);
[UIAlertView showWithTitle:NSLocalizedString(@"internetErrorGeneral_title", @"Connection Error")
message:[error localizedDescription]
cancelButtonTitle:NSLocalizedString(@"alertOkButton", @"OK")
otherButtonTitles:nil
tapBlock:nil];
}];
}
else // No username, get only session status
{
[Model sharedInstance].hadOpenedSession = NO;
[self getSessionStatus];
[KeychainAccess resetKeychain];
}
}
-(NSString*)cleanServerString:(NSString*)serverString
{
NSString *server = serverString;
NSRange httpRange = [server rangeOfString:@"http://" options:NSCaseInsensitiveSearch];
if(httpRange.location == 0)
{
server = [server substringFromIndex:7];
}
NSRange httpsRange = [server rangeOfString:@"https://" options:NSCaseInsensitiveSearch];
if(httpsRange.location == 0)
{
server = [server substringFromIndex:8];
}
return server;
}
-(void)getSessionStatus
{
[SessionService getStatusOnCompletion:^(NSDictionary *responseObject) {
[self hideLoading];
if(responseObject)
{
if([@"2.7" compare:[Model sharedInstance].version options:NSNumericSearch] != NSOrderedAscending)
{ // they need to update
[UIAlertView showWithTitle:NSLocalizedString(@"serverVersionNotCompatible_title", @"Server Incompatible")
message:[NSString stringWithFormat:NSLocalizedString(@"serverVersionNotCompatible_message", @"Your server version is %@. Piwigo Mobile only supports a version of at least 2.7. Please update your server to use Piwigo Mobile\nDo you still want to continue?"), [Model sharedInstance].version]
cancelButtonTitle:NSLocalizedString(@"alertNoButton", @"No")
otherButtonTitles:@[NSLocalizedString(@"alertYesButton", @"Yes")]
tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
if(buttonIndex == 1)
{ // proceed at their own risk
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate loadNavigation];
}
}];
}
else
{ // their version is Ok
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate loadNavigation];
}
}
else
{
UIAlertView *failAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"sessionStatusError_title", @"Authentication Fail")
message:NSLocalizedString(@"sessionStatusError_message", @"Failed to authenticate with server.\nTry logging in again.")
delegate:nil
cancelButtonTitle:NSLocalizedString(@"alertOkButton", @"OK")
otherButtonTitles:nil];
[failAlert show];
}
} onFailure:^(NSURLSessionTask *task, NSError *error) {
[self hideLoading];
}];
}
-(void)getSessionPluginsList
{
[SessionService getPluginsListOnCompletion:^(NSDictionary *responseObject) {
[self hideLoading];
} onFailure:^(NSURLSessionTask *task, NSError *error) {
[self hideLoading];
}];
}
-(void)showLoading
{
self.loadingView.hidden = NO;
[self.spinner startAnimating];
}
-(void)hideLoading
{
[self.spinner stopAnimating];
self.loadingView.hidden = YES;
}
-(void)showLoginFail
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"loginError_title", @"Login Fail")
message:NSLocalizedString(@"loginError_message", @"The username and password don't match on the given server")
delegate:nil
cancelButtonTitle:NSLocalizedString(@"alertOkButton", @"OK")
otherButtonTitles:nil];
[alert show];
}
#pragma mark -- UITextField Delegate Methods
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField == self.serverTextField.textField) {
[self.userTextField becomeFirstResponder];
} else if (textField == self.userTextField) {
[self.passwordTextField becomeFirstResponder];
} else if (textField == self.passwordTextField) {
if(self.view.frame.size.height > 320)
{
[self moveTextFieldsBy:self.topConstraintAmount];
}
[self performLogin];
}
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if(self.view.frame.size.height > 500) return;
NSInteger amount = 0;
if (textField == self.userTextField)
{
amount = -self.topConstraintAmount;
}
else if (textField == self.passwordTextField)
{
amount = -self.topConstraintAmount * 2;
}
[self moveTextFieldsBy:amount];
}
@end