Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / darwin / CHIPTool / CHIPTool / View Controllers / Wifi / WifiViewController.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 "WifiViewController.h"
19 #import "CHIPUIViewUtils.h"
20 #import "DefaultsUtils.h"
21
22 @interface WifiViewController ()
23 @property (strong, nonatomic) UITextField * networkSSID;
24 @property (strong, nonatomic) UITextField * networkPassword;
25 @property (strong, nonatomic) UIButton * saveButton;
26 @property (strong, nonatomic) UIButton * clearButton;
27 @end
28
29 @implementation WifiViewController
30
31 // MARK: UIViewController methods
32
33 - (void)viewDidLoad
34 {
35     [super viewDidLoad];
36     [self setupUI];
37
38     UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
39     [self.view addGestureRecognizer:tap];
40 }
41
42 - (void)viewWillAppear:(BOOL)animated
43 {
44     [self fillNetworkConfigWithDefaults];
45 }
46
47 - (void)dismissKeyboard
48 {
49     [self.networkSSID resignFirstResponder];
50     [self.networkPassword resignFirstResponder];
51 }
52
53 // MARK: UI helpers
54
55 - (void)setupUI
56 {
57     self.view.backgroundColor = UIColor.whiteColor;
58
59     // Title
60     UILabel * titleLabel = [CHIPUIViewUtils addTitle:@"Network Configuration" toView:self.view];
61
62     // stack view
63     UIStackView * stackView = [UIStackView new];
64     stackView.axis = UILayoutConstraintAxisVertical;
65     stackView.distribution = UIStackViewDistributionFill;
66     stackView.alignment = UIStackViewAlignmentLeading;
67     stackView.spacing = 30;
68     [self.view addSubview:stackView];
69
70     stackView.translatesAutoresizingMaskIntoConstraints = false;
71     [stackView.topAnchor constraintEqualToAnchor:titleLabel.bottomAnchor constant:30].active = YES;
72     [stackView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:30].active = YES;
73     [stackView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-30].active = YES;
74
75     // info label
76     UILabel * informationLabel = [UILabel new];
77     informationLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightThin];
78     informationLabel.text = @"Please provide the home network your phone is connected to.";
79     informationLabel.numberOfLines = 0;
80     [stackView addArrangedSubview:informationLabel];
81     informationLabel.translatesAutoresizingMaskIntoConstraints = false;
82
83     // wifi entry
84     UILabel * ssidLabel = [UILabel new];
85     ssidLabel.text = @"SSID";
86     _networkSSID = [UITextField new];
87     UIView * ssidView = [CHIPUIViewUtils viewWithLabel:ssidLabel textField:_networkSSID];
88     [stackView addArrangedSubview:ssidView];
89     ssidView.translatesAutoresizingMaskIntoConstraints = false;
90     [ssidView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
91
92     // password entry
93     UILabel * passwordLabel = [UILabel new];
94     passwordLabel.text = @"Password";
95     _networkPassword = [UITextField new];
96     [_networkPassword setSecureTextEntry:YES];
97     UIView * passwordView = [CHIPUIViewUtils viewWithLabel:passwordLabel textField:_networkPassword];
98     [stackView addArrangedSubview:passwordView];
99     [passwordView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
100
101     // Button stack view
102     _clearButton = [UIButton new];
103     [_clearButton setTitle:@"Clear" forState:UIControlStateNormal];
104     _saveButton = [UIButton new];
105     [_saveButton setTitle:@"Save" forState:UIControlStateNormal];
106     [_clearButton addTarget:self action:@selector(clearCredientials:) forControlEvents:UIControlEventTouchUpInside];
107     [_saveButton addTarget:self action:@selector(saveCredientials:) forControlEvents:UIControlEventTouchUpInside];
108
109     UIStackView * stackViewButtons = [CHIPUIViewUtils stackViewWithButtons:@[ _clearButton, _saveButton ]];
110     [stackView addArrangedSubview:stackViewButtons];
111     [stackViewButtons.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
112 }
113
114 - (void)fillNetworkConfigWithDefaults
115 {
116     NSString * networkSSID = CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey);
117     if ([networkSSID length] > 0) {
118         self.networkSSID.text = networkSSID;
119     }
120
121     NSString * networkPassword = CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkPasswordDefaultsKey);
122     if ([networkPassword length] > 0) {
123         self.networkPassword.text = networkPassword;
124     }
125 }
126
127 // MARK: Button methods
128
129 - (IBAction)saveCredientials:(id)sender
130 {
131     if ([self.networkSSID.text length] > 0) {
132         CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey, self.networkSSID.text);
133     }
134
135     if ([self.networkPassword.text length] > 0) {
136         CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkPasswordDefaultsKey, self.networkPassword.text);
137     }
138
139     [self.navigationController popViewControllerAnimated:YES];
140 }
141
142 - (IBAction)clearCredientials:(id)sender
143 {
144     if ([self.networkSSID.text length] > 0) {
145         CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey, nil);
146     }
147
148     if ([self.networkPassword.text length] > 0) {
149         CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkPasswordDefaultsKey, nil);
150     }
151
152     self.networkSSID.text = @"";
153     self.networkPassword.text = @"";
154 }
155 @end