// // ASProductListViewController.m // Asteria // // Created by iOS on 2023/6/12. // #import "ASProductListViewController.h" #import "HomeFlashDealSubCollectCell.h" #import "ASProductListViewModel.h" #import "ASProductListMenuHeaderView.h" #import "ASProductListActiveHeaderView.h" #import "ASProductListImageCell.h" #import "ASProductListTypeDesCell.h" #import "ASMoreBtCell.h" #import "ASProductSortFilterView.h" #import "KWProductMenuFilterView.h" @interface ASProductListViewController () @property (nonatomic, assign) BOOL desMoreOpen; @property (nonatomic, assign) BOOL canShowMoreBt; @property (nonatomic, assign) NSInteger page; @property (nonatomic, strong) ASProductListViewModel *vm; @property (nonatomic, strong) UICollectionView *collectV; @property (nonatomic, strong) UIView *timerView; @property (nonatomic, strong) UILabel *timerLb; @property (nonatomic, strong) NSTimer * _Nullable timer; // sort 相关 @property (nonatomic,strong, nullable) ASProductSortFilterView *sortTypeV; @property (nonatomic,assign) NSInteger currentSortType; @property (nonatomic,strong, nullable) KWProductMenuFilterView *menuFilterV; @end @implementation ASProductListViewController - (void)viewDidLoad { [super viewDidLoad]; self.currentSortType = 2; self.desMoreOpen = false; [self loadSubVs]; [self loadTimerV]; [self loasActiveDataAndV]; self.vm = [ASProductListViewModel new]; [self.collectV reloadData]; [self.collectV.mj_header beginRefreshing]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.timer invalidate]; self.timer = nil; if (self.secondTime > 0) { self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:true]; [NSRunLoop.currentRunLoop addTimer:self.timer forMode:NSRunLoopCommonModes]; } } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [self.timer invalidate]; self.timer = nil; } - (void)dealloc { [self.timer invalidate]; self.timer = nil; } - (void)configFilter { if (_type && ![_type isEqualToString:@""]) { [self.menuFilterV configTypeId:self.type]; } } - (void)timerAction { self.secondTime -= 1; [self setTimerStr]; } - (void)setTimerStr { if (self.secondTime <= 0) { self.timerView.hidden = true; [self.timer invalidate]; self.timer = nil; // TODO:获取积分请求 [MBProgressHUD showHUDAddedTo:self.view animated:true]; __weak typeof(self) weakSelf = self; [self.vm getPoint:[NSString stringWithFormat:@"%ld", self.pointGetType] com:^(BOOL isSuc, NSString * _Nonnull msg) { [MBProgressHUD hideHUDForView:weakSelf.view animated:true]; if (!isSuc) { [weakSelf.view makeToast:msg]; } }]; return; } NSInteger minus = self.secondTime/60; NSInteger second = self.secondTime%60; NSString *str = [NSString stringWithFormat:@"%ld:%02ld", minus, second]; self.timerLb.text = str; } // MARK: sub mas - (void)loadTimerV { if (self.secondTime <= 0) { return; } [self.view addSubview:self.timerView]; [self.timerView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.view).offset(-180); make.trailing.equalTo(self.view).offset(-8); make.width.equalTo(@50); make.height.equalTo(@50); }]; [self setTimerStr]; } - (void)loadSubVs { [self setNavRightSearch:^{ // TODO: - to Search VC }]; [self.view addSubview:self.collectV]; [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.customNavBar.mas_bottom); make.leading.trailing.equalTo(self.view); make.bottom.equalTo(self.view); }]; self.sortTypeV.hidden = true; [self.view addSubview:self.menuFilterV]; [self.menuFilterV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.customNavBar.mas_bottom); make.left.right.bottom.equalTo(self.view); }]; self.menuFilterV.hidden = true; __weak typeof(self) weakSelf = self; self.collectV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ weakSelf.page = 1; [weakSelf.collectV.mj_footer resetNoMoreData]; [weakSelf getNetData]; }]; self.collectV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ weakSelf.page += 1; [weakSelf getNetData]; }]; } - (void)loasActiveDataAndV { NSMutableArray *tempArr = [NSMutableArray array]; for (int i=0;i < (arc4random()%6); i++) { ASHomeBannerModel *m = [ASHomeBannerModel demoModelWithType:i%3+1]; [tempArr addObject:m]; } // self.topLinkArr = tempArr; [self.collectV reloadData]; } // MARK: - delegate datasource - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section != 2) { return; } NSInteger tempOff = self.vm.cateModel.image.length > 0 ? 1 : 0; if (self.vm.productList.count <= indexPath.row-tempOff) { return; } ASProductBaseModel *m = self.vm.productList[indexPath.row-tempOff]; UIViewController *viewController = [[CTMediator sharedInstance] Goods_GoodsDetailsC:@{@"entity_id":m.Id}]; [self.navigationController pushViewController:viewController animated:YES]; } // MARK: - UICollectionViewDataSource - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { switch (indexPath.section) { case 3:{ if (self.canShowMoreBt) { if (indexPath.row == 0) { ASMoreBtCell *btCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASMoreBtCell" forIndexPath:indexPath]; return btCell; } } ASProductListTypeDesCell *desCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASProductListTypeDesCell" forIndexPath:indexPath]; [desCell setData:self.vm.cateModel.name des:self.vm.cateModel.meta_description moreStatus:self.desMoreOpen]; desCell.moreStatusChange = ^{ self.desMoreOpen = !self.desMoreOpen; [self.collectV reloadData]; }; return desCell; } default: { if (indexPath.row == 0 && self.vm.cateModel.image.length > 0) { ASProductListImageCell *c = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASProductListImageCell" forIndexPath:indexPath]; [c setImgStr:[NSString stringWithFormat:@"https://%@%@%@",HostPath, CategoryImgPath, self.vm.cateModel.image]]; return c; } HomeFlashDealSubCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell" forIndexPath:indexPath]; NSInteger tempOff = self.vm.cateModel.image.length > 0 ? 1 : 0; if (self.vm.productList.count <= indexPath.row-tempOff) { return cell; } ASProductBaseModel *m = self.vm.productList[indexPath.row-tempOff]; cell.model = m; cell.contView.addCartBt.hidden = false; return cell; } } } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 4; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == 0 || section == 1) { return 0; } if (section == 3) { NSInteger count = 0; if (self.canShowMoreBt) { count += 1; } if (self.vm.cateModel != nil && self.vm.cateModel.meta_description.length > 0) { count += 1; } return count; } return self.vm.productList.count + (self.vm.cateModel.image.length > 0 ? 1 : 0); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (section == 0) { CGSize size = CGSizeZero; if (self.vm.topLinks.count > 0) { size = CGSizeMake(KScreenWidth, 40); } return size; } if (section == 1) { return CGSizeMake(KScreenWidth, 60); } return CGSizeZero; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0 && [kind isEqualToString:UICollectionElementKindSectionHeader]) { ASProductListActiveHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView" forIndexPath:indexPath]; __weak typeof(self) weakSelf = self; [v setTopLinkData:self.vm.topLinks tapBlock:^(KWSearchHotKeyModel * _Nullable m) { NSLog(@"---toplink:-%@--", m.code); [weakSelf jumpWithModel:[m toJumpM]]; }]; return v; } if (indexPath.section == 1 && [kind isEqualToString:UICollectionElementKindSectionHeader]) { ASProductListMenuHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView" forIndexPath:indexPath]; __block typeof(self) weakself = self; NSString *titleStr = @"SORT BY"; if (self.currentSortType < self.sortTypeV.titles.count) { titleStr = self.sortTypeV.titles[self.currentSortType]; } [v setData:titleStr sortBlock:^{ // if (weakself.productArr.count > 0) { // [weakself.collectV scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:true]; // } [weakself showSortV]; } menuBlock:^{ [weakself showMenuV]; }]; // if (self.sortTypeV.titles.count > self.currentSortType) { // [v.sortBt setTitle:self.sortTypeV.titles[self.currentSortType] forState:UIControlStateNormal]; // } return v; } return nil; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 3:{ if (self.canShowMoreBt) { if (indexPath.row == 0) { return CGSizeMake(KScreenWidth-20, 20+36+50); } } if (!self.desMoreOpen) { return CGSizeMake(KScreenWidth-20, 35+60+24); } //设置行间距 NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle1 setLineSpacing:12]; CGFloat h = [self.vm.cateModel.meta_description boundingRectWithSize:CGSizeMake(KScreenWidth-10-35, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : paragraphStyle1} context:nil].size.height; return CGSizeMake(KScreenWidth-20, 35+60+(h > 24 ? h : 24)); } case 2: { if (indexPath.row == 0 && self.vm.cateModel.image.length > 0) { CGFloat w = KScreenWidth-20; CGFloat collH = w/355.0*118; return CGSizeMake(w, collH); } CGFloat marg = 10; CGFloat w = (KScreenWidth-3*marg)/2; CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg; return CGSizeMake(w, collH); } default:{ CGFloat marg = 10; CGFloat w = (KScreenWidth-3*marg)/2; CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg; return CGSizeMake(w, collH); } } } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 10; } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 10; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { if (section == 0 || section == 1) { return UIEdgeInsetsZero; } return UIEdgeInsetsMake(10, 10, 10, 10); } // MARK: - subvs - (UIView *)timerView { if (!_timerView) { UIView *v = [UIView baseV]; v.backgroundColor = UIColor.clearColor; UIImageView *imgV = [UIImageView baseImgV]; imgV.contentMode = UIViewContentModeScaleAspectFit; imgV.image = [UIImage imageNamed:@"product_list_timer"]; [v addSubview:imgV]; [v addSubview:self.timerLb]; _timerView = v; [imgV mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(_timerView); }]; [self.timerLb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(_timerView).offset(5); make.leading.equalTo(_timerView).offset(5); make.trailing.bottom.equalTo(_timerView).offset(-5); }]; } return _timerView; } - (UILabel *)timerLb { if (!_timerLb) { UILabel *lb = [UILabel baseLb]; lb.font = [UIFont fontWithName:Rob_BlackItalic size:12]; lb.textAlignment = NSTextAlignmentCenter; lb.textColor = Col_000; _timerLb = lb; } return _timerLb; } -(UICollectionView *)collectV { if (!_collectV) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0); layout.scrollDirection = UICollectionViewScrollDirectionVertical; CGFloat collH = (KScreenWidth-30)/2 + 126; layout.itemSize = CGSizeMake((KScreenWidth - 30)/2, collH); UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, collH) collectionViewLayout:layout]; collV.backgroundColor = [UIColor whiteColor]; collV.alwaysBounceVertical = true; collV.scrollEnabled = true; collV.delegate = self; collV.dataSource = self; collV.showsHorizontalScrollIndicator = false; collV.showsVerticalScrollIndicator = false; [collV registerClass:[HomeFlashDealSubCollectCell class] forCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell"]; [collV registerClass:[ASProductListImageCell class] forCellWithReuseIdentifier:@"ASProductListImageCell"]; [collV registerClass:[ASProductListTypeDesCell class] forCellWithReuseIdentifier:@"ASProductListTypeDesCell"]; [collV registerClass:[ASMoreBtCell class] forCellWithReuseIdentifier:@"ASMoreBtCell"]; [collV registerClass:[ASProductListActiveHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView"]; [collV registerClass:[ASProductListMenuHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView"]; _collectV = collV; } return _collectV; } - (KWProductMenuFilterView *)menuFilterV { if (!_menuFilterV) { KWProductMenuFilterView *v = [[KWProductMenuFilterView alloc] initWithFrame:self.view.bounds]; __block typeof(self) weakself = self; [v setResultFilterDic:^(NSDictionary * _Nonnull arr) { weakself.vm.selectDic = [NSMutableDictionary dictionaryWithDictionary:arr]; // [weakself.collectV.mj_header beginRefreshing]; }]; _menuFilterV = v; } return _menuFilterV; } - (ASProductSortFilterView *)sortTypeV { if (!_sortTypeV) { ASProductSortFilterView *v = [[ASProductSortFilterView alloc] initWithFrame:self.view.bounds]; __block typeof(self) weakself = self; [v setSelectIndex:^(NSInteger index) { weakself.currentSortType = index; [weakself.collectV reloadData]; [weakself.collectV.mj_header beginRefreshing]; }]; _sortTypeV = v; } return _sortTypeV; } -(void)showSortV { [self.sortTypeV showAnimate:![self.keyWord isEqualToString:@""]]; } -(void)showMenuV { [self.menuFilterV showAnimate:self.vm.selectDic]; } -(void)getNetData { [MBProgressHUD showHUDAddedTo:self.view animated:true]; __weak typeof(self) weakSelf = self; [self.vm getTopLinkList:^{ [weakSelf.collectV reloadData]; }]; [self.vm getProductListData:self.type page:self.page orderBy:self.currentSortType dir:@"DESC" com:^(BOOL hasNext, NSString * _Nonnull msg) { [MBProgressHUD hideHUDForView:weakSelf.view animated:true]; [weakSelf.collectV.mj_header endRefreshing]; [weakSelf.collectV.mj_footer endRefreshing]; weakSelf.canShowMoreBt = true; if (!hasNext) { weakSelf.canShowMoreBt = false; [weakSelf.collectV.mj_footer endRefreshingWithNoMoreData]; } [self.collectV reloadData]; if (!msg.isEmpty) { [weakSelf.view makeToast:msg]; } }]; } @end