ASProductListViewController.m 18 KB

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