KWHisAndHotWordsView.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // KWHisAndHotWordsView.m
  3. // westkissMob
  4. //
  5. // Created by iOS on 2022/9/14.
  6. //
  7. #import "KWHisAndHotWordsView.h"
  8. #import "KWCustomLayout.h"
  9. #import "KWHisKeyWordCell.h"
  10. #import "KWHisCollectHeaderView.h"
  11. #import "KWNoHisWordsCell.h"
  12. @interface KWHisAndHotWordsView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  13. @end
  14. @implementation KWHisAndHotWordsView
  15. - (void)reloadData {
  16. self.hisWords = [self.vm getLocalSearchDatas];
  17. @weakify(self);
  18. [self.vm getHotKeyList:^{
  19. [weak_self.collectV reloadData];
  20. }];
  21. [self.collectV reloadData];
  22. }
  23. - (instancetype)initWithFrame:(CGRect)frame {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. [self configSubV];
  27. }
  28. return self;
  29. }
  30. -(void)configSubV {
  31. self.vm = [[KWHisAndHotWordsViewModel alloc] init];
  32. self.backgroundColor = [UIColor colorWithHexString:@"f8f8f8"];
  33. self.collectV.backgroundColor = [UIColor colorWithHexString:@"f8f8f8"];
  34. [self addSubview:self.collectV];
  35. [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.edges.equalTo(self);
  37. }];
  38. @weakify(self);
  39. [self.vm setDataReload:^{
  40. [weak_self reloadData];
  41. }];
  42. }
  43. - (UICollectionView *)collectV {
  44. if (!_collectV) {
  45. KWCustomLayout *layout = [[KWCustomLayout alloc] init];
  46. layout.sectionInset = UIEdgeInsetsMake(5, 20, 20, 20);
  47. layout.minimumLineSpacing = 10;
  48. layout.minimumInteritemSpacing = 10;
  49. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  50. UICollectionView *v = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 400) collectionViewLayout:layout];
  51. v.delegate = self;
  52. v.dataSource = self;
  53. [v registerClass:[KWHisKeyWordCell class] forCellWithReuseIdentifier:@"cell"];
  54. [v registerClass:[KWNoHisWordsCell class] forCellWithReuseIdentifier:@"KWNoHisWordsCell"];
  55. [v registerClass:[KWHisCollectHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
  56. _collectV = v;
  57. }
  58. return _collectV;
  59. }
  60. #pragma mark - UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource
  61. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  62. if (self.callBack) {
  63. NSString *title;
  64. if (indexPath.section == 0) {
  65. title = _hisWords[indexPath.item];
  66. self.callBack(title);
  67. } else {
  68. KWSearchHotKeyModel *keyM = self.vm.hotKeys[indexPath.item];
  69. if (self.hotCallBack) {
  70. self.hotCallBack(keyM);
  71. }
  72. }
  73. }
  74. }
  75. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  76. return 2;
  77. }
  78. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  79. if (section == 0) {
  80. if (_hisWords.count == 0) {
  81. return 1;
  82. }
  83. return _hisWords.count;
  84. }
  85. return self.vm.hotKeys.count;
  86. }
  87. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  88. if (indexPath.section == 0 && _hisWords.count == 0) {
  89. KWNoHisWordsCell *ce = [collectionView dequeueReusableCellWithReuseIdentifier:@"KWNoHisWordsCell" forIndexPath:indexPath];
  90. return ce;
  91. }
  92. KWHisKeyWordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  93. NSString *title;
  94. if (indexPath.section == 0) {
  95. title = _hisWords[indexPath.item];
  96. } else {
  97. title = self.vm.hotKeys[indexPath.item].title;
  98. }
  99. cell.titleLb.text = title;
  100. return cell;
  101. }
  102. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  103. if (indexPath.section == 0 && _hisWords.count == 0) {
  104. return CGSizeMake(KScreenWidth-40, 90);
  105. }
  106. NSString *title;
  107. if (indexPath.section == 0) {
  108. title = _hisWords[indexPath.item];
  109. } else {
  110. title = self.vm.hotKeys[indexPath.item].title;
  111. }
  112. CGFloat wid = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 34) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:Rob_Regular size:12]} context:nil].size.width + 12;
  113. // CGFloat wid = [title widthForFont:[UIFont fontWithName:Rob_Regular size:12]] + 12;
  114. wid = wid > KScreenWidth-40 ? KScreenWidth - 40 : wid;
  115. return CGSizeMake(wid, 34);
  116. }
  117. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  118. if (section == 1 && self.vm.hotKeys.count == 0) {
  119. return CGSizeZero;
  120. }
  121. return CGSizeMake(KScreenWidth-40, 40);
  122. }
  123. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  124. if ([kind isEqualToString:UICollectionElementKindSectionHeader] ) {
  125. KWHisCollectHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  126. header.titleLb.text = indexPath.section == 1 ? @"HOT SEARCH" : @"SEARCH HISTORY";
  127. header.imgV.hidden = indexPath.section == 1;
  128. header.btn.hidden = indexPath.section == 1;
  129. @weakify(self);
  130. header.callBack = ^{
  131. [weak_self.vm cleanLocalHisDatas];
  132. [weak_self reloadData];
  133. };
  134. return header;
  135. }
  136. return nil;
  137. }
  138. @end