-
Notifications
You must be signed in to change notification settings - Fork 3
/
DBCreateAccountController.m
437 lines (359 loc) · 14.3 KB
/
DBCreateAccountController.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//
// DBCreateAccountController.m
// DropboxSDK
//
// Created by Brian Smith on 5/20/10.
// Copyright 2010 Dropbox, Inc. All rights reserved.
//
#import "DBCreateAccountController.h"
#import "DBLoadingView.h"
#import "DBLoginController.h"
#import "DBRestClient.h"
enum {
kRowFirstName,
kRowLastName,
kRowEmail,
kRowPassword,
kRowCount
};
#define kTextFieldFrame CGRectMake(108, 11, 182, 24)
#define CLEAR_OBJ(FIELD) [FIELD release]; FIELD = nil;
@interface DBCreateAccountController () <UITextFieldDelegate, DBRestClientDelegate,
UITableViewDataSource, UITableViewDelegate>
- (void)didPressCreateAccount;
- (void)setWorking:(BOOL)working;
- (void)updateActionButton;
- (void)errorWithTitle:(NSString*)title message:(NSString*)message;
@property (nonatomic, readonly) DBRestClient* restClient;
@end
@implementation DBCreateAccountController
- (void)viewDidLoad {
[super viewDidLoad];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
self.title = @"Create Account";
UIBarButtonItem* saveItem =
[[[UIBarButtonItem alloc]
initWithTitle:@"Create" style:UIBarButtonItemStyleDone
target:self action:@selector(didPressCreateAccount)]
autorelease];
self.navigationItem.rightBarButtonItem = saveItem;
} else {
self.title = @"Create Dropbox Account";
}
UIImageView* background =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"db_background.png"]];
background.frame = self.view.bounds;
background.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:background];
[background release];
CGRect tableFrame = self.view.bounds;
tableFrame.size.width = 320;
tableFrame.origin.x = floor(self.view.bounds.size.width/2 - tableFrame.size.width/2);
tableView =
[[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];
tableView.autoresizingMask =
UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin;
tableView.backgroundColor = [UIColor clearColor];
if ([tableView respondsToSelector:@selector(setBackgroundView:)]) {
[tableView performSelector:@selector(setBackgroundView:) withObject:nil];
}
tableView.scrollEnabled = NO;
tableView.dataSource = self;
tableView.delegate = self;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIView* tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 64)];
tableView.tableHeaderView = tableHeaderView;
[tableHeaderView release];
}
[self.view addSubview:tableView];
}
- (void)releaseViews {
CLEAR_OBJ(tableView);
CLEAR_OBJ(footerView);
CLEAR_OBJ(firstNameCell);
CLEAR_OBJ(firstNameField);
CLEAR_OBJ(lastNameCell);
CLEAR_OBJ(lastNameField);
CLEAR_OBJ(emailCell);
CLEAR_OBJ(emailField);
CLEAR_OBJ(passwordCell);
CLEAR_OBJ(passwordField);
CLEAR_OBJ(footerView);
CLEAR_OBJ(activityIndicator);
CLEAR_OBJ(loadingView);
}
- (void)viewDidUnload {
[super viewDidUnload];
[self releaseViews];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self updateActionButton];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[firstNameField becomeFirstResponder];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
return interfaceOrientation == UIInterfaceOrientationPortrait;
} else {
return YES;
}
}
- (void)dealloc {
[self releaseViews];
[restClient release];
[super dealloc];
}
@synthesize loginController;
- (IBAction)didPressCreateAccount {
if ([firstNameField.text length] == 0) {
[self errorWithTitle:@"First Name Required" message:@"Please enter your first name."];
return;
} else if ([lastNameField.text length] == 0) {
[self errorWithTitle:@"Last Name Required" message:@"Please enter your last name."];
return;
} else if ([emailField.text length] == 0) {
[self errorWithTitle:@"Email Address Required" message:@"Please enter your email address."];
return;
} else if ([passwordField.text length] == 0) {
[self errorWithTitle:@"Password Required" message:@"Please enter your desired password"];
return;
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[firstNameField resignFirstResponder];
[lastNameField resignFirstResponder];
[emailField resignFirstResponder];
[passwordField resignFirstResponder];
}
[self setWorking:YES];
if (!hasCreatedAccount) {
[self.restClient createAccount:emailField.text password:passwordField.text
firstName:firstNameField.text lastName:lastNameField.text];
} else {
[self.restClient loginWithEmail:emailField.text password:passwordField.text];
}
}
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
if (textField == firstNameField) {
[lastNameField becomeFirstResponder];
} else if (textField == lastNameField) {
[emailField becomeFirstResponder];
} else if (textField == emailField) {
[passwordField becomeFirstResponder];
} else {
[self didPressCreateAccount];
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
// Update the button after the replacement has been made
[self performSelector:@selector(updateActionButton) withObject:nil afterDelay:0];
return YES;
}
#pragma mark UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return kRowCount;
}
- (UITableViewCell*)newCellWithLabel:(NSString*)label textField:(UITextField*)textField {
UITableViewCell* cell =
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = label;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
textField.frame = kTextFieldFrame;
textField.borderStyle = UITextBorderStyleNone;
textField.delegate = self;
[cell.contentView addSubview:textField];
return cell;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
switch ([indexPath row]) {
case kRowFirstName:
if (!firstNameCell) {
firstNameField = [UITextField new];
firstNameField.placeholder = @"John";
firstNameField.returnKeyType = UIReturnKeyNext;
firstNameField.autocorrectionType = UITextAutocorrectionTypeNo;
firstNameCell = [self newCellWithLabel:@"First Name" textField:firstNameField];
}
return firstNameCell;
case kRowLastName:
if (!lastNameCell) {
lastNameField = [UITextField new];
lastNameField.placeholder = @"Appleseed";
lastNameField.returnKeyType = UIReturnKeyNext;
lastNameField.autocorrectionType = UITextAutocorrectionTypeNo;
lastNameCell = [self newCellWithLabel:@"Last Name" textField:lastNameField];
}
return lastNameCell;
case kRowEmail:
if (!emailCell) {
emailField = [UITextField new];
emailField.placeholder = @"example@gmail.com";
emailField.keyboardType = UIKeyboardTypeEmailAddress;
emailField.autocapitalizationType = UITextAutocapitalizationTypeNone;
emailField.returnKeyType = UIReturnKeyNext;
emailField.autocorrectionType = UITextAutocorrectionTypeNo;
emailCell = [self newCellWithLabel:@"Email" textField:emailField];
}
return emailCell;
case kRowPassword:
if (!passwordCell) {
passwordField = [UITextField new];
passwordField.secureTextEntry = YES;
passwordField.returnKeyType = UIReturnKeyDone;
passwordField.placeholder = @"Required";
passwordCell = [self newCellWithLabel:@"Password" textField:passwordField];
}
return passwordCell;
default:
return nil;
}
}
#pragma mark UITableViewDelegate methods
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
switch ([indexPath row]) {
case kRowFirstName:
[firstNameField becomeFirstResponder];
return;
case kRowLastName:
[lastNameField becomeFirstResponder];
return;
case kRowEmail:
[emailField becomeFirstResponder];
return;
case kRowPassword:
[passwordField becomeFirstResponder];
return;
}
}
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) return 0;
return 53;
}
- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) return nil;
if (headerView == nil) {
headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"db_link_header.png"]];
headerView.contentMode = UIViewContentModeCenter;
}
return headerView;
}
- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) return 0;
return 80;
}
- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) return nil;
if (footerView == nil) {
footerView = [UIView new];
footerView.backgroundColor = [UIColor clearColor];
UIButton* createButton = [UIButton buttonWithType:UIButtonTypeCustom];
[createButton setImage:[UIImage imageNamed:@"db_create_account_button.png"]
forState:UIControlStateNormal];
[createButton setImage:[UIImage imageNamed:@"db_create_account_button_down.png"]
forState:UIControlStateHighlighted];
[createButton addTarget:self action:@selector(didPressCreateAccount)
forControlEvents:UIControlEventTouchUpInside];
[createButton sizeToFit];
CGRect buttonFrame = createButton.frame;
buttonFrame.origin.x = 320 - buttonFrame.size.width - 8;
buttonFrame.origin.y = 8;
createButton.frame = buttonFrame;
[footerView addSubview:createButton];
activityIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
CGRect activityFrame = activityIndicator.frame;
activityFrame.origin.x = 320 - activityFrame.size.width - 21;
activityFrame.origin.y = 17;
activityIndicator.frame = activityFrame;
[footerView addSubview:activityIndicator];
}
return footerView;
}
#pragma mark DBRestClientDelegate methods
- (void)restClientCreatedAccount:(DBRestClient*)client {
hasCreatedAccount = YES;
[self.restClient loginWithEmail:emailField.text password:passwordField.text];
}
- (void)restClient:(DBRestClient*)client createAccountFailedWithError:(NSError*)error {
[self setWorking:NO];
NSString* message = @"An unknown error occured.";
if ([error.domain isEqual:NSURLErrorDomain]) {
message = @"There was an error connecting to Dropbox.";
} else {
NSObject* errorResponse = [[error userInfo] objectForKey:@"error"];
if ([errorResponse isKindOfClass:[NSString class]]) {
message = (NSString*)errorResponse;
} else if ([errorResponse isKindOfClass:[NSDictionary class]]) {
NSDictionary* errorDict = (NSDictionary*)errorResponse;
message = [errorDict objectForKey:[[errorDict allKeys] objectAtIndex:0]];
}
}
[self errorWithTitle:@"Create Account Failed" message:message];
}
- (void)restClientDidLogin:(DBRestClient*)client {
[self setWorking:NO];
[self.navigationController.parentViewController dismissModalViewControllerAnimated:YES];
[loginController.delegate loginControllerDidLogin:loginController];
}
- (void)restClient:(DBRestClient*)client loginFailedWithError:(NSError*)error {
[self setWorking:NO];
// Need to make sure they don't change the email or password if create account succeeded
// but login failed
emailField.enabled = NO;
passwordField.enabled = NO;
[self errorWithTitle:@"Login Failed" message:@"Please try again."];
}
#pragma mark private methods
- (void)setWorking:(BOOL)working {
self.view.userInteractionEnabled = !working;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if (working) {
loadingView = [[DBLoadingView alloc] initWithTitle:@"Creating Account"];
[loadingView show];
} else {
[loadingView dismissAnimated:NO];
[loadingView release];
loadingView = nil;
}
[self updateActionButton];
} else {
if (working) {
[activityIndicator startAnimating];
} else {
[activityIndicator stopAnimating];
}
}
}
- (void)updateActionButton {
self.navigationItem.rightBarButtonItem.enabled =
[firstNameField.text length] > 0 &&
[lastNameField.text length] > 0 &&
[emailField.text length] > 0 &&
[passwordField.text length] > 0 &&
!loadingView;
}
- (void)errorWithTitle:(NSString*)title message:(NSString*)message {
[[[[UIAlertView alloc]
initWithTitle:title message:message delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil]
autorelease]
show];
}
- (DBRestClient*)restClient {
if (restClient == nil) {
restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
}
return restClient;
}
@end