ASProductListViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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, strong) NSArray <ASHomeBannerModel *>*topLinkArr;
  19. @property (nonatomic, assign) BOOL desMoreOpen;
  20. @property (nonatomic, assign) BOOL canShowMoreBt;
  21. @property (nonatomic, assign) NSInteger page;
  22. @property (nonatomic, strong) ASProductListViewModel *vm;
  23. @property (nonatomic, strong) UICollectionView *collectV;
  24. // sort 相关
  25. @property (nonatomic,strong, nullable) ASProductSortFilterView *sortTypeV;
  26. @property (nonatomic,assign) NSInteger currentSortType;
  27. @property (nonatomic,strong, nullable) KWProductMenuFilterView *menuFilterV;
  28. @end
  29. @implementation ASProductListViewController
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. self.currentSortType = 2;
  33. self.desMoreOpen = false;
  34. [self loadSubVs];
  35. [self loasActiveDataAndV];
  36. self.vm = [ASProductListViewModel new];
  37. [self.collectV reloadData];
  38. [self.collectV.mj_header beginRefreshing];
  39. }
  40. - (void)configFilter {
  41. if (_type && ![_type isEqualToString:@""]) {
  42. [self.menuFilterV configTypeId:self.type];
  43. }
  44. }
  45. // MARK: sub mas
  46. - (void)loadSubVs {
  47. [self setNavRightSearch:^{
  48. // TODO: - to Search VC
  49. }];
  50. [self.view addSubview:self.collectV];
  51. [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.top.equalTo(self.customNavBar.mas_bottom);
  53. make.leading.trailing.equalTo(self.view);
  54. make.bottom.equalTo(self.view);
  55. }];
  56. self.sortTypeV.hidden = true;
  57. [self.view addSubview:self.menuFilterV];
  58. [self.menuFilterV mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.top.equalTo(self.customNavBar.mas_bottom);
  60. make.left.right.bottom.equalTo(self.view);
  61. }];
  62. self.menuFilterV.hidden = true;
  63. __weak typeof(self) weakSelf = self;
  64. self.collectV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  65. weakSelf.page = 1;
  66. [weakSelf.collectV.mj_footer resetNoMoreData];
  67. [weakSelf getNetData];
  68. }];
  69. self.collectV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  70. weakSelf.page += 1;
  71. [weakSelf getNetData];
  72. }];
  73. }
  74. - (void)loasActiveDataAndV {
  75. NSMutableArray *tempArr = [NSMutableArray array];
  76. for (int i=0;i < (arc4random()%6); i++) {
  77. ASHomeBannerModel *m = [ASHomeBannerModel demoModelWithType:i%3+1];
  78. [tempArr addObject:m];
  79. }
  80. self.topLinkArr = tempArr;
  81. [self.collectV reloadData];
  82. }
  83. // MARK: - delegate datasource
  84. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  85. }
  86. // MARK: - UICollectionViewDataSource
  87. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  88. switch (indexPath.section) {
  89. case 3:{
  90. if (self.canShowMoreBt) {
  91. if (indexPath.row == 0) {
  92. ASMoreBtCell *btCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASMoreBtCell" forIndexPath:indexPath];
  93. return btCell;
  94. }
  95. }
  96. ASProductListTypeDesCell *desCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASProductListTypeDesCell" forIndexPath:indexPath];
  97. [desCell setData:self.vm.cateModel.name des:self.vm.cateModel.meta_description moreStatus:self.desMoreOpen];
  98. desCell.moreStatusChange = ^{
  99. self.desMoreOpen = !self.desMoreOpen;
  100. [self.collectV reloadData];
  101. };
  102. return desCell;
  103. }
  104. default: {
  105. if (indexPath.row == 0 && self.vm.cateModel.image.length > 0) {
  106. ASProductListImageCell *c = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASProductListImageCell" forIndexPath:indexPath];
  107. [c setImgStr:[NSString stringWithFormat:@"https://%@%@%@",HostPath, CategoryImgPath, self.vm.cateModel.image]];
  108. return c;
  109. }
  110. HomeFlashDealSubCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell" forIndexPath:indexPath];
  111. NSInteger tempOff = self.vm.cateModel.image.length > 0 ? 1 : 0;
  112. if (self.vm.productList.count <= indexPath.row-tempOff)
  113. {
  114. return cell;
  115. }
  116. ASProductBaseModel *m = self.vm.productList[indexPath.row-tempOff];
  117. cell.model = m;
  118. cell.contView.addCartBt.hidden = false;
  119. return cell;
  120. }
  121. }
  122. }
  123. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  124. return 4;
  125. }
  126. - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  127. if (section == 0 || section == 1) {
  128. return 0;
  129. }
  130. if (section == 3) {
  131. NSInteger count = 0;
  132. if (self.canShowMoreBt) {
  133. count += 1;
  134. }
  135. if (self.vm.cateModel != nil && self.vm.cateModel.meta_description.length > 0) {
  136. count += 1;
  137. }
  138. return count;
  139. }
  140. return self.vm.productList.count + (self.vm.cateModel.image.length > 0 ? 1 : 0);
  141. }
  142. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  143. if (section == 0) {
  144. CGSize size = CGSizeZero;
  145. if (self.topLinkArr.count > 0) {
  146. size = CGSizeMake(KScreenWidth, 40);
  147. }
  148. return size;
  149. }
  150. if (section == 1) {
  151. return CGSizeMake(KScreenWidth, 60);
  152. }
  153. return CGSizeZero;
  154. }
  155. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  156. if (indexPath.section == 0 && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
  157. ASProductListActiveHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView" forIndexPath:indexPath];
  158. [v setData:self.topLinkArr tapBlock:^(ASHomeBannerModel * _Nullable m) {
  159. }];
  160. return v;
  161. }
  162. if (indexPath.section == 1 && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
  163. ASProductListMenuHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView" forIndexPath:indexPath];
  164. __block typeof(self) weakself = self;
  165. NSString *titleStr = @"SORT BY";
  166. if (self.currentSortType < self.sortTypeV.titles.count) {
  167. titleStr = self.sortTypeV.titles[self.currentSortType];
  168. }
  169. [v setData:titleStr sortBlock:^{
  170. // if (weakself.productArr.count > 0) {
  171. // [weakself.collectV scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:true];
  172. // }
  173. [weakself showSortV];
  174. } menuBlock:^{
  175. [weakself showMenuV];
  176. }];
  177. // if (self.sortTypeV.titles.count > self.currentSortType) {
  178. // [v.sortBt setTitle:self.sortTypeV.titles[self.currentSortType] forState:UIControlStateNormal];
  179. // }
  180. return v;
  181. }
  182. return nil;
  183. }
  184. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  185. switch (indexPath.section) {
  186. case 3:{
  187. if (self.canShowMoreBt) {
  188. if (indexPath.row == 0) {
  189. return CGSizeMake(KScreenWidth-20, 20+36+50);
  190. }
  191. }
  192. if (!self.desMoreOpen) {
  193. return CGSizeMake(KScreenWidth-20, 35+60+24);
  194. }
  195. //设置行间距
  196. NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
  197. [paragraphStyle1 setLineSpacing:12];
  198. CGFloat h = [self.vm.cateModel.meta_description boundingRectWithSize:CGSizeMake(KScreenWidth-10-35, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : paragraphStyle1} context:nil].size.height;
  199. return CGSizeMake(KScreenWidth-20, 35+60+(h > 24 ? h : 24));
  200. }
  201. case 2: {
  202. if (indexPath.row == 0 && self.vm.cateModel.image.length > 0) {
  203. CGFloat w = KScreenWidth-20;
  204. CGFloat collH = w/355.0*118;
  205. return CGSizeMake(w, collH);
  206. }
  207. CGFloat marg = 10;
  208. CGFloat w = (KScreenWidth-3*marg)/2;
  209. CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg;
  210. return CGSizeMake(w, collH);
  211. }
  212. default:{
  213. CGFloat marg = 10;
  214. CGFloat w = (KScreenWidth-3*marg)/2;
  215. CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg;
  216. return CGSizeMake(w, collH);
  217. }
  218. }
  219. }
  220. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  221. return 10;
  222. }
  223. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  224. return 10;
  225. }
  226. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  227. if (section == 0 || section == 1) {
  228. return UIEdgeInsetsZero;
  229. }
  230. return UIEdgeInsetsMake(10, 10, 10, 10);
  231. }
  232. // MARK: - subvs
  233. -(UICollectionView *)collectV {
  234. if (!_collectV) {
  235. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  236. layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
  237. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  238. CGFloat collH = (KScreenWidth-30)/2 + 126;
  239. layout.itemSize = CGSizeMake((KScreenWidth - 30)/2, collH);
  240. UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, collH) collectionViewLayout:layout];
  241. collV.backgroundColor = [UIColor whiteColor];
  242. collV.alwaysBounceVertical = true;
  243. collV.scrollEnabled = true;
  244. collV.delegate = self;
  245. collV.dataSource = self;
  246. collV.showsHorizontalScrollIndicator = false;
  247. collV.showsVerticalScrollIndicator = false;
  248. [collV registerClass:[HomeFlashDealSubCollectCell class] forCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell"];
  249. [collV registerClass:[ASProductListImageCell class] forCellWithReuseIdentifier:@"ASProductListImageCell"];
  250. [collV registerClass:[ASProductListTypeDesCell class] forCellWithReuseIdentifier:@"ASProductListTypeDesCell"];
  251. [collV registerClass:[ASMoreBtCell class] forCellWithReuseIdentifier:@"ASMoreBtCell"];
  252. [collV registerClass:[ASProductListActiveHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView"];
  253. [collV registerClass:[ASProductListMenuHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView"];
  254. _collectV = collV;
  255. }
  256. return _collectV;
  257. }
  258. - (KWProductMenuFilterView *)menuFilterV {
  259. if (!_menuFilterV) {
  260. KWProductMenuFilterView *v = [[KWProductMenuFilterView alloc] initWithFrame:self.view.bounds];
  261. __block typeof(self) weakself = self;
  262. [v setResultFilterDic:^(NSDictionary<NSString *,KWProductListFilterSubModel *> * _Nonnull arr) {
  263. weakself.vm.selectDic = [NSMutableDictionary dictionaryWithDictionary:arr];
  264. // [weakself.collectV.mj_header beginRefreshing];
  265. }];
  266. _menuFilterV = v;
  267. }
  268. return _menuFilterV;
  269. }
  270. - (ASProductSortFilterView *)sortTypeV {
  271. if (!_sortTypeV) {
  272. ASProductSortFilterView *v = [[ASProductSortFilterView alloc] initWithFrame:self.view.bounds];
  273. __block typeof(self) weakself = self;
  274. [v setSelectIndex:^(NSInteger index) {
  275. weakself.currentSortType = index;
  276. [weakself.collectV reloadData];
  277. [weakself.collectV.mj_header beginRefreshing];
  278. }];
  279. _sortTypeV = v;
  280. }
  281. return _sortTypeV;
  282. }
  283. -(void)showSortV {
  284. [self.sortTypeV showAnimate:![self.keyWord isEqualToString:@""]];
  285. }
  286. -(void)showMenuV {
  287. [self.menuFilterV showAnimate:self.vm.selectDic];
  288. }
  289. -(void)getNetData {
  290. [MBProgressHUD showHUDAddedTo:self.view animated:true];
  291. __weak typeof(self) weakSelf = self;
  292. [self.vm getProductListData:self.type page:self.page orderBy:self.currentSortType dir:@"DESC" com:^(BOOL hasNext, NSString * _Nonnull msg) {
  293. [MBProgressHUD hideHUDForView:weakSelf.view animated:true];
  294. [weakSelf.collectV.mj_header endRefreshing];
  295. [weakSelf.collectV.mj_footer endRefreshing];
  296. weakSelf.canShowMoreBt = true;
  297. if (!hasNext) {
  298. weakSelf.canShowMoreBt = false;
  299. [weakSelf.collectV.mj_footer endRefreshingWithNoMoreData];
  300. }
  301. [self.collectV reloadData];
  302. if (!msg.isEmpty) {
  303. [weakSelf.view makeToast:msg];
  304. }
  305. }];
  306. }
  307. @end