// // QXSearchViewController.m // QXLive // // Created by 启星 on 2025/5/8. // #import "QXSearchViewController.h" #import "ZLCollectionViewVerticalLayout.h" #import "QXSearchTopView.h" #import "QXSearchHeaderView.h" #import "QXSearchCell.h" #import "QXFileManager.h" #import "QXMineNetwork.h" #import "QXSearchModel.h" #import "QXHomeSearchResultVC.h" @interface QXSearchViewController () @property(nonatomic,strong)UICollectionView* collectionView; @property(nonatomic,strong)QXSearchTopView *topView; @property(nonatomic,strong)QXSearchListModel *model; @end @implementation QXSearchViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES animated:YES]; } - (void)initSubViews{ [self.view addSubview:self.topView]; [self.view addSubview:self.collectionView]; [self getHistoryData]; } -(void)getHistoryData{ self.dataArray = [NSMutableArray arrayWithArray:[QXFileManager getSearchHistory]]; [self.collectionView reloadData]; } #pragma mark - QXSearchTopViewDelegate -(void)didClickCancel{ [self.navigationController popViewControllerAnimated:YES]; } -(void)didClickSearchWithKeywords:(NSString *)keywords{ // 搜索 [QXFileManager writeSearchHistoryWithContent:keywords]; self.dataArray = [NSMutableArray arrayWithArray:[QXFileManager getSearchHistory]]; [self.collectionView reloadData]; [self searchActionWithKeywords:keywords]; } -(void)searchActionWithKeywords:(NSString *)keywords{ MJWeakSelf [QXMineNetwork searchListApiWithKetwords:keywords successBlock:^(NSDictionary * _Nonnull dict) { QXSearchListModel *model = [QXSearchListModel yy_modelWithJSON:dict]; weakSelf.model = model; if (weakSelf.model.users.count == 0 && weakSelf.model.rooms.count == 0) { showToast(@"暂无搜索结果"); return; } [weakSelf pushToResultVC]; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { showToast(msg); }]; } -(void)pushToResultVC{ QXHomeSearchResultVC *vc = [[QXHomeSearchResultVC alloc] init]; vc.model = self.model; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark - UICollectionViewDelegate,UICollectionViewDataSource -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.dataArray.count; } -(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ QXSearchCell *cell = [QXSearchCell cellWithCollectionView:collectionView forIndexPath:indexPath]; cell.titleLabel.text = self.dataArray[indexPath.row]; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake([self.dataArray[indexPath.row] boundingRectWithSize:CGSizeMake(1000000, 22) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:12]} context:nil].size.width + 20, 22); } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ([kind isEqualToString : UICollectionElementKindSectionHeader]){ QXSearchHeaderView* headerView = [QXSearchHeaderView headerViewWithCollectionView:collectionView forIndexPath:indexPath]; headerView.title = QXText(@"历史记录"); return headerView; } return nil; } -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ return CGSizeMake(SCREEN_WIDTH, 24); } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(12, 16, 12, 16); } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSString *keywords = self.dataArray[indexPath.row]; [self searchActionWithKeywords:keywords]; } -(UICollectionView *)collectionView{ if (!_collectionView) { ZLCollectionViewVerticalLayout *flowLayout = [[ZLCollectionViewVerticalLayout alloc] init]; flowLayout.delegate = self; flowLayout.canDrag = NO; flowLayout.isFloor = NO; flowLayout.header_suspension = NO; flowLayout.columnSortType = Sequence; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.topView.bottom+12, SCREEN_WIDTH, SCREEN_HEIGHT-self.topView.bottom-12) collectionViewLayout:flowLayout]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.backgroundColor = [UIColor clearColor]; [_collectionView registerClass:[QXSearchHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[QXSearchHeaderView headerViewIdentifier]]; [_collectionView registerClass:[QXSearchCell class] forCellWithReuseIdentifier:[QXSearchCell cellIdentifier]]; _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; } return _collectionView; } -(QXSearchTopView *)topView{ if (!_topView) { _topView = [[QXSearchTopView alloc] initWithFrame:CGRectMake(0, kSafeAreaTop, SCREEN_WIDTH, NavHeight)]; _topView.delegate = self; } return _topView; } @end