Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / darwin / CHIPTool / CHIPTool / View Controllers / UnpairDevices / UnpairDevicesViewController.m
1 /**
2  *
3  *    Copyright (c) 2021 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 "UnpairDevicesViewController.h"
19 #import "CHIPUIViewUtils.h"
20 #import "DefaultsUtils.h"
21 #import "DeviceSelector.h"
22 #import <CHIP/CHIP.h>
23
24 @interface UnpairDevicesViewController ()
25 @property (strong, nonatomic) UISwitch * unpairAllDevicesSwitch;
26 @property (nonatomic, strong) UILabel * unpairLabel;
27 @property (nonatomic, strong) UILabel * titleLabel;
28 @property (nonatomic, strong) UIStackView * stackView;
29 @property (nonatomic, strong) DeviceSelector * deviceSelector;
30 @property (strong, nonatomic) UIButton * unpairButton;
31 @end
32
33 @implementation UnpairDevicesViewController
34
35 // MARK: UIViewController methods
36
37 - (void)viewDidLoad
38 {
39     [super viewDidLoad];
40
41     UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
42     [self.view addGestureRecognizer:tap];
43
44     [self setupUIElements];
45 }
46
47 - (void)dismissKeyboard
48 {
49     [_deviceSelector resignFirstResponder];
50 }
51
52 // MARK: UI Setup
53
54 - (void)setupUIElements
55 {
56     self.view.backgroundColor = UIColor.whiteColor;
57
58     // Title
59     _titleLabel = [CHIPUIViewUtils addTitle:@"Unpair Devices" toView:self.view];
60     [self setupStackView];
61 }
62
63 - (void)setupStackView
64 {
65     // stack view
66     UIStackView * stackView = [UIStackView new];
67     stackView.axis = UILayoutConstraintAxisVertical;
68     stackView.distribution = UIStackViewDistributionEqualSpacing;
69     stackView.alignment = UIStackViewAlignmentLeading;
70     stackView.spacing = 30;
71     [_stackView removeFromSuperview];
72     _stackView = stackView;
73     [self.view addSubview:stackView];
74
75     stackView.translatesAutoresizingMaskIntoConstraints = false;
76     [stackView.topAnchor constraintEqualToAnchor:_titleLabel.bottomAnchor constant:30].active = YES;
77     [stackView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:30].active = YES;
78     [stackView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-30].active = YES;
79
80     // Unpair All Devices
81     UILabel * unpairAllDevices = [UILabel new];
82     unpairAllDevices.text = @"Unpair all devices";
83     _unpairAllDevicesSwitch = [UISwitch new];
84     [_unpairAllDevicesSwitch setOn:YES];
85     UIView * openPairingOnAllDevicesView = [CHIPUIViewUtils viewWithLabel:unpairAllDevices toggle:_unpairAllDevicesSwitch];
86     [_unpairAllDevicesSwitch addTarget:self action:@selector(unpairAllDevicesButton:) forControlEvents:UIControlEventTouchUpInside];
87     [stackView addArrangedSubview:openPairingOnAllDevicesView];
88     openPairingOnAllDevicesView.translatesAutoresizingMaskIntoConstraints = false;
89     [openPairingOnAllDevicesView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
90
91     // Device List and picker
92     _deviceSelector = [DeviceSelector new];
93     [_deviceSelector setEnabled:NO];
94
95     UILabel * deviceIDLabel = [UILabel new];
96     deviceIDLabel.text = @"Device ID:";
97     UIView * deviceIDView = [CHIPUIViewUtils viewWithLabel:deviceIDLabel textField:_deviceSelector];
98     [stackView addArrangedSubview:deviceIDView];
99
100     deviceIDView.translatesAutoresizingMaskIntoConstraints = false;
101     [deviceIDView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = true;
102
103     // Unpair devices button
104     _unpairButton = [UIButton new];
105     [_unpairButton setTitle:@"Unpair Devices" forState:UIControlStateNormal];
106     [_unpairButton addTarget:self action:@selector(unpairSelectedDevices:) forControlEvents:UIControlEventTouchUpInside];
107     _unpairButton.backgroundColor = UIColor.systemBlueColor;
108     _unpairButton.titleLabel.font = [UIFont systemFontOfSize:17];
109     _unpairButton.titleLabel.textColor = [UIColor whiteColor];
110     _unpairButton.layer.cornerRadius = 5;
111     _unpairButton.clipsToBounds = YES;
112     [stackView addArrangedSubview:_unpairButton];
113
114     _unpairButton.translatesAutoresizingMaskIntoConstraints = false;
115     [_unpairButton.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
116 }
117
118 // MARK: UIButton actions
119
120 - (IBAction)unpairAllDevicesButton:(id)sender
121 {
122     if ([_unpairAllDevicesSwitch isOn]) {
123         [_deviceSelector setEnabled:NO];
124     } else {
125         [_deviceSelector setEnabled:YES];
126     }
127 }
128
129 - (IBAction)unpairSelectedDevices:(id)sender
130 {
131     [_deviceSelector forSelectedDevices:^(uint64_t deviceId) {
132         CHIPUnpairDeviceWithID(deviceId);
133     }];
134
135     [_deviceSelector refreshDeviceList];
136     //[_deviceSelector setEnabled:NO];
137 }
138
139 @end