// // LDFindSearchResultViewController.m // YouHuiProject // // Created by xiaoxi on 2018/2/1. // Copyright © 2018年 kuxuan. All rights reserved. // #import "LDFindSearchResultViewController.h" #import "LDGoodCollectionCell.h" #import "LDChildGoodModel.h" #import "LDGoodListViewController.h" #import "LDFindRequestViewModel.h" #import "LDCollectionView.h" #import "LDGoodHorzitolCollectionCell.h" #import "LDSearchNoDataView.h" #import "LDSearchTitleView.h" #import "SelectView.h" #import "AdPopModel.h" #import "LDSearchSpecialView.h" #import "AdJumpManager.h" #import "LDTodayHorizontalGoodCell.h" #import "LDSectionAllGoodView.h" static NSString *const cellID = @"LDGoodCollectionCell"; static NSString *const HorzitolCellId = @"LDGoodHorzitolCollectionCell"; static NSString *const collectionViewHeaderTop = @"top"; static NSString *const collectionViewHeaderMiddle = @"middle"; static NSString *const collectionViewHeaderAllgood = @"allgood"; static NSString *const collectionViewSpecailHeader = @"specailHeader"; //static BOOL isNoData = NO; static CGFloat selectedViewHeight = 80.f; typedef enum : NSUInteger { ResultCellTypeVertical, ResultCellTypeHorzitol, } ResultCellType; @interface LDFindSearchResultViewController () < 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; //大淘客的数据 @property (nonatomic, strong) NSMutableArray *yorkerArray; @end @implementation LDFindSearchResultViewController - (void)viewDidLoad { [super viewDidLoad]; [self requesFiltrateParam]; [self configDefauleParamKey]; [self initNavBar]; [self initSubviews]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; self.navigationItem.hidesBackButton = YES; 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 YHColorWithHex:0xf5f4f4]; if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) { self.automaticallyAdjustsScrollViewInsets = YES; } UIView *statusBarBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, KStatusBarHeight)]; statusBarBgView.backgroundColor = [UIColor whiteColor]; // [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 = ResultCellTypeHorzitol; } rightBtn.selected = self.cellType==ResultCellTypeHorzitol?YES:NO; self.navigationController.interactivePopGestureRecognizer.delegate = (id)self; LDSearchTitleView *titleView = [[LDSearchTitleView 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]; [LDHttp 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{ 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]; [LDHttp post:urlString params:param success:^(id json) { [self.view addSubview:self.selectedView]; NSArray *arr = [NSArray yy_modelArrayWithClass:[LDChildGoodModel class] json:json[@"data"]]; NSArray *haoArr = [NSArray yy_modelArrayWithClass:[LDChildGoodModel class] json:json[@"haoLists"]]; self.special_search = json[@"special"]; self.adModel = [AdPopModel yy_modelWithJSON:json[@"special"]]; if (refresh) { [self.goodsArr removeAllObjects]; [self.collectionView reloadData]; [self.yorkerArray removeAllObjects]; } //首先判断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]; if (self.yorkerArray.count ==0 ) { [self.yorkerArray addObjectsFromArray:haoArr]; } [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]; } } } [self.collectionView reloadData]; [LoadingView dismiss]; switchButton.selected = !switchButton.selected; [self.collectionView.mj_header endRefreshing]; } failure:^(NSError *error) { [self.collectionView.mj_footer endRefreshing]; [LoadingView dismiss]; [MBProgressHUD showMessage:@"该商品未参加优惠券和返佣推广活动,建议亲换个店铺试试"]; }]; } - (void)requestGuessYouLike { [LDFindRequestViewModel 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.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 3; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == 1 ) { return self.yorkerArray.count; } if (section == 2) { return self.goodsArr.count; } return 0; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { LDChildGoodModel *model =[[LDChildGoodModel alloc]init]; if (indexPath.section == 1) { model = self.yorkerArray[indexPath.item]; }else if (indexPath.section == 2){ model =self.goodsArr[indexPath.item]; } if (self.cellType == ResultCellTypeVertical) { LDGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; cell.model = model; return cell; }else { LDTodayHorizontalGoodCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:HorzitolCellId forIndexPath:indexPath]; cell.model = model; return cell; } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { LDChildGoodModel *model =[[LDChildGoodModel alloc]init]; if (indexPath.section == 1) { model = self.yorkerArray[indexPath.item]; }else if (indexPath.section == 2){ model =self.goodsArr[indexPath.item]; } //详情 LDGoodDetailViewController *detailVC = [[LDGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detailVC.requestModel = requestModel; NSString *action; if (indexPath.section == 1) { action = HaoListsSearchAction; }else { action = searchAction; } LDEventModel *evevtModel = [[LDEventModel alloc] initWithOrigin:model.origin category_id:@"0" source:action]; 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 == 2) { if (self.yorkerArray.count !=0) { LDSectionAllGoodView *sectionview =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderAllgood forIndexPath:indexPath]; return sectionview; } UICollectionReusableView *guessYouLikeView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderMiddle forIndexPath:indexPath]; view = guessYouLikeView; return view; } if (indexPath.section == 0) { if (self.special_search != nil) { LDSearchSpecialView *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 { LDSearchNoDataView *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 { LDChildGoodModel *model = self.goodsArr[indexPath.row]; if (self.cellType == ResultCellTypeVertical) { CGFloat width = (SCREEN_WIDTH-5)/2; CGFloat height = width + 135; if (![AccountTool isLogin] && ![model.red_active_status boolValue]) { height = width +112; } CGSize itemSize = CGSizeMake(width, height); return itemSize; }else { return CGSizeMake(SCREEN_WIDTH, 147); } } - (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 (section == 2 ) { if (self.yorkerArray.count == 0) { return CGSizeMake(0, 0); } return CGSizeMake(kScreenWidth, FITSIZE(52)); } 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]; } //排序选择 -(void)selectItme:(SelectView *)selectView sortDic:(NSDictionary *)selectDic { _type = selectDic[@"type"]; [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 = [[LDCollectionView alloc] initWithFrame:CGRectMake(0, selectedViewHeight, kScreenWidth, kScreenHeight-NavBarHeight-selectedViewHeight) collectionViewLayout:flowLayout]; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerClass:[LDGoodCollectionCell class] forCellWithReuseIdentifier:cellID]; [_collectionView registerClass:[LDTodayHorizontalGoodCell class] forCellWithReuseIdentifier:HorzitolCellId]; [_collectionView registerClass:[LDSearchSpecialView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewSpecailHeader]; [_collectionView registerClass:[LDSearchNoDataView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderTop]; [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderMiddle]; [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderMiddle]; [_collectionView registerClass:[LDSectionAllGoodView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeaderAllgood]; MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{ page = 1; [self requestVoucherFindIsRefresh:YES switchButton:nil]; }]; header.lastUpdatedTimeLabel.hidden = YES; header.stateLabel.hidden = YES; //正在刷新的图片 NSArray *imgArr = @[[UIImage imageNamed:@"ant1"],[UIImage imageNamed:@"ant2"]]; [header setImages:imgArr duration:0.3 forState:MJRefreshStateRefreshing]; [header setImages:@[[UIImage imageNamed:@"ant1"]] duration:1 forState:MJRefreshStateWillRefresh]; [header setImages:@[[UIImage imageNamed:@"ant1"]] duration:1 forState:MJRefreshStatePulling]; _collectionView.mj_header = header; 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; } - (NSMutableArray *)yorkerArray { if (!_yorkerArray) { _yorkerArray = [NSMutableArray array]; } return _yorkerArray; } @end