123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- //
- // 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 ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
- @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<NSString *,KWProductListFilterSubModel *> * _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
|