// // HCClassifyListController.m // hc // // Created by hc on 2018/5/9. // Copyright © 2018年 hc. All rights reserved. // #import "HCClassifyListController.h" #import "HCGoodCollectionCell.h" #import "HCGoodDetailViewController.h" #import "HCTypeButtonHeader.h" #import "HCGoodDetailViewController.h" #import "HCOtherFilterView.h" #import "HCClassifyTagsView.h" #import "HCClassifyTagsModel.h" static NSString *cellID = @"HCGoodCollectionCell"; #define KHeaderBarHeight 40 @interface HCClassifyListController () < UICollectionViewDelegate, UICollectionViewDataSource, ClassifySelectViewDelegate, UICollectionViewDelegateFlowLayout > { NSInteger _page; NSInteger _type; NSInteger _is_has_coupon; } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *goodsArr; @property (nonatomic, strong) NSArray *tagsArr; @property (nonatomic, strong) HCOtherFilterView *filterView; @property (nonatomic, strong) HCClassifyTagsView *tagsView; @end @implementation HCClassifyListController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; } - (void)viewDidLoad { [super viewDidLoad]; [self initHUD]; [self configParam]; [self configNavigationBar]; [self configCollectionView]; [self loadCategoryGoodsList:NO switchButton:nil]; [self loadTagsData]; } #pragma mark - HUD - (void)initHUD { [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom]; [SVProgressHUD setForegroundColor:[UIColor YHColorWithHex:0xff2420]]; [SVProgressHUD setBackgroundColor:[UIColor YHColorWithHex:0xf5f4f4]]; [SVProgressHUD show]; } - (void)configParam { _page = 1; _type = 1; //默认请求推荐的数据 _is_has_coupon = 0; } - (void)configNavigationBar { self.view.backgroundColor = [UIColor whiteColor]; [self.navigationBar setNavTitle:self.name]; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; [self.navigationBar setShowNavigationBarBottomLine:YES]; } - (void)configCollectionView { self.filterView = [[HCOtherFilterView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, KHeaderBarHeight) withArr:nil]; self.filterView.delegate = self; [self.view addSubview:self.filterView]; self.tagsView = [[HCClassifyTagsView alloc] initWithFrame:CGRectMake(0, self.filterView.bottom, SCREEN_WIDTH, 40)]; self.tagsView.backgroundColor = [UIColor yhGrayColor]; __weak typeof(self) weakSelf = self; self.tagsView.selectBlock = ^(NSInteger index) { HCClassifyTagsModel *model = weakSelf.tagsArr[index]; weakSelf.cate_id = model.Id; _page = 1; [weakSelf loadCategoryGoodsList:YES switchButton:nil]; }; [self.view addSubview:self.tagsView]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; flowLayout.minimumLineSpacing = 5; flowLayout.minimumInteritemSpacing = 0; flowLayout.headerReferenceSize = CGSizeMake(0, 0); self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight+KHeaderBarHeight+self.tagsView.height, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-self.tagsView.height) collectionViewLayout:flowLayout]; [self.collectionView registerClass:[HCGoodCollectionCell class] forCellWithReuseIdentifier:cellID]; self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6]; self.collectionView.showsVerticalScrollIndicator = NO; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ [self loadMoreData]; }]; [self.view addSubview: self.collectionView]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } #pragma mark ---- #pragma mark ClassifySelectViewDelegate - (void)ClassifyselectTopButton:(HCOtherFilterView *)selectView withIndex:(NSInteger)index withButtonType:(JCButtonClickType)type button:(UIButton *)button { [SVProgressHUD show]; //价格 if (index == 2&& type) { switch (type) { case JCButtonClickTypeNormal: //正常价格 { NSLog(@"上边按钮的正常价格"); } break; case JCButtonClickTypeUp: //价格升序排列 { NSLog(@"上边按钮的价格升序排列"); _type = 3; } break; case JCButtonClickTypeDown: //价格降序排列 { NSLog(@"上边按钮的价格降序排列"); _type = 4; } break; default: break; } }else if (index == 0){//综合 NSLog(@"上边按钮的综合"); _type = 1; }else if (index == 1){//销量 NSLog(@"上边按钮的销量"); _type = 2; }else{//筛选 _is_has_coupon = button.selected ? 0 : 1; [self loadCategoryGoodsList:YES switchButton:button]; return; NSLog(@"上边有券按钮"); } [self loadCategoryGoodsList:YES switchButton:nil]; } /** 上拉加载 */ - (void)loadMoreData { _page++; [self loadCategoryGoodsList:NO switchButton:nil]; } /** 加载下部商品列表 */ - (void)loadCategoryGoodsList:(BOOL)refresh switchButton:(UIButton *)switchButton{ if (![self.collectionView.mj_footer isRefreshing]) { [SVProgressHUD show]; } NSDictionary *para = @{@"page":@(_page), @"category_id":self.firstCategoryId, @"sort":@(_type), @"sub_category_id":self.cate_id, @"stype":@"0", @"is_has_coupon":@(_is_has_coupon) }; [HCHttp post:StocklistByCategoryId params:para success:^(id json) { if (refresh) [self.goodsArr removeAllObjects]; NSArray *list = [NSArray yy_modelArrayWithClass:[HCChildGoodModel class] json:json[@"data"]]; [self.goodsArr addObjectsFromArray:list]; [self.collectionView reloadData]; if (list.count > 0) { [self.collectionView.mj_footer endRefreshing]; }else { [self.collectionView.mj_footer endRefreshingWithNoMoreData]; } [SVProgressHUD dismiss]; switchButton.selected = !switchButton.selected; } failure:^(NSError *error) { [self.collectionView.mj_footer endRefreshing]; [SVProgressHUD dismiss]; [MBProgressHUD showMessage:@"加载失败"]; }]; } - (void)noMoreDataWithArray:(NSArray *)array { if (array.count > 0) { self.collectionView.footRefreshState = MJFooterRefreshStateNoMore; }else { self.collectionView.footRefreshState = MJFooterRefreshStateLoadMore; } } - (void)loadTagsData { NSString *url = [NSString stringWithFormat:@"%@/api/v2/category/childlist",BaseURL]; NSString *cateId = self.cate_id==nil?@"":self.cate_id; NSDictionary *para = @{@"category_id":cateId}; [HCHttp get:url params:para success:^(id json) { self.tagsArr = [NSArray yy_modelArrayWithClass:[HCClassifyTagsModel class] json:json[@"data"]]; NSMutableArray *tags = [NSMutableArray array]; for (HCClassifyTagsModel *model in self.tagsArr) { [tags addObject:model.name]; } [self.tagsView setTagsData:tags]; if (self.tagsArr.count == 0) { self.collectionView.y = NavBarHeight+KHeaderBarHeight; self.collectionView.height = SCREEN_HEIGHT-NavBarHeight-KHeaderBarHeight; self.tagsView.height = 0; } } failure:^(NSError *error) { }]; } #pragma mark - scrollView - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势 UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer; //获取到拖拽的速度 >0 向下拖动 <0 向上拖动 CGFloat velocity = [pan velocityInView:scrollView].y; if (velocity <- 10) { //向上拖动,隐藏导航栏 if (self.navigationBar.y == -NavBarHeight) { return; } [UIView animateWithDuration:0.15 animations:^{ self.navigationBar.y = -NavBarHeight; self.filterView.y = KStatusBarHeight; self.collectionView.y = KStatusBarHeight + KHeaderBarHeight+self.tagsView.height; self.collectionView.height = SCREEN_HEIGHT-KHeaderBarHeight-KStatusBarHeight-self.tagsView.height; self.tagsView.y = KStatusBarHeight + KHeaderBarHeight; }]; }else if (velocity > 10) { //向下拖动,显示导航栏 if (self.navigationBar.y == 0) { return; } [UIView animateWithDuration:0.15 animations:^{ self.navigationBar.y = 0; self.filterView.y = NavBarHeight; self.tagsView.y = NavBarHeight + KHeaderBarHeight; self.collectionView.y = NavBarHeight + KHeaderBarHeight+self.tagsView.height; }]; }else if(velocity == 0){ //停止拖拽 } } #pragma mark ============ UICollectionView Delegate && DataSource ========== - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.goodsArr.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { HCChildGoodModel *model = self.goodsArr[indexPath.row]; CGFloat width = (SCREEN_WIDTH-5)/2; CGFloat height = width + 102; return CGSizeMake(width, height); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(0, 0); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(0, 0); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { HCGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; HCChildGoodModel *model = self.goodsArr[indexPath.row]; cell.model = model; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { HCChildGoodModel *model = self.goodsArr[indexPath.row]; //详情 HCGoodDetailViewController *detailVC = [[HCGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detailVC.requestModel = requestModel; if (self.isOtherPage) { HCEventModel *evevtModel = [[HCEventModel alloc] initWithOrigin:model.origin category_id:self.cate_id source:HomeClassifyAction]; detailVC.eventModel = evevtModel; }else { HCEventModel *evevtModel = [[HCEventModel alloc] initWithOrigin:model.origin category_id:self.cate_id source:ListClassifyAction]; detailVC.eventModel = evevtModel; } [self.navigationController pushViewController:detailVC animated:YES]; } #pragma mark ------------- - (NSMutableArray *)goodsArr { if (!_goodsArr) { _goodsArr = [NSMutableArray array]; } return _goodsArr; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end