BogoSearchViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // BogoSearchViewController.m
  3. // BuguLive
  4. //
  5. // Created by Mac on 2021/9/27.
  6. // Copyright © 2021 xfg. All rights reserved.
  7. //
  8. #import "BogoSearchViewController.h"
  9. #import "BogoShopKit.h"
  10. #import "BogoSearchSubViewController.h"
  11. #import "BogoSearchHistoryItemCell.h"
  12. #import "BogoSearchHistoryModel.h"
  13. #import "BogoSearchHeaderView.h"
  14. #import "BogoSearchVideoSubViewController.h"
  15. @interface BogoSearchViewController ()<QMUITextFieldDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,BogoSearchSubViewControllerDelegate>
  16. @property (weak, nonatomic) IBOutlet QMUITextField *textField;
  17. @property (weak, nonatomic) IBOutlet UIView *historyView;
  18. @property (weak, nonatomic) IBOutlet UIView *resultView;
  19. @property (weak, nonatomic) IBOutlet UICollectionView *historyCollectionView;
  20. @property(nonatomic, strong) NSMutableArray <BogoSearchHistoryModel *>*historyDataArray;
  21. @property(nonatomic, strong) NSMutableArray *vcArray;
  22. @property(nonatomic, strong) MLMSegmentHead *segHead;
  23. @property(nonatomic, strong) MLMSegmentScroll *segScroll;
  24. @property(nonatomic, strong) BogoSearchVideoSubViewController *videoVC;
  25. @end
  26. @implementation BogoSearchViewController
  27. - (void)viewDidLoad{
  28. [super viewDidLoad];
  29. self.textField.delegate = self;
  30. UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 37, 32)];
  31. UIImageView *leftImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bogo_home_top_search"]];
  32. leftImageView.frame = CGRectMake(14, 7, 18, 18);
  33. [leftView addSubview:leftImageView];
  34. self.textField.leftView = leftView;
  35. self.textField.leftViewMode = UITextFieldViewModeAlways;
  36. UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 28, 32)];
  37. UIImageView *rightImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bogo_home_live_search_clear"]];
  38. rightImageView.frame = CGRectMake(0, 7, 18, 18);
  39. rightImageView.userInteractionEnabled = YES;
  40. [rightImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearTextField)]];
  41. [rightView addSubview:rightImageView];
  42. self.textField.rightView = rightView;
  43. self.textField.rightViewMode = UITextFieldViewModeWhileEditing;
  44. [self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  45. self.historyCollectionView.delegate = self;
  46. self.historyCollectionView.dataSource = self;
  47. [self.historyCollectionView registerNib:[UINib nibWithNibName:@"BogoSearchHistoryItemCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"BogoSearchHistoryItemCell"];
  48. [self addSubController];
  49. }
  50. - (void)viewWillAppear:(BOOL)animated{
  51. [super viewWillAppear:animated];
  52. [self requestSearchHistoryData];
  53. [self.navigationController setNavigationBarHidden:YES animated:NO];
  54. }
  55. - (void)requestSearchHistoryData{
  56. // /mapi/index.php?ctl=index&act=search_log
  57. [self.httpsManager POSTWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"act":@"search_log",@"ctl":@"index"}] SuccessBlock:^(NSDictionary *responseJson) {
  58. if ([responseJson toInt:@"status"] == 1) {
  59. [self.historyDataArray removeAllObjects];
  60. for (NSDictionary *dict in responseJson[@"list"]) {
  61. BogoSearchHistoryModel *model = [BogoSearchHistoryModel mj_objectWithKeyValues:dict];
  62. [self.historyDataArray addObject:model];
  63. }
  64. [self.historyCollectionView reloadData];
  65. }else{
  66. [[BGHUDHelper sharedInstance] tipMessage:[responseJson toString:@"error"]];
  67. }
  68. } FailureBlock:^(NSError *error) {
  69. [[BGHUDHelper sharedInstance] tipMessage:error.localizedDescription];
  70. }];
  71. }
  72. - (void)clearTextField{
  73. self.textField.text = @"";
  74. }
  75. - (void)addSubController {
  76. _segHead = [[MLMSegmentHead alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 40) titles:@[ASLocalizedString(@"全部"),ASLocalizedString(@"用户"),ASLocalizedString(@"短视频"),ASLocalizedString(@"动态")] headStyle:SegmentHeadStyleLine layoutStyle:MLMSegmentLayoutDefault];
  77. //tab颜色
  78. _segHead.selectColor = [UIColor colorWithHexString:@"#9152F8"];
  79. _segHead.deSelectColor = [UIColor colorWithHexString:@"#777777"];
  80. _segHead.lineColor = [UIColor colorWithHexString:@"#9E64FF"];
  81. _segHead.lineHeight = 4;
  82. _segHead.selectFont = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  83. _segHead.deSelectFont = [UIFont systemFontOfSize:16];
  84. _segHead.lineScale = .25;
  85. _segHead.headColor = FD_WhiteColor;
  86. _segHead.bottomLineHeight = 0;
  87. _segHead.tag = 1101;
  88. _segScroll = [[MLMSegmentScroll alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_segHead.frame), FD_ScreenWidth, FD_ScreenHeight - 40 - FD_Top_Height) vcOrViews:self.vcArray];
  89. _segScroll.loadAll = NO;
  90. _segScroll.showIndex = 0;
  91. __weak __typeof(self)weakSelf = self;
  92. [MLMSegmentManager associateHead:_segHead withScroll:_segScroll completion:^{
  93. __strong __typeof(weakSelf)strongSelf = weakSelf;
  94. [strongSelf.resultView addSubview:strongSelf.segHead];
  95. [strongSelf.resultView addSubview:strongSelf.segScroll];
  96. }];
  97. }
  98. -(NSMutableArray *)vcArray{
  99. if (!_vcArray) {
  100. _vcArray = [NSMutableArray array];
  101. for (NSInteger i = 0; i < 4; i++) {
  102. BogoSearchSubViewController *listVC = [[BogoSearchSubViewController alloc]init];
  103. listVC.type = i;
  104. listVC.delegate = self;
  105. if (i == 2) {
  106. BogoSearchVideoSubViewController *hotVC = [[BogoSearchVideoSubViewController alloc]init];
  107. hotVC.isHaveNavBar = NO;
  108. [_vcArray addObject:hotVC];
  109. self.videoVC = hotVC;
  110. }else{
  111. [_vcArray addObject:listVC];
  112. }
  113. }
  114. }
  115. return _vcArray;
  116. }
  117. #pragma mark - Action
  118. - (IBAction)backBtnAction:(UIButton *)sender {
  119. [self.navigationController popViewControllerAnimated:YES];
  120. }
  121. - (IBAction)cancelBtnAction:(UIButton *)sender {
  122. [self.navigationController popViewControllerAnimated:YES];
  123. }
  124. - (IBAction)clearBtnAction:(UIButton *)sender {
  125. FDAlertView *alertView = [[FDAlertView alloc]initWithTitle:@"" message:ASLocalizedString(@"确认删除全部历史搜索记录?")];
  126. [alertView addAction:[FDAction actionWithTitle:ASLocalizedString(@"取消") type:FDActionTypeCancel CallBack:nil]];
  127. [alertView addAction:[FDAction actionWithTitle:ASLocalizedString(@"确定") type:FDActionTypeDefault CallBack:^{
  128. // /mapi/index.php?ctl=index&act=del_search
  129. [self.httpsManager POSTWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"act":@"del_search",@"ctl":@"index"}] SuccessBlock:^(NSDictionary *responseJson) {
  130. if ([responseJson toInt:@"status"] == 1) {
  131. [self.historyDataArray removeAllObjects];
  132. [self.historyCollectionView reloadData];
  133. }else{
  134. [[BGHUDHelper sharedInstance] tipMessage:[responseJson toString:@"error"]];
  135. }
  136. } FailureBlock:^(NSError *error) {
  137. [[BGHUDHelper sharedInstance] tipMessage:error.localizedDescription];
  138. }];
  139. }]];
  140. [alertView show:self.view];
  141. }
  142. #pragma mark - BogoSearchSubViewControllerDelegate
  143. - (void)subVC:(BogoSearchSubViewController *)subVC headerView:(BogoSearchHeaderView *)headerView didClickAllBtn:(UIButton *)sender{
  144. switch (headerView.type) {
  145. case BogoSearchHeaderViewTypeUser:
  146. [self.segHead setSelectIndex:1];
  147. [self.segScroll setContentOffset:CGPointMake(kScreenW, 0) animated:YES];
  148. break;
  149. case BogoSearchHeaderViewTypeVideo:
  150. [self.segHead setSelectIndex:2];
  151. [self.segScroll setContentOffset:CGPointMake(kScreenW * 2, 0) animated:YES];
  152. break;
  153. case BogoSearchHeaderViewTypeDynamic:
  154. [self.segHead setSelectIndex:3];
  155. [self.segScroll setContentOffset:CGPointMake(kScreenW * 3, 0) animated:YES];
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. #pragma mark - QMUITextFieldDelegate
  162. - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  163. if (textField.text.length) {
  164. [self searchText:self.textField.text];
  165. [textField resignFirstResponder];
  166. return YES;
  167. }
  168. return NO;
  169. }
  170. - (void)textFieldDidChange:(QMUITextField *)textField{
  171. if (!textField.text.length) {
  172. self.historyView.hidden = NO;
  173. self.resultView.hidden = YES;
  174. }
  175. }
  176. - (void)searchText:(NSString *)text{
  177. self.historyView.hidden = YES;
  178. self.resultView.hidden = NO;
  179. for (UIViewController *subVC in self.vcArray) {
  180. if ([subVC isKindOfClass:[BogoSearchVideoSubViewController class]]) {
  181. BogoSearchVideoSubViewController *videoVC = subVC;
  182. videoVC.keyword = text;
  183. }else{
  184. BogoSearchSubViewController *listVC = subVC;
  185. listVC.keyword = text;
  186. }
  187. }
  188. }
  189. #pragma mark - UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
  190. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  191. return 1;
  192. }
  193. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  194. return self.historyDataArray.count;
  195. }
  196. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  197. BogoSearchHistoryItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BogoSearchHistoryItemCell" forIndexPath:indexPath];
  198. if (indexPath.row < self.historyDataArray.count) {
  199. cell.model = self.historyDataArray[indexPath.item];
  200. }
  201. return cell;
  202. }
  203. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  204. NSString *title = self.historyDataArray[indexPath.item].title;
  205. CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
  206. return CGSizeMake(size.width + 20 + 10, 25);
  207. }
  208. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  209. return UIEdgeInsetsMake(15, 10, 15, 10);
  210. }
  211. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  212. return 15;
  213. }
  214. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  215. return 10;
  216. }
  217. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  218. NSString *keyword = self.historyDataArray[indexPath.item].title;
  219. self.textField.text = keyword;
  220. [self searchText:keyword];
  221. }
  222. #pragma mark - Lazy Load
  223. - (NSMutableArray<BogoSearchHistoryModel *> *)historyDataArray{
  224. if (!_historyDataArray) {
  225. _historyDataArray = [NSMutableArray array];
  226. }
  227. return _historyDataArray;
  228. }
  229. @end