summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/LoginViewController.m
blob: c0b1a0d9cc139e90e271b298b3907a05629d517c (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
//
//  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"
#import "AppDelegate.h"
#import "APIClient.h"

@interface LoginViewController()
@property (nonatomic, assign) int paddingValue;
@end

@implementation LoginViewController

- (void) awakeFromNib {
    [super awakeFromNib];
    self.title = @"Sign in";
    [self.view setBackgroundColor:[ColorScheme mainBackground]];
    [self.visualEffectView.contentView setBackgroundColor:[ColorScheme headerBackground]];
    
    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.text = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.username"];
    self.passwordField.text = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.password"];;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
}

-(void) keyboardDidShow:(NSNotification *)sender {
    CGRect keyboardRect = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.bottomConstraint.constant = keyboardRect.size.height - self.view.safeAreaInsets.bottom + self.paddingValue;
    [self.view layoutIfNeeded];
}
-(void) keyboardWillHide:(NSNotification *)sender {
    self.bottomConstraint.constant = self.paddingValue;
    [self.view layoutIfNeeded];
}


- (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 dismissViewControllerAnimated:NO completion:nil];
}

- (void) doneSignIn {
    [User checkIsValid:^(BOOL success) {
        if (success) {
            NSString *username = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.username"];
            NSString *password = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.password"];
            [[APIClient sharedClient].manager.requestSerializer setAuthorizationHeaderFieldWithUsername:username password:password];
            
            AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [app registerForRemoteNotifications];
            CATransition* transition = [CATransition animation];
            transition.duration = 0.3;
            transition.type = kCATransitionFade;
            transition.subtype = kCATransitionFromTop;
            
            [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
            [self.navigationController dismissViewControllerAnimated:NO completion:nil];
        } else {
            [User throwUnableToLogin:self];
        }
    }];
    
}
- (IBAction)passwordChanged:(id)sender {
    if ([self.passwordField.text isKindOfClass:[NSString class]]) {
        [SAMKeychain setPassword:self.passwordField.text forService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.password"];
    }
}

- (IBAction)usernameChanged:(id)sender {
    if ([self.usernameField.text isKindOfClass:[NSString class]]) {
        [SAMKeychain setPassword:self.usernameField.text forService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.username"];
    }
}
@end