ASProductListViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. //
  2. // ASProductListViewController.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/6/12.
  6. //
  7. #import "ASProductListViewController.h"
  8. #import "HomeFlashDealSubCollectCell.h"
  9. #import "ASProductListViewModel.h"
  10. #import "ASProductListMenuHeaderView.h"
  11. #import "ASProductListActiveHeaderView.h"
  12. #import "ASProductListImageCell.h"
  13. #import "ASProductListTypeDesCell.h"
  14. #import "ASMoreBtCell.h"
  15. #import "ASProductSortFilterView.h"
  16. #import "KWProductMenuFilterView.h"
  17. @interface ASProductListViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  18. @property (nonatomic, assign) BOOL desMoreOpen;
  19. @property (nonatomic, assign) BOOL canShowMoreBt;
  20. @property (nonatomic, assign) NSInteger page;
  21. @property (nonatomic, strong) ASProductListViewModel *vm;
  22. @property (nonatomic, strong) UICollectionView *collectV;
  23. @property (nonatomic, strong) UIView *timerView;
  24. @property (nonatomic, strong) UILabel *timerLb;
  25. @property (nonatomic, strong) NSTimer * _Nullable timer;
  26. // sort 相关
  27. @property (nonatomic,strong, nullable) ASProductSortFilterView *sortTypeV;
  28. @property (nonatomic,assign) NSInteger currentSortType;
  29. @property (nonatomic,strong, nullable) KWProductMenuFilterView *menuFilterV;
  30. @end
  31. @implementation ASProductListViewController
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.currentSortType = 2;
  35. self.desMoreOpen = false;
  36. [self loadSubVs];
  37. [self loadTimerV];
  38. [self loasActiveDataAndV];
  39. self.vm = [ASProductListViewModel new];
  40. [self.collectV reloadData];
  41. [self.collectV.mj_header beginRefreshing];
  42. }
  43. - (void)viewDidAppear:(BOOL)animated {
  44. [super viewDidAppear:animated];
  45. [self.timer invalidate];
  46. self.timer = nil;
  47. if (self.secondTime > 0) {
  48. self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
  49. [NSRunLoop.currentRunLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
  50. }
  51. }
  52. - (void)viewDidDisappear:(BOOL)animated {
  53. [super viewDidDisappear:animated];
  54. [self.timer invalidate];
  55. self.timer = nil;
  56. }
  57. - (void)dealloc
  58. {
  59. [self.timer invalidate];
  60. self.timer = nil;
  61. }
  62. - (void)configFilter {
  63. if (_type && ![_type isEqualToString:@""]) {
  64. [self.menuFilterV configTypeId:self.type];
  65. }
  66. }
  67. - (void)timerAction {
  68. self.secondTime -= 1;
  69. [self setTimerStr];
  70. }
  71. - (void)setTimerStr {
  72. if (self.secondTime <= 0) {
  73. self.timerView.hidden = true;
  74. [self.timer invalidate];
  75. self.timer = nil;
  76. // TODO:获取积分请求
  77. [MBProgressHUD showHUDAddedTo:self.view animated:true];
  78. __weak typeof(self) weakSelf = self;
  79. [self.vm getPoint:[NSString stringWithFormat:@"%ld", self.pointGetType] com:^(BOOL isSuc, NSString * _Nonnull msg) {
  80. [MBProgressHUD hideHUDForView:weakSelf.view animated:true];
  81. if (!isSuc) {
  82. [weakSelf.view makeToast:msg];
  83. }
  84. }];
  85. return;
  86. }
  87. NSInteger minus = self.secondTime/60;
  88. NSInteger second = self.secondTime%60;
  89. NSString *str = [NSString stringWithFormat:@"%ld:%02ld", minus, second];
  90. self.timerLb.text = str;
  91. }
  92. // MARK: sub mas
  93. - (void)loadTimerV {
  94. if (self.secondTime <= 0) {
  95. return;
  96. }
  97. [self.view addSubview:self.timerView];
  98. [self.timerView mas_makeConstraints:^(MASConstraintMaker *make) {
  99. make.bottom.equalTo(self.view).offset(-180);
  100. make.trailing.equalTo(self.view).offset(-8);
  101. make.width.equalTo(@50);
  102. make.height.equalTo(@50);
  103. }];
  104. [self setTimerStr];
  105. }
  106. - (void)loadSubVs {
  107. [self setNavRightSearch:^{
  108. // TODO: - to Search VC
  109. }];
  110. [self.view addSubview:self.collectV];
  111. [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) {
  112. make.top.equalTo(self.customNavBar.mas_bottom);
  113. make.leading.trailing.equalTo(self.view);
  114. make.bottom.equalTo(self.view);
  115. }];
  116. self.sortTypeV.hidden = true;
  117. [self.view addSubview:self.menuFilterV];
  118. [self.menuFilterV mas_makeConstraints:^(MASConstraintMaker *make) {
  119. make.top.equalTo(self.customNavBar.mas_bottom);
  120. make.left.right.bottom.equalTo(self.view);
  121. }];
  122. self.menuFilterV.hidden = true;
  123. __weak typeof(self) weakSelf = self;
  124. self.collectV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  125. weakSelf.page = 1;
  126. [weakSelf.collectV.mj_footer resetNoMoreData];
  127. [weakSelf getNetData];
  128. }];
  129. self.collectV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  130. weakSelf.page += 1;
  131. [weakSelf getNetData];
  132. }];
  133. }
  134. - (void)loasActiveDataAndV {
  135. NSMutableArray *tempArr = [NSMutableArray array];
  136. for (int i=0;i < (arc4random()%6); i++) {
  137. ASHomeBannerModel *m = [ASHomeBannerModel demoModelWithType:i%3+1];
  138. [tempArr addObject:m];
  139. }
  140. // self.topLinkArr = tempArr;
  141. [self.collectV reloadData];
  142. }
  143. // MARK: - delegate datasource
  144. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  145. if (indexPath.section != 2) {
  146. return;
  147. }
  148. NSInteger tempOff = self.vm.cateModel.image.length > 0 ? 1 : 0;
  149. if (self.vm.productList.count <= indexPath.row-tempOff)
  150. {
  151. return;
  152. }
  153. ASProductBaseModel *m = self.vm.productList[indexPath.row-tempOff];
  154. UIViewController *viewController = [[CTMediator sharedInstance] Goods_GoodsDetailsC:@{@"entity_id":m.Id}];
  155. [self.navigationController pushViewController:viewController animated:YES];
  156. }
  157. // MARK: - UICollectionViewDataSource
  158. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  159. switch (indexPath.section) {
  160. case 3:{
  161. if (self.canShowMoreBt) {
  162. if (indexPath.row == 0) {
  163. ASMoreBtCell *btCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASMoreBtCell" forIndexPath:indexPath];
  164. return btCell;
  165. }
  166. }
  167. ASProductListTypeDesCell *desCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASProductListTypeDesCell" forIndexPath:indexPath];
  168. [desCell setData:self.vm.cateModel.name des:self.vm.cateModel.meta_description moreStatus:self.desMoreOpen];
  169. desCell.moreStatusChange = ^{
  170. self.desMoreOpen = !self.desMoreOpen;
  171. [self.collectV reloadData];
  172. };
  173. return desCell;
  174. }
  175. default: {
  176. if (indexPath.row == 0 && self.vm.cateModel.image.length > 0) {
  177. ASProductListImageCell *c = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASProductListImageCell" forIndexPath:indexPath];
  178. [c setImgStr:[NSString stringWithFormat:@"https://%@%@%@",HostPath, CategoryImgPath, self.vm.cateModel.image]];
  179. return c;
  180. }
  181. HomeFlashDealSubCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell" forIndexPath:indexPath];
  182. NSInteger tempOff = self.vm.cateModel.image.length > 0 ? 1 : 0;
  183. if (self.vm.productList.count <= indexPath.row-tempOff)
  184. {
  185. return cell;
  186. }
  187. ASProductBaseModel *m = self.vm.productList[indexPath.row-tempOff];
  188. cell.model = m;
  189. cell.contView.addCartBt.hidden = false;
  190. return cell;
  191. }
  192. }
  193. }
  194. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  195. return 4;
  196. }
  197. - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  198. if (section == 0 || section == 1) {
  199. return 0;
  200. }
  201. if (section == 3) {
  202. NSInteger count = 0;
  203. if (self.canShowMoreBt) {
  204. count += 1;
  205. }
  206. if (self.vm.cateModel != nil && self.vm.cateModel.meta_description.length > 0) {
  207. count += 1;
  208. }
  209. return count;
  210. }
  211. return self.vm.productList.count + (self.vm.cateModel.image.length > 0 ? 1 : 0);
  212. }
  213. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  214. if (section == 0) {
  215. CGSize size = CGSizeZero;
  216. if (self.vm.topLinks.count > 0) {
  217. size = CGSizeMake(KScreenWidth, 40);
  218. }
  219. return size;
  220. }
  221. if (section == 1) {
  222. return CGSizeMake(KScreenWidth, 60);
  223. }
  224. return CGSizeZero;
  225. }
  226. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  227. if (indexPath.section == 0 && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
  228. ASProductListActiveHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView" forIndexPath:indexPath];
  229. __weak typeof(self) weakSelf = self;
  230. [v setTopLinkData:self.vm.topLinks tapBlock:^(KWSearchHotKeyModel * _Nullable m) {
  231. NSLog(@"---toplink:-%@--", m.code);
  232. [weakSelf jumpWithModel:[m toJumpM]];
  233. }];
  234. return v;
  235. }
  236. if (indexPath.section == 1 && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
  237. ASProductListMenuHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView" forIndexPath:indexPath];
  238. __block typeof(self) weakself = self;
  239. NSString *titleStr = @"SORT BY";
  240. if (self.currentSortType < self.sortTypeV.titles.count) {
  241. titleStr = self.sortTypeV.titles[self.currentSortType];
  242. }
  243. [v setData:titleStr sortBlock:^{
  244. // if (weakself.productArr.count > 0) {
  245. // [weakself.collectV scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:true];
  246. // }
  247. [weakself showSortV];
  248. } menuBlock:^{
  249. [weakself showMenuV];
  250. }];
  251. // if (self.sortTypeV.titles.count > self.currentSortType) {
  252. // [v.sortBt setTitle:self.sortTypeV.titles[self.currentSortType] forState:UIControlStateNormal];
  253. // }
  254. return v;
  255. }
  256. return nil;
  257. }
  258. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  259. switch (indexPath.section) {
  260. case 3:{
  261. if (self.canShowMoreBt) {
  262. if (indexPath.row == 0) {
  263. return CGSizeMake(KScreenWidth-20, 20+36+50);
  264. }
  265. }
  266. if (!self.desMoreOpen) {
  267. return CGSizeMake(KScreenWidth-20, 35+60+24);
  268. }
  269. //设置行间距
  270. NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
  271. [paragraphStyle1 setLineSpacing:12];
  272. CGFloat h = [self.vm.cateModel.meta_description boundingRectWithSize:CGSizeMake(KScreenWidth-10-35, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : paragraphStyle1} context:nil].size.height;
  273. return CGSizeMake(KScreenWidth-20, 35+60+(h > 24 ? h : 24));
  274. }
  275. case 2: {
  276. if (indexPath.row == 0 && self.vm.cateModel.image.length > 0) {
  277. CGFloat w = KScreenWidth-20;
  278. CGFloat collH = w/355.0*118;
  279. return CGSizeMake(w, collH);
  280. }
  281. CGFloat marg = 10;
  282. CGFloat w = (KScreenWidth-3*marg)/2;
  283. CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg;
  284. return CGSizeMake(w, collH);
  285. }
  286. default:{
  287. CGFloat marg = 10;
  288. CGFloat w = (KScreenWidth-3*marg)/2;
  289. CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg;
  290. return CGSizeMake(w, collH);
  291. }
  292. }
  293. }
  294. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  295. return 10;
  296. }
  297. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  298. return 10;
  299. }
  300. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  301. if (section == 0 || section == 1) {
  302. return UIEdgeInsetsZero;
  303. }
  304. return UIEdgeInsetsMake(10, 10, 10, 10);
  305. }
  306. // MARK: - subvs
  307. - (UIView *)timerView {
  308. if (!_timerView) {
  309. UIView *v = [UIView baseV];
  310. v.backgroundColor = UIColor.clearColor;
  311. UIImageView *imgV = [UIImageView baseImgV];
  312. imgV.contentMode = UIViewContentModeScaleAspectFit;
  313. imgV.image = [UIImage imageNamed:@"product_list_timer"];
  314. [v addSubview:imgV];
  315. [v addSubview:self.timerLb];
  316. _timerView = v;
  317. [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
  318. make.edges.equalTo(_timerView);
  319. }];
  320. [self.timerLb mas_makeConstraints:^(MASConstraintMaker *make) {
  321. make.top.equalTo(_timerView).offset(5);
  322. make.leading.equalTo(_timerView).offset(5);
  323. make.trailing.bottom.equalTo(_timerView).offset(-5);
  324. }];
  325. }
  326. return _timerView;
  327. }
  328. - (UILabel *)timerLb {
  329. if (!_timerLb) {
  330. UILabel *lb = [UILabel baseLb];
  331. lb.font = [UIFont fontWithName:Rob_BlackItalic size:12];
  332. lb.textAlignment = NSTextAlignmentCenter;
  333. lb.textColor = Col_000;
  334. _timerLb = lb;
  335. }
  336. return _timerLb;
  337. }
  338. -(UICollectionView *)collectV {
  339. if (!_collectV) {
  340. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  341. layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
  342. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  343. CGFloat collH = (KScreenWidth-30)/2 + 126;
  344. layout.itemSize = CGSizeMake((KScreenWidth - 30)/2, collH);
  345. UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, collH) collectionViewLayout:layout];
  346. collV.backgroundColor = [UIColor whiteColor];
  347. collV.alwaysBounceVertical = true;
  348. collV.scrollEnabled = true;
  349. collV.delegate = self;
  350. collV.dataSource = self;
  351. collV.showsHorizontalScrollIndicator = false;
  352. collV.showsVerticalScrollIndicator = false;
  353. [collV registerClass:[HomeFlashDealSubCollectCell class] forCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell"];
  354. [collV registerClass:[ASProductListImageCell class] forCellWithReuseIdentifier:@"ASProductListImageCell"];
  355. [collV registerClass:[ASProductListTypeDesCell class] forCellWithReuseIdentifier:@"ASProductListTypeDesCell"];
  356. [collV registerClass:[ASMoreBtCell class] forCellWithReuseIdentifier:@"ASMoreBtCell"];
  357. [collV registerClass:[ASProductListActiveHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView"];
  358. [collV registerClass:[ASProductListMenuHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView"];
  359. _collectV = collV;
  360. }
  361. return _collectV;
  362. }
  363. - (KWProductMenuFilterView *)menuFilterV {
  364. if (!_menuFilterV) {
  365. KWProductMenuFilterView *v = [[KWProductMenuFilterView alloc] initWithFrame:self.view.bounds];
  366. __block typeof(self) weakself = self;
  367. [v setResultFilterDic:^(NSDictionary<NSString *,KWProductListFilterSubModel *> * _Nonnull arr) {
  368. weakself.vm.selectDic = [NSMutableDictionary dictionaryWithDictionary:arr];
  369. // [weakself.collectV.mj_header beginRefreshing];
  370. }];
  371. _menuFilterV = v;
  372. }
  373. return _menuFilterV;
  374. }
  375. - (ASProductSortFilterView *)sortTypeV {
  376. if (!_sortTypeV) {
  377. ASProductSortFilterView *v = [[ASProductSortFilterView alloc] initWithFrame:self.view.bounds];
  378. __block typeof(self) weakself = self;
  379. [v setSelectIndex:^(NSInteger index) {
  380. weakself.currentSortType = index;
  381. [weakself.collectV reloadData];
  382. [weakself.collectV.mj_header beginRefreshing];
  383. }];
  384. _sortTypeV = v;
  385. }
  386. return _sortTypeV;
  387. }
  388. -(void)showSortV {
  389. [self.sortTypeV showAnimate:![self.keyWord isEqualToString:@""]];
  390. }
  391. -(void)showMenuV {
  392. [self.menuFilterV showAnimate:self.vm.selectDic];
  393. }
  394. -(void)getNetData {
  395. [MBProgressHUD showHUDAddedTo:self.view animated:true];
  396. __weak typeof(self) weakSelf = self;
  397. [self.vm getTopLinkList:^{
  398. [weakSelf.collectV reloadData];
  399. }];
  400. [self.vm getProductListData:self.type page:self.page orderBy:self.currentSortType dir:@"DESC" com:^(BOOL hasNext, NSString * _Nonnull msg) {
  401. [MBProgressHUD hideHUDForView:weakSelf.view animated:true];
  402. [weakSelf.collectV.mj_header endRefreshing];
  403. [weakSelf.collectV.mj_footer endRefreshing];
  404. weakSelf.canShowMoreBt = true;
  405. if (!hasNext) {
  406. weakSelf.canShowMoreBt = false;
  407. [weakSelf.collectV.mj_footer endRefreshingWithNoMoreData];
  408. }
  409. [self.collectV reloadData];
  410. if (!msg.isEmpty) {
  411. [weakSelf.view makeToast:msg];
  412. }
  413. }];
  414. }
  415. @end