Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / darwin / CHIPTool / CHIPTool / View Controllers / DeviceSelector.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 "DeviceSelector.h"
19
20 #import "CHIPUIViewUtils.h"
21 #import "DefaultsUtils.h"
22
23 @interface DeviceSelector ()
24 @end
25
26 @implementation DeviceSelector {
27     NSMutableArray * _deviceList;
28     UIPickerView * _devicePicker;
29     NSUInteger _selectedDeviceIndex;
30 }
31
32 - (id)init
33 {
34     if (self = [super init]) {
35         [self refreshDeviceList];
36         [self setupView];
37         [self setEnabled:YES];
38     }
39     return self;
40 }
41
42 - (void)refreshDeviceList
43 {
44     uint64_t nextDeviceID = CHIPGetNextAvailableDeviceID();
45     _deviceList = [NSMutableArray new];
46     for (uint64_t i = 0; i < nextDeviceID; i++) {
47         if (CHIPGetPairedDeviceWithID(i) != nil) {
48             [_deviceList addObject:[@(i) stringValue]];
49         }
50     }
51     _selectedDeviceIndex = 0;
52
53     // This will refresh the view with the updated device list
54     [self setEnabled:self.isEnabled];
55 }
56
57 - (void)forSelectedDevices:(DeviceAction)action
58 {
59     if ([self isEnabled]) {
60         if ([_deviceList count] > 0) {
61             uint64_t nodeId;
62             NSScanner * scanner = [NSScanner scannerWithString:[_deviceList objectAtIndex:_selectedDeviceIndex]];
63             [scanner scanUnsignedLongLong:&nodeId];
64             action(nodeId);
65         }
66     } else {
67         for (id device in _deviceList) {
68             uint64_t nodeId;
69             NSScanner * scanner = [NSScanner scannerWithString:device];
70             [scanner scanUnsignedLongLong:&nodeId];
71             action(nodeId);
72         }
73     }
74 }
75
76 - (void)setupView
77 {
78     _devicePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 0, 0)];
79     self.inputView = _devicePicker;
80     [_devicePicker setDataSource:self];
81     [_devicePicker setDelegate:self];
82     self.delegate = self;
83
84     UIToolbar * deviceSelectButtonView = [[UIToolbar alloc] init];
85     [deviceSelectButtonView sizeToFit];
86     UIBarButtonItem * deviceSelectButton = [[UIBarButtonItem alloc] initWithTitle:@"Select"
87                                                                             style:UIBarButtonItemStylePlain
88                                                                            target:self
89                                                                            action:@selector(deviceSelectClicked:)];
90     UIBarButtonItem * flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
91                                                                                target:self
92                                                                                action:nil];
93     [deviceSelectButtonView setItems:[NSArray arrayWithObjects:flexible, deviceSelectButton, nil]];
94     self.inputAccessoryView = deviceSelectButtonView;
95 }
96
97 - (void)setEnabled:(BOOL)enabled
98 {
99     [super setEnabled:enabled];
100     if (enabled == NO) {
101         self.text = [_deviceList description];
102     } else if ([_deviceList count] > 0) {
103         self.text = [NSString stringWithFormat:@"%@", [_deviceList objectAtIndex:_selectedDeviceIndex]];
104     }
105 }
106
107 // MARK: UIPickerView
108
109 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
110 {
111     if ([_deviceList count] > 0) {
112         NSLog(@"%@", [_deviceList objectAtIndex:row]);
113         self.text = [NSString stringWithFormat:@"%@", [_deviceList objectAtIndex:row]];
114     }
115 }
116
117 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
118 {
119     return [_deviceList count];
120 }
121
122 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
123 {
124     return 1;
125 }
126
127 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
128 {
129     if ([_deviceList count] > 0) {
130         return [_deviceList objectAtIndex:row];
131     } else {
132         return [NSString new];
133     }
134 }
135
136 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
137 {
138     return 200;
139 }
140
141 - (IBAction)deviceSelectClicked:(id)sender
142 {
143     if ([_deviceList count] > 0) {
144         _selectedDeviceIndex = [_deviceList indexOfObject:self.text];
145     }
146     [self resignFirstResponder];
147 }
148
149 // MARK: CHIPDeviceControllerDelegate
150 - (void)deviceControllerOnConnected
151 {
152     NSLog(@"Status: Device connected");
153 }
154
155 - (void)deviceControllerOnError:(nonnull NSError *)error
156 {
157 }
158
159 @end