// // 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" @interface ASProductListViewController () @property (nonatomic, strong) NSArray *topLinkArr; @property (nonatomic, strong) ASProductListViewModel *vm; @property (nonatomic, strong) UICollectionView *collectV; @property (nonatomic, strong) UIView *bottomV; @property (nonatomic, strong) UIButton *moreBt; @property (nonatomic, strong) UIButton *searchNavBt; @end @implementation ASProductListViewController - (void)viewDidLoad { [super viewDidLoad]; [self loadSubVs]; [self loasActiveDataAndV]; self.vm = [ASProductListViewModel new]; [self.collectV reloadData]; } - (void)loadSubVs { [self.view addSubview:self.collectV]; [self.customNavBar addSubview:self.searchNavBt]; [self.searchNavBt mas_makeConstraints:^(MASConstraintMaker *make) { make.width.height.equalTo(@44); make.trailing.equalTo(self.customNavBar).offset(-10); make.centerY.equalTo(self.customNavBar); }]; [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.customNavBar.mas_bottom); make.leading.trailing.equalTo(self.view); make.bottom.equalTo(self.view); }]; } - (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 { } // MARK: - UICollectionViewDataSource - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { HomeFlashDealSubCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell" forIndexPath:indexPath]; if (self.vm.productList.count <= indexPath.row) { return cell; } ASProductBaseModel *m = self.vm.productList[indexPath.row]; cell.model = m; cell.contView.addCartBt.hidden = true; return cell; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 3; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == 0 || section == 1) { return 0; } return self.vm.productList.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (section == 0) { CGSize size = CGSizeZero; if (self.topLinkArr.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]; [v setData:self.topLinkArr tapBlock:^(ASHomeBannerModel * _Nullable m) { }]; return v; } if (indexPath.section == 1 && [kind isEqualToString:UICollectionElementKindSectionHeader]) { ASProductListMenuHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView" forIndexPath:indexPath]; [v setData:@"SORT BY" sortBlock:^{ } menuBlock:^{ }]; return v; } return nil; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 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: - actions - (void)search_navAction { // TODO: to searchVC } // MARK: - subvs -(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:[ASProductListActiveHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView"]; [collV registerClass:[ASProductListMenuHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView"]; _collectV = collV; } return _collectV; } -(UIView *)bottomV { if (!_bottomV) { UIView *v = [UIView baseV]; v.backgroundColor = Col_FFF; v.clipsToBounds = true; _bottomV = v; } return _bottomV; } - (UIButton *)moreBt { if (!_moreBt) { UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom]; [bt setTitle:@"VIEW MORE" forState:UIControlStateNormal]; [bt setTitleColor:Col_000 forState:UIControlStateNormal]; bt.titleLabel.font = [UIFont fontWithName:Rob_Regular size:16]; bt.layer.cornerRadius = 4; bt.layer.borderColor = Col_000.CGColor; bt.layer.borderWidth = 1; bt.layer.masksToBounds = true; _moreBt = bt; } return _moreBt; } - (UIButton *)searchNavBt { if (!_searchNavBt) { UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom]; [bt setImage:[UIImage imageNamed:@"nav_search"] forState:UIControlStateNormal]; [bt addTarget:self action:@selector(search_navAction) forControlEvents:UIControlEventTouchUpInside]; _searchNavBt = bt; } return _searchNavBt; } @end