Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / darwin / CHIPTool / CHIPTool / View Controllers / Echo client / EchoViewController.m
1 /**
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #import "EchoViewController.h"
19
20 #import "CHIPUIViewUtils.h"
21 #import "DefaultsUtils.h"
22
23 @interface EchoViewController ()
24
25 @property (strong, nonatomic) UITextField * messageTextField;
26 @property (strong, nonatomic) UIButton * sendButton;
27
28 @property (nonatomic, strong) UILabel * resultLabel;
29 @property (nonatomic, strong) UIStackView * stackView;
30
31 @property (readwrite) CHIPBasic * cluster;
32
33 @end
34
35 @implementation EchoViewController
36
37 // MARK: UIViewController methods
38
39 - (void)viewDidLoad
40 {
41     [super viewDidLoad];
42     [self setupUIElements];
43
44     // listen for taps to dismiss the keyboard
45     UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
46     [self.view addGestureRecognizer:tap];
47
48     self.cluster = [[CHIPBasic alloc] initWithDevice:CHIPGetPairedDevice() endpoint:1 queue:dispatch_get_main_queue()];
49 }
50
51 - (void)dismissKeyboard
52 {
53     [self.messageTextField resignFirstResponder];
54 }
55
56 // MARK: UI Setup
57
58 - (void)setupUIElements
59 {
60     self.view.backgroundColor = UIColor.whiteColor;
61
62     // Title
63     UILabel * titleLabel = [CHIPUIViewUtils addTitle:@"Echo client" toView:self.view];
64
65     // stack 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];
72
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;
77
78     // Send message
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;
88
89     // Result message
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];
97
98     _resultLabel.translatesAutoresizingMaskIntoConstraints = false;
99     [_resultLabel.trailingAnchor constraintEqualToAnchor:_stackView.trailingAnchor].active = YES;
100     _resultLabel.adjustsFontSizeToFitWidth = YES;
101 }
102
103 - (void)updateResult:(NSString *)result
104 {
105     _resultLabel.hidden = NO;
106     _resultLabel.text = result;
107 }
108
109 // MARK: UIButton actions
110
111 - (IBAction)sendMessage:(id)sender
112 {
113     if (!self.cluster) {
114         [self updateResult:@"Something went wrong. Cluster is not initialized."];
115     }
116
117     NSString * msg = [self.messageTextField text];
118     if (msg.length == 0) {
119         msg = [self.messageTextField placeholder];
120     }
121
122     [self updateResult:@"MfgSpecificPing command sent..."];
123
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];
128     }];
129 }
130
131 @end