3 * Copyright (c) 2021 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 "UnpairDevicesViewController.h"
19 #import "CHIPUIViewUtils.h"
20 #import "DefaultsUtils.h"
21 #import "DeviceSelector.h"
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;
33 @implementation UnpairDevicesViewController
35 // MARK: UIViewController methods
41 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
42 [self.view addGestureRecognizer:tap];
44 [self setupUIElements];
47 - (void)dismissKeyboard
49 [_deviceSelector resignFirstResponder];
54 - (void)setupUIElements
56 self.view.backgroundColor = UIColor.whiteColor;
59 _titleLabel = [CHIPUIViewUtils addTitle:@"Unpair Devices" toView:self.view];
60 [self setupStackView];
63 - (void)setupStackView
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];
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;
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;
91 // Device List and picker
92 _deviceSelector = [DeviceSelector new];
93 [_deviceSelector setEnabled:NO];
95 UILabel * deviceIDLabel = [UILabel new];
96 deviceIDLabel.text = @"Device ID:";
97 UIView * deviceIDView = [CHIPUIViewUtils viewWithLabel:deviceIDLabel textField:_deviceSelector];
98 [stackView addArrangedSubview:deviceIDView];
100 deviceIDView.translatesAutoresizingMaskIntoConstraints = false;
101 [deviceIDView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = true;
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];
114 _unpairButton.translatesAutoresizingMaskIntoConstraints = false;
115 [_unpairButton.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
118 // MARK: UIButton actions
120 - (IBAction)unpairAllDevicesButton:(id)sender
122 if ([_unpairAllDevicesSwitch isOn]) {
123 [_deviceSelector setEnabled:NO];
125 [_deviceSelector setEnabled:YES];
129 - (IBAction)unpairSelectedDevices:(id)sender
131 [_deviceSelector forSelectedDevices:^(uint64_t deviceId) {
132 CHIPUnpairDeviceWithID(deviceId);
135 [_deviceSelector refreshDeviceList];
136 //[_deviceSelector setEnabled:NO];