JLChen
2021-11-29 06c09ecbdf83cc5cc33971ffb75ba81e85b6eb33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
//  HDLSiriSceneListViewController.m
//  HdlSmartIoT
//
//  Created by 陈嘉乐 on 2021/11/17.
//
 
#import "HDLSiriSceneListViewController.h"
#import <Intents/Intents.h>
#import <IntentsUI/IntentsUI.h>
#import "HDLRunSceneIntent.h"   //上方“设置Custom Intents”图中右边箭头指的“Class Name”
#import "HDLSiriSceneListCell.h"
#import "HDLSectionHeaderView.h"
#import "HDLSceneSiri.h"
#import "TopBarView.h"
#import "HDLSiriShortcutModel.h"
 
 
NSString * const HDLSiriSceneListCellIdentifier  = @"HDLSiriSceneListCellIdentifier";
 
@interface HDLSiriSceneListViewController ()<INUIAddVoiceShortcutButtonDelegate,INUIAddVoiceShortcutViewControllerDelegate,INUIEditVoiceShortcutViewControllerDelegate>
 
/// TopBarView
@property (nonatomic, strong)  TopBarView *topBarView;
 
@property (copy,nonatomic) NSArray *siriShortcutList;
 
@end
 
@implementation HDLSiriSceneListViewController
 
- (instancetype)init {
    if (self = [super init]) {
        _tableViewStyle = UITableViewStylePlain;
    }
    return self;
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.view.backgroundColor = HEXCOLORA(0xF2F3F7,1.0);
    [self initView];
    // Do any additional setup after loading the view.
}
 
-(void)initLlanguage{
    NSString *languageName = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];
    
    // 简体中文
    if ([languageName rangeOfString:@"zh-Hans"].location != NSNotFound) {
        //标题
        if(self.titleName == nil || [self.titleName isEqual:@""]){
            self.titleName = @"Siri快捷指令";
        }
        
    }else{
        //标题
        if(self.titleName == nil || [self.titleName isEqual:@""]){
            self.titleName = @"Siri Shortcuts";
        }
        
    }
}
 
/// initView
- (void)initView{
    [self initLlanguage];
    [self setTopBarViewWithTitle:self.titleName];
    [self initTableView];
    [self refreshSiri];
}
 
#pragma mark - TopBarView
- (TopBarView *)topBarView{
    if (!_topBarView) {
        _topBarView = [[TopBarView alloc] initWithFrame: CGRectMake(0, 0, APP_SCREEN_WIDTH, 40)];
        [_topBarView.backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _topBarView;
}
//设置标题和返回按钮
- (void)setTopBarViewWithTitle:(NSString *)title{
    [self.view addSubview:self.topBarView];
    self.topBarView.backgroundColor = HEXCOLORA(0xF7F7F7, 1.0);
    self.topBarView.titleLabel.text = title;
}
//返回
- (void)goBack{
    if ([self.navigationController.viewControllers indexOfObject:self] == 0) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
 
#pragma NSMutableArray
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
 
#pragma mark - UITableView
- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, APP_SCREEN_WIDTH, 0) style:self.tableViewStyle];
//        _tableView.backgroundColor = COLOR_MAIN_BACKGROUND;
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = NO;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}
- (void)initTableView{
    self.tableView.frame = CGRectMake(0, APP_TOP_BAR_HEIGHT , APP_SCREEN_WIDTH, APP_VISIBLE_HEIGHT - APP_TAB_BOTTOM_HEIGHT);
//    self.tableView.backgroundColor = HEXCOLORA(0xF7F7FC,1.0);
    [self.view addSubview:self.tableView];
    //    [self.tableView reloadData];
    
}
#pragma mark - 让tableView的每个section的headerview随tableview一起滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 65.0f;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}
 
#pragma mark - UITableViewDataSourceXW
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 65.0f;
}
 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HDLSectionHeaderView *view = [[HDLSectionHeaderView alloc]init];
    HDLSiriShortcutModel *model = self.dataSource[section];
    if(model){
        view.titleLabel.text = model.title;
        view.messageLabel.text = model.content;
    }
    return view;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8.0f;
}
 
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    HDLSiriShortcutModel *model = self.dataSource[section];
    if(model && model.list){
        return model.list.count;
    }
    return 0;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HDLSiriSceneListCell *cell = [tableView dequeueReusableCellWithIdentifier:HDLSiriSceneListCellIdentifier];
    if (cell == nil) {
        cell = [[HDLSiriSceneListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HDLSiriSceneListCellIdentifier];
    }
    cell.homeId = self.homeId;
    HDLSiriControlModel *model = [self getModelWithNSIndexPath:indexPath];
    if(model){
        if (@available(iOS 12.0, *)) {
            HDLRunSceneIntent *intent = [self getSceneIntent: model.controlId];
            [cell initModel:model intent:intent];
        }
    }
    return cell;
}
 
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (@available(iOS 12.0, *)) {
        HDLSiriControlModel *model = [self getModelWithNSIndexPath:indexPath];
        if(model){
            INVoiceShortcut *voiceShortcut = [self getSceneINVoiceShortcut:model.controlId];
            [self addOrEditVoiceShortcut:voiceShortcut model:model];
        }
        
    }
    
}
 
/// 根据NSIndexPath 获取model
/// @param indexPath indexPath
-(HDLSiriControlModel *)getModelWithNSIndexPath:(NSIndexPath *)indexPath {
    HDLSiriControlModel *model = nil;
    HDLSiriShortcutModel *ssModel = self.dataSource[indexPath.section];
    if(ssModel && ssModel.list && (ssModel.list.count > indexPath.row)){
        model = ssModel.list[indexPath.row];
    }
    return model;
}
 
/// 添加或者编辑快捷指令
/// @param voiceShortcut 快捷指令
/// @param model 场景
-(void)addOrEditVoiceShortcut:(INVoiceShortcut *)voiceShortcut model:(HDLSiriControlModel*)model API_AVAILABLE(ios(12.0)){
 
    if(voiceShortcut==nil){
        //1.如果不存在则添加
        INUIAddVoiceShortcutViewController *vc = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:[self getINShortcut:model]];
        vc.delegate = self;
        if(@available(iOS 13.0, *)) {
            vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
        }
        [self presentViewController:vc animated:YES completion:nil];
        
    }else{
        //2.存在则编辑
        INUIEditVoiceShortcutViewController* vc = [[INUIEditVoiceShortcutViewController alloc] initWithVoiceShortcut:voiceShortcut];
        vc.delegate = self;
        if(@available(iOS 13.0, *)) {
            vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
        }
        [self presentViewController:vc animated:YES completion:nil];
        
    }
}
 
 
- (INShortcut *) getINShortcut:(HDLSiriControlModel*)sceneModel  API_AVAILABLE(ios(12.0)){
    NSString *title = sceneModel.controlName;
    HDLRunSceneIntent *intent = [[HDLRunSceneIntent alloc] init];
    intent.suggestedInvocationPhrase = title;   //在Siri语音设置时显示的建议设置唤起文字
    intent.controlId = sceneModel.controlId;
    intent.controlName = title;
    intent.homeId = self.homeId;
    intent.controlType = sceneModel.controlType;
    intent.controlJSONStr = sceneModel.controlJSONStr;
    intent.actionName = sceneModel.actionName;
    INShortcut *shortCut = [[INShortcut alloc] initWithIntent:intent];
    return shortCut;
}
 
 
/// 根据场景ID或者之前配置过低快捷指令
/// @param controlId 唯一标识ID
- (HDLRunSceneIntent*)getSceneIntent:(NSString *)controlId API_AVAILABLE(ios(12.0)){
    HDLRunSceneIntent *intentFind = nil;
    for (INVoiceShortcut *voiceShortcut in self.siriShortcutList) {
        HDLRunSceneIntent *intent = (HDLRunSceneIntent *)voiceShortcut.shortcut.intent;
        if(intent){
            if([intent.controlId isEqualToString:controlId]){
                intentFind = intent;
                break;
            }
        }
    }
    return intentFind;
}
 
/// 根据场景ID或者之前配置过低快捷指令
/// @param controlId 唯一标识ID
- (INVoiceShortcut*)getSceneINVoiceShortcut:(NSString *)controlId API_AVAILABLE(ios(12.0)){
    INVoiceShortcut *voiceShortcutFind = nil;
    for (INVoiceShortcut *voiceShortcut in self.siriShortcutList) {
        HDLRunSceneIntent *intent = (HDLRunSceneIntent *)voiceShortcut.shortcut.intent;
        if(intent){
            if([intent.controlId isEqualToString:controlId]){
                voiceShortcutFind = voiceShortcut;
                break;
            }
        }
    }
    return voiceShortcutFind;
}
 
#pragma mark - refreshSiri
- (BOOL)checkCurrentClass:(INVoiceShortcut*)voiceShortcut API_AVAILABLE(ios(12.0)){
    return [voiceShortcut.shortcut.intent isKindOfClass:[HDLRunSceneIntent class]];
}
/// refreshSiri
- (void)refreshSiri {
    if (@available(iOS 12.0, *)) {
        NSMutableArray *temp = [NSMutableArray array];
        [[INVoiceShortcutCenter sharedCenter] getAllVoiceShortcutsWithCompletion:^(NSArray<INVoiceShortcut *> * _Nullable voiceShortcuts, NSError * _Nullable error) {
            //该方法不是在主线程运行的,所以如果ok了需要回到主线程来
            dispatch_async(dispatch_get_main_queue(), ^{
                for (INVoiceShortcut *voiceShortcut in voiceShortcuts) {
                    if ([self checkCurrentClass:voiceShortcut]) {
                        [temp addObject:voiceShortcut];
                    }
                }
                
                self.siriShortcutList = [NSArray arrayWithArray:temp];
                [self.tableView reloadData];
                
            });
        }];
    }
    [self.tableView reloadData];
}
 
#pragma mark - INUIAddVoiceShortcutButtonDelegate
- (void)presentAddVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)addVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
    addVoiceShortcutViewController.delegate = self;
    if(@available(iOS 13.0, *)) {
        addVoiceShortcutViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    [self presentViewController:addVoiceShortcutViewController animated:YES completion:nil];
}
 
- (void)presentEditVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)editVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
    editVoiceShortcutViewController.delegate = self;
    if(@available(iOS 13.0, *)) {
        editVoiceShortcutViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    [self presentViewController:editVoiceShortcutViewController animated:YES completion:nil];
}
 
#pragma mark - INUIAddVoiceShortcutViewControllerDelegate
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error
API_AVAILABLE(ios(12.0)){
    [self refreshSiri];
    [controller dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [self refreshSiri];
    [controller dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - INUIEditVoiceShortcutViewControllerDelegate
- (void)editVoiceShortcutViewControllerDidCancel:(INUIEditVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [self refreshSiri];
    [controller dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didUpdateVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error API_AVAILABLE(ios(12.0)){
    [self refreshSiri];
    [controller dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didDeleteVoiceShortcutWithIdentifier:(NSUUID *)deletedVoiceShortcutIdentifier API_AVAILABLE(ios(12.0)){
    [self refreshSiri];
    [controller dismissViewControllerAnimated:YES completion:nil];
}
 
 
 
@end