ASProductListViewController.m 17 KB

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