// // KBFindSearchResultViewController.m // YouHuiProject // // Created by xiaoxi on 2018/2/1. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBFindSearchResultViewController.h" #import "KBGoodCollectionCell.h" #import "KBChildGoodModel.h" #import "KBGoodListViewController.h" #import "KBGoodDetailViewController.h" #import "KBFindRequestViewModel.h" #import "KBCollectionView.h" #import "KBGoodHorzitolCollectionCell.h" #import "KBSearchNoDataView.h" #import "KBSearchTitleView.h" #import "KBSearchSpecialView.h" #import "SelectView.h" #import "AdJumpManager.h" static NSString *const cellID = @"KBGoodCollectionCell"; static NSString *const HorzitolCellId = @"KBGoodHorzitolCollectionCell"; static NSString *const collectionViewHeaderTop = @"top"; static NSString *const collectionViewHeaderMiddle = @"middle"; static NSString *const collectionViewSpecailHeader = @"specailHeader"; //static BOOL isNoData = NO; static CGFloat selectedViewHeight = 80.f; typedef enum : NSUInteger { ResultCellTypeVertical, ResultCellTypeHorzitol, } ResultCellType; @interface KBFindSearchResultViewController () < UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, YHSearchNoDataViewDelegate, NY_SelectViewDelegate > { NSString *_type; //1->默认 2->销量由高到低 3->价格由高到低 4->价格由低到高 5-综合筛选 NSString *_maxPrice; //最高价格 NSString *_minPrice; //最低价格 NSInteger page; NSInteger guessYouLikePage; NSInteger _is_has_coupon; BOOL isNoData; } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *goodsArr; @property (nonatomic, strong) SelectView *selectedView; @property (nonatomic, assign) ResultCellType cellType; @property (nonatomic, strong) NSArray *filtrateArray; @property (nonatomic, strong) NSDictionary *special_search; @property (nonatomic, strong) AdPopModel *adModel; @end @implementation KBFindSearchResultViewController - (void)viewDidLoad { [super viewDidLoad]; [self requesFiltrateParam]; [self configDefauleParamKey]; [self initNavBar]; [self initSubviews]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; self.navigationController.navigationBar.hidden = NO; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; self.navigationController.navigationBar.hidden = self.hiddenNavigationWhenPop; [LoadingView dismiss]; } - (void)willMoveToParentViewController:(UIViewController *)parent { [super willMoveToParentViewController:parent]; //处理侧滑手势 if (!parent) { [self.navigationController setNavigationBarHidden:NO animated:NO]; self.selectedView.y = 0; if (self.special_search) { self.collectionView.y = 0; self.collectionView.height = kScreenHeight-NavBarHeight; }else { self.collectionView.y = selectedViewHeight; self.collectionView.height = kScreenHeight-NavBarHeight-selectedViewHeight; } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)configDefauleParamKey { page = 1; guessYouLikePage = 1; _type = @"1"; _minPrice = @"0"; _maxPrice = @"-1"; } - (void)initNavBar { self.view.backgroundColor = [UIColor yhGrayColor]; if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) { self.automaticallyAdjustsScrollViewInsets = YES; } UIView *statusBarBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, KStatusBarHeight)]; statusBarBgView.backgroundColor = [UIColor yhGrayColor]; // [self.view addSubview:statusBarBgView]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 32)]; [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [button setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)]; [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 32)]; [rightBtn setImage:[UIImage imageNamed:@"Vertical_items"] forState:UIControlStateNormal]; [rightBtn setImage:[UIImage imageNamed:@"Horizontal_items"] forState:UIControlStateSelected]; [rightBtn addTarget:self action:@selector(switchItemsType:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn]; NSNumber *cellType = [[NSUserDefaults standardUserDefaults] objectForKey:@"ResultCellType"]; if (cellType) { self.cellType = cellType.integerValue; }else { self.cellType = ResultCellTypeVertical; } rightBtn.selected = self.cellType==ResultCellTypeHorzitol?YES:NO; self.navigationController.interactivePopGestureRecognizer.delegate = (id)self; KBSearchTitleView *titleView = [[KBSearchTitleView alloc] initWithFrame:CGRectMake(0, 0, FITSIZE(270), 32) searchName:self.searchName click:^{ [self backAction]; }]; titleView.closeBlock = ^{ [self backAction]; self.searchBar.text = nil; }; self.navigationController.navigationBar.translucent = NO; self.navigationItem.titleView = titleView; } - (void)backAction { [self.navigationController popViewControllerAnimated:NO]; } - (void)switchItemsType:(UIButton *)sender { sender.selected = !sender.selected; self.cellType = sender.selected ? ResultCellTypeHorzitol:ResultCellTypeVertical; [[NSUserDefaults standardUserDefaults] setObject:@(self.cellType) forKey:@"ResultCellType"]; [[NSUserDefaults standardUserDefaults] synchronize]; [self.collectionView reloadData]; } - (void)initSubviews { [self.view addSubview:self.collectionView]; // [self.view addSubview:self.selectedView]; [LoadingView show]; } #pragma mark - request /** 加载筛选范围 */ - (void)requesFiltrateParam { NSString *urlString = [NSString stringWithFormat:@"%@/api/categoryNew/priceSection", BaseURL]; [KBHttp post:urlString params:nil success:^(id json) { self.filtrateArray = json[@"data"]; [self requestVoucherFindIsRefresh:NO switchButton:nil]; } failure:^(NSError *error) { }]; } - (void)requestVoucherFindIsRefresh:(BOOL)refresh switchButton:(UIButton *)switchButton{ if (refresh) { [LoadingView show]; } NSDictionary *param = @{@"page":@(page), @"keyword":self.searchName, @"sort":_type, @"min_price":_minPrice, @"max_price":_maxPrice, @"is_has_coupon":@(_is_has_coupon) }; NSString *urlString = [NSString stringWithFormat:@"%@/api/v2/goods/search", BaseURL]; [KBHttp post:urlString params:param success:^(id json) { [self.view addSubview:self.selectedView]; NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]]; self.special_search = json[@"special"]; self.adModel = [AdPopModel yy_modelWithJSON:json[@"special"]]; if (refresh) { [self.goodsArr removeAllObjects]; [self.collectionView reloadData]; } //首先判断special_search有没有数据,如果没有再去判断商品列表 if (self.special_search != nil) { self.selectedView.hidden = YES; self.collectionView.y = 0; self.collectionView.height = SCREEN_HEIGHT-NavBarHeight; [self.collectionView.mj_footer endRefreshingWithNoMoreData]; if (page == 1) { [self requestGuessYouLike]; } [self.collectionView reloadData]; }else { if (arr.count > 0) { [self.collectionView.mj_footer endRefreshing]; [self.goodsArr addObjectsFromArray:arr]; [self.collectionView reloadData]; if (page == 1) { isNoData = NO; self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ page ++; [self requestVoucherFindIsRefresh:NO switchButton:nil]; }]; self.collectionView.footRefreshState = MJFooterRefreshStateNormal; } } else { [self.collectionView.mj_footer endRefreshingWithNoMoreData]; if (page == 1) { isNoData = YES; [self requestGuessYouLike]; } } } [LoadingView dismiss]; switchButton.selected = !switchButton.selected; } failure:^(NSError *error) { [self.collectionView.mj_footer endRefreshing]; [LoadingView dismiss]; [MBProgressHUD showMessage:@"该商品未参加优惠券和返佣推广活动,建议亲换个店铺试试"]; }]; } - (void)requestGuessYouLike { [KBFindRequestViewModel requestGuessYouLikeParamPage:guessYouLikePage success:^(NSArray *array) { if (array.count > 0) { [self.goodsArr addObjectsFromArray:array]; [self.collectionView reloadData]; if (guessYouLikePage == 1) { kWeak(self); _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ guessYouLikePage ++; [selfWeak requestGuessYouLike]; }]; } } [self.collectionView reloadData]; [self.collectionView.mj_footer endRefreshing]; } failure:^(NSError *error) { [self.collectionView.mj_footer endRefreshing]; [LoadingView dismiss]; [MBProgressHUD showMessage:@"加载失败"]; }]; } #pragma mark - SearchNoDataViewDelegate - (void)yh_SearchNoDataViewDidRefresh { [self.goodsArr removeAllObjects]; [self requestVoucherFindIsRefresh:YES switchButton:nil]; } #pragma mark - collectionView - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == 1) { return self.goodsArr.count; } return 0; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.goodsArr[indexPath.item]; if (self.cellType == ResultCellTypeVertical) { KBGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; cell.model = model; return cell; }else { KBGoodHorzitolCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:HorzitolCellId forIndexPath:indexPath]; cell.model = model; return cell; } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.goodsArr[indexPath.item]; //详情 KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detailVC.requestModel = requestModel; KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:@"0" source:searchAction]; detailVC.eventModel = evevtModel; [self.navigationController pushViewController:detailVC animated:YES]; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *view = nil; if (indexPath.section == 0) { if (self.special_search != nil) { KBSearchSpecialView *searchSpecial =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewSpecailHeader forIndexPath:indexPath]; searchSpecial.model = self.adModel; searchSpecial.tapImgBlock = ^(AdPopModel * _Nonnull model) { [AdJumpManager jumpToPageWithModel:model ParentViewControll:self]; }; return searchSpecial; }else { KBSearchNoDataView *noData = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderTop forIndexPath:indexPath]; noData.delegate = self; noData.hidden = !isNoData; view = noData; return view; } } else { UICollectionReusableView *guessYouLikeView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderMiddle forIndexPath:indexPath]; UIImageView *middleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth/2-FITSIZE(99)/2, FITSIZE(14), FITSIZE(99), FITSIZE(12))]; middleImageView.backgroundColor = [UIColor clearColor]; middleImageView.image = [UIImage imageNamed:@"guess_like_gray"]; guessYouLikeView.clipsToBounds = YES; [guessYouLikeView addSubview:middleImageView]; view = guessYouLikeView; } return view; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.goodsArr[indexPath.row]; if (self.cellType == ResultCellTypeVertical) { CGFloat width = (SCREEN_WIDTH-5)/2; CGFloat height = width + 102; CGSize itemSize = CGSizeMake(width, height); return itemSize; }else { return CGSizeMake(SCREEN_WIDTH, 140); } } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { if (self.cellType == ResultCellTypeVertical) { return 5; }else { return 1; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (self.special_search != nil) { if (section == 0) { return CGSizeMake(kScreenWidth, FITSIZE(157)); } else { return CGSizeMake(kScreenWidth, FITSIZE(40)); } } if (isNoData) { if (section == 0) { return CGSizeMake(kScreenWidth, FITSIZE(264)); } else { return CGSizeMake(kScreenWidth, FITSIZE(40)); } } else { if (section == 0) { return CGSizeMake(kScreenWidth, 0.1); } else { return CGSizeMake(kScreenWidth, 0.1); } } } #pragma mark NY_SelectViewDelegate //筛选确定点击 - (void)filtrateViewMakeSureFromValue:(NSString *)fromValue toValue:(NSString *)toValue { [self.collectionView scrollToTop]; _minPrice = fromValue; _maxPrice = toValue; page = 1; [self requestVoucherFindIsRefresh:YES switchButton:nil]; } //仅显示优惠券 - (void)selectItme:(SelectView *)selectView onlyTicketClick:(UIButton *)switchBtn { _is_has_coupon = switchBtn.selected ? 0 : 1; [self requestVoucherFindIsRefresh:YES switchButton:switchBtn]; } //上方条件选择 -(void)selectTopButton:(SelectView *)selectView withIndex:(NSInteger)index withButtonType:(ButtonClickType)type1{ [self.collectionView scrollToTop]; //价格 if (index == 2&&type1) { switch (type1) { case ButtonClickTypeNormal: //正常价格 { NSLog(@"上边按钮的正常价格"); } break; case ButtonClickTypeUp: //价格升序排列 { NSLog(@"上边按钮的价格升序排列"); _type = @"3"; } break; case ButtonClickTypeDown: //价格降序排列 { NSLog(@"上边按钮的价格降序排列"); _type = @"4"; } break; default: break; } }else if (index == 0){//综合 NSLog(@"上边按钮的综合"); _type = @"1"; }else if (index == 1){//销量 NSLog(@"上边按钮的销量"); _type = @"2"; }else{//筛选 NSLog(@"上边按钮的筛选"); } [self requestVoucherFindIsRefresh:YES switchButton:nil]; } #pragma mark - scrollView - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势 UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer; //获取到拖拽的速度 >0 向下拖动 <0 向上拖动 CGFloat velocity = [pan velocityInView:scrollView].y; if (velocity <- 10) { //向上拖动,隐藏导航栏 [self.navigationController setNavigationBarHidden:YES animated:YES]; if (self.special_search != nil) { self.collectionView.height = kScreenHeight-KStatusBarHeight; self.collectionView.y = KStatusBarHeight; }else { self.collectionView.height = kScreenHeight-NavBarHeight-selectedViewHeight+NavBarHeight; self.collectionView.y = self.selectedView.height+KStatusBarHeight; } self.selectedView.y = KStatusBarHeight; }else if (velocity > 10) { //向下拖动,显示导航栏 [self.navigationController setNavigationBarHidden:NO animated:YES]; if (self.special_search != nil) { self.collectionView.height = kScreenHeight-NavBarHeight; self.collectionView.y = 0; }else { self.collectionView.height = kScreenHeight-NavBarHeight-selectedViewHeight; self.collectionView.y = selectedViewHeight; } self.selectedView.y = 0; }else if(velocity == 0){ //停止拖拽 } } #pragma mark - lazy - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; flowLayout.minimumLineSpacing = 5; flowLayout.minimumInteritemSpacing = 5; _collectionView = [[KBCollectionView alloc] initWithFrame:CGRectMake(0, selectedViewHeight, kScreenWidth, kScreenHeight-NavBarHeight-selectedViewHeight) collectionViewLayout:flowLayout]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.backgroundColor = [UIColor yhGrayColor]; [_collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID]; [_collectionView registerClass:[KBGoodHorzitolCollectionCell class] forCellWithReuseIdentifier:HorzitolCellId]; // [_collectionView registerClass:[KBSearchSpecialView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewSpecailHeader]; [_collectionView registerClass:[KBSearchNoDataView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderTop]; [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderMiddle]; if (@available(iOS 11.0, *)) { _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } return _collectionView; } - (SelectView *)selectedView { if (!_selectedView) { _selectedView = [[SelectView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, selectedViewHeight) withArr:self.filtrateArray]; UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, -KStatusBarHeight, SCREEN_WIDTH, KStatusBarHeight)]; bgView.backgroundColor = [UIColor whiteColor]; [_selectedView addSubview:bgView]; _selectedView.delegate = self; } return _selectedView; } - (NSMutableArray *)goodsArr { if (!_goodsArr) { _goodsArr = [NSMutableArray array]; } return _goodsArr; } @end