summaryrefslogtreecommitdiff
path: root/Juick/LoginViewController.m
blob: a803c489863ee90047ed510003686c005456c93f (plain) (blame)
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
//
//  LoginViewController.m
//  Juick
//
//  Created by Vitaly on 30.01.14.
//  Copyright (c) 2014 com.juick. All rights reserved.
//

#import "LoginViewController.h"
#import "User.h"
#import "ColorScheme.h"

@implementation LoginViewController

static NSString *CellIdentifier = @"CellIdentifier";

-(void) viewDidLoad {
    self.title = @"Sign in";
    [self.view setBackgroundColor:[ColorScheme mainBackground]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                          target:self action:@selector(cancelSignIn)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                                           target:self action:@selector(doneSignIn)];
    [self.usernameField setText:nil];
    [self.passwordField setText:nil];
}


- (void) cancelSignIn {
    CATransition* transition = [CATransition animation];
    transition.duration = 0.3;
    transition.type = kCATransitionFade;
    transition.subtype = kCATransitionFromTop;
    
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
    [self.navigationController popViewControllerAnimated:NO];
}

- (void) doneSignIn {
    [User checkIsValid:^(BOOL success) {
        if (success) {
            CATransition* transition = [CATransition animation];
            transition.duration = 0.3;
            transition.type = kCATransitionFade;
            transition.subtype = kCATransitionFromTop;
            
            [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
            [self.navigationController popToRootViewControllerAnimated:NO];
        } else {
            [User throwUnableToLogin];
        }
    }];
    
}

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *key;
    if (textField.tag == 0) {
        // username change
        key = @"com.juick.username";
    } else {
        key = @"com.juick.password";
    }
    [[PDKeychainBindings sharedKeychainBindings] setObject:[textField.text stringByReplacingCharactersInRange:range withString:string] forKey:key];
    return YES;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    JVFloatLabeledTextField *textField = [[JVFloatLabeledTextField alloc] init];
    textField.tag = indexPath.row;
    textField.delegate = self;
    if (indexPath.row == 0) {
        textField.floatingLabel.text = @"Username";
        textField.placeholder = @"Username";
        textField.text = [[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"];
    } else {
        textField.floatingLabel.text = @"Password";
        textField.placeholder = @"Password";
        textField.secureTextEntry = YES;
        textField.text = [[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"];
    }
    [cell.contentView addSubview:textField];
    return cell;
}

@end