blob: 3c9e46e148c26e630a320dceceb4c7f91947924f [file] [log] [blame]
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * 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.orgef2a5dd2015-01-15 22:38:21 +00009 */
10
11#import "ARDMainView.h"
12
13#import "UIImage+ARDUtilities.h"
14
15// TODO(tkchin): retrieve status bar height dynamically.
16static CGFloat const kStatusBarHeight = 20;
17
18static CGFloat const kRoomTextButtonSize = 40;
19static CGFloat const kRoomTextFieldHeight = 40;
20static CGFloat const kRoomTextFieldMargin = 8;
haysc913e6452015-10-02 11:44:03 -070021static CGFloat const kCallControlMargin = 8;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000022static CGFloat const kAppLabelHeight = 20;
23
24@class ARDRoomTextField;
25@protocol ARDRoomTextFieldDelegate <NSObject>
26- (void)roomTextField:(ARDRoomTextField *)roomTextField
27 didInputRoom:(NSString *)room;
28@end
29
30// Helper view that contains a text field and a clear button.
31@interface ARDRoomTextField : UIView <UITextFieldDelegate>
32@property(nonatomic, weak) id<ARDRoomTextFieldDelegate> delegate;
haysc913e6452015-10-02 11:44:03 -070033@property(nonatomic, readonly) NSString *roomText;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000034@end
35
36@implementation ARDRoomTextField {
37 UITextField *_roomText;
38 UIButton *_clearButton;
39}
40
41@synthesize delegate = _delegate;
42
43- (instancetype)initWithFrame:(CGRect)frame {
44 if (self = [super initWithFrame:frame]) {
45 _roomText = [[UITextField alloc] initWithFrame:CGRectZero];
46 _roomText.borderStyle = UITextBorderStyleNone;
47 _roomText.font = [UIFont fontWithName:@"Roboto" size:12];
48 _roomText.placeholder = @"Room name";
49 _roomText.delegate = self;
50 [_roomText addTarget:self
51 action:@selector(textFieldDidChange:)
52 forControlEvents:UIControlEventEditingChanged];
53 [self addSubview:_roomText];
54
55 _clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
56 UIImage *image = [UIImage imageForName:@"ic_clear_black_24dp.png"
57 color:[UIColor colorWithWhite:0 alpha:.4]];
58
59 [_clearButton setImage:image forState:UIControlStateNormal];
60 [_clearButton addTarget:self
61 action:@selector(onClear:)
62 forControlEvents:UIControlEventTouchUpInside];
63 _clearButton.hidden = YES;
64 [self addSubview:_clearButton];
65
66 // Give rounded corners and a light gray border.
67 self.layer.borderWidth = 1;
68 self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
69 self.layer.cornerRadius = 2;
70 }
71 return self;
72}
73
74- (void)layoutSubviews {
75 CGRect bounds = self.bounds;
76 _clearButton.frame = CGRectMake(CGRectGetMaxX(bounds) - kRoomTextButtonSize,
77 CGRectGetMinY(bounds),
78 kRoomTextButtonSize,
79 kRoomTextButtonSize);
80 _roomText.frame = CGRectMake(
81 CGRectGetMinX(bounds) + kRoomTextFieldMargin,
82 CGRectGetMinY(bounds),
83 CGRectGetMinX(_clearButton.frame) - CGRectGetMinX(bounds) -
84 kRoomTextFieldMargin,
85 kRoomTextFieldHeight);
86}
87
88- (CGSize)sizeThatFits:(CGSize)size {
89 size.height = kRoomTextFieldHeight;
90 return size;
91}
92
haysc913e6452015-10-02 11:44:03 -070093- (NSString *)roomText {
94 return _roomText.text;
95}
96
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000097#pragma mark - UITextFieldDelegate
98
99- (void)textFieldDidEndEditing:(UITextField *)textField {
100 [_delegate roomTextField:self didInputRoom:textField.text];
101}
102
103- (BOOL)textFieldShouldReturn:(UITextField *)textField {
104 // There is no other control that can take focus, so manually resign focus
105 // when return (Join) is pressed to trigger |textFieldDidEndEditing|.
106 [textField resignFirstResponder];
107 return YES;
108}
109
110#pragma mark - Private
111
112- (void)textFieldDidChange:(id)sender {
113 [self updateClearButton];
114}
115
116- (void)onClear:(id)sender {
117 _roomText.text = @"";
118 [self updateClearButton];
119 [_roomText resignFirstResponder];
120}
121
122- (void)updateClearButton {
123 _clearButton.hidden = _roomText.text.length == 0;
124}
125
126@end
127
128@interface ARDMainView () <ARDRoomTextFieldDelegate>
129@end
130
131@implementation ARDMainView {
132 UILabel *_appLabel;
133 ARDRoomTextField *_roomText;
haysc913e6452015-10-02 11:44:03 -0700134 UILabel *_callOptionsLabel;
135 UISwitch *_audioOnlySwitch;
136 UILabel *_audioOnlyLabel;
137 UISwitch *_loopbackSwitch;
138 UILabel *_loopbackLabel;
139 UIButton *_startCallButton;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000140}
141
142@synthesize delegate = _delegate;
143
144- (instancetype)initWithFrame:(CGRect)frame {
145 if (self = [super initWithFrame:frame]) {
146 _appLabel = [[UILabel alloc] initWithFrame:CGRectZero];
147 _appLabel.text = @"AppRTCDemo";
148 _appLabel.font = [UIFont fontWithName:@"Roboto" size:34];
149 _appLabel.textColor = [UIColor colorWithWhite:0 alpha:.2];
150 [_appLabel sizeToFit];
151 [self addSubview:_appLabel];
152
153 _roomText = [[ARDRoomTextField alloc] initWithFrame:CGRectZero];
154 _roomText.delegate = self;
155 [self addSubview:_roomText];
156
haysc913e6452015-10-02 11:44:03 -0700157 UIFont *controlFont = [UIFont fontWithName:@"Roboto" size:20];
158 UIColor *controlFontColor = [UIColor colorWithWhite:0 alpha:.6];
159
160 _callOptionsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
161 _callOptionsLabel.text = @"Call Options";
162 _callOptionsLabel.font = controlFont;
163 _callOptionsLabel.textColor = controlFontColor;
164 [_callOptionsLabel sizeToFit];
165 [self addSubview:_callOptionsLabel];
166
167 _audioOnlySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
168 [_audioOnlySwitch sizeToFit];
169 [self addSubview:_audioOnlySwitch];
170
171 _audioOnlyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
172 _audioOnlyLabel.text = @"Audio only";
173 _audioOnlyLabel.font = controlFont;
174 _audioOnlyLabel.textColor = controlFontColor;
175 [_audioOnlyLabel sizeToFit];
176 [self addSubview:_audioOnlyLabel];
177
178 _loopbackSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
179 [_loopbackSwitch sizeToFit];
180 [self addSubview:_loopbackSwitch];
181
182 _loopbackLabel = [[UILabel alloc] initWithFrame:CGRectZero];
183 _loopbackLabel.text = @"Loopback mode";
184 _loopbackLabel.font = controlFont;
185 _loopbackLabel.textColor = controlFontColor;
186 [_loopbackLabel sizeToFit];
187 [self addSubview:_loopbackLabel];
188
189 _startCallButton = [[UIButton alloc] initWithFrame:CGRectZero];
190
191 _startCallButton = [UIButton buttonWithType:UIButtonTypeSystem];
192 _startCallButton.backgroundColor = [UIColor blueColor];
193 _startCallButton.layer.cornerRadius = 10;
194 _startCallButton.clipsToBounds = YES;
195 _startCallButton.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
196 [_startCallButton setTitle:@"Start call"
197 forState:UIControlStateNormal];
198 _startCallButton.titleLabel.font = controlFont;
199 [_startCallButton setTitleColor:[UIColor whiteColor]
200 forState:UIControlStateNormal];
201 [_startCallButton setTitleColor:[UIColor lightGrayColor]
202 forState:UIControlStateSelected];
203 [_startCallButton sizeToFit];
204 [_startCallButton addTarget:self
205 action:@selector(onStartCall:)
206 forControlEvents:UIControlEventTouchUpInside];
207 [self addSubview:_startCallButton];
208
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000209 self.backgroundColor = [UIColor whiteColor];
210 }
211 return self;
212}
213
214- (void)layoutSubviews {
215 CGRect bounds = self.bounds;
216 CGFloat roomTextWidth = bounds.size.width - 2 * kRoomTextFieldMargin;
217 CGFloat roomTextHeight = [_roomText sizeThatFits:bounds.size].height;
218 _roomText.frame = CGRectMake(kRoomTextFieldMargin,
219 kStatusBarHeight + kRoomTextFieldMargin,
220 roomTextWidth,
221 roomTextHeight);
222 _appLabel.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
haysc913e6452015-10-02 11:44:03 -0700223
224 CGFloat callOptionsLabelTop =
225 CGRectGetMaxY(_roomText.frame) + kCallControlMargin * 4;
226 _callOptionsLabel.frame = CGRectMake(kCallControlMargin,
227 callOptionsLabelTop,
228 _callOptionsLabel.frame.size.width,
229 _callOptionsLabel.frame.size.height);
230
231 CGFloat audioOnlyTop =
232 CGRectGetMaxY(_callOptionsLabel.frame) + kCallControlMargin * 2;
233 CGRect audioOnlyRect = CGRectMake(kCallControlMargin * 3,
234 audioOnlyTop,
235 _audioOnlySwitch.frame.size.width,
236 _audioOnlySwitch.frame.size.height);
237 _audioOnlySwitch.frame = audioOnlyRect;
238 CGFloat audioOnlyLabelCenterX = CGRectGetMaxX(audioOnlyRect) +
239 kCallControlMargin + _audioOnlyLabel.frame.size.width / 2;
240 _audioOnlyLabel.center = CGPointMake(audioOnlyLabelCenterX,
241 CGRectGetMidY(audioOnlyRect));
242
243 CGFloat loopbackModeTop =
244 CGRectGetMaxY(_audioOnlySwitch.frame) + kCallControlMargin;
245 CGRect loopbackModeRect = CGRectMake(kCallControlMargin * 3,
246 loopbackModeTop,
247 _loopbackSwitch.frame.size.width,
248 _loopbackSwitch.frame.size.height);
249 _loopbackSwitch.frame = loopbackModeRect;
250 CGFloat loopbackModeLabelCenterX = CGRectGetMaxX(loopbackModeRect) +
251 kCallControlMargin + _loopbackLabel.frame.size.width / 2;
252 _loopbackLabel.center = CGPointMake(loopbackModeLabelCenterX,
253 CGRectGetMidY(loopbackModeRect));
254
255 CGFloat startCallTop =
256 CGRectGetMaxY(loopbackModeRect) + kCallControlMargin * 3;
257 _startCallButton.frame = CGRectMake(kCallControlMargin,
258 startCallTop,
259 _startCallButton.frame.size.width,
260 _startCallButton.frame.size.height);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000261}
262
263#pragma mark - ARDRoomTextFieldDelegate
264
265- (void)roomTextField:(ARDRoomTextField *)roomTextField
266 didInputRoom:(NSString *)room {
haysc913e6452015-10-02 11:44:03 -0700267 [_delegate mainView:self
268 didInputRoom:room
269 isLoopback:NO
270 isAudioOnly:_audioOnlySwitch.isOn];
271}
272
273#pragma mark - Private
274
275- (void)onStartCall:(id)sender {
276 NSString *room = _roomText.roomText;
277 // If this is a loopback call, allow a generated room name.
278 if (!room.length && _loopbackSwitch.isOn) {
279 room = [[NSUUID UUID] UUIDString];
280 }
281 room = [room stringByReplacingOccurrencesOfString:@"-" withString:@""];
282 [_delegate mainView:self
283 didInputRoom:room
284 isLoopback:_loopbackSwitch.isOn
285 isAudioOnly:_audioOnlySwitch.isOn];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000286}
287
288@end