// // KBMyCollectionViewController.m // YouHuiProject // // Created by 小花 on 2018/5/3. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBMyCollectionViewController.h" #import "JDragonTypeButtonView.h" #import "KBGoodHorizontalCollectionCell.h" #import "KBGoodCollectionCell.h" #import "KBFindRequestViewModel.h" #import "KBSearchNoDataView.h" #import "KBCollectionMainReusableView.h" #import "KBGoodDetailViewController.h" #import "KBCollectionNoDataReusableView.h" #import "KBCollectionUnloginReusableView.h" #import "KBLoginViewController.h" #import "KBCollectionNoDataReusableView.h" #import "KBGoodDetailRequestViewModel.h" static NSString *KHorizontalCellId = @"KBGoodHorizontalCollectionCell"; static NSString *KVerticalCellId = @"KBGoodCollectionCell"; static NSString *KSectionHeaderId = @"SectionHeaderId"; static NSString *KNoDataViewId = @"NoDataViewId"; static NSString *KUnloginId = @"UnloginId "; @interface KBMyCollectionViewController () < JDragonTypeButtonActionDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout > { NSInteger _youLikePage; NSInteger _collectionPage; NSInteger _page; } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *collectionDataArr; @property (nonatomic, strong) NSMutableArray *youLikeArr; @property (nonatomic, strong) NSArray *typeArr; @end @implementation KBMyCollectionViewController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; } - (void)viewDidLoad { [super viewDidLoad]; [self configNavigationBar]; [self configCollectionView]; [self requestData]; } - (void)configNavigationBar { _page = 1; [self.navigationBar setNavTitle:@"我的收藏"]; self.navigationBar.backgroundColor = [UIColor changeColor]; self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor]; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)configCollectionView { _youLikePage = 1; _collectionPage = 1; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout]; flowLayout.minimumInteritemSpacing = 2; [self.collectionView registerClass:[KBGoodHorizontalCollectionCell class] forCellWithReuseIdentifier:KHorizontalCellId]; [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:KVerticalCellId]; [self.collectionView registerClass:[KBCollectionMainReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeaderId]; [self.collectionView registerClass:[KBCollectionNoDataReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KNoDataViewId]; [self.collectionView registerClass:[KBCollectionUnloginReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KUnloginId]; self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6]; self.collectionView.showsVerticalScrollIndicator = NO; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ [self loadMoreData]; }]; [self.collectionView.mj_footer beginRefreshing]; [self.view addSubview:self.collectionView]; } - (void)requestData { if ([AccountTool isLogin]) { [self requestMyCollectionData]; [self requestYouLikeData]; }else { [self requestYouLikeData]; } } - (void)requestMyCollectionData { NSDictionary *para = @{@"type":self.typeArr[self.selectedIndex], @"page":@(_collectionPage) }; NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/favoriteslist",BaseURL]; [KBHttp get:url params:para success:^(id json) { [self.collectionDataArr removeAllObjects]; NSArray *list = [NSArray yy_modelArrayWithClass:[KBCollectionModel class] json:json[@"data"]]; [self.collectionDataArr addObjectsFromArray:list]; [self.collectionView reloadData]; } failure:^(NSError *error) { }]; } - (void)requestYouLikeData { [KBFindRequestViewModel requestGuessYouLikeParamPage:_youLikePage success:^(NSArray *array) { [self.youLikeArr addObjectsFromArray:array]; [self.collectionView reloadData]; [self noMoreDataWithArray:array]; [self endRefreshing]; } failure:^(NSError *error) { [MBProgressHUD showMessage:@"网络错误"]; [SVProgressHUD dismiss]; [self endRefreshing]; }]; } - (void)noMoreDataWithArray:(NSArray *)array { if (array.count <= 0) { MJRefreshAutoNormalFooter *foot = (MJRefreshAutoNormalFooter *)self.collectionView.mj_footer; [foot setTitle:@"到底啦" forState:MJRefreshStateIdle]; } } - (void)loadMoreData { _youLikePage++; [self requestYouLikeData]; } - (void)endRefreshing { [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; } /** 取消收藏 */ - (void)cancelCollectionGoodWith:(NSIndexPath *)indexPath { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要取消收藏该商品?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *sure = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/delFavorites",BaseURL]; KBCollectionModel *model = self.collectionDataArr[indexPath.row]; NSDictionary *para = @{@"id":model.Id}; [KBHttp get:url params:para success:^(id json) { [self.collectionDataArr removeObjectAtIndex:indexPath.row]; // [self.collectionView deleteItemsAtIndexPaths:@[indexPath]]; [self.collectionView reloadData]; } failure:^(NSError *error) { }]; }]; [alert addAction:cancel]; [alert addAction:sure]; [self presentViewController:alert animated:YES completion:nil]; } #pragma mark ============ UICollectionView Delegate && DataSource ========== - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { switch (section) { case 0: return self.collectionDataArr.count; break; case 1: return self.youLikeArr.count; default: return 0; break; } } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return CGSizeMake(SCREEN_WIDTH, 120); }else { CGFloat width = (SCREEN_WIDTH-5)/2; CGFloat height = width + 102; return CGSizeMake(width, height); } } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { if (section == 0) { return 1; }else { return 5; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (section == 0) { if (self.collectionDataArr.count == 0 || ![AccountTool isLogin]) { return CGSizeMake(kScreenWidth, FITSIZE(264)); }else { return CGSizeZero; } }else { return CGSizeMake(SCREEN_WIDTH, 40); } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(0, 0); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { KBCollectionModel *model = self.collectionDataArr[indexPath.row]; KBGoodHorizontalCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KHorizontalCellId forIndexPath:indexPath]; cell.collectionModel = model; cell.cancelBlock = ^{ [self cancelCollectionGoodWith:indexPath]; }; return cell; }else { KBChildGoodModel *model = self.youLikeArr[indexPath.row]; KBGoodCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KVerticalCellId forIndexPath:indexPath]; cell.model = model; return cell; } } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { if (![AccountTool isLogin]) { KBCollectionUnloginReusableView *unlog = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KUnloginId forIndexPath:indexPath]; unlog.loginClick = ^{ KBLoginViewController *login = [[KBLoginViewController alloc] init]; [self.parentViewController presentViewController:login animated:YES completion:nil]; }; return unlog; }else if (self.collectionDataArr.count <= 0){ KBCollectionNoDataReusableView *noData = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KNoDataViewId forIndexPath:indexPath]; noData.refreshClick = ^{ [self requestMyCollectionData]; }; return noData; }else { return nil; } }else { KBCollectionMainReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeaderId forIndexPath:indexPath]; return header; } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init]; if (indexPath.section == 0) { KBCollectionModel *model = self.collectionDataArr[indexPath.row]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id is_coupon:model.is_coupon coupon_price:model.coupon_price price:model.price discount_price:model.discount_price commission_rate:model.commission_rate coupon_start_time:model.coupon_start_time coupon_end_time:model.coupon_end_time]; detail.requestModel = requestModel; KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:@"0" category_id:@"0" source:collectAction]; detail.eventModel = evevtModel; }else { KBChildGoodModel *model = self.youLikeArr[indexPath.row]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detail.requestModel = requestModel; KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:@"0" source:collectLikeAction]; detail.eventModel = evevtModel; } [self.navigationController pushViewController:detail animated:YES]; } #pragma mark ---- layzer ---- - (NSMutableArray *)collectionDataArr { if (!_collectionDataArr) { _collectionDataArr = [NSMutableArray array]; } return _collectionDataArr; } - (NSArray *)typeArr { if (!_typeArr) { _typeArr = @[@"all",@"no_expired",@"expired"]; } return _typeArr; } - (NSMutableArray *)youLikeArr { if (!_youLikeArr) { _youLikeArr = [NSMutableArray array]; } return _youLikeArr; } - (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