tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #import "ARDMainView.h" |
| 12 | |
| 13 | #import "UIImage+ARDUtilities.h" |
| 14 | |
| 15 | // TODO(tkchin): retrieve status bar height dynamically. |
| 16 | static CGFloat const kStatusBarHeight = 20; |
| 17 | |
| 18 | static CGFloat const kRoomTextButtonSize = 40; |
| 19 | static CGFloat const kRoomTextFieldHeight = 40; |
| 20 | static CGFloat const kRoomTextFieldMargin = 8; |
| 21 | static CGFloat const kAppLabelHeight = 20; |
| 22 | |
| 23 | @class ARDRoomTextField; |
| 24 | @protocol ARDRoomTextFieldDelegate <NSObject> |
| 25 | - (void)roomTextField:(ARDRoomTextField *)roomTextField |
| 26 | didInputRoom:(NSString *)room; |
| 27 | @end |
| 28 | |
| 29 | // Helper view that contains a text field and a clear button. |
| 30 | @interface ARDRoomTextField : UIView <UITextFieldDelegate> |
| 31 | @property(nonatomic, weak) id<ARDRoomTextFieldDelegate> delegate; |
| 32 | @end |
| 33 | |
| 34 | @implementation ARDRoomTextField { |
| 35 | UITextField *_roomText; |
| 36 | UIButton *_clearButton; |
| 37 | } |
| 38 | |
| 39 | @synthesize delegate = _delegate; |
| 40 | |
| 41 | - (instancetype)initWithFrame:(CGRect)frame { |
| 42 | if (self = [super initWithFrame:frame]) { |
| 43 | _roomText = [[UITextField alloc] initWithFrame:CGRectZero]; |
| 44 | _roomText.borderStyle = UITextBorderStyleNone; |
| 45 | _roomText.font = [UIFont fontWithName:@"Roboto" size:12]; |
| 46 | _roomText.placeholder = @"Room name"; |
| 47 | _roomText.delegate = self; |
| 48 | [_roomText addTarget:self |
| 49 | action:@selector(textFieldDidChange:) |
| 50 | forControlEvents:UIControlEventEditingChanged]; |
| 51 | [self addSubview:_roomText]; |
| 52 | |
| 53 | _clearButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 54 | UIImage *image = [UIImage imageForName:@"ic_clear_black_24dp.png" |
| 55 | color:[UIColor colorWithWhite:0 alpha:.4]]; |
| 56 | |
| 57 | [_clearButton setImage:image forState:UIControlStateNormal]; |
| 58 | [_clearButton addTarget:self |
| 59 | action:@selector(onClear:) |
| 60 | forControlEvents:UIControlEventTouchUpInside]; |
| 61 | _clearButton.hidden = YES; |
| 62 | [self addSubview:_clearButton]; |
| 63 | |
| 64 | // Give rounded corners and a light gray border. |
| 65 | self.layer.borderWidth = 1; |
| 66 | self.layer.borderColor = [[UIColor lightGrayColor] CGColor]; |
| 67 | self.layer.cornerRadius = 2; |
| 68 | } |
| 69 | return self; |
| 70 | } |
| 71 | |
| 72 | - (void)layoutSubviews { |
| 73 | CGRect bounds = self.bounds; |
| 74 | _clearButton.frame = CGRectMake(CGRectGetMaxX(bounds) - kRoomTextButtonSize, |
| 75 | CGRectGetMinY(bounds), |
| 76 | kRoomTextButtonSize, |
| 77 | kRoomTextButtonSize); |
| 78 | _roomText.frame = CGRectMake( |
| 79 | CGRectGetMinX(bounds) + kRoomTextFieldMargin, |
| 80 | CGRectGetMinY(bounds), |
| 81 | CGRectGetMinX(_clearButton.frame) - CGRectGetMinX(bounds) - |
| 82 | kRoomTextFieldMargin, |
| 83 | kRoomTextFieldHeight); |
| 84 | } |
| 85 | |
| 86 | - (CGSize)sizeThatFits:(CGSize)size { |
| 87 | size.height = kRoomTextFieldHeight; |
| 88 | return size; |
| 89 | } |
| 90 | |
| 91 | #pragma mark - UITextFieldDelegate |
| 92 | |
| 93 | - (void)textFieldDidEndEditing:(UITextField *)textField { |
| 94 | [_delegate roomTextField:self didInputRoom:textField.text]; |
| 95 | } |
| 96 | |
| 97 | - (BOOL)textFieldShouldReturn:(UITextField *)textField { |
| 98 | // There is no other control that can take focus, so manually resign focus |
| 99 | // when return (Join) is pressed to trigger |textFieldDidEndEditing|. |
| 100 | [textField resignFirstResponder]; |
| 101 | return YES; |
| 102 | } |
| 103 | |
| 104 | #pragma mark - Private |
| 105 | |
| 106 | - (void)textFieldDidChange:(id)sender { |
| 107 | [self updateClearButton]; |
| 108 | } |
| 109 | |
| 110 | - (void)onClear:(id)sender { |
| 111 | _roomText.text = @""; |
| 112 | [self updateClearButton]; |
| 113 | [_roomText resignFirstResponder]; |
| 114 | } |
| 115 | |
| 116 | - (void)updateClearButton { |
| 117 | _clearButton.hidden = _roomText.text.length == 0; |
| 118 | } |
| 119 | |
| 120 | @end |
| 121 | |
| 122 | @interface ARDMainView () <ARDRoomTextFieldDelegate> |
| 123 | @end |
| 124 | |
| 125 | @implementation ARDMainView { |
| 126 | UILabel *_appLabel; |
| 127 | ARDRoomTextField *_roomText; |
| 128 | } |
| 129 | |
| 130 | @synthesize delegate = _delegate; |
| 131 | |
| 132 | - (instancetype)initWithFrame:(CGRect)frame { |
| 133 | if (self = [super initWithFrame:frame]) { |
| 134 | _appLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 135 | _appLabel.text = @"AppRTCDemo"; |
| 136 | _appLabel.font = [UIFont fontWithName:@"Roboto" size:34]; |
| 137 | _appLabel.textColor = [UIColor colorWithWhite:0 alpha:.2]; |
| 138 | [_appLabel sizeToFit]; |
| 139 | [self addSubview:_appLabel]; |
| 140 | |
| 141 | _roomText = [[ARDRoomTextField alloc] initWithFrame:CGRectZero]; |
| 142 | _roomText.delegate = self; |
| 143 | [self addSubview:_roomText]; |
| 144 | |
| 145 | self.backgroundColor = [UIColor whiteColor]; |
| 146 | } |
| 147 | return self; |
| 148 | } |
| 149 | |
| 150 | - (void)layoutSubviews { |
| 151 | CGRect bounds = self.bounds; |
| 152 | CGFloat roomTextWidth = bounds.size.width - 2 * kRoomTextFieldMargin; |
| 153 | CGFloat roomTextHeight = [_roomText sizeThatFits:bounds.size].height; |
| 154 | _roomText.frame = CGRectMake(kRoomTextFieldMargin, |
| 155 | kStatusBarHeight + kRoomTextFieldMargin, |
| 156 | roomTextWidth, |
| 157 | roomTextHeight); |
| 158 | _appLabel.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); |
| 159 | } |
| 160 | |
| 161 | #pragma mark - ARDRoomTextFieldDelegate |
| 162 | |
| 163 | - (void)roomTextField:(ARDRoomTextField *)roomTextField |
| 164 | didInputRoom:(NSString *)room { |
| 165 | [_delegate mainView:self didInputRoom:room]; |
| 166 | } |
| 167 | |
| 168 | @end |