OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
6 #error "This file requires ARC support." | |
7 #endif | |
8 | |
9 #import "remoting/ios/ui/host_list_view_controller.h" | |
10 | |
11 #import "remoting/ios/authorize.h" | |
12 #import "remoting/ios/host.h" | |
13 #import "remoting/ios/host_cell.h" | |
14 #import "remoting/ios/host_refresh.h" | |
15 #import "remoting/ios/utility.h" | |
16 #import "remoting/ios/ui/host_view_controller.h" | |
17 | |
18 @interface HostListViewController (Private) | |
19 - (void)refreshHostList; | |
20 - (void)checkUserAndRefreshHostList; | |
21 - (BOOL)isSignedIn; | |
22 - (void)signInUser; | |
23 // Callback from [Authorize createLoginController...] | |
24 - (void)viewController:(UIViewController*)viewController | |
25 finishedWithAuth:(GTMOAuth2Authentication*)authResult | |
26 error:(NSError*)error; | |
27 @end | |
28 | |
29 @implementation HostListViewController | |
30 | |
31 @synthesize userEmail = _userEmail; | |
32 @synthesize authorization = _authorization; | |
33 | |
34 // Override default setter | |
35 - (void)setAuthorization:(GTMOAuth2Authentication*)authorization { | |
36 _authorization = authorization; | |
37 if (_authorization.canAuthorize) { | |
38 _userEmail = _authorization.userEmail; | |
39 } else { | |
40 _userEmail = nil; | |
41 } | |
42 | |
43 NSString* userName = _userEmail; | |
44 | |
45 if (userName == nil) { | |
46 userName = @"Not logged in"; | |
47 } | |
48 | |
49 [_btnAccount setTitle:userName forState:UIControlStateNormal]; | |
50 | |
51 [self refreshHostList]; | |
52 } | |
53 | |
54 // Override UIViewController | |
55 // Create google+ service for google authentication and oAuth2 authorization. | |
56 - (void)viewDidLoad { | |
57 [super viewDidLoad]; | |
58 | |
59 [_tableHostList setDataSource:self]; | |
60 [_tableHostList setDelegate:self]; | |
61 | |
62 _versionInfo.title = [Utility appVersionNumberDisplayString]; | |
63 } | |
64 | |
65 // Override UIViewController | |
66 - (void)viewWillAppear:(BOOL)animated { | |
67 [super viewWillAppear:animated]; | |
68 [self.navigationController setNavigationBarHidden:NO animated:NO]; | |
69 [self setAuthorization:[Authorize getAnyExistingAuthorization]]; | |
70 } | |
71 | |
72 // Override UIViewController | |
73 // Cancel segue when host status is not online | |
74 - (BOOL)shouldPerformSegueWithIdentifier:(NSString*)identifier | |
75 sender:(id)sender { | |
76 if ([identifier isEqualToString:@"ConnectToHost"]) { | |
77 Host* host = [self hostAtIndex:[_tableHostList indexPathForCell:sender]]; | |
78 if (![host.status isEqualToString:@"ONLINE"]) { | |
79 return NO; | |
80 } | |
81 } | |
82 return YES; | |
83 } | |
84 | |
85 // Override UIViewController | |
86 // check for segues defined in the storyboard by identifier, and set a few | |
87 // properties before transitioning | |
88 - (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender { | |
89 if ([segue.identifier isEqualToString:@"ConnectToHost"]) { | |
90 // the designationViewController type is defined by the storyboard | |
91 HostViewController* hostView = | |
92 static_cast<HostViewController*>(segue.destinationViewController); | |
93 | |
94 NSString* authToken = | |
95 [_authorization.parameters valueForKey:@"access_token"]; | |
96 | |
97 if (authToken == nil) { | |
98 authToken = _authorization.authorizationTokenKey; | |
99 } | |
100 | |
101 [hostView setHostDetails:[self hostAtIndex:[_tableHostList | |
102 indexPathForCell:sender]] | |
103 userEmail:_userEmail | |
104 authorizationToken:authToken]; | |
105 } | |
106 } | |
107 | |
108 // @protocol HostRefreshDelegate, remember received host list for the table | |
109 // view to refresh from | |
110 - (void)hostListRefresh:(NSArray*)hostList | |
111 errorMessage:(NSString*)errorMessage { | |
112 if (hostList != nil) { | |
113 _hostList = hostList; | |
114 [_tableHostList reloadData]; | |
115 } | |
116 [_refreshActivityIndicator stopAnimating]; | |
117 if (errorMessage != nil) { | |
118 [Utility showAlert:@"Host Refresh Failed" message:errorMessage]; | |
119 } | |
120 } | |
121 | |
122 // @protocol UITableViewDataSource | |
123 // Only have 1 section and it contains all the hosts | |
124 - (NSInteger)tableView:(UITableView*)tableView | |
125 numberOfRowsInSection:(NSInteger)section { | |
126 return [_hostList count]; | |
127 } | |
128 | |
129 // @protocol UITableViewDataSource | |
130 // Convert a host entry to a table row | |
131 - (HostCell*)tableView:(UITableView*)tableView | |
132 cellForRowAtIndexPath:(NSIndexPath*)indexPath { | |
133 static NSString* CellIdentifier = @"HostStatusCell"; | |
134 | |
135 HostCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier | |
136 forIndexPath:indexPath]; | |
137 | |
138 Host* host = [self hostAtIndex:indexPath]; | |
139 cell.labelHostName.text = host.hostName; | |
140 cell.labelStatus.text = host.status; | |
141 | |
142 UIColor* statColor = nil; | |
143 if ([host.status isEqualToString:@"ONLINE"]) { | |
144 statColor = [[UIColor alloc] initWithRed:0 green:1 blue:0 alpha:1]; | |
145 } else { | |
146 statColor = [[UIColor alloc] initWithRed:1 green:0 blue:0 alpha:1]; | |
147 } | |
148 [cell.labelStatus setTextColor:statColor]; | |
149 | |
150 return cell; | |
151 } | |
152 | |
153 // @protocol UITableViewDataSource | |
154 // Rows are not editable via standard UI mechanisms | |
155 - (BOOL)tableView:(UITableView*)tableView | |
156 canEditRowAtIndexPath:(NSIndexPath*)indexPath { | |
157 return NO; | |
158 } | |
159 | |
160 - (IBAction)btnRefreshHostListPressed:(id)sender { | |
161 [self refreshHostList]; | |
162 } | |
163 | |
164 - (IBAction)btnAccountPressed:(id)sender { | |
165 [self signInUser]; | |
166 } | |
167 | |
168 - (void)refreshHostList { | |
169 [_refreshActivityIndicator startAnimating]; | |
170 _hostList = [[NSArray alloc] init]; | |
171 [_tableHostList reloadData]; | |
172 | |
173 // Insert a small delay so the user is well informed that something is | |
174 // happening by the animating activity indicator | |
175 [self performSelector:@selector(checkUserAndRefreshHostList) | |
176 withObject:nil | |
177 afterDelay:.5]; | |
178 } | |
179 | |
180 // Most likely you want to call refreshHostList | |
181 - (void)checkUserAndRefreshHostList { | |
182 if (![self isSignedIn]) { | |
183 [self signInUser]; | |
184 } else { | |
185 HostRefresh* hostRefresh = [[HostRefresh alloc] init]; | |
186 [hostRefresh refreshHostList:_authorization delegate:self]; | |
187 } | |
188 } | |
189 | |
190 - (BOOL)isSignedIn { | |
191 return (_userEmail != nil); | |
192 } | |
193 | |
194 // Launch the google.com authentication and authorization process. If a user is | |
195 // already signed in, begin by signing out so another account could be | |
196 // signed in. | |
197 - (void)signInUser { | |
198 [self presentViewController: | |
199 [Authorize createLoginController:self | |
200 finishedSelector:@selector(viewController: | |
201 finishedWithAuth: | |
202 error:)] | |
203 animated:YES | |
204 completion:nil]; | |
205 } | |
206 | |
207 // Callback from [Authorize createLoginController...] | |
208 // Handle completion of the authentication process, and updates the service | |
209 // with the new credentials. | |
210 - (void)viewController:(UIViewController*)viewController | |
211 finishedWithAuth:(GTMOAuth2Authentication*)authResult | |
212 error:(NSError*)error { | |
213 [viewController.presentingViewController dismissViewControllerAnimated:NO | |
214 completion:nil]; | |
215 | |
216 if (error != nil) { | |
217 [Utility showAlert:@"Authentication Error" | |
218 message:error.localizedDescription]; | |
219 [self setAuthorization:nil]; | |
220 } else { | |
221 [self setAuthorization:authResult]; | |
222 } | |
223 } | |
224 | |
225 - (Host*)hostAtIndex:(NSIndexPath*)indexPath { | |
226 return [_hostList objectAtIndex:indexPath.row]; | |
227 } | |
228 | |
229 @end | |
OLD | NEW |