// SearchViewController.m // AICity // // Created by 刘伟伟 on 2023/7/8. // Copyright © 2023 wei.z. All rights reserved. // #import "SearchViewController.h" #import "UICollectionViewLeftAlignedLayout.h" #import "SearchCommonBarView.h" #import "SearchResultViewController.h" #import "ZFNormalViewController.h" @interface SearchViewController () @property(nonatomic,strong)SearchCommonBarView *barView; @property(nonatomic,strong)UICollectionView *collect; @property(nonatomic,strong)NSMutableArray *searchList; @property(nonatomic,strong)NSArray *hotkeyDataArr; @property(nonatomic,strong)NSArray *hotMoviesArr; @property(nonatomic,strong)NSArray *searchResArr; @property(nonatomic,assign)int vid; @end @implementation SearchViewController -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.searchList = [NSMutableArray array]; id arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"mylocationhistory"]; if([arr isKindOfClass:[NSArray class]] && [(NSArray *)arr count] > 0){ [self.searchList addObjectsFromArray:(NSArray *)arr]; } [self.collect reloadData]; // UITapGestureRecognizer *g = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEd)]; // [self.view addGestureRecognizer:g]; } //-(void)endEd{ // [self.view endEditing:true]; //} - (void)viewDidLoad { [super viewDidLoad]; self.fd_prefersNavigationBarHidden = true; self.searchList = [NSMutableArray array]; id arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"mylocationhistory"]; if([arr isKindOfClass:[NSArray class]] && [(NSArray *)arr count] > 0){ [self.searchList addObjectsFromArray:(NSArray *)arr]; } // Do any additional setup after loading the view. [self.view addSubview:self.barView]; // [self.barView mas_makeConstraints:^(MASConstraintMaker *make) { // make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 48)); // make.left.equalTo(self.view); // make.top.equalTo(self.view).offset(NAV_HEIGHT); // }]; [self.view addSubview:self.collect]; [self.collect mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.barView.mas_bottom); make.left.right.bottom.equalTo(self.view); }]; [self requestData]; } -(UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent; } -(void)requestData{ //热门关键字 [RequestTool post:@"search/getHotSearchKey" params:@{@"count":@(8)} success:^(id responseObject) { NSLog(@"home/getHotSearchKey-%@",responseObject); if([responseObject[@"code"] intValue] == 0){ id hotKeys = responseObject[@"data"][@"hot_keys"]; if([hotKeys isKindOfClass:[NSArray class]]){ self.hotkeyDataArr = hotKeys; }else{ self.hotkeyDataArr = @[]; } }else{ self.hotkeyDataArr = @[]; } [self.collect reloadData]; } failure:^(NSError *error) { self.hotkeyDataArr = @[]; [self.collect reloadData]; }]; //热搜影片 [RequestTool post:@"search/getHotSearchMovies" params:@{@"page":@{@"page":@(1),@"pageSize":@(10)}} success:^(id responseObject) { NSLog(@"home/homePage-%@",responseObject); if([responseObject[@"code"] intValue] == 0){ id movies = responseObject[@"data"][@"list"]; if([movies isKindOfClass:[NSArray class]]){ self.hotMoviesArr = movies; }else{ self.hotMoviesArr = @[]; } }else{ self.hotMoviesArr = @[]; } [self.collect reloadData]; } failure:^(NSError *error) { self.hotMoviesArr = @[]; [self.collect reloadData]; }]; } -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [self.view endEditing:YES]; } -(void)search:(NSString *)keyword{ [self.view endEditing:true]; if(![self.searchList containsObject:keyword]){ [self.searchList addObject:keyword]; [[NSUserDefaults standardUserDefaults] setObject:self.searchList forKey:@"mylocationhistory"]; } [self.collect reloadData]; //搜索影片 // __weak typeof(self) weakSelf = self; // [RequestTool post:@"search/searchMovie" params:@{@"search_key":keyword,@"page":@{@"page":@(1),@"pageSize":@(10)}} success:^(id responseObject) { // NSLog(@"home/homePage-%@",responseObject); // if([responseObject[@"code"] intValue] == 0){ // weakSelf.searchResArr = responseObject[@"data"][@"list"]; // SearchResultViewController *vc = [SearchResultViewController new]; // vc.searchResArr = weakSelf.searchResArr; // vc.txt = keyword; // vc.hidesBottomBarWhenPushed = true; // [weakSelf.navigationController pushViewController:vc animated:true]; // } // } failure:^(NSError *error) {}]; SearchResultViewController *vc = [SearchResultViewController new]; vc.txt = keyword; vc.hidesBottomBarWhenPushed = true; [self.navigationController pushViewController:vc animated:true]; } -(void)playItem{ ZFNormalViewController *vc = [[ZFNormalViewController alloc] init]; vc.vid = self.vid; vc.hidesBottomBarWhenPushed = true; [self.navigationController pushViewController:vc animated:true]; } #pragma mark -- collicationdelegate //返回分区个数 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } //返回每个分区的item个数 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ if(self.searchList.count>0){ return 3+self.searchList.count; }else{ return 2; } } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ __weak typeof (self) weakSelf = self; if(self.searchList.count>0){ if(indexPath.row == 0){ SearchTitCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SearchTitCell class]) forIndexPath:indexPath]; cell.delBlock = ^{ [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"mylocationhistory"]; weakSelf.searchList = [NSMutableArray array]; [weakSelf.collect reloadData]; }; return cell; }else if(indexPath.row<=self.searchList.count){ SearchItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SearchItemCell class]) forIndexPath:indexPath]; cell.nameLab.text = self.searchList[indexPath.row - 1]; return cell; }else if(indexPath.row == self.searchList.count+1){ FoundCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FoundCell class]) forIndexPath:indexPath]; cell.hotkeyDataArr = self.hotkeyDataArr; cell.tapBlock = ^(NSString * _Nonnull str) { [weakSelf search:str]; }; return cell; }else{ HotSearchCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HotSearchCell class]) forIndexPath:indexPath]; cell.hotMoviesArr = self.hotMoviesArr; cell.tapBlock = ^(int vid) { weakSelf.vid = vid; [weakSelf playItem]; }; return cell; } }else{ if(indexPath.row == 0){ FoundCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FoundCell class]) forIndexPath:indexPath]; cell.hotkeyDataArr = self.hotkeyDataArr; cell.tapBlock = ^(NSString * _Nonnull str) { [weakSelf search:str]; }; return cell; }else{ HotSearchCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HotSearchCell class]) forIndexPath:indexPath]; cell.hotMoviesArr = self.hotMoviesArr; cell.tapBlock = ^(int vid) { weakSelf.vid = vid; [weakSelf playItem]; }; return cell; } } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if(self.searchList.count>0){ if(indexPath.row == 0){ //do nothing }else if(indexPath.row<=self.searchList.count){ [self search:self.searchList[indexPath.row - 1]]; } } } #pragma mark - UICollectionViewDelegateFlowLayout - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if(self.searchList.count>0){ if(indexPath.row == 0){ return CGSizeMake(SCREEN_WIDTH-24, 37); }else if(indexPath.row<=self.searchList.count){ NSString *str=self.searchList[indexPath.row-1]; return CGSizeMake(str.length*12+25, 30); }else if(indexPath.row == self.searchList.count+1){ return CGSizeMake(SCREEN_WIDTH-24, 160); }else{ return CGSizeMake(SCREEN_WIDTH-24, 415+58*4); } }else{ if(indexPath.row == 0){ return CGSizeMake(SCREEN_WIDTH-24, 160); }else{ return CGSizeMake(SCREEN_WIDTH-24, 415+58*4); } } } - (UICollectionView *)collect { if (!_collect) { UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init]; layout.minimumLineSpacing = 8; //跟滚动方向相同的间距 layout.minimumInteritemSpacing = 8; //跟滚动方向垂直的间距 layout.scrollDirection = UICollectionViewScrollDirectionVertical; _collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH-60, 110) collectionViewLayout:layout]; _collect.contentInset = UIEdgeInsetsMake(0, 12, 12, 12); //注册cell [_collect registerClass:[SearchTitCell class] forCellWithReuseIdentifier:@"SearchTitCell"]; [_collect registerClass:[SearchItemCell class] forCellWithReuseIdentifier:@"SearchItemCell"]; [_collect registerClass:[FoundCell class] forCellWithReuseIdentifier:@"FoundCell"]; [_collect registerClass:[HotSearchCell class] forCellWithReuseIdentifier:@"HotSearchCell"]; _collect.dataSource = self; _collect.delegate = self; _collect.backgroundColor = rgba(18, 23, 41, 1); } return _collect; } -(SearchCommonBarView *)barView{ if(!_barView){ CGFloat h=0; if(@available(iOS 13.0, *)){ UIStatusBarManager *sc= [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager; CGRect f=sc.statusBarFrame; h=f.size.height; }else{ h=[UIApplication sharedApplication].statusBarFrame.size.height; } if(h>=40){ h=30; } _barView =[[SearchCommonBarView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 72-20+h)]; _barView.backgroundColor = rgba(18, 23, 41, 1); __weak typeof(self) weakSelf = self; _barView.searchAction = ^(NSString * _Nonnull tit) { [weakSelf search:tit]; }; } return _barView; } @end #pragma mark -- SearchTitCell @interface SearchTitCell() @end @implementation SearchTitCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setUp]; } return self; } -(void)delAction{ if(self.delBlock){ self.delBlock(); } } - (void)setUp { self.backgroundColor = [UIColor clearColor]; UILabel *nameLab = [UILabel new]; nameLab.text = @"搜索记录"; nameLab.font = [UIFont boldSystemFontOfSize:16]; nameLab.textColor = [UIColor whiteColor]; [self.contentView addSubview:nameLab]; [nameLab mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(0); make.bottom.equalTo(self.contentView).offset(-4); }]; UIImageView *iv = [UIImageView new]; iv.image = [UIImage imageNamed:@"Frame90"]; iv.userInteractionEnabled = true; UITapGestureRecognizer *g=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(delAction)]; [iv addGestureRecognizer:g]; [self.contentView addSubview:iv]; [iv mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(0); make.centerY.equalTo(nameLab); }]; } @end @implementation SearchItemCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setUp]; } return self; } - (void)setUp { self.backgroundColor = [UIColor clearColor]; UIView *bgView =[UIView new]; bgView.backgroundColor = rgba(52, 56, 68, 1); [self.contentView addSubview:bgView]; [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.contentView); }]; UILabel *nameLab = [UILabel new]; self.nameLab = nameLab; nameLab.text = @"流浪地球"; nameLab.font = [UIFont boldSystemFontOfSize:12]; nameLab.textColor = rgba(255, 255, 255, 0.8); [bgView addSubview:nameLab]; [nameLab mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(bgView); }]; bgView.layer.cornerRadius = 16; bgView.layer.masksToBounds = true; } @end @interface FoundCell() @property(nonatomic,strong)NSArray *nameArr; @property(nonatomic,strong)NSArray *arr; @end @implementation FoundCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setUp]; } return self; } -(void)setHotkeyDataArr:(NSArray *)hotkeyDataArr{ if(![hotkeyDataArr isKindOfClass:[NSArray class]]){ hotkeyDataArr = @[]; } _hotkeyDataArr = hotkeyDataArr; NSMutableArray *arr = [NSMutableArray array]; self.arr = arr; if(hotkeyDataArr.count){ [arr addObjectsFromArray:hotkeyDataArr]; } if(self.nameArr.count){ [arr addObjectsFromArray:self.nameArr]; } // if(hotkeyDataArr.count<8){ // [arr addObjectsFromArray:hotkeyDataArr]; // [arr addObjectsFromArray:self.nameArr]; // self.nameArr = arr; // }else{ // self.nameArr = hotkeyDataArr; // [arr addObjectsFromArray:hotkeyDataArr]; // } for(int i = 0;i<8;i++){ UILabel *item = [self.contentView viewWithTag:100+i]; BOOL shouldHide = i>=arr.count; item.hidden = shouldHide; if(shouldHide){ item.attributedText = nil; item.text = @""; continue; } if(i<3){ NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d",i+1] attributes:@{NSForegroundColorAttributeName:rgba(255, 212, 0, 1), NSFontAttributeName:[UIFont boldSystemFontOfSize:14]}]; NSString *str=[NSString stringWithFormat:@" %@",arr[i]]; NSMutableAttributedString *attrString2 = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:14]}]; [attrString1 appendAttributedString:attrString2]; item.attributedText = attrString1; }else{ item.text =[NSString stringWithFormat:@"%d %@",i+1,arr[i]]; } } } -(void)searchAct:(UITapGestureRecognizer *)g{ NSInteger x = g.view.tag - 100; if(x<0 || x>=self.arr.count){ return; } NSString *str = self.arr[x]; if(self.tapBlock){ self.tapBlock(str); } } - (void)setUp { self.backgroundColor = [UIColor clearColor]; UILabel *nameLab = [UILabel new]; nameLab.text = @"搜索发现"; nameLab.font = [UIFont boldSystemFontOfSize:16]; nameLab.textColor = rgba(255, 212, 0, 1); [self.contentView addSubview:nameLab]; [nameLab mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(0); make.top.equalTo(self.contentView).offset(16); }]; self.nameArr = @[@"检查风云",@"宇宙探索编辑部",@"昆仑神宫",@"不止不休",@"幕后玩家",@"我的女友们",@"普罗米修斯",@"古董局中局"]; for(int i=0;i<8;i++){ UILabel *item = [UILabel new]; item.tag = 100+i; UITapGestureRecognizer *g = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchAct:)]; item.userInteractionEnabled = true; [item addGestureRecognizer:g]; if(i<3){ NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d",i+1] attributes:@{NSForegroundColorAttributeName:rgba(255, 212, 0, 1), NSFontAttributeName:[UIFont boldSystemFontOfSize:14]}]; NSMutableAttributedString *attrString2 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",self.nameArr[i]] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:14]}]; [attrString1 appendAttributedString:attrString2]; item.attributedText = attrString1; }else{ item.font = [UIFont boldSystemFontOfSize:14]; item.textColor = [UIColor whiteColor]; item.text =[NSString stringWithFormat:@"%d %@",i+1,self.nameArr[i]]; } [self.contentView addSubview:item]; [item mas_makeConstraints:^(MASConstraintMaker *make) { if(i<4){ make.left.equalTo(self.contentView).offset(1); }else{ make.left.equalTo(self.contentView).offset(180-12); } make.top.equalTo(nameLab.mas_bottom).offset(12+30*(i%4)); }]; } } @end @implementation HotSearchCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setUp]; } return self; } -(void)tapAct:(UITapGestureRecognizer *)g{ NSInteger x = g.view.tag - 200; if(self.hotMoviesArr.count>x){ int vid = [self.hotMoviesArr[x][@"vid"] intValue]; if(self.tapBlock){ self.tapBlock(vid); } } } -(void)setHotMoviesArr:(NSArray *)hotMoviesArr{ if(![hotMoviesArr isKindOfClass:[NSArray class]]){ hotMoviesArr = @[]; } _hotMoviesArr = hotMoviesArr; UIView *container = [self.contentView viewWithTag:100]; for(int i=0;i<10;i++){ UIView *iv = [container viewWithTag:200+i]; BOOL shouldHide = i >= hotMoviesArr.count; iv.hidden = shouldHide; if(shouldHide){ continue; } UIImageView *icon = [iv viewWithTag:300]; [icon sd_setImageWithURL:[NSURL URLWithString:hotMoviesArr[i][@"vertical_cover_url"]]]; UILabel *lab1 = [iv viewWithTag:301]; if(i<3){ NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d",i+1] attributes:@{NSForegroundColorAttributeName:rgba(255, 61, 0, 1), NSFontAttributeName:[UIFont boldSystemFontOfSize:14]}]; NSString *str = [NSString stringWithFormat:@" %@",hotMoviesArr[i][@"ch_name"]]; NSMutableAttributedString *attrString2 = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:14]}]; [attrString1 appendAttributedString:attrString2]; lab1.attributedText = attrString1; }else{ lab1.text =[NSString stringWithFormat:@"%d %@",i+1,hotMoviesArr[i][@"ch_name"]]; } UILabel *lab2 = [iv viewWithTag:302]; // lab2.text = @"2023 / 9.6分 / 剧情 / 美国 / 沈腾"; NSDate* date = [NSDate dateWithTimeIntervalSince1970:[hotMoviesArr[i][@"screen_time"] doubleValue]]; NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy"]; NSString* dateString = [formatter stringFromDate:date]; NSString *c = [NSString stringWithFormat:@"%@ / %.1f ",dateString,[hotMoviesArr[i][@"score"] floatValue]]; NSArray *class_list = hotMoviesArr[i][@"class_list"]; if(![class_list isKindOfClass:[NSArray class]]){ class_list = @[]; } for(int j=0;j