ASProductListViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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)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. [self goto_WKM_GoodsDetailsC:m.Id];
  155. }
  156. // MARK: - UICollectionViewDataSource
  157. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  158. switch (indexPath.section) {
  159. case 3:{
  160. if (self.canShowMoreBt) {
  161. if (indexPath.row == 0) {
  162. ASMoreBtCell *btCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASMoreBtCell" forIndexPath:indexPath];
  163. return btCell;
  164. }
  165. }
  166. ASProductListTypeDesCell *desCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASProductListTypeDesCell" forIndexPath:indexPath];
  167. [desCell setData:self.vm.cateModel.name des:self.vm.cateModel.meta_description moreStatus:self.desMoreOpen];
  168. desCell.moreStatusChange = ^{
  169. self.desMoreOpen = !self.desMoreOpen;
  170. [self.collectV reloadData];
  171. };
  172. return desCell;
  173. }
  174. default: {
  175. if (indexPath.row == 0 && self.vm.cateModel.image.length > 0) {
  176. ASProductListImageCell *c = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASProductListImageCell" forIndexPath:indexPath];
  177. [c setImgStr:self.vm.cateModel.image];
  178. // [c setImgStr:[NSString stringWithFormat:@"https://%@%@",HostPath, 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. __weak typeof(self) weakSelf = self;
  191. cell.contView.addCartBlock = ^(ASProductBaseModel * _Nonnull model) {
  192. [weakSelf action_GoodsSizeC: model.Id];
  193. };
  194. return cell;
  195. }
  196. }
  197. }
  198. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  199. return 4;
  200. }
  201. - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  202. if (section == 0 || section == 1) {
  203. return 0;
  204. }
  205. if (section == 3) {
  206. NSInteger count = 0;
  207. if (self.canShowMoreBt) {
  208. count += 1;
  209. }
  210. if (self.vm.cateModel != nil && self.vm.cateModel.meta_description.length > 0) {
  211. count += 1;
  212. }
  213. return count;
  214. }
  215. return self.vm.productList.count + (self.vm.cateModel.image.length > 0 ? 1 : 0);
  216. }
  217. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  218. if (section == 0) {
  219. CGSize size = CGSizeZero;
  220. if (self.vm.topLinks.count > 0) {
  221. size = CGSizeMake(KScreenWidth, 40);
  222. }
  223. return size;
  224. }
  225. if (section == 1) {
  226. return CGSizeMake(KScreenWidth, 60);
  227. }
  228. return CGSizeZero;
  229. }
  230. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  231. if (indexPath.section == 0 && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
  232. ASProductListActiveHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView" forIndexPath:indexPath];
  233. __weak typeof(self) weakSelf = self;
  234. [v setTopLinkData:self.vm.topLinks tapBlock:^(KWSearchHotKeyModel * _Nullable m) {
  235. NSLog(@"---toplink:-%@--", m.code);
  236. [weakSelf jumpWithModel:[m toJumpM]];
  237. }];
  238. return v;
  239. }
  240. if (indexPath.section == 1 && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
  241. ASProductListMenuHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView" forIndexPath:indexPath];
  242. __block typeof(self) weakself = self;
  243. NSString *titleStr = @"SORT BY";
  244. if (self.currentSortType < self.sortTypeV.titles.count) {
  245. titleStr = self.sortTypeV.titles[self.currentSortType];
  246. }
  247. [v setData:titleStr sortBlock:^{
  248. // if (weakself.productArr.count > 0) {
  249. // [weakself.collectV scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:true];
  250. // }
  251. [weakself showSortV];
  252. } menuBlock:^{
  253. [weakself showMenuV];
  254. }];
  255. // if (self.sortTypeV.titles.count > self.currentSortType) {
  256. // [v.sortBt setTitle:self.sortTypeV.titles[self.currentSortType] forState:UIControlStateNormal];
  257. // }
  258. return v;
  259. }
  260. return nil;
  261. }
  262. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  263. switch (indexPath.section) {
  264. case 3:{
  265. if (self.canShowMoreBt) {
  266. if (indexPath.row == 0) {
  267. return CGSizeMake(KScreenWidth-20, 20+36+50);
  268. }
  269. }
  270. if (!self.desMoreOpen) {
  271. return CGSizeMake(KScreenWidth-20, 35+60+24);
  272. }
  273. //设置行间距
  274. NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
  275. [paragraphStyle1 setLineSpacing:12];
  276. CGFloat h = [self.vm.cateModel.meta_description boundingRectWithSize:CGSizeMake(KScreenWidth-10-35, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : paragraphStyle1} context:nil].size.height;
  277. return CGSizeMake(KScreenWidth-20, 35+60+(h > 24 ? h : 24));
  278. }
  279. case 2: {
  280. if (indexPath.row == 0 && self.vm.cateModel.image.length > 0) {
  281. CGFloat w = KScreenWidth-20;
  282. CGFloat collH = w/355.0*118;
  283. return CGSizeMake(w, collH);
  284. }
  285. CGFloat marg = 10;
  286. CGFloat w = (KScreenWidth-3*marg)/2;
  287. CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg;
  288. return CGSizeMake(w, collH);
  289. }
  290. default:{
  291. CGFloat marg = 10;
  292. CGFloat w = (KScreenWidth-3*marg)/2;
  293. CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg;
  294. return CGSizeMake(w, collH);
  295. }
  296. }
  297. }
  298. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  299. return 10;
  300. }
  301. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  302. return 10;
  303. }
  304. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  305. if (section == 0 || section == 1) {
  306. return UIEdgeInsetsZero;
  307. }
  308. return UIEdgeInsetsMake(10, 10, 10, 10);
  309. }
  310. // MARK: - subvs
  311. - (UIView *)timerView {
  312. if (!_timerView) {
  313. UIView *v = [UIView baseV];
  314. v.backgroundColor = UIColor.clearColor;
  315. UIImageView *imgV = [UIImageView baseImgV];
  316. imgV.contentMode = UIViewContentModeScaleAspectFit;
  317. imgV.image = [UIImage imageNamed:@"product_list_timer"];
  318. [v addSubview:imgV];
  319. [v addSubview:self.timerLb];
  320. _timerView = v;
  321. [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
  322. make.edges.equalTo(_timerView);
  323. }];
  324. [self.timerLb mas_makeConstraints:^(MASConstraintMaker *make) {
  325. make.top.equalTo(_timerView).offset(5);
  326. make.leading.equalTo(_timerView).offset(5);
  327. make.trailing.bottom.equalTo(_timerView).offset(-5);
  328. }];
  329. }
  330. return _timerView;
  331. }
  332. - (UILabel *)timerLb {
  333. if (!_timerLb) {
  334. UILabel *lb = [UILabel baseLb];
  335. lb.font = [UIFont fontWithName:Rob_BlackItalic size:12];
  336. lb.textAlignment = NSTextAlignmentCenter;
  337. lb.textColor = Col_000;
  338. _timerLb = lb;
  339. }
  340. return _timerLb;
  341. }
  342. -(UICollectionView *)collectV {
  343. if (!_collectV) {
  344. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  345. layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
  346. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  347. CGFloat collH = (KScreenWidth-30)/2 + 126;
  348. layout.itemSize = CGSizeMake((KScreenWidth - 30)/2, collH);
  349. UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, collH) collectionViewLayout:layout];
  350. collV.backgroundColor = [UIColor whiteColor];
  351. collV.alwaysBounceVertical = true;
  352. collV.scrollEnabled = true;
  353. collV.delegate = self;
  354. collV.dataSource = self;
  355. collV.showsHorizontalScrollIndicator = false;
  356. collV.showsVerticalScrollIndicator = false;
  357. [collV registerClass:[HomeFlashDealSubCollectCell class] forCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell"];
  358. [collV registerClass:[ASProductListImageCell class] forCellWithReuseIdentifier:@"ASProductListImageCell"];
  359. [collV registerClass:[ASProductListTypeDesCell class] forCellWithReuseIdentifier:@"ASProductListTypeDesCell"];
  360. [collV registerClass:[ASMoreBtCell class] forCellWithReuseIdentifier:@"ASMoreBtCell"];
  361. [collV registerClass:[ASProductListActiveHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView"];
  362. [collV registerClass:[ASProductListMenuHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView"];
  363. _collectV = collV;
  364. }
  365. return _collectV;
  366. }
  367. - (KWProductMenuFilterView *)menuFilterV {
  368. if (!_menuFilterV) {
  369. KWProductMenuFilterView *v = [[KWProductMenuFilterView alloc] initWithFrame:self.view.bounds];
  370. __block typeof(self) weakself = self;
  371. [v setResultFilterDic:^(NSDictionary<NSString *,KWProductListFilterSubModel *> * _Nonnull arr) {
  372. weakself.vm.selectDic = [NSMutableDictionary dictionaryWithDictionary:arr];
  373. [weakself.collectV.mj_header beginRefreshing];
  374. }];
  375. _menuFilterV = v;
  376. }
  377. return _menuFilterV;
  378. }
  379. - (ASProductSortFilterView *)sortTypeV {
  380. if (!_sortTypeV) {
  381. ASProductSortFilterView *v = [[ASProductSortFilterView alloc] initWithFrame:self.view.bounds];
  382. __block typeof(self) weakself = self;
  383. [v setSelectIndex:^(NSInteger index) {
  384. weakself.currentSortType = index;
  385. [weakself.collectV reloadData];
  386. [weakself.collectV.mj_header beginRefreshing];
  387. }];
  388. _sortTypeV = v;
  389. }
  390. return _sortTypeV;
  391. }
  392. -(void)showSortV {
  393. BOOL isSearch = self.keyWord != nil && self.keyWord.length > 0;
  394. [self.sortTypeV showAnimate:isSearch];
  395. }
  396. -(void)showMenuV {
  397. self.menuFilterV.vm = self.vm;
  398. [self.menuFilterV showAnimate:self.vm.selectDic];
  399. }
  400. -(void)getNetData {
  401. [MBProgressHUD showHUDAddedTo:self.view animated:true];
  402. __weak typeof(self) weakSelf = self;
  403. [self.vm getTopLinkList:^{
  404. [weakSelf.collectV reloadData];
  405. }];
  406. [self.vm getProductListData:self.type page:self.page orderBy:self.currentSortType dir:@"DESC" com:^(BOOL hasNext, NSString * _Nonnull msg) {
  407. [MBProgressHUD hideHUDForView:weakSelf.view animated:true];
  408. [weakSelf.collectV.mj_header endRefreshing];
  409. [weakSelf.collectV.mj_footer endRefreshing];
  410. weakSelf.canShowMoreBt = true;
  411. if (!hasNext) {
  412. weakSelf.canShowMoreBt = false;
  413. [weakSelf.collectV.mj_footer endRefreshingWithNoMoreData];
  414. }
  415. [self.collectV reloadData];
  416. if (!msg.isEmpty) {
  417. [weakSelf.view makeToast:msg];
  418. }
  419. }];
  420. }
  421. @end