123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- //
- // 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<UIGestureRecognizerDelegate>)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
|