// // KDPSupplyContentViewController.m // KuDianProject // // Created by admin on 2019/7/4. // Copyright © 2019 KDP. All rights reserved. // #import "KDPSupplyContentViewController.h" #import "KDPSupplyCommentViewCell.h" #import "KDPSupplyGoodCollectionViewCell.h" #import "KDPSupplyContentReusableView.h" #import "KDPScreenViewController.h" #import #import "KDPSupplyListViewController.h" #import "KDPNoticeViewController.h" @interface KDPSupplyContentViewController () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, assign) NSInteger page; @property (nonatomic, strong) NSMutableDictionary *mutabParams; @property (nonatomic, strong) NSMutableArray *dataSource; @property (nonatomic, strong) NSMutableArray *topGoodList; @property (nonatomic, assign) BOOL shouldHiddenScreen; @end @implementation KDPSupplyContentViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setUpUI]; self.shouldHiddenScreen = YES; if ([self.classModel.type isEqualToString:@"cate"]) { [self requestTopListWithGoodId:self.classModel.Id]; } } - (void)setUpUI{ self.navBar.hidden = YES; self.view.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.collectionView]; } #pragma mark 网络请求 - (void)requestGoodlistData{ NSString *goodlistUrl = [NSString stringWithFormat:@"%@api/goods/listGoods",KDURL]; [self.mutabParams setValue:@(self.page) forKey:@"page"]; [KDPNetworkRequestHTTP postURL:goodlistUrl params:self.mutabParams success:^(id _Nonnull json) { [LoadingView dismiss]; NSArray *array =[NSArray yy_modelArrayWithClass:[KDPGoodsModel class] json:json[@"data"]]; [self.collectionView.mj_footer endRefreshing]; [self.collectionView.mj_header endRefreshing]; if (self.page == 1) { [self.dataSource removeAllObjects]; } if (array.count > 0) { [self.dataSource addObjectsFromArray:array]; } if (array.count == 0) { [self.collectionView.mj_footer endRefreshingWithNoMoreData]; } [self.collectionView reloadData]; } failure:^(NSError * _Nonnull error) { [self.collectionView.mj_footer endRefreshing]; [self.collectionView.mj_header endRefreshing]; [LoadingView dismiss]; }]; } - (void)requestTopListWithGoodId:(NSString *)Id{ [LoadingView showInView:self.view]; NSString *childListUrl = [NSString stringWithFormat:@"%@api/goods/childList",KDURL]; [KDPNetworkRequestHTTP postURL:childListUrl params:@{@"pid":Id} success:^(id _Nonnull json) { [self.topGoodList removeAllObjects]; NSArray *array =[NSArray yy_modelArrayWithClass:[KDPClassModel class] json:json[@"list"]]; [self.topGoodList addObjectsFromArray:array]; [LoadingView dismiss]; [self.collectionView reloadData]; } failure:^(NSError * _Nonnull error) { [LoadingView dismiss]; }]; } #pragma mark - lazy load - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumLineSpacing = 0; layout.sectionHeadersPinToVisibleBounds = YES; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KDTabBarHeight-KDStatusHeight-88) collectionViewLayout:layout]; _collectionView.backgroundColor = [UIColor clearColor]; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.emptyDataSetSource = self; _collectionView.emptyDataSetDelegate = self; [_collectionView registerClass:[KDPSupplyCommentViewCell class] forCellWithReuseIdentifier:NSStringFromClass([KDPSupplyCommentViewCell class])]; [_collectionView registerClass:[KDPSupplyGoodCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([KDPSupplyGoodCollectionViewCell class])]; [_collectionView registerClass:[KDPSupplyContentReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class])]; _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ self.page = 1; [self requestGoodlistData]; }]; _collectionView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{ self.page ++; [self requestGoodlistData]; }]; [_collectionView.mj_header beginRefreshing]; } return _collectionView; } - (NSMutableDictionary *)mutabParams{ if (!_mutabParams) { _mutabParams = [NSMutableDictionary dictionaryWithDictionary:@{@"page":@(self.page),@"id":self.classModel.Id,@"type":self.classModel.type,@"start_price":@"",@"end_price":@"",@"start_tk_rate":@""}]; } return _mutabParams; } - (NSMutableArray *)dataSource{ if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } - (NSMutableArray *)topGoodList{ if (!_topGoodList) { _topGoodList = [NSMutableArray array]; } return _topGoodList; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { KDPSupplyCommentViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KDPSupplyCommentViewCell class]) forIndexPath:indexPath]; cell.delegate = self; cell.dataArray = self.topGoodList; return cell; } if (indexPath.section == 1) { KDPSupplyGoodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KDPSupplyGoodCollectionViewCell class]) forIndexPath:indexPath]; [cell configCellWithModel:self.dataSource[indexPath.row] indexPath:indexPath]; return cell; } return nil; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 2; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ KDPGoodDetailVC *detailVC = [[KDPGoodDetailVC alloc] init]; detailVC.model = self.dataSource[indexPath.row]; [self.navigationController pushViewController:detailVC animated:YES]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ if (section == 0) { if (!self.topGoodList.count) { return 0; } return 1; } if (section == 1) { return self.dataSource.count; } return 0; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { if (self.topGoodList.count > 4) { return CGSizeMake(SCREEN_WIDTH-20, 200); } else if(self.topGoodList.count > 0 && self.topGoodList.count < 4) { return CGSizeMake(SCREEN_WIDTH-20, 110); } else{ return CGSizeMake(SCREEN_WIDTH, 0.1f); } } if (indexPath.section == 1) { return CGSizeMake((SCREEN_WIDTH-25)/2, 284); } return CGSizeMake(SCREEN_WIDTH, 0.1f); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ if (section == 1) { return CGSizeMake(SCREEN_WIDTH, 33); } return CGSizeZero; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ if (section == 0) { if (!self.topGoodList.count) { return UIEdgeInsetsMake(0, 0, 5, 0); } return UIEdgeInsetsMake(10, 10, 10, 10); } if (section == 1) { return UIEdgeInsetsMake(7, 10, 0, 10); } return UIEdgeInsetsZero; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ if (section == 1) { return 5; } return 0; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ if (section == 1) { return 5; } return 0; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ KDPSupplyContentReusableView *reusAbleView; if (kind == UICollectionElementKindSectionHeader) { if (indexPath.section == 1) { reusAbleView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class]) forIndexPath:indexPath]; WeakSelf(weakSelf); reusAbleView.hidden = NO; if (self.dataSource.count ==0 && self.shouldHiddenScreen == YES) { reusAbleView.hidden = YES; } __weak KDPSupplyContentReusableView *weakReusAbleView = reusAbleView; reusAbleView.changeBlock = ^(BOOL isPrice) { weakSelf.shouldHiddenScreen = NO; KDPScreenViewController *screenVC = [[KDPScreenViewController alloc] initWithPrice:isPrice]; screenVC.params = weakSelf.mutabParams; screenVC.view.frame = CGRectMake(91, 0, SCREEN_WIDTH-91, SCREEN_HEIGHT); screenVC.resetBlock = ^(BOOL isPrice) { if (isPrice == YES) { [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor colorWithHex:0x333333] text:@"价格筛选"]; [self.mutabParams setValue:@"" forKey:@"start_price"]; [self.mutabParams setValue:@"" forKey:@"end_price"]; } else{ [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor colorWithHex:0x333333] text:@"利润筛选"]; [self.mutabParams setValue:@"" forKey:@"start_tk_rate"]; } [weakSelf.collectionView.mj_header beginRefreshing]; }; screenVC.profitBlock = ^(NSString *baseScale) { if (!baseScale.length) { [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor colorWithHex:0x333333] text:[NSString stringWithFormat:@"利润筛选"]]; [self.mutabParams setValue:@"" forKey:@"start_tk_rate"]; } else{ [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor baseColor] text:[NSString stringWithFormat:@"佣金%@%%以上",baseScale]]; [self.mutabParams setValue:baseScale forKey:@"start_tk_rate"]; } [weakSelf.collectionView.mj_header beginRefreshing]; }; screenVC.priceBlock = ^(NSString *minPrice, NSString *maxPrice) { if (minPrice.length != 0 || maxPrice.length != 0) { if (minPrice.length == 0) { minPrice = @"0"; } NSString *price=[NSString stringWithFormat:@"价格%@-%@元",minPrice,maxPrice]; if (maxPrice.integerValue == 0 || [maxPrice intValue] <= [minPrice intValue]) { maxPrice = @""; price=[NSString stringWithFormat:@"价格%@元以上",minPrice]; } [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor baseColor] text:price]; } else{ [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor colorWithHex:0x333333] text:@"价格筛选"]; } [weakSelf.mutabParams setValue:minPrice forKey:@"start_price"]; [weakSelf.mutabParams setValue:maxPrice forKey:@"end_price"]; [weakSelf.collectionView.mj_header beginRefreshing]; }; CWLateralSlideConfiguration *conf = [CWLateralSlideConfiguration configurationWithDistance:0 maskAlpha:0.7 scaleY:1 direction:CWDrawerTransitionFromRight backImage:nil]; [weakSelf cw_showDrawerViewController:screenVC animationType:0 configuration:conf]; }; return reusAbleView; } } return nil; } #pragma DZNEmptyDataSetSource and delegate - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{ return [UIImage imageNamed:@"no_order"]; } - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{ return YES; } - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{ return 50; } - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{ return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}]; } - (void)commentCollectionView:(UICollectionView *)collectionView didSelectIndex:(NSInteger )index{ KDPSupplyListViewController *listVC = [[KDPSupplyListViewController alloc] init]; listVC.mode = self.topGoodList[index]; [self.navigationController pushViewController:listVC animated:YES]; } @end