Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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/client/ios/app/client_connection_view_controller.h" | |
| 10 | |
| 11 #import "ios/third_party/material_components_ios/src/components/ActivityIndicato r/src/MDCActivityIndicator.h" | |
| 12 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate rialButtons.h" | |
| 13 #import "ios/third_party/material_components_ios/src/components/NavigationBar/sr c/MaterialNavigationBar.h" | |
| 14 #import "remoting/client/ios/app/pin_entry_view.h" | |
| 15 #import "remoting/client/ios/domain/client_session_details.h" | |
| 16 #import "remoting/client/ios/session/remoting_client.h" | |
| 17 | |
| 18 #include "base/strings/sys_string_conversions.h" | |
| 19 #include "remoting/protocol/client_authentication_config.h" | |
| 20 | |
| 21 static const CGFloat kIconRadius = 30.f; | |
| 22 static const CGFloat kActivityIndicatorStrokeWidth = 3.f; | |
| 23 static const CGFloat kActivityIndicatorRadius = 33.f; | |
| 24 | |
| 25 static const CGFloat kPinEntryViewWidth = 240.f; | |
| 26 static const CGFloat kPinEntryViewHeight = 90.f; | |
| 27 | |
| 28 static const CGFloat kCenterShift = -80.f; | |
| 29 static const CGFloat kPadding = 20.f; | |
| 30 static const CGFloat kMargin = 20.f; | |
| 31 | |
| 32 static const CGFloat kBarHeight = 58.f; | |
| 33 | |
| 34 @interface ClientConnectionViewController ()<PinEntryDelegate> { | |
| 35 UIImageView* _iconView; | |
| 36 MDCActivityIndicator* _activityIndicator; | |
| 37 UILabel* _statusLabel; | |
| 38 MDCNavigationBar* _navBar; | |
| 39 PinEntryView* _pinEntryView; | |
| 40 NSString* _remoteHostName; | |
| 41 } | |
| 42 @end | |
| 43 | |
| 44 @implementation ClientConnectionViewController | |
| 45 | |
| 46 @synthesize state = _state; | |
| 47 @synthesize delegate = _delegate; | |
| 48 | |
| 49 - (id)init { | |
| 50 self = [super init]; | |
| 51 if (self) { | |
| 52 self.navigationItem.rightBarButtonItem = | |
| 53 [[UIBarButtonItem alloc] initWithTitle:@"CANCEL" | |
|
Yuwei
2017/05/01 23:01:15
Is this a placeholder before we do i18n? Or is thi
nicholss
2017/05/02 16:39:39
it is placeholder. There will have to be a i18n sw
| |
| 54 style:UIBarButtonItemStylePlain | |
| 55 target:self | |
| 56 action:@selector(didTapCancel:)]; | |
| 57 | |
| 58 _navBar = [[MDCNavigationBar alloc] initWithFrame:CGRectZero]; | |
| 59 [_navBar observeNavigationItem:self.navigationItem]; | |
| 60 | |
| 61 [_navBar setBackgroundColor:[UIColor blackColor]]; | |
| 62 MDCNavigationBarTextColorAccessibilityMutator* mutator = | |
| 63 [[MDCNavigationBarTextColorAccessibilityMutator alloc] init]; | |
| 64 [mutator mutate:_navBar]; | |
| 65 [self.view addSubview:_navBar]; | |
| 66 _navBar.translatesAutoresizingMaskIntoConstraints = NO; | |
| 67 } | |
| 68 return self; | |
| 69 } | |
| 70 | |
| 71 #pragma mark - UIViewController | |
| 72 | |
| 73 - (void)loadView { | |
| 74 [super loadView]; | |
| 75 | |
| 76 self.view.backgroundColor = [UIColor blackColor]; | |
| 77 | |
| 78 _navBar.frame = CGRectMake(0.f, 0.f, self.view.frame.size.width, kBarHeight); | |
|
Yuwei
2017/05/01 23:01:15
Shall these be placed in layoutSubviews? What will
nicholss
2017/05/02 16:39:40
Keeping me honest. thanks.
nicholss
2017/05/02 17:12:18
An update, only a view has layoutSubviews, a view
Yuwei
2017/05/02 18:24:43
Yes. I figured it out when working with the GLKVie
| |
| 79 [_navBar setNeedsLayout]; | |
| 80 | |
| 81 _activityIndicator = [[MDCActivityIndicator alloc] initWithFrame:CGRectZero]; | |
| 82 [self.view addSubview:_activityIndicator]; | |
| 83 | |
| 84 _statusLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
| 85 [self.view addSubview:_statusLabel]; | |
| 86 | |
| 87 _iconView = [[UIImageView alloc] initWithFrame:CGRectZero]; | |
| 88 [self.view addSubview:_iconView]; | |
| 89 | |
| 90 _pinEntryView = [[PinEntryView alloc] init]; | |
| 91 [self.view addSubview:_pinEntryView]; | |
| 92 _pinEntryView.delegate = self; | |
| 93 } | |
| 94 | |
| 95 - (void)viewDidLoad { | |
| 96 [super viewDidLoad]; | |
| 97 | |
| 98 _iconView.frame = CGRectMake(0, 0, kIconRadius * 2, kIconRadius * 2.f); | |
| 99 _iconView.center = | |
|
Yuwei
2017/05/01 23:01:15
Positioning code should be in layoutSubviews?
nicholss
2017/05/02 16:39:39
Done.
| |
| 100 CGPointMake(self.view.center.x, self.view.center.y + kCenterShift); | |
| 101 _iconView.contentMode = UIViewContentModeCenter; | |
| 102 _iconView.alpha = 0.87f; | |
| 103 _iconView.backgroundColor = UIColor.lightGrayColor; | |
| 104 _iconView.layer.cornerRadius = kIconRadius; | |
| 105 _iconView.layer.masksToBounds = YES; | |
| 106 _iconView.image = [UIImage imageNamed:@"ic_desktop"]; | |
| 107 | |
| 108 _activityIndicator.radius = kActivityIndicatorRadius; | |
| 109 _activityIndicator.trackEnabled = YES; | |
| 110 _activityIndicator.strokeWidth = kActivityIndicatorStrokeWidth; | |
| 111 _activityIndicator.cycleColors = @[ [UIColor whiteColor] ]; | |
| 112 [_activityIndicator sizeToFit]; | |
| 113 _activityIndicator.center = _iconView.center; | |
| 114 | |
| 115 _statusLabel.numberOfLines = 1; | |
| 116 _statusLabel.lineBreakMode = NSLineBreakByTruncatingTail; | |
| 117 _statusLabel.textColor = [UIColor whiteColor]; | |
| 118 _statusLabel.textAlignment = NSTextAlignmentCenter; | |
| 119 | |
| 120 _statusLabel.frame = | |
| 121 CGRectMake(kMargin, _activityIndicator.center.y + kIconRadius + kPadding, | |
|
Yuwei
2017/05/01 23:01:15
ditto
nicholss
2017/05/02 16:39:39
Done.
| |
| 122 self.view.frame.size.width - kMargin * 2.f, | |
| 123 _statusLabel.font.pointSize * _statusLabel.numberOfLines); | |
| 124 | |
| 125 _pinEntryView.frame = CGRectMake( | |
| 126 (self.view.frame.size.width - kPinEntryViewWidth) / 2.f, | |
|
Yuwei
2017/05/01 23:01:15
ditto
nicholss
2017/05/02 16:39:40
Done.
| |
| 127 _statusLabel.frame.origin.y + _statusLabel.frame.size.height + kPadding, | |
| 128 kPinEntryViewWidth, kPinEntryViewHeight); | |
| 129 _pinEntryView.hidden = YES; | |
| 130 } | |
| 131 | |
| 132 - (void)viewWillAppear:(BOOL)animated { | |
| 133 [super viewWillAppear:animated]; | |
| 134 [self.navigationController setNavigationBarHidden:YES animated:animated]; | |
| 135 | |
| 136 [[NSNotificationCenter defaultCenter] | |
| 137 addObserver:self | |
| 138 selector:@selector(hostSessionStatusChanged:) | |
| 139 name:kHostSessionStatusChanged | |
| 140 object:nil]; | |
| 141 } | |
| 142 | |
| 143 - (void)viewDidAppear:(BOOL)animated { | |
| 144 [super viewDidAppear:animated]; | |
| 145 [[NSNotificationCenter defaultCenter] | |
| 146 addObserver:self | |
| 147 selector:@selector(keyboardWillShow:) | |
| 148 name:UIKeyboardWillShowNotification | |
| 149 object:nil]; | |
| 150 | |
| 151 [[NSNotificationCenter defaultCenter] | |
| 152 addObserver:self | |
| 153 selector:@selector(keyboardWillHide:) | |
| 154 name:UIKeyboardWillHideNotification | |
| 155 object:nil]; | |
| 156 | |
| 157 [_activityIndicator startAnimating]; | |
| 158 } | |
| 159 | |
| 160 - (void)viewWillDisappear:(BOOL)animated { | |
| 161 [super viewWillDisappear:animated]; | |
| 162 [_activityIndicator stopAnimating]; | |
| 163 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 164 } | |
| 165 | |
| 166 #pragma mark - Keyboard | |
| 167 | |
| 168 - (void)keyboardWillShow:(NSNotification*)notification { | |
| 169 CGSize keyboardSize = | |
| 170 [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] | |
| 171 CGRectValue] | |
| 172 .size; | |
| 173 | |
| 174 [UIView | |
| 175 animateWithDuration:0.3 | |
| 176 animations:^{ | |
| 177 CGRect f = self.view.frame; | |
| 178 CGFloat newHeight = | |
| 179 self.view.frame.size.height - keyboardSize.height; | |
| 180 CGFloat overlap = | |
| 181 newHeight - (_pinEntryView.frame.origin.y + | |
| 182 _pinEntryView.frame.size.height + kPadding); | |
| 183 if (overlap < 0) { | |
| 184 f.origin.y = overlap; | |
| 185 self.view.frame = f; | |
| 186 } | |
| 187 }]; | |
| 188 } | |
| 189 | |
| 190 - (void)keyboardWillHide:(NSNotification*)notification { | |
| 191 [UIView animateWithDuration:0.3 | |
| 192 animations:^{ | |
| 193 CGRect f = self.view.frame; | |
| 194 f.origin.y = 0.f; | |
| 195 self.view.frame = f; | |
| 196 }]; | |
| 197 } | |
| 198 | |
| 199 #pragma mark - Properties | |
| 200 | |
| 201 - (void)setState:(ClientConnectionViewState)state { | |
| 202 _state = state; | |
|
Yuwei
2017/05/01 23:01:15
You might want to return here if the delegate (_re
nicholss
2017/05/02 16:39:40
I set a default of "" for _remoteHostName.
| |
| 203 switch (_state) { | |
| 204 case ClientViewConnecting: | |
| 205 [self showConnectingState]; | |
| 206 break; | |
| 207 case ClientViewPinPrompt: | |
| 208 [self showPinPromptState]; | |
| 209 break; | |
| 210 case ClientViewConnected: | |
| 211 [self showConnectedState]; | |
| 212 break; | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 - (void)setDelegate:(id<ClientConnectionViewControllerDelegate>)delegate { | |
|
Yuwei
2017/05/01 23:01:15
Is this method not being used yet?
nicholss
2017/05/02 16:39:39
This is a magic part of obj-c, in the calling clas
Yuwei
2017/05/02 18:24:44
Acknowledged.
| |
| 217 _delegate = delegate; | |
| 218 if (_delegate) { | |
| 219 _remoteHostName = [_delegate getConnectingHostName]; | |
| 220 // To get the view to use the new remote host name. | |
| 221 [self setState:_state]; | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 #pragma mark - Private | |
| 226 | |
| 227 - (void)showConnectingState { | |
| 228 [_pinEntryView endEditing:YES]; | |
| 229 _statusLabel.text = | |
| 230 [NSString stringWithFormat:@"Connecting to %@", _remoteHostName]; | |
| 231 [_activityIndicator stopAnimating]; | |
| 232 _activityIndicator.cycleColors = @[ [UIColor whiteColor] ]; | |
| 233 _activityIndicator.indicatorMode = MDCActivityIndicatorModeIndeterminate; | |
| 234 _activityIndicator.hidden = NO; | |
| 235 _pinEntryView.hidden = YES; | |
| 236 [_activityIndicator startAnimating]; | |
| 237 } | |
| 238 | |
| 239 - (void)showPinPromptState { | |
| 240 _statusLabel.text = [NSString stringWithFormat:@"%@", _remoteHostName]; | |
| 241 [_activityIndicator stopAnimating]; | |
| 242 _activityIndicator.hidden = YES; | |
| 243 _pinEntryView.hidden = NO; | |
| 244 [_pinEntryView becomeFirstResponder]; | |
| 245 } | |
| 246 | |
| 247 - (void)showConnectedState { | |
| 248 [_pinEntryView endEditing:YES]; | |
| 249 _statusLabel.text = | |
| 250 [NSString stringWithFormat:@"Connected to %@", _remoteHostName]; | |
| 251 _activityIndicator.progress = 0.0; | |
| 252 _pinEntryView.hidden = YES; | |
| 253 _activityIndicator.hidden = NO; | |
| 254 _activityIndicator.indicatorMode = MDCActivityIndicatorModeDeterminate; | |
| 255 _activityIndicator.cycleColors = @[ [UIColor greenColor] ]; | |
| 256 [_activityIndicator startAnimating]; | |
| 257 _activityIndicator.progress = 1.0; | |
| 258 [self dismissViewControllerAnimated:YES | |
| 259 completion:^{ | |
| 260 [_delegate clientConnected]; | |
| 261 }]; | |
| 262 } | |
| 263 | |
| 264 - (void)didProvidePin:(NSString*)pin createPairing:(BOOL)createPairing { | |
| 265 // TODO(nicholss): send event. | |
|
Yuwei
2017/05/01 23:01:15
Isn't the code below already sending event?
Also
nicholss
2017/05/02 16:39:40
Done.
| |
| 266 [[NSNotificationCenter defaultCenter] | |
| 267 postNotificationName:kHostSessionPinProvided | |
| 268 object:self | |
| 269 userInfo:[NSDictionary dictionaryWithObject:pin | |
| 270 forKey:kHostSessionPin]]; | |
| 271 } | |
| 272 | |
| 273 - (void)didTapCancel:(id)sender { | |
| 274 NSLog(@"%@ was tapped.", NSStringFromClass([sender class])); | |
| 275 // TODO(nicholss): Need to cancel the pending connection. | |
| 276 [self dismissViewControllerAnimated:YES completion:nil]; | |
| 277 } | |
| 278 | |
| 279 - (void)hostSessionStatusChanged:(NSNotification*)notification { | |
| 280 ClientConnectionViewState state; | |
| 281 ClientSessionDetails* sessionDetails = | |
| 282 [[notification userInfo] objectForKey:kSessionDetails]; | |
| 283 switch (sessionDetails.state) { | |
| 284 case SessionInitializing: | |
| 285 // Same as HostConnecting in UI. Fall-though. | |
| 286 case SessionAuthenticated: | |
| 287 // Same as HostConnecting in UI. Fall-though. | |
| 288 case SessionConnecting: | |
| 289 state = ClientViewConnecting; | |
| 290 break; | |
| 291 case SessionPinPrompt: | |
| 292 state = ClientViewPinPrompt; | |
| 293 break; | |
| 294 case SessionConnected: | |
| 295 state = ClientViewConnected; | |
| 296 break; | |
| 297 case SessionFailed: | |
| 298 // TODO(nicholss): Implement. | |
| 299 case SessionClosed: | |
| 300 // TODO(nicholss): Implement. | |
| 301 default: | |
| 302 LOG(ERROR) << "Unknown State for Session, " << sessionDetails.state; | |
| 303 return; | |
| 304 } | |
| 305 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ | |
| 306 [self setState:state]; | |
| 307 }]; | |
| 308 } | |
| 309 | |
| 310 @end | |
| OLD | NEW |