重庆迈尖科技有限公司
2021-08-11 6d4592fd614cdf3301d5d26399a923c0ebc81b5e
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//
//  SearchController.m
//  BuWanVideo2.0
//
//  Created by Aeline on 2021/5/30.
//  Copyright © 2021 com.yeshi.buwansheque.ios. All rights reserved.
//
#import "AppDelegate.h"
#import "SearchController.h"
#import "searchDetailViewController.h"
 
#import "SearchNavView.h"
#import "SearchCell.h"
#import "SearchHotCell.h"
#import "SearchRecordCell.h"
#import "SearchHotSearchCell.h"
#import "SearchADCell.h"
#import "SearchRecordHeaderView.h"
 
#import "UICollectionViewLeftAlignedLayout.h"
#import "GDTNativeExpressAd.h"
#import "GDTNativeExpressAdView.h"
 
@interface SearchController () <UICollectionViewDelegate, UICollectionViewDataSource, SearchDelegate, UITableViewDelegate, UITableViewDataSource,GDTNativeExpressAdDelegete>
 
@property (nonatomic, strong) SearchNavView *viewSearchNav;
@property (nonatomic, nullable, strong) UICollectionView *collectionView;
@property (nonatomic, nullable, strong) UITableView *tableView;//模糊搜索弹出的表格
 
@property (nonatomic, nullable, strong) NSMutableArray *arrayHot;
@property (nonatomic, nullable, strong) NSMutableArray *arrayRecord;
@property (nonatomic, nullable, strong) NSDictionary *dictionaryHotSerch;
 
@property (nonatomic, nullable, strong) NSMutableArray *arraySearch;
 
@property (nonatomic, strong) GDTNativeExpressAd *nativeExpressAd;
@end
 
@implementation SearchController
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    self.navigationController.navigationBar.translucent = NO;
}
 
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
 
- (void)dealloc
{
    
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupDataConfig];
    [self setupViewConfig];
    [self loadData];
}
 
- (void)setupDataConfig {
    self.plist.dataName = searchRecord;
    [self.arrayRecord addObjectsFromArray:[self.plist readFile][0]];
}
 
- (void)loadData {
    [SVProgressHUD show];
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
    // 创建全局并行
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        
        [[YTHNetInterface startInterface] getHotSerachWithUid:[YTHsharedManger startManger].Uid withSystem:@"1" withblock:^(BOOL isSuccessful, id result, NSString *error) {
            if (isSuccessful) {
                NSDictionary *dic = (NSDictionary *)result;
                NSArray *ar = [[dic objectForKey:@"Data"] objectForKey:@"data"];
                for (int i =0; i < ar.count; i++) {
                    [self.arrayHot addObject:ar[i]];
                }
                
            } else {
                if ([error compare:@"似乎已断开与互联网的连接。"] == 0) {
                    [self autoDisappearAlertTime:1 msg:@"网络不可用,请检查网络"];
                }
            }
            dispatch_semaphore_signal(semaphore);
        }];
        
    });
    
    dispatch_group_async(group, queue, ^{
        [[YTHNetInterface startInterface] fetchHotSerchListWithUid:^(BOOL isSuccessful, id result, NSString *error) {
            if (isSuccessful) {
                self.dictionaryHotSerch = result[@"Data"][@"data"];
            }
            dispatch_semaphore_signal(semaphore);
        }];
    });
    
    dispatch_group_notify(group, queue, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [self.collectionView reloadData];
        });
    });
    
    [self loadAd];
}
 
- (void)setupViewConfig {
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewSearchNav];
    self.viewSearchNav.title = self.ptitle;
    
    __weak typeof(self) weakSelf = self;
    self.viewSearchNav.onCacel = ^{
        if (weakSelf.tableView.hidden) {
            [weakSelf.view endEditing:YES];
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        } else {
            weakSelf.tableView.hidden = YES;
        }
    };
    
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.tableView];
}
 
- (void)writeSearchKey:(NSString *)name {
    self.plist.dataName = searchRecord;
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[self.plist readFile][0]];
    
    [array removeObject:name];
    [array insertObject:name atIndex:0];
    if (array.count > 10) {
        [array removeLastObject];
    }
    [self.plist writeFileWithData:array];
}
 
- (void)pushDetail:(NSString *)name {
    searchDetailViewController *vc = [[searchDetailViewController alloc] init];
    vc.searchString = name;
    [self.navigationController pushViewController:vc animated:YES];
}
 
/// 根据输入框的值搜索数据
/// @param text key
- (void)fetchSearchWithKey:(NSString *)text {
    [[YTHNetInterface startInterface] getSuggestSearchWithUid:[YTHsharedManger startManger].Uid withKey:text withSystem:@"1" withPage:@"1" withblock:^(BOOL isSuccessful, id result, NSString *error) {
        if (isSuccessful) {
            [self.arraySearch removeAllObjects];
            NSArray *suggestSearch = [[result objectForKey:@"Data"] objectForKey:@"data"];
            [self.arraySearch addObjectsFromArray:suggestSearch];
            [self.tableView reloadData];
        }
    }];
}
 
 
/**
 *  加载广点通原生广告
 */
-(void)loadAd{
    self.nativeExpressAd = [[GDTNativeExpressAd alloc] initWithAppId:GDTADkey placementId:GDTYSADkey4 adSize:CGSizeMake(KScreenW-20, (KScreenW-20)/16*9 + 10)];
    self.nativeExpressAd.delegate = self;
    // 配置视频播放属性
    self.nativeExpressAd.videoAutoPlayOnWWAN = YES;
    self.nativeExpressAd.videoMuted = NO;
    [self.nativeExpressAd loadAd:1];
}
- (void)nativeExpressAdSuccessToLoad:(GDTNativeExpressAd
                                      *)nativeExpressAd views:(NSArray<__kindof
                                                               GDTNativeExpressAdView *> *)views
{
    GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)views[0];
   
 
    adView.controller = self;
    [adView render];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
    UICollectionViewCell *cell = (UICollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    cell.sd_layout.leftSpaceToView(self, 10);
    [cell addSubview:adView];
    // 广告位 render 后刷新 tableView
    [self.collectionView reloadData];
}
- (void)nativeExpressAdFailToLoad:(GDTNativeExpressAd *)nativeExpressAd error:(NSError *)error{
    NSLog(@"%@",error);
}
 
#pragma mark ---------- SearchDelegate ---------
- (void)hotSearchEvent:(NSString *)name {
    [self writeSearchKey:name];
    [self pushDetail:name];
    
    [self.arrayRecord removeObject:name];
    [self.arrayRecord insertObject:name atIndex:0];
    if (_arrayRecord.count > 10) {
        [self.arrayRecord removeLastObject];
    }
    [self.collectionView reloadData];
}
 
- (void)deleteRecordEvent {
    self.plist.dataName = searchRecord;
    [self.plist deleteFile];
    
    [self.arrayRecord removeAllObjects];
    [self.collectionView reloadData];
}
 
- (void)textFieldSearch:(NSString *)text {
    if (!text || [text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请输入搜索内容"];
        return;
    }
    [self.view endEditing:YES];
    [self hotSearchEvent:text];
}
 
- (void)textFieldValueChange:(NSString *)text {
    
    if (text.length > 0) {
        [self fetchSearchWithKey:text];
        self.tableView.hidden = NO;
        
    } else {
        self.tableView.hidden = YES;
    }
}
 
- (CGFloat)calculateRowWidth:(NSString *)string fontSize:(float)fontSize height:(float)height {
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(0, height)options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width;
}
 
- (void)cellClick:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        NSString *name = _arrayRecord[indexPath.row];
        [self hotSearchEvent:name];
    }
}
 
#pragma mark UICollectionViewDelegate, UICollectionViewDataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self cellClick:indexPath];
}
 
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 4;
}
 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
        
    } else if (section == 1) {
        return self.arrayRecord.count;
    } else {
        return 1;
    }
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SearchHotCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchHotCell" forIndexPath:indexPath];
        return cell;
        
    }  else if (indexPath.section == 1) {
        SearchRecordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchRecordCell" forIndexPath:indexPath];
        cell.name = self.arrayRecord[indexPath.row];
        return cell;
        
    } else if (indexPath.section == 2) {
        SearchHotSearchCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchHotSearchCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.data = _dictionaryHotSerch;
        return cell;
        
    }else if (indexPath.section == 3) {
        SearchADCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchADCell" forIndexPath:indexPath];
        return cell;
    } else {
        SearchHotSearchCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchHotSearchCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.data = _dictionaryHotSerch;
        return cell;
    }
}
 
// 定制collectionView的head和foot
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 1 && self.arrayRecord.count != 0) {
            SearchRecordHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchRecordHeaderView" forIndexPath:indexPath];
            view.delegate = self;
            return view;
        }
    }
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableView" forIndexPath:indexPath];
    return view;
}
 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    //宽度随便定,系统会自动取collectionView的宽度
    //高度为分组头的高度
    if (section == 0) {
        return CGSizeMake(KScreenW, 10);
        
    } else if (section == 1) {
        if (self.arrayRecord.count == 0) {
            return CGSizeMake(KScreenW, 0);
        }
        return CGSizeMake(KScreenW, 20 + 16 + 16);
    }else if (section == 3) {
        return CGSizeMake(KScreenW, 10);
    }
    return CGSizeMake(KScreenW, 28);
}
 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(KScreenW, CGFLOAT_MIN);
}
 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return  CGSizeMake(KScreenW, 36);
        
    } else if (indexPath.section == 1) {
        NSString *IntroductionStr = nil;
        IntroductionStr = _arrayRecord[indexPath.row];
        CGFloat width = [self calculateRowWidth:IntroductionStr fontSize:13 height:29];
        CGSize titleSize = CGSizeMake(width + 30, 29);
        return titleSize;
        
    } else {
        return  CGSizeMake(KScreenW, 198);
    }
}
 
//协议中的方法,用于返回整个CollectionView上、左、下、右距四边的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 1) {
        return UIEdgeInsetsMake(0, 11, 0, 11);
        
    }
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
 
//两行cell之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1) {
        return 14;
    }
    return CGFLOAT_MIN;
}
 
//同一行两个cell的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1) {
        return 16;
    }
    return 0;
}
 
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewLeftAlignedLayout *flow = [[UICollectionViewLeftAlignedLayout alloc] init];
        //指定布局方式为垂直
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        // STATUSBAR_HEIGHT + 27
        //创建CollectionView并指定布局对象
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kStatusBarH + ALNavBarH,  KScreenW, KScreenH  - kStatusBarH - ALNavBarH) collectionViewLayout:flow];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.delaysContentTouches = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        
        [_collectionView registerClass:[SearchHotCell class] forCellWithReuseIdentifier:@"SearchHotCell"];
        [_collectionView registerClass:[SearchRecordCell class] forCellWithReuseIdentifier:@"SearchRecordCell"];
        [_collectionView registerClass:[SearchHotSearchCell class] forCellWithReuseIdentifier:@"SearchHotSearchCell"];
        [_collectionView registerClass:[SearchADCell class] forCellWithReuseIdentifier:@"SearchADCell"];
        
        
        
        [_collectionView registerClass:[SearchRecordHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchRecordHeaderView"];
        
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableView"];
    }
    return _collectionView;
}
 
#pragma mark  UITableViewDelegate UITableViewDataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
    self.tableView.hidden = YES;
    NSString *name = _arraySearch[indexPath.row];
    [self hotSearchEvent:name];
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arraySearch.count;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *cellIden = @"searchCell";
    SearchCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIden];
    if (!cell){
        cell  = [[SearchCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: cellIden];
    }
    cell.title = _arraySearch[indexPath.row];
    return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 55;
}
 
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusBarH + ALNavBarH,  KScreenW, KScreenH  - kStatusBarH - ALNavBarH)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = YES;
        _tableView.hidden = YES;
    }
    return _tableView;
}
 
- (SearchNavView *)viewSearchNav {
    if (!_viewSearchNav) {
        _viewSearchNav = [[SearchNavView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, kStatusBarH + ALNavBarH)];
        _viewSearchNav.delegate = self;
    }
    return _viewSearchNav;
}
 
- (NSMutableArray *)arrayRecord {
    if (!_arrayRecord) {
        _arrayRecord = [[NSMutableArray alloc] init];
    }
    return _arrayRecord;
}
 
- (NSMutableArray *)arrayHot {
    if (!_arrayHot) {
        _arrayHot = [[NSMutableArray alloc] init];
    }
    return _arrayHot;
}
 
- (NSMutableArray *)arraySearch {
    if (!_arraySearch) {
        _arraySearch = [[NSMutableArray alloc] init];
    }
    return _arraySearch;
}
 
@end