3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #import "EchoViewController.h"
20 #import "CHIPUIViewUtils.h"
21 #import "DefaultsUtils.h"
23 @interface EchoViewController ()
25 @property (strong, nonatomic) UITextField * messageTextField;
26 @property (strong, nonatomic) UIButton * sendButton;
28 @property (nonatomic, strong) UILabel * resultLabel;
29 @property (nonatomic, strong) UIStackView * stackView;
31 @property (readwrite) CHIPBasic * cluster;
35 @implementation EchoViewController
37 // MARK: UIViewController methods
42 [self setupUIElements];
44 // listen for taps to dismiss the keyboard
45 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
46 [self.view addGestureRecognizer:tap];
48 self.cluster = [[CHIPBasic alloc] initWithDevice:CHIPGetPairedDevice() endpoint:1 queue:dispatch_get_main_queue()];
51 - (void)dismissKeyboard
53 [self.messageTextField resignFirstResponder];
58 - (void)setupUIElements
60 self.view.backgroundColor = UIColor.whiteColor;
63 UILabel * titleLabel = [CHIPUIViewUtils addTitle:@"Echo client" toView:self.view];
66 _stackView = [UIStackView new];
67 _stackView.axis = UILayoutConstraintAxisVertical;
68 _stackView.distribution = UIStackViewDistributionEqualSpacing;
69 _stackView.alignment = UIStackViewAlignmentLeading;
70 _stackView.spacing = 30;
71 [self.view addSubview:_stackView];
73 _stackView.translatesAutoresizingMaskIntoConstraints = false;
74 [_stackView.topAnchor constraintEqualToAnchor:titleLabel.bottomAnchor constant:30].active = YES;
75 [_stackView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:30].active = YES;
76 [_stackView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-30].active = YES;
79 _messageTextField = [UITextField new];
80 _messageTextField.placeholder = @"Hello from iOS!";
81 _sendButton = [UIButton new];
82 [_sendButton setTitle:@"Send" forState:UIControlStateNormal];
83 [_sendButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
84 UIView * sendMessageView = [CHIPUIViewUtils viewWithUITextField:_messageTextField button:_sendButton];
85 [_stackView addArrangedSubview:sendMessageView];
86 sendMessageView.translatesAutoresizingMaskIntoConstraints = false;
87 [sendMessageView.trailingAnchor constraintEqualToAnchor:_stackView.trailingAnchor].active = YES;
90 _resultLabel = [UILabel new];
91 _resultLabel.hidden = YES;
92 _resultLabel.font = [UIFont systemFontOfSize:17];
93 _resultLabel.textColor = UIColor.systemBlueColor;
94 _resultLabel.lineBreakMode = NSLineBreakByWordWrapping;
95 _resultLabel.numberOfLines = 0;
96 [_stackView addArrangedSubview:_resultLabel];
98 _resultLabel.translatesAutoresizingMaskIntoConstraints = false;
99 [_resultLabel.trailingAnchor constraintEqualToAnchor:_stackView.trailingAnchor].active = YES;
100 _resultLabel.adjustsFontSizeToFitWidth = YES;
103 - (void)updateResult:(NSString *)result
105 _resultLabel.hidden = NO;
106 _resultLabel.text = result;
109 // MARK: UIButton actions
111 - (IBAction)sendMessage:(id)sender
114 [self updateResult:@"Something went wrong. Cluster is not initialized."];
117 NSString * msg = [self.messageTextField text];
118 if (msg.length == 0) {
119 msg = [self.messageTextField placeholder];
122 [self updateResult:@"MfgSpecificPing command sent..."];
124 [self.cluster mfgSpecificPing:^(NSError * error, NSDictionary * values) {
125 NSString * resultString = (error == nil) ? @"MfgSpecificPing command: success!"
126 : [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code];
127 [self updateResult:resultString];