// // ASHomeViewController.m // Asteria // // Created by iOS on 2023/6/5. // #import "ASHomeViewController.h" #import "ASHomeBannerCell.h" #import "ASCategaryListCell.h" #import "ASHomeTipCell.h" #import "ASHomeBestSellCell.h" #import "ASHomeNewInCellTableViewCell.h" #import "ASHomeImgCell.h" #import "ASHomeFlashDealCell.h" #import "ASHomeLookingCell.h" #import "ASHomeActiveView.h" #import "ASProductListViewController.h" #import "ASProductListViewModel.h" #import "ASHomeViewModel.h" @interface ASHomeViewController () @property (nonatomic, strong) ASProductListViewModel *topVm; @property (nonatomic, strong) ASHomeViewModel *vm; @property (nonatomic, strong) UITableView *tableV; @property (nonatomic, strong, nullable) ASHomeActiveView *activeV; @end @implementation ASHomeViewController - (void)viewDidLoad { [super viewDidLoad]; self.vm = [ASHomeViewModel new]; self.topVm = [ASProductListViewModel new]; __block typeof(self) wSelf = self; [self ucHomeStyle:^{ // TODO: 跳转搜索模块 }]; [self addSubV]; [self.tableV.mj_header beginRefreshing]; [self updateTopLinkData]; } - (void)getTopLinkData { __weak typeof(self) weakSelf = self; [self.topVm getTopLinkList:^{ [weakSelf updateTopLinkData]; }]; } - (void)getHomeListData { __weak typeof(self) weakSelf = self; [self.vm getHomeListData:^{ [weakSelf.tableV.mj_header endRefreshing]; [weakSelf.tableV reloadData]; }]; } - (void)getHomeTopData { __weak typeof(self) weakSelf = self; [self.vm getHomeTopData:^{ [weakSelf.tableV reloadData]; }]; } - (void)updateTopLinkData { if (self.topVm.topLinks.count > 0) { self.activeV = [[ASHomeActiveView alloc] initWithFrame:CGRectZero]; __weak typeof(self) weakSelf = self; [self.activeV setTapTopBlock:^(KWSearchHotKeyModel * _Nullable m) { NSLog(@"---toplink:-%@--", m.code); [weakSelf jumpWithModel:[m toJumpM]]; }]; self.tableV.tableHeaderView = self.activeV; [self.activeV setTopData:self.topVm.topLinks]; } else { self.activeV.tapBlock = nil; self.activeV = nil; self.tableV.tableHeaderView = nil; } } - (void)addSubV { [self.view addSubview:self.tableV]; [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.customNavBar.mas_bottom); make.leading.trailing.equalTo(self.view); make.bottom.equalTo(self.view); }]; __weak typeof(self) weakSelf = self; self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [weakSelf getTopLinkData]; [weakSelf getHomeTopData]; [weakSelf getHomeListData]; }]; } // MARK: - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { // banner return; } } // MARK: - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.vm.listData.count + 3; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch (section) { case 0:// banner return self.vm.banners.count > 0 ? 1 : 0; break; case 1:// category return self.vm.categorys.count > 0 ? 1 : 0; break; case 2:// tips return 1; default: { if (self.vm.listData.count < section-3) { return 0; } ASHomeMainListModel *m = self.vm.listData[section-3]; NSInteger showtype = m.showType; switch (showtype) { case 1:// best seller return m.productList.count > 0 ? 1 : 0; case 2:// new in return m.productList.count > 0 ? 1 : 0; case 3:// return m.imgUrl.length > 0 ? 1 : 0; case 4:// New Arrivals return m.productList.count > 0 ? 1 : 0; case 5:// 单图活动 return m.imgUrl.length > 0 ? 1 : 0; case 6:// shop by looking return m.titleArr.count > 0 ? 1 : 0; case 7:// memship return m.imgUrl.length > 0 ? 1 : 0; // case 8:// video Active // return m.videoUrl.length > 0 ? 1 : 0; // case 9:// You may also like // return m.productList.count > 0 ? 1 : 0; // case 10:// today deals // return m.productList.count > 0 ? 1 : 0; // case 11: // 带背景色和图片的 cycle cell // return m.productList.count > 0 ? 1 : 0; default: return 0; } return 1; } } return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { // banner ASHomeBannerCell *bcell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeBannerCell" forIndexPath:indexPath]; @weakify(self); bcell.selectCallBack = ^(NSInteger index, NSString * _Nonnull title, NSString * _Nonnull typeId, id _Nonnull model) { if (model == nil || ![model isKindOfClass:[ASJumpModel class]]) { return; } ASJumpModel *m = (ASJumpModel *)model; [weak_self jumpWithModel:m]; }; [bcell showData:self.vm.banners]; return bcell; } if (indexPath.section == 1) { // category ASCategaryListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASCategaryListCell" forIndexPath:indexPath]; cell.arr = self.vm.categorys; cell.selectCategory = ^(ASJumpModel * _Nonnull model, NSIndexPath * _Nonnull index) { // TODO: - to productlist [self jumpWithModel:model]; }; return cell; } if (indexPath.section == 2) { ASHomeTipCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeTipCell" forIndexPath:indexPath]; return cell; } if (self.vm.listData.count <= indexPath.section-3) { return [UITableViewCell new]; } ASHomeMainListModel *m = self.vm.listData[indexPath.section-3]; __weak typeof(self) weakSelf = self; switch (m.showType) { case 1: {// bestSell ASHomeBestSellCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeBestSellCell" forIndexPath:indexPath]; cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull m) { [weakSelf goto_WKM_GoodsDetailsC:m.Id]; }; cell.moreClick = ^(ASHomeMainListModel * _Nonnull m) { [weakSelf pushToProductList:m.title typeid:m.typeId]; }; cell.model = m; return cell; } case 2: { ASHomeNewInCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeNewInCellTableViewCell" forIndexPath:indexPath]; cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull model) { [weakSelf goto_WKM_GoodsDetailsC: model.Id]; }; cell.addCartBlock = ^(NSInteger i, ASProductBaseModel * _Nonnull model) { [weakSelf action_GoodsSizeC: model.Id]; }; cell.moreClick = ^(ASHomeMainListModel * _Nonnull m) { [weakSelf pushToProductList:m.title typeid:m.typeId]; }; [cell setData:m]; return cell; } case 3: { ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath]; cell.tapImgBlock = ^{ [weakSelf jumpWithModel:[m toJumpM]]; }; [cell setImgStr:m.imgUrl]; return cell; } case 5: { ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath]; cell.tapImgBlock = ^{ [weakSelf jumpWithModel:[m toJumpM]]; }; [cell setImgStr:m.imgUrl]; return cell; } case 7: { ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath]; cell.tapImgBlock = ^{ [weakSelf jumpWithModel:[m toJumpM]]; }; [cell setImgStr:m.imgUrl]; return cell; } case 4: { ASHomeFlashDealCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeFlashDealCell" forIndexPath:indexPath]; cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull model) { [weakSelf goto_WKM_GoodsDetailsC:model.Id]; }; cell.proAddCartClick = ^(NSInteger i, ASProductBaseModel * _Nonnull model) { [weakSelf action_GoodsSizeC:model.Id]; }; cell.moreClick = ^(ASHomeMainListModel * _Nonnull m) { [weakSelf pushToProductList:m.title typeid:m.typeId]; }; cell.model = m; return cell; } case 6: { ASHomeLookingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeLookingCell" forIndexPath:indexPath]; cell.itemClick = ^(NSInteger i, ASHomeCategoryModel * _Nonnull model) { [weakSelf jumpWithModel:[model toJumpM]]; }; cell.model = m; return cell; } default: break; } return [UITableViewCell new]; } - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 40; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.01; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *footer = [[UIView alloc] initWithFrame:CGRectZero]; footer.backgroundColor = [UIColor clearColor]; return footer; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footer = [[UIView alloc] initWithFrame:CGRectZero]; footer.backgroundColor = [UIColor clearColor]; return footer; } -(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { } // MARK: - nextViews - (void)pushToProductList:(NSString *)typeId title:(NSString *)title { ASProductListViewController *vc = [ASProductListViewController new]; vc.titleStr = title; [self.navigationController pushViewController:vc animated:true]; } // MARK: - subVs -(UITableView *)tableV { if (!_tableV) { UITableView *tabV = [[UITableView alloc] initWithFrame:self.view.bounds]; tabV.delegate = self; tabV.dataSource = self; [tabV baseSet]; tabV.backgroundColor = UIColor.whiteColor; tabV.separatorStyle = UITableViewCellSeparatorStyleNone; [tabV registerClass:[ASHomeBannerCell class] forCellReuseIdentifier:@"ASHomeBannerCell"]; [tabV registerClass:[ASCategaryListCell class] forCellReuseIdentifier:@"ASCategaryListCell"]; [tabV registerClass:[ASHomeTipCell class] forCellReuseIdentifier:@"ASHomeTipCell"]; [tabV registerClass:[ASHomeBestSellCell class] forCellReuseIdentifier:@"ASHomeBestSellCell"]; [tabV registerClass:[ASHomeNewInCellTableViewCell class] forCellReuseIdentifier:@"ASHomeNewInCellTableViewCell"]; [tabV registerClass:[ASHomeImgCell class] forCellReuseIdentifier:@"ASHomeImgCell"]; [tabV registerClass:[ASHomeFlashDealCell class] forCellReuseIdentifier:@"ASHomeFlashDealCell"]; [tabV registerClass:[ASHomeLookingCell class] forCellReuseIdentifier:@"ASHomeLookingCell"]; _tableV = tabV; } return _tableV; } @end