OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 #import "LoginPresenter.h" | |
6 | |
7 @interface LoginPresenter() | |
8 @property UINavigationController* navigationController; | |
9 @property Node* loginState; | |
10 @end | |
11 | |
12 @implementation LoginPresenter | |
13 | |
14 - (void)viewDidLoad { | |
15 self.password.delegate = self; | |
16 [self.logoutButton setEnabled:NO]; | |
17 } | |
18 | |
19 -(id)initWithCoder:(NSCoder*)aDecoder { | |
20 self = [super initWithCoder:aDecoder]; | |
21 self.navigationController = | |
22 [[UINavigationController alloc] initWithRootViewController:self]; | |
23 return self; | |
24 } | |
25 | |
26 - (void)presentLogin:(LoginNode*)node { | |
27 [self updateLoginState:node.state]; | |
28 } | |
29 | |
30 - (void)patchLogin:(LoginPatch*)patch { | |
31 [self updateLoginState:patch.current.state]; | |
32 } | |
33 | |
34 - (void)updateLoginState:(Node*)state { | |
35 self.loginState = state; | |
36 if ([state is:LoginRequestStateNode.class]) { | |
37 | |
38 LoginRequestStateNode* loginRequestNode = | |
39 [state as:LoginRequestStateNode.class]; | |
40 HttpsRequestNode* requestNode = loginRequestNode.request; | |
41 | |
42 NSString* authToken = requestNode.authorization; | |
43 NSURL* url = [NSURL URLWithString:requestNode.url]; | |
44 NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; | |
45 [request setValue:authToken forHTTPHeaderField:@"Authorization"]; | |
46 NSURLSession* session = [NSURLSession sharedSession]; | |
47 NSURLSessionDataTask* postDataTask = | |
48 [session dataTaskWithRequest:request | |
49 completionHandler:^(NSData* data, | |
50 NSURLResponse* response, | |
51 NSError* error) { | |
52 NSString* json = | |
53 [[NSString alloc] initWithData:data | |
54 encoding:NSUTF8StringEncoding]; | |
55 requestNode.handleResponse(json); | |
56 }]; | |
57 [postDataTask resume]; | |
58 } else if ([state is:LoggedInStateNode.class]) { | |
59 LoggedInStateNode* loggedInStateNode = [state as:LoggedInStateNode.class]; | |
60 | |
61 dispatch_async(dispatch_get_main_queue(),^() { | |
62 [self.logoutButton setEnabled:YES]; | |
63 [self.loginButton setEnabled:NO]; | |
64 [self.username setEnabled:NO]; | |
65 [self.password setEnabled:NO]; | |
66 [self.user setText:loggedInStateNode.user]; | |
67 [self.response setText:@""]; | |
68 }); | |
69 } else if ([state is:LoggedOutStateNode.class]) { | |
70 dispatch_async(dispatch_get_main_queue(),^() { | |
71 LoggedOutStateNode* loggedOutNode = [state as:LoggedOutStateNode.class]; | |
72 | |
73 [self.logoutButton setEnabled:NO]; | |
74 [self.loginButton setEnabled:YES]; | |
75 [self.username setEnabled:YES]; | |
76 [self.password setEnabled:YES]; | |
77 [self.response setText:loggedOutNode.message]; | |
78 [self.user setText:@""]; | |
79 }); | |
80 } | |
81 } | |
82 | |
83 - (IBAction)handleLogin:(id)sender { | |
84 if ([self.loginState is:LoggedOutStateNode.class]) { | |
85 LoggedOutStateNode* node = [self.loginState as:LoggedOutStateNode.class]; | |
86 node.login(self.username.text, self.password.text); | |
87 | |
88 [self.username setText: @""]; | |
89 [self.password setText: @""]; | |
90 } | |
91 } | |
92 | |
93 - (BOOL)textFieldShouldReturn:(UITextField*)textField { | |
94 [self handleLogin:textField]; | |
95 return YES; | |
96 } | |
97 | |
98 - (IBAction)handleLogout:(id)sender { | |
99 if ([self.loginState is:LoggedInStateNode.class]) { | |
100 LoggedInStateNode* loggedIn = [self.loginState as:LoggedInStateNode.class]; | |
101 loggedIn.logout(); | |
102 } | |
103 } | |
104 | |
105 - (UIViewController*)viewController { | |
106 return self.navigationController; | |
107 } | |
108 | |
109 @end | |
OLD | NEW |