省钱达人

DRFindSearchResultViewController.m 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. //
  2. // DRFindSearchResultViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by xiaoxi on 2018/2/1.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRFindSearchResultViewController.h"
  9. #import "DRGoodCollectionCell.h"
  10. #import "DRChildGoodModel.h"
  11. #import "DRGoodListViewController.h"
  12. #import "DRGoodDetailViewController.h"
  13. #import "DRFindRequestViewModel.h"
  14. #import "DRCollectionView.h"
  15. #import "DRGoodHorzitolCollectionCell.h"
  16. #import "DRSearchNoDataView.h"
  17. #import "DRSearchTitleView.h"
  18. #import "SelectView.h"
  19. static NSString *const cellID = @"DRGoodCollectionCell";
  20. static NSString *const HorzitolCellId = @"DRGoodHorzitolCollectionCell";
  21. static NSString *const collectionViewHeaderTop = @"top";
  22. static NSString *const collectionViewHeaderMiddle = @"middle";
  23. static BOOL isNoData = NO;
  24. static CGFloat selectedViewHeight = 80.f;
  25. typedef enum : NSUInteger {
  26. ResultCellTypeVertical,
  27. ResultCellTypeHorzitol,
  28. } ResultCellType;
  29. @interface DRFindSearchResultViewController ()
  30. <
  31. UICollectionViewDelegate,
  32. UICollectionViewDataSource,
  33. UICollectionViewDelegateFlowLayout,
  34. UIGestureRecognizerDelegate,
  35. YHSearchNoDataViewDelegate,
  36. NY_SelectViewDelegate
  37. >
  38. {
  39. NSString *_type; //1->默认 2->销量由高到低 3->价格由高到低 4->价格由低到高 5-综合筛选
  40. NSString *_maxPrice; //最高价格
  41. NSString *_minPrice; //最低价格
  42. NSInteger page;
  43. NSInteger guessYouLikePage;
  44. NSInteger _is_has_coupon;
  45. }
  46. @property (nonatomic, strong) UICollectionView *collectionView;
  47. @property (nonatomic, strong) NSMutableArray *goodsArr;
  48. @property (nonatomic, strong) SelectView *selectedView;
  49. @property (nonatomic, assign) ResultCellType cellType;
  50. @property (nonatomic, strong) NSArray *filtrateArray;
  51. @end
  52. @implementation DRFindSearchResultViewController
  53. - (void)viewDidLoad {
  54. [super viewDidLoad];
  55. [self requesFiltrateParam];
  56. [self configDefauleParamKey];
  57. [self initNavBar];
  58. [self initSubviews];
  59. [self requestVoucherFindIsRefresh:NO switchButton:nil];
  60. }
  61. - (void)viewWillAppear:(BOOL)animated {
  62. [super viewWillAppear:animated];
  63. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  64. self.navigationController.navigationBar.hidden = NO;
  65. }
  66. - (void)viewWillDisappear:(BOOL)animated {
  67. [super viewWillDisappear:animated];
  68. self.navigationController.navigationBar.hidden = self.hiddenNavigationWhenPop;
  69. }
  70. - (void)willMoveToParentViewController:(UIViewController *)parent {
  71. [super willMoveToParentViewController:parent];
  72. //处理侧滑手势
  73. if (!parent) {
  74. [self.navigationController setNavigationBarHidden:NO animated:NO];
  75. self.collectionView.height = kScreenHeight-NavBarHeight-selectedViewHeight;
  76. self.selectedView.y = 0;
  77. self.collectionView.y = selectedViewHeight;
  78. }
  79. }
  80. - (void)didReceiveMemoryWarning {
  81. [super didReceiveMemoryWarning];
  82. }
  83. - (void)configDefauleParamKey {
  84. page = 1;
  85. guessYouLikePage = 1;
  86. _type = @"1";
  87. _minPrice = @"0";
  88. _maxPrice = @"-1";
  89. }
  90. - (void)initNavBar {
  91. self.view.backgroundColor = [UIColor YHColorWithHex:0xf5f4f4];
  92. if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
  93. self.automaticallyAdjustsScrollViewInsets = YES;
  94. }
  95. UIView *statusBarBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, KStatusBarHeight)];
  96. statusBarBgView.backgroundColor = [UIColor whiteColor];
  97. [self.view addSubview:statusBarBgView];
  98. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
  99. [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  100. [button setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
  101. [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  102. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
  103. UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
  104. [rightBtn setImage:[UIImage imageNamed:@"Vertical_items"] forState:UIControlStateNormal];
  105. [rightBtn setImage:[UIImage imageNamed:@"Horizontal_items"] forState:UIControlStateSelected];
  106. [rightBtn addTarget:self action:@selector(switchItemsType:) forControlEvents:UIControlEventTouchUpInside];
  107. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
  108. NSNumber *cellType = [[NSUserDefaults standardUserDefaults] objectForKey:@"ResultCellType"];
  109. if (cellType) {
  110. self.cellType = cellType.integerValue;
  111. }else {
  112. self.cellType = ResultCellTypeVertical;
  113. }
  114. rightBtn.selected = self.cellType==ResultCellTypeHorzitol?YES:NO;
  115. self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
  116. DRSearchTitleView *titleView = [[DRSearchTitleView alloc] initWithFrame:CGRectMake(0, 0, FITSIZE(270), 32) searchName:self.searchName click:^{
  117. [self backAction];
  118. }];
  119. titleView.closeBlock = ^{
  120. [self backAction];
  121. self.searchBar.text = nil;
  122. };
  123. self.navigationController.navigationBar.translucent = NO;
  124. self.navigationItem.titleView = titleView;
  125. }
  126. - (void)backAction {
  127. [self.navigationController popViewControllerAnimated:NO];
  128. }
  129. - (void)switchItemsType:(UIButton *)sender {
  130. sender.selected = !sender.selected;
  131. self.cellType = sender.selected ? ResultCellTypeHorzitol:ResultCellTypeVertical;
  132. [[NSUserDefaults standardUserDefaults] setObject:@(self.cellType) forKey:@"ResultCellType"];
  133. [[NSUserDefaults standardUserDefaults] synchronize];
  134. [self.collectionView reloadData];
  135. }
  136. - (void)initSubviews {
  137. [self.view addSubview:self.collectionView];
  138. // [self.view addSubview:self.selectedView];
  139. [SVProgressHUD show];
  140. }
  141. #pragma mark - request
  142. /**
  143. 加载筛选范围
  144. */
  145. - (void)requesFiltrateParam {
  146. NSString *urlString = [NSString stringWithFormat:@"%@/api/categoryNew/priceSection", BaseURL];
  147. [DRHttp post:urlString params:nil success:^(id json) {
  148. self.filtrateArray = json[@"data"];
  149. [self.view addSubview:self.selectedView];
  150. } failure:^(NSError *error) {
  151. }];
  152. }
  153. - (void)requestVoucherFindIsRefresh:(BOOL)refresh switchButton:(UIButton *)switchButton{
  154. NSDictionary *param = @{@"page":@(page),
  155. @"keyword":self.searchName,
  156. @"sort":_type,
  157. @"min_price":_minPrice,
  158. @"max_price":_maxPrice,
  159. @"is_has_coupon":@(_is_has_coupon)
  160. };
  161. NSString *urlString = [NSString stringWithFormat:@"%@/api/v2/goods/search", BaseURL];
  162. [DRHttp post:urlString params:param success:^(id json) {
  163. NSArray *arr = [NSArray yy_modelArrayWithClass:[DRChildGoodModel class] json:json[@"data"]];
  164. if (refresh) {
  165. [self.goodsArr removeAllObjects];
  166. [self.collectionView reloadData];
  167. }
  168. if (arr.count > 0) {
  169. [self.collectionView.mj_footer endRefreshing];
  170. [self.goodsArr addObjectsFromArray:arr];
  171. [self.collectionView reloadData];
  172. if (page == 1) {
  173. isNoData = NO;
  174. self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  175. page ++;
  176. [self requestVoucherFindIsRefresh:NO switchButton:nil];
  177. }];
  178. self.collectionView.footRefreshState = MJFooterRefreshStateNormal;
  179. }
  180. }
  181. else {
  182. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  183. if (page == 1) {
  184. isNoData = YES;
  185. [self requestGuessYouLike];
  186. }
  187. }
  188. [SVProgressHUD dismiss];
  189. switchButton.selected = !switchButton.selected;
  190. } failure:^(NSError *error) {
  191. [self.collectionView.mj_footer endRefreshing];
  192. [SVProgressHUD dismiss];
  193. [MBProgressHUD showMessage:@"该商品未参加优惠券和返佣推广活动,建议亲换个店铺试试"];
  194. }];
  195. }
  196. - (void)requestGuessYouLike {
  197. [DRFindRequestViewModel requestGuessYouLikeParamPage:guessYouLikePage success:^(NSArray *array) {
  198. if (array.count > 0) {
  199. [self.goodsArr addObjectsFromArray:array];
  200. [self.collectionView reloadData];
  201. if (guessYouLikePage == 1) {
  202. kWeak(self);
  203. _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  204. guessYouLikePage ++;
  205. [selfWeak requestGuessYouLike];
  206. }];
  207. }
  208. }
  209. [self.collectionView.mj_footer endRefreshing];
  210. } failure:^(NSError *error) {
  211. [self.collectionView.mj_footer endRefreshing];
  212. [SVProgressHUD dismiss];
  213. [MBProgressHUD showMessage:@"加载失败"];
  214. }];
  215. }
  216. #pragma mark - SearchNoDataViewDelegate
  217. - (void)yh_SearchNoDataViewDidRefresh {
  218. [self.goodsArr removeAllObjects];
  219. [self requestVoucherFindIsRefresh:YES switchButton:nil];
  220. }
  221. #pragma mark - collectionView
  222. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  223. return 2;
  224. }
  225. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  226. if (section == 1) {
  227. return self.goodsArr.count;
  228. }
  229. return 0;
  230. }
  231. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  232. DRChildGoodModel *model = self.goodsArr[indexPath.item];
  233. if (self.cellType == ResultCellTypeVertical) {
  234. DRGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  235. cell.model = model;
  236. return cell;
  237. }else {
  238. DRGoodHorzitolCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:HorzitolCellId forIndexPath:indexPath];
  239. cell.model = model;
  240. return cell;
  241. }
  242. }
  243. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  244. DRChildGoodModel *model = self.goodsArr[indexPath.item];
  245. //详情
  246. DRGoodDetailViewController *detailVC = [[DRGoodDetailViewController alloc] init];
  247. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  248. detailVC.requestModel = requestModel;
  249. DREventModel *evevtModel = [[DREventModel alloc] initWithOrigin:model.origin category_id:@"0" source:searchAction];
  250. detailVC.eventModel = evevtModel;
  251. [self.navigationController pushViewController:detailVC animated:YES];
  252. }
  253. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  254. UICollectionReusableView *view = nil;
  255. if (indexPath.section == 0) {
  256. DRSearchNoDataView *noData = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderTop forIndexPath:indexPath];
  257. noData.delegate = self;
  258. noData.hidden = !isNoData;
  259. view = noData;
  260. return view;
  261. }
  262. else {
  263. UICollectionReusableView *guessYouLikeView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderMiddle forIndexPath:indexPath];
  264. UIImageView *middleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth/2-FITSIZE(99)/2, FITSIZE(14), FITSIZE(99), FITSIZE(12))];
  265. middleImageView.backgroundColor = [UIColor clearColor];
  266. middleImageView.image = [UIImage imageNamed:@"guess_like_gray"];
  267. [guessYouLikeView addSubview:middleImageView];
  268. guessYouLikeView.hidden = !isNoData;
  269. view = guessYouLikeView;
  270. }
  271. return view;
  272. }
  273. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  274. DRChildGoodModel *model = self.goodsArr[indexPath.row];
  275. if (self.cellType == ResultCellTypeVertical) {
  276. CGFloat width = (SCREEN_WIDTH-5)/2;
  277. CGFloat height = width + 102;
  278. CGSize itemSize = CGSizeMake(width, height);
  279. return itemSize;
  280. }else {
  281. return CGSizeMake(SCREEN_WIDTH, 140);
  282. }
  283. }
  284. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  285. if (self.cellType == ResultCellTypeVertical) {
  286. return 5;
  287. }else {
  288. return 1;
  289. }
  290. }
  291. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  292. if (isNoData) {
  293. if (section == 0) {
  294. return CGSizeMake(kScreenWidth, FITSIZE(264));
  295. }
  296. else {
  297. return CGSizeMake(kScreenWidth, FITSIZE(40));
  298. }
  299. }
  300. else {
  301. if (section == 0) {
  302. return CGSizeMake(kScreenWidth, 0.1);
  303. }
  304. else {
  305. return CGSizeMake(kScreenWidth, 0.1);
  306. }
  307. }
  308. }
  309. #pragma mark NY_SelectViewDelegate
  310. //筛选确定点击
  311. - (void)filtrateViewMakeSureFromValue:(NSString *)fromValue toValue:(NSString *)toValue {
  312. [self.collectionView scrollToTop];
  313. _minPrice = fromValue;
  314. _maxPrice = toValue;
  315. page = 1;
  316. [self requestVoucherFindIsRefresh:YES switchButton:nil];
  317. }
  318. //仅显示优惠券
  319. - (void)selectItme:(SelectView *)selectView onlyTicketClick:(UIButton *)switchBtn {
  320. _is_has_coupon = switchBtn.selected ? 0 : 1;
  321. [self requestVoucherFindIsRefresh:YES switchButton:switchBtn];
  322. }
  323. //上方条件选择
  324. -(void)selectTopButton:(SelectView *)selectView withIndex:(NSInteger)index withButtonType:(ButtonClickType)type1{
  325. [self.collectionView scrollToTop];
  326. //价格
  327. if (index == 2&&type1) {
  328. switch (type1) {
  329. case ButtonClickTypeNormal:
  330. //正常价格
  331. {
  332. NSLog(@"上边按钮的正常价格");
  333. }
  334. break;
  335. case ButtonClickTypeUp:
  336. //价格升序排列
  337. {
  338. NSLog(@"上边按钮的价格升序排列");
  339. _type = @"3";
  340. }
  341. break;
  342. case ButtonClickTypeDown:
  343. //价格降序排列
  344. {
  345. NSLog(@"上边按钮的价格降序排列");
  346. _type = @"4";
  347. }
  348. break;
  349. default:
  350. break;
  351. }
  352. }else if (index == 0){//综合
  353. NSLog(@"上边按钮的综合");
  354. _type = @"1";
  355. }else if (index == 1){//销量
  356. NSLog(@"上边按钮的销量");
  357. _type = @"2";
  358. }else{//筛选
  359. NSLog(@"上边按钮的筛选");
  360. }
  361. [self requestVoucherFindIsRefresh:YES switchButton:nil];
  362. }
  363. #pragma mark - scrollView
  364. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  365. {
  366. //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
  367. UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
  368. //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
  369. CGFloat velocity = [pan velocityInView:scrollView].y;
  370. if (velocity <- 10) {
  371. //向上拖动,隐藏导航栏
  372. [self.navigationController setNavigationBarHidden:YES animated:YES];
  373. self.collectionView.height = kScreenHeight-NavBarHeight-selectedViewHeight+NavBarHeight;
  374. self.selectedView.y = KStatusBarHeight;
  375. self.collectionView.y = self.selectedView.height+KStatusBarHeight;
  376. }else if (velocity > 10) {
  377. //向下拖动,显示导航栏
  378. [self.navigationController setNavigationBarHidden:NO animated:YES];
  379. self.collectionView.height = kScreenHeight-NavBarHeight-selectedViewHeight;
  380. self.selectedView.y = 0;
  381. self.collectionView.y = selectedViewHeight;
  382. }else if(velocity == 0){
  383. //停止拖拽
  384. }
  385. }
  386. #pragma mark - lazy
  387. - (UICollectionView *)collectionView {
  388. if (!_collectionView) {
  389. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  390. flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  391. flowLayout.minimumLineSpacing = 5;
  392. flowLayout.minimumInteritemSpacing = 5;
  393. _collectionView = [[DRCollectionView alloc] initWithFrame:CGRectMake(0, selectedViewHeight, kScreenWidth, kScreenHeight-NavBarHeight-selectedViewHeight) collectionViewLayout:flowLayout];
  394. _collectionView.delegate = self;
  395. _collectionView.dataSource = self;
  396. [_collectionView registerClass:[DRGoodCollectionCell class] forCellWithReuseIdentifier:cellID];
  397. [_collectionView registerClass:[DRGoodHorzitolCollectionCell class] forCellWithReuseIdentifier:HorzitolCellId];
  398. [_collectionView registerClass:[DRSearchNoDataView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderTop];
  399. [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderMiddle];
  400. if (@available(iOS 11.0, *)) {
  401. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  402. }
  403. }
  404. return _collectionView;
  405. }
  406. - (SelectView *)selectedView {
  407. if (!_selectedView) {
  408. _selectedView = [[SelectView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, selectedViewHeight) withArr:self.filtrateArray];
  409. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, -KStatusBarHeight, SCREEN_WIDTH, KStatusBarHeight)];
  410. bgView.backgroundColor = [UIColor whiteColor];
  411. [_selectedView addSubview:bgView];
  412. _selectedView.delegate = self;
  413. }
  414. return _selectedView;
  415. }
  416. - (NSMutableArray *)goodsArr {
  417. if (!_goodsArr) {
  418. _goodsArr = [NSMutableArray array];
  419. }
  420. return _goodsArr;
  421. }
  422. @end