ASProductListViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 sortBlock:^{
  243. // if (weakself.productArr.count > 0) {
  244. // [weakself.collectV scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:true];
  245. // }
  246. [weakself showSortV];
  247. } menuBlock:^{
  248. [weakself showMenuV];
  249. }];
  250. // if (self.sortTypeV.titles.count > self.currentSortType) {
  251. // [v.sortBt setTitle:self.sortTypeV.titles[self.currentSortType] forState:UIControlStateNormal];
  252. // }
  253. return v;
  254. }
  255. return nil;
  256. }
  257. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  258. switch (indexPath.section) {
  259. case 3:{
  260. if (self.canShowMoreBt) {
  261. if (indexPath.row == 0) {
  262. return CGSizeMake(KScreenWidth-20, 20+36+50);
  263. }
  264. }
  265. if (!self.desMoreOpen) {
  266. return CGSizeMake(KScreenWidth-20, 35+60+24);
  267. }
  268. //设置行间距
  269. NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
  270. [paragraphStyle1 setLineSpacing:12];
  271. CGFloat h = [self.vm.cateModel.meta_description boundingRectWithSize:CGSizeMake(KScreenWidth-10-35, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : paragraphStyle1} context:nil].size.height;
  272. return CGSizeMake(KScreenWidth-20, 35+60+(h > 24 ? h : 24));
  273. }
  274. case 2: {
  275. if (indexPath.row == 0 && self.vm.cateModel.image.length > 0) {
  276. CGFloat w = KScreenWidth-20;
  277. CGFloat collH = w/355.0*118;
  278. return CGSizeMake(w, collH);
  279. }
  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. default:{
  286. CGFloat marg = 10;
  287. CGFloat w = (KScreenWidth-3*marg)/2;
  288. CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg;
  289. return CGSizeMake(w, collH);
  290. }
  291. }
  292. }
  293. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  294. return 10;
  295. }
  296. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  297. return 10;
  298. }
  299. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  300. if (section == 0 || section == 1) {
  301. return UIEdgeInsetsZero;
  302. }
  303. return UIEdgeInsetsMake(10, 10, 10, 10);
  304. }
  305. // MARK: - subvs
  306. - (UIView *)timerView {
  307. if (!_timerView) {
  308. UIView *v = [UIView baseV];
  309. v.backgroundColor = UIColor.clearColor;
  310. UIImageView *imgV = [UIImageView baseImgV];
  311. imgV.contentMode = UIViewContentModeScaleAspectFit;
  312. imgV.image = [UIImage imageNamed:@"product_list_timer"];
  313. [v addSubview:imgV];
  314. [v addSubview:self.timerLb];
  315. _timerView = v;
  316. [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
  317. make.edges.equalTo(_timerView);
  318. }];
  319. [self.timerLb mas_makeConstraints:^(MASConstraintMaker *make) {
  320. make.top.equalTo(_timerView).offset(5);
  321. make.leading.equalTo(_timerView).offset(5);
  322. make.trailing.bottom.equalTo(_timerView).offset(-5);
  323. }];
  324. }
  325. return _timerView;
  326. }
  327. - (UILabel *)timerLb {
  328. if (!_timerLb) {
  329. UILabel *lb = [UILabel baseLb];
  330. lb.font = [UIFont fontWithName:Rob_BlackItalic size:12];
  331. lb.textAlignment = NSTextAlignmentCenter;
  332. lb.textColor = Col_000;
  333. _timerLb = lb;
  334. }
  335. return _timerLb;
  336. }
  337. -(UICollectionView *)collectV {
  338. if (!_collectV) {
  339. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  340. layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
  341. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  342. CGFloat collH = (KScreenWidth-30)/2 + 126;
  343. layout.itemSize = CGSizeMake((KScreenWidth - 30)/2, collH);
  344. UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, collH) collectionViewLayout:layout];
  345. collV.backgroundColor = [UIColor whiteColor];
  346. collV.alwaysBounceVertical = true;
  347. collV.scrollEnabled = true;
  348. collV.delegate = self;
  349. collV.dataSource = self;
  350. collV.showsHorizontalScrollIndicator = false;
  351. collV.showsVerticalScrollIndicator = false;
  352. [collV registerClass:[HomeFlashDealSubCollectCell class] forCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell"];
  353. [collV registerClass:[ASProductListImageCell class] forCellWithReuseIdentifier:@"ASProductListImageCell"];
  354. [collV registerClass:[ASProductListTypeDesCell class] forCellWithReuseIdentifier:@"ASProductListTypeDesCell"];
  355. [collV registerClass:[ASMoreBtCell class] forCellWithReuseIdentifier:@"ASMoreBtCell"];
  356. [collV registerClass:[ASProductListActiveHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView"];
  357. [collV registerClass:[ASProductListMenuHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView"];
  358. _collectV = collV;
  359. }
  360. return _collectV;
  361. }
  362. - (KWProductMenuFilterView *)menuFilterV {
  363. if (!_menuFilterV) {
  364. KWProductMenuFilterView *v = [[KWProductMenuFilterView alloc] initWithFrame:self.view.bounds];
  365. __block typeof(self) weakself = self;
  366. [v setResultFilterDic:^(NSDictionary<NSString *,KWProductListFilterSubModel *> * _Nonnull arr) {
  367. weakself.vm.selectDic = [NSMutableDictionary dictionaryWithDictionary:arr];
  368. [weakself.collectV.mj_header beginRefreshing];
  369. }];
  370. _menuFilterV = v;
  371. }
  372. return _menuFilterV;
  373. }
  374. - (ASProductSortFilterView *)sortTypeV {
  375. if (!_sortTypeV) {
  376. ASProductSortFilterView *v = [[ASProductSortFilterView alloc] initWithFrame:self.view.bounds];
  377. __block typeof(self) weakself = self;
  378. [v setSelectIndex:^(NSInteger index) {
  379. weakself.currentSortType = index;
  380. [weakself.collectV reloadData];
  381. [weakself.collectV.mj_header beginRefreshing];
  382. }];
  383. _sortTypeV = v;
  384. }
  385. return _sortTypeV;
  386. }
  387. -(void)showSortV {
  388. BOOL isSearch = self.keyWord != nil && self.keyWord.length > 0;
  389. [self.sortTypeV showAnimate:isSearch];
  390. }
  391. -(void)showMenuV {
  392. self.menuFilterV.vm = self.vm;
  393. [self.menuFilterV showAnimate:self.vm.selectDic];
  394. }
  395. -(void)getNetData {
  396. [MBProgressHUD showHUDAddedTo:self.view animated:true];
  397. __weak typeof(self) weakSelf = self;
  398. [self.vm getTopLinkList:^{
  399. [weakSelf.collectV reloadData];
  400. }];
  401. [self.vm getProductListData:self.type page:self.page orderBy:self.currentSortType dir:@"DESC" com:^(BOOL hasNext, NSString * _Nonnull msg) {
  402. [MBProgressHUD hideHUDForView:weakSelf.view animated:true];
  403. [weakSelf.collectV.mj_header endRefreshing];
  404. [weakSelf.collectV.mj_footer endRefreshing];
  405. weakSelf.canShowMoreBt = true;
  406. if (!hasNext) {
  407. weakSelf.canShowMoreBt = false;
  408. [weakSelf.collectV.mj_footer endRefreshingWithNoMoreData];
  409. }
  410. [self.collectV reloadData];
  411. if (!msg.isEmpty) {
  412. [weakSelf.view makeToast:msg];
  413. }
  414. }];
  415. }
  416. @end