// // KBNativeShopCarViewController.m // YouHuiProject // // Created by 小花 on 2018/11/2. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBNativeShopCarViewController.h" #import "JsonTool.h" #import "KBNativeShopCarHeader.h" #import "KBNativeShopCarCollectionCell.h" #import "DRChildGoodModel.h" #import "DRGoodDetailViewController.h" #import "KBNoDataMsgView.h" #import "DRNativeShopCarHeader.h" static NSString *shopCarHorizontalGoodCell = @"shopCarHorizontalGoodCell"; static NSString *shopCarHorizontalHeader = @"shopCarHorizontalHeader"; @interface KBNativeShopCarViewController () < UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout > { NSInteger _page; ActivityIndicatorView *_indicatorView; KBNativeShopCarHeader *_header; NSString *totalMoney; } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *dataArray; @property (nonatomic, strong) NSArray *goodsIdArr; @property (nonatomic, strong) KBNoDataMsgView *noDataView; @end @implementation KBNativeShopCarViewController - (void)viewDidLoad { [super viewDidLoad]; _page = 1; [self addWebShopCarCompleteNoti]; [self initSubViews]; } - (void)initSubViews { UIView *back =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)]; back.backgroundColor =[UIColor changeColor]; [self.view addSubview:back]; KBNativeShopCarHeader *header = [[KBNativeShopCarHeader alloc] initWithFrame:CGRectMake(FITSIZE(10), 0, SCREEN_WIDTH-FITSIZE(20), 27)]; _header = header; [self.view addSubview:header]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, header.bottom+FITSIZE(5), SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight-header.height-FITSIZE(5)) collectionViewLayout:flowLayout]; flowLayout.minimumLineSpacing = 5; flowLayout.minimumInteritemSpacing = 1; self.collectionView.showsVerticalScrollIndicator=NO; self.collectionView.showsHorizontalScrollIndicator=NO; self.collectionView.backgroundColor = [UIColor yhGrayColor]; self.collectionView.dataSource = self; self.collectionView.delegate = self; [self.collectionView registerClass:[KBNativeShopCarCollectionCell class] forCellWithReuseIdentifier:shopCarHorizontalGoodCell]; [self.collectionView registerClass:[DRNativeShopCarHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:shopCarHorizontalHeader]; [self.view addSubview:self.collectionView]; MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ _page = 1; [self.collectionView.mj_footer resetNoMoreData]; [self requestGoods:YES]; }]; refreshHeader.lastUpdatedTimeLabel.hidden=YES; [refreshHeader setTitle:@"数据正在刷新" forState:MJRefreshStateRefreshing]; self.collectionView.mj_header = refreshHeader; self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ _page++; [self requestGoods:NO]; }]; ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)]; _indicatorView = indicatorView; [self.collectionView addSubview:self.noDataView]; } /** 监听web购物车是否加载完成 */ - (void)addWebShopCarCompleteNoti { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadShopCarGoods:) name:ShopCarCompleteKey object:nil]; } - (void)loadShopCarGoods:(NSNotification *)noti { NSDictionary *dic = noti.userInfo; NSArray *goodArr = dic[@"goodsIdStr"]; self.goodsIdArr = goodArr; [self requestGoods:YES]; } /** 加载购物车数据 */ - (void)requestGoods:(BOOL)refresh { NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/cartGoods",BaseURL]; NSString *goodsStr = [JsonTool arrayToJSONString:self.goodsIdArr]; NSDictionary *para = @{@"goodsIdStr":goodsStr, @"page":@(_page) }; [DRHttp post:url params:para success:^(id json) { if (refresh) { [self.dataArray removeAllObjects]; } NSArray *list = json[@"data"]; totalMoney = json[@"saveTotalMoney"]; _header.totalMoney = totalMoney; for (NSDictionary *dict in list) { NSArray *arr = [NSArray yy_modelArrayWithClass:[DRChildGoodModel class] json:dict[@"goodsList"]]; [self.dataArray addObject:arr]; } if (list.count > 0) { [self.collectionView.mj_footer endRefreshing]; }else { [self.collectionView.mj_footer endRefreshingWithNoMoreData]; } if (_page == 1 && list.count == 0 ) { self.noDataView.hidden = NO; }else { self.noDataView.hidden = YES; } [self.collectionView.mj_header endRefreshing]; [self.collectionView reloadData]; [_indicatorView stopAnimating]; } failure:^(NSError *error) { [_indicatorView stopAnimating]; }]; } #pragma mark --- #pragma mark ============ UICollectionView Delegate && DataSource ========== - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { NSArray *array = self.dataArray[section]; return array.count; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return self.dataArray.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(SCREEN_WIDTH-20, 110); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(SCREEN_WIDTH-20, 35); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(SCREEN_WIDTH, 10); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBNativeShopCarCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:shopCarHorizontalGoodCell forIndexPath:indexPath]; DRChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row]; cell.model = model; if (indexPath.row == [self.dataArray[indexPath.section] count]-1) { // 设置圆角 UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = cell.bounds; maskLayer.path = maskPath.CGPath; cell.layer.mask = maskLayer; cell.backgroundColor = [UIColor whiteColor]; } return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ DRNativeShopCarHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:shopCarHorizontalHeader forIndexPath:indexPath]; header.layer.cornerRadius=4; header.layer.masksToBounds=YES; DRChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row]; header.model = model; return header; } #pragma mark -内边距 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 5, 0, 5); } #pragma mark -行间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 0; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { DRChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row]; DREventModel *evevtModel = [[DREventModel alloc] initWithOrigin:model.origin category_id:@"0" source:ShoppingAction]; DRGoodDetailViewController *detail = [[DRGoodDetailViewController alloc] init]; detail.eventModel = evevtModel; DetailRequestModel *requesModel = [[DetailRequestModel alloc] initWithChildModel:model]; detail.requestModel = requesModel; [self.navigationController pushViewController:detail animated:YES]; } - (NSMutableArray *)dataArray { if (!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray; } - (KBNoDataMsgView *)noDataView { if (!_noDataView) { _noDataView = [[KBNoDataMsgView alloc] initWithFrame:self.collectionView.bounds title:@"没有发现购物车里的商品"]; _noDataView.hidden = YES; _noDataView.refreshClick = ^{ [[NSNotificationCenter defaultCenter] postNotificationName:ChangeTaoBaoAuthor object:nil]; }; } return _noDataView; } @end