//
|
// 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
|