Skip to content

Commit

Permalink
Disable submit button until needed.
Browse files Browse the repository at this point in the history
No need to have the submit button enabled until there's something
to submit. While at it:

* Make the submit button actually save the new address to the database.
* Enable the button as soon as any text is typed in.
* Disable the button again on submit.
* Use the "Send" button on the keyboard and have it submit when tapped.
  • Loading branch information
theory committed Mar 13, 2010
1 parent 363112d commit e795842
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions Classes/AddressModel.h
Expand Up @@ -18,5 +18,6 @@
+ (void) reorderAddressesFrom:(NSArray *)a;
- (AddressModel *) initWithEmail:(char *)e confirmed:(int)c;
- (void) confirm;
- (void) add;

@end
10 changes: 10 additions & 0 deletions Classes/AddressModel.m
Expand Up @@ -83,6 +83,16 @@ - (void) confirm {
}
}

- (void) add {
sqlite3 *db = [BuzzalotAppDelegate getDBConnection];
sqlite3_stmt *sth;
if (sqlite3_prepare_v2(db, "INSERT INTO addresses (email, position) VALUES (?, (SELECT MAX(position) + 1 FROM addresses));", -1, &sth, nil) == SQLITE_OK ) {
sqlite3_bind_text(sth, 1, [self.email UTF8String], -1, NULL);
sqlite3_step(sth);
sqlite3_finalize(sth);
}
}

- (void)dealloc {
[email release];
[super dealloc];
Expand Down
4 changes: 3 additions & 1 deletion Classes/AddressViewController.h
Expand Up @@ -33,7 +33,9 @@
@property (nonatomic, retain) UIView *submitView;
@property (nonatomic, retain) MBProgressHUD *hud;

- (void) emailChanged:(id)sender;
- (void) fieldChanged:(id)sender;
- (void) emailEdited:(id)sender;
- (void) emailExited:(id)sender;
- (void) requestButtonTapped:(id)sender;
- (void) confirmButtonTapped:(id)sender;
- (void) setButtonToCofirm;
Expand Down
26 changes: 21 additions & 5 deletions Classes/AddressViewController.m
Expand Up @@ -23,22 +23,26 @@ - (void)viewDidLoad {
emailField.autocorrectionType = UITextAutocorrectionTypeNo;
emailField.autocapitalizationType = UITextAutocapitalizationTypeNone;
emailField.clearButtonMode = UITextFieldViewModeWhileEditing;
emailField.returnKeyType = UIReturnKeyDone;
emailField.returnKeyType = UIReturnKeySend;
emailField.enablesReturnKeyAutomatically = YES;
emailField.keyboardType = UIKeyboardTypeEmailAddress;
emailField.textColor = [UIColor configTextColor];
emailField.delegate = self;
[emailField addTarget:self action:@selector(emailChanged:) forControlEvents:UIControlEventEditingDidEnd];
[emailField addTarget:self action:@selector(emailEdited:) forControlEvents:UIControlEventEditingDidEnd];
[emailField addTarget:self action:@selector(emailExited:) forControlEvents:UIControlEventEditingDidEndOnExit];
[emailField addTarget:self action:@selector(fieldChanged:) forControlEvents:UIControlEventEditingChanged];

self.codeField = [[UITextField alloc] initWithFrame:frame];
codeField.autocorrectionType = UITextAutocorrectionTypeNo;
codeField.autocapitalizationType = UITextAutocapitalizationTypeNone;
codeField.clearButtonMode = UITextFieldViewModeWhileEditing;
codeField.returnKeyType = UIReturnKeyDone;
codeField.returnKeyType = UIReturnKeySend;
codeField.enablesReturnKeyAutomatically = YES;
codeField.keyboardType = UIKeyboardTypeNumberPad;
codeField.textColor = [UIColor configTextColor];
codeField.delegate = self;
[codeField addTarget:self action:@selector(confirmButtonTapped:) forControlEvents:UIControlEventEditingDidEndOnExit];
[codeField addTarget:self action:@selector(fieldChanged:) forControlEvents:UIControlEventEditingChanged];

if (self.address) {
emailField.text = self.address.email;
Expand All @@ -48,7 +52,7 @@ - (void)viewDidLoad {

self.submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
submitButton.frame = CGRectMake(5, 10, self.tableView.bounds.size.width-10, 44);
// submitButton.enabled = NO;
submitButton.enabled = NO;
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
self.submitView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, screenRect.size.width, 44.0)];
[submitView addSubview:submitButton];
Expand Down Expand Up @@ -76,12 +80,21 @@ - (void)dealloc {

#pragma mark -

- (void) emailChanged:(id)sender {
- (void) emailEdited:(id)sender {
if (!self.address)
self.address = [[[AddressModel alloc] init] autorelease];
self.address.email = self.emailField.text;
}

- (void) emailExited:(id)sender {
[self emailEdited:sender];
[self requestButtonTapped:sender];
}

- (void) fieldChanged:(id)sender {
submitButton.enabled = emailField.text.length > 0 ? YES : NO;
}

- (void) startHudWithMessage:(NSString *)message executing:(SEL)selector{
self.hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
Expand All @@ -93,11 +106,14 @@ - (void) startHudWithMessage:(NSString *)message executing:(SEL)selector{

- (void) requestButtonTapped:(id)sender {
[self.emailField resignFirstResponder];
submitButton.enabled = NO;
[self.address add];
[self startHudWithMessage:@"Sending…" executing:@selector(sendRequest)];
}

- (void) confirmButtonTapped:(id)sender {
[self.codeField resignFirstResponder];
submitButton.enabled = NO;
[self startHudWithMessage:@"Confirming…" executing:@selector(sendConfirm)];
}

Expand Down

0 comments on commit e795842

Please sign in to comment.