tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #import "ARDMainView.h" |
| 29 | |
| 30 | #import "UIImage+ARDUtilities.h" |
| 31 | |
| 32 | // TODO(tkchin): retrieve status bar height dynamically. |
| 33 | static CGFloat const kStatusBarHeight = 20; |
| 34 | |
| 35 | static CGFloat const kRoomTextButtonSize = 40; |
| 36 | static CGFloat const kRoomTextFieldHeight = 40; |
| 37 | static CGFloat const kRoomTextFieldMargin = 8; |
| 38 | static CGFloat const kAppLabelHeight = 20; |
| 39 | |
| 40 | @class ARDRoomTextField; |
| 41 | @protocol ARDRoomTextFieldDelegate <NSObject> |
| 42 | - (void)roomTextField:(ARDRoomTextField *)roomTextField |
| 43 | didInputRoom:(NSString *)room; |
| 44 | @end |
| 45 | |
| 46 | // Helper view that contains a text field and a clear button. |
| 47 | @interface ARDRoomTextField : UIView <UITextFieldDelegate> |
| 48 | @property(nonatomic, weak) id<ARDRoomTextFieldDelegate> delegate; |
| 49 | @end |
| 50 | |
| 51 | @implementation ARDRoomTextField { |
| 52 | UITextField *_roomText; |
| 53 | UIButton *_clearButton; |
| 54 | } |
| 55 | |
| 56 | @synthesize delegate = _delegate; |
| 57 | |
| 58 | - (instancetype)initWithFrame:(CGRect)frame { |
| 59 | if (self = [super initWithFrame:frame]) { |
| 60 | _roomText = [[UITextField alloc] initWithFrame:CGRectZero]; |
| 61 | _roomText.borderStyle = UITextBorderStyleNone; |
| 62 | _roomText.font = [UIFont fontWithName:@"Roboto" size:12]; |
| 63 | _roomText.placeholder = @"Room name"; |
| 64 | _roomText.delegate = self; |
| 65 | [_roomText addTarget:self |
| 66 | action:@selector(textFieldDidChange:) |
| 67 | forControlEvents:UIControlEventEditingChanged]; |
| 68 | [self addSubview:_roomText]; |
| 69 | |
| 70 | _clearButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 71 | UIImage *image = [UIImage imageForName:@"ic_clear_black_24dp.png" |
| 72 | color:[UIColor colorWithWhite:0 alpha:.4]]; |
| 73 | |
| 74 | [_clearButton setImage:image forState:UIControlStateNormal]; |
| 75 | [_clearButton addTarget:self |
| 76 | action:@selector(onClear:) |
| 77 | forControlEvents:UIControlEventTouchUpInside]; |
| 78 | _clearButton.hidden = YES; |
| 79 | [self addSubview:_clearButton]; |
| 80 | |
| 81 | // Give rounded corners and a light gray border. |
| 82 | self.layer.borderWidth = 1; |
| 83 | self.layer.borderColor = [[UIColor lightGrayColor] CGColor]; |
| 84 | self.layer.cornerRadius = 2; |
| 85 | } |
| 86 | return self; |
| 87 | } |
| 88 | |
| 89 | - (void)layoutSubviews { |
| 90 | CGRect bounds = self.bounds; |
| 91 | _clearButton.frame = CGRectMake(CGRectGetMaxX(bounds) - kRoomTextButtonSize, |
| 92 | CGRectGetMinY(bounds), |
| 93 | kRoomTextButtonSize, |
| 94 | kRoomTextButtonSize); |
| 95 | _roomText.frame = CGRectMake( |
| 96 | CGRectGetMinX(bounds) + kRoomTextFieldMargin, |
| 97 | CGRectGetMinY(bounds), |
| 98 | CGRectGetMinX(_clearButton.frame) - CGRectGetMinX(bounds) - |
| 99 | kRoomTextFieldMargin, |
| 100 | kRoomTextFieldHeight); |
| 101 | } |
| 102 | |
| 103 | - (CGSize)sizeThatFits:(CGSize)size { |
| 104 | size.height = kRoomTextFieldHeight; |
| 105 | return size; |
| 106 | } |
| 107 | |
| 108 | #pragma mark - UITextFieldDelegate |
| 109 | |
| 110 | - (void)textFieldDidEndEditing:(UITextField *)textField { |
| 111 | [_delegate roomTextField:self didInputRoom:textField.text]; |
| 112 | } |
| 113 | |
| 114 | - (BOOL)textFieldShouldReturn:(UITextField *)textField { |
| 115 | // There is no other control that can take focus, so manually resign focus |
| 116 | // when return (Join) is pressed to trigger |textFieldDidEndEditing|. |
| 117 | [textField resignFirstResponder]; |
| 118 | return YES; |
| 119 | } |
| 120 | |
| 121 | #pragma mark - Private |
| 122 | |
| 123 | - (void)textFieldDidChange:(id)sender { |
| 124 | [self updateClearButton]; |
| 125 | } |
| 126 | |
| 127 | - (void)onClear:(id)sender { |
| 128 | _roomText.text = @""; |
| 129 | [self updateClearButton]; |
| 130 | [_roomText resignFirstResponder]; |
| 131 | } |
| 132 | |
| 133 | - (void)updateClearButton { |
| 134 | _clearButton.hidden = _roomText.text.length == 0; |
| 135 | } |
| 136 | |
| 137 | @end |
| 138 | |
| 139 | @interface ARDMainView () <ARDRoomTextFieldDelegate> |
| 140 | @end |
| 141 | |
| 142 | @implementation ARDMainView { |
| 143 | UILabel *_appLabel; |
| 144 | ARDRoomTextField *_roomText; |
| 145 | } |
| 146 | |
| 147 | @synthesize delegate = _delegate; |
| 148 | |
| 149 | - (instancetype)initWithFrame:(CGRect)frame { |
| 150 | if (self = [super initWithFrame:frame]) { |
| 151 | _appLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 152 | _appLabel.text = @"AppRTCDemo"; |
| 153 | _appLabel.font = [UIFont fontWithName:@"Roboto" size:34]; |
| 154 | _appLabel.textColor = [UIColor colorWithWhite:0 alpha:.2]; |
| 155 | [_appLabel sizeToFit]; |
| 156 | [self addSubview:_appLabel]; |
| 157 | |
| 158 | _roomText = [[ARDRoomTextField alloc] initWithFrame:CGRectZero]; |
| 159 | _roomText.delegate = self; |
| 160 | [self addSubview:_roomText]; |
| 161 | |
| 162 | self.backgroundColor = [UIColor whiteColor]; |
| 163 | } |
| 164 | return self; |
| 165 | } |
| 166 | |
| 167 | - (void)layoutSubviews { |
| 168 | CGRect bounds = self.bounds; |
| 169 | CGFloat roomTextWidth = bounds.size.width - 2 * kRoomTextFieldMargin; |
| 170 | CGFloat roomTextHeight = [_roomText sizeThatFits:bounds.size].height; |
| 171 | _roomText.frame = CGRectMake(kRoomTextFieldMargin, |
| 172 | kStatusBarHeight + kRoomTextFieldMargin, |
| 173 | roomTextWidth, |
| 174 | roomTextHeight); |
| 175 | _appLabel.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); |
| 176 | } |
| 177 | |
| 178 | #pragma mark - ARDRoomTextFieldDelegate |
| 179 | |
| 180 | - (void)roomTextField:(ARDRoomTextField *)roomTextField |
| 181 | didInputRoom:(NSString *)room { |
| 182 | [_delegate mainView:self didInputRoom:room]; |
| 183 | } |
| 184 | |
| 185 | @end |