blob: a0e9b9768e8953a494c5ed83f5d314294eabc770 (
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"
NSString * const UserChangedNotificationName = @"UserSignedIn";
@interface LoginViewController()
@property (nonatomic, assign) int paddingValue;
-(void) refreshState;
@end
@implementation LoginViewController
- (void) awakeFromNib {
[super awakeFromNib];
[self.view setBackgroundColor:[UIColor colorNamed:@"Background"]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:@selector(cancelSignIn)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self action:@selector(doneSignIn)];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[self refreshState];
}
-(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 {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void) doneSignIn {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[AppDelegate shared].api authenticateWithUser:self.usernameField.text password:self.passwordField.text callback:^(BOOL success){
if (success) {
[[NSNotificationCenter defaultCenter] postNotificationName:UserChangedNotificationName object:nil];
[[AppDelegate shared] registerForRemoteNotifications];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
}];
}
- (void)refreshState {
if ([AppDelegate shared].api.currentUser) {
self.title = @"Profile";
[self.usernameField setHidden:YES];
[self.passwordField setHidden:YES];
[self.currentUser setHidden:NO];
self.currentUser.text = [AppDelegate shared].api.currentUser.uname;
__weak UIImageView *weakAttach = self.imageView;
[[AppDelegate shared].api fetchImageWithURL:[NSURL URLWithString:[AppDelegate shared].api.currentUser.avatar] callback:^(NSData *data) {
[UIView transitionWithView:weakAttach
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
weakAttach.image = [UIImage imageWithData:data];
}
completion:nil];
}];
[self.signOutButton setHidden:NO];
} else {
self.title = @"Sign in";
self.imageView.image = [UIImage imageNamed:@"Splash"];
[self.usernameField setHidden:NO];
[self.passwordField setHidden:NO];
[self.currentUser setHidden:YES];
[self.signOutButton setHidden:YES];
}
}
- (IBAction)signoutPressed:(id)sender {
[[AppDelegate shared].api signout];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self.navigationController dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:UserChangedNotificationName object:nil];
}];
}
@end
|