// // KWHisAndHotWordsView.m // westkissMob // // Created by iOS on 2022/9/14. // #import "KWHisAndHotWordsView.h" #import "KWCustomLayout.h" #import "KWHisKeyWordCell.h" #import "KWHisCollectHeaderView.h" #import "KWNoHisWordsCell.h" @interface KWHisAndHotWordsView () @end @implementation KWHisAndHotWordsView - (void)reloadData { self.hisWords = [self.vm getLocalSearchDatas]; @weakify(self); [self.vm getHotKeyList:^{ [weak_self.collectV reloadData]; }]; [self.collectV reloadData]; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self configSubV]; } return self; } -(void)configSubV { self.vm = [[KWHisAndHotWordsViewModel alloc] init]; self.backgroundColor = [UIColor colorWithHexString:@"f8f8f8"]; self.collectV.backgroundColor = [UIColor colorWithHexString:@"f8f8f8"]; [self addSubview:self.collectV]; [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; @weakify(self); [self.vm setDataReload:^{ [weak_self reloadData]; }]; } - (UICollectionView *)collectV { if (!_collectV) { KWCustomLayout *layout = [[KWCustomLayout alloc] init]; layout.sectionInset = UIEdgeInsetsMake(5, 20, 20, 20); layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 10; layout.scrollDirection = UICollectionViewScrollDirectionVertical; UICollectionView *v = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 400) collectionViewLayout:layout]; v.delegate = self; v.dataSource = self; [v registerClass:[KWHisKeyWordCell class] forCellWithReuseIdentifier:@"cell"]; [v registerClass:[KWNoHisWordsCell class] forCellWithReuseIdentifier:@"KWNoHisWordsCell"]; [v registerClass:[KWHisCollectHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"]; _collectV = v; } return _collectV; } #pragma mark - UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.callBack) { NSString *title; if (indexPath.section == 0) { title = _hisWords[indexPath.item]; self.callBack(title); } else { KWSearchHotKeyModel *keyM = self.vm.hotKeys[indexPath.item]; if (self.hotCallBack) { self.hotCallBack(keyM); } } } } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == 0) { if (_hisWords.count == 0) { return 1; } return _hisWords.count; } return self.vm.hotKeys.count; } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0 && _hisWords.count == 0) { KWNoHisWordsCell *ce = [collectionView dequeueReusableCellWithReuseIdentifier:@"KWNoHisWordsCell" forIndexPath:indexPath]; return ce; } KWHisKeyWordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; NSString *title; if (indexPath.section == 0) { title = _hisWords[indexPath.item]; } else { title = self.vm.hotKeys[indexPath.item].title; } cell.titleLb.text = title; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0 && _hisWords.count == 0) { return CGSizeMake(KScreenWidth-40, 90); } NSString *title; if (indexPath.section == 0) { title = _hisWords[indexPath.item]; } else { title = self.vm.hotKeys[indexPath.item].title; } CGFloat wid = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 34) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:Rob_Regular size:12]} context:nil].size.width + 12; // CGFloat wid = [title widthForFont:[UIFont fontWithName:Rob_Regular size:12]] + 12; wid = wid > KScreenWidth-40 ? KScreenWidth - 40 : wid; return CGSizeMake(wid, 34); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (section == 1 && self.vm.hotKeys.count == 0) { return CGSizeZero; } return CGSizeMake(KScreenWidth-40, 40); } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ([kind isEqualToString:UICollectionElementKindSectionHeader] ) { KWHisCollectHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath]; header.titleLb.text = indexPath.section == 1 ? @"HOT SEARCH" : @"SEARCH HISTORY"; header.imgV.hidden = indexPath.section == 1; header.btn.hidden = indexPath.section == 1; @weakify(self); header.callBack = ^{ [weak_self.vm cleanLocalHisDatas]; [weak_self reloadData]; }; return header; } return nil; } @end