ASHomeViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // ASHomeViewController.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/6/5.
  6. //
  7. #import "ASHomeViewController.h"
  8. #import "ASHomeBannerCell.h"
  9. #import "ASCategaryListCell.h"
  10. #import "ASHomeTipCell.h"
  11. #import "ASHomeBestSellCell.h"
  12. #import "ASHomeNewInCellTableViewCell.h"
  13. #import "ASHomeImgCell.h"
  14. #import "ASHomeFlashDealCell.h"
  15. #import "ASHomeLookingCell.h"
  16. #import "ASHomeActiveView.h"
  17. #import "ASProductListViewController.h"
  18. #import "ASProductListViewModel.h"
  19. #import "ASHomeViewModel.h"
  20. @interface ASHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
  21. @property (nonatomic, strong) ASProductListViewModel *topVm;
  22. @property (nonatomic, strong) ASHomeViewModel *vm;
  23. @property (nonatomic, strong) UITableView *tableV;
  24. @property (nonatomic, strong, nullable) ASHomeActiveView *activeV;
  25. @end
  26. @implementation ASHomeViewController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. self.vm = [ASHomeViewModel new];
  30. self.topVm = [ASProductListViewModel new];
  31. __block typeof(self) wSelf = self;
  32. [self ucHomeStyle:^{
  33. // TODO: 跳转搜索模块
  34. }];
  35. [self addSubV];
  36. [self.tableV.mj_header beginRefreshing];
  37. [self updateTopLinkData];
  38. }
  39. - (void)getTopLinkData {
  40. __weak typeof(self) weakSelf = self;
  41. [self.topVm getTopLinkList:^{
  42. [weakSelf updateTopLinkData];
  43. }];
  44. }
  45. - (void)getHomeListData {
  46. __weak typeof(self) weakSelf = self;
  47. [self.vm getHomeListData:^{
  48. [weakSelf.tableV.mj_header endRefreshing];
  49. [weakSelf.tableV reloadData];
  50. }];
  51. }
  52. - (void)getHomeTopData {
  53. __weak typeof(self) weakSelf = self;
  54. [self.vm getHomeTopData:^{
  55. [weakSelf.tableV reloadData];
  56. }];
  57. }
  58. - (void)updateTopLinkData {
  59. if (self.topVm.topLinks.count > 0) {
  60. self.activeV = [[ASHomeActiveView alloc] initWithFrame:CGRectZero];
  61. __weak typeof(self) weakSelf = self;
  62. [self.activeV setTapTopBlock:^(KWSearchHotKeyModel * _Nullable m) {
  63. NSLog(@"---toplink:-%@--", m.code);
  64. [weakSelf jumpWithModel:[m toJumpM]];
  65. }];
  66. self.tableV.tableHeaderView = self.activeV;
  67. [self.activeV setTopData:self.topVm.topLinks];
  68. } else {
  69. self.activeV.tapBlock = nil;
  70. self.activeV = nil;
  71. self.tableV.tableHeaderView = nil;
  72. }
  73. }
  74. - (void)addSubV {
  75. [self.view addSubview:self.tableV];
  76. [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
  77. make.top.equalTo(self.customNavBar.mas_bottom);
  78. make.leading.trailing.equalTo(self.view);
  79. make.bottom.equalTo(self.view);
  80. }];
  81. __weak typeof(self) weakSelf = self;
  82. self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  83. [weakSelf getTopLinkData];
  84. [weakSelf getHomeTopData];
  85. [weakSelf getHomeListData];
  86. }];
  87. }
  88. // MARK: - UITableViewDelegate
  89. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  90. {
  91. if (indexPath.section == 0) { // banner
  92. return;
  93. }
  94. }
  95. // MARK: - UITableViewDataSource
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  97. {
  98. return self.vm.listData.count + 3;
  99. }
  100. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  101. {
  102. switch (section) {
  103. case 0:// banner
  104. return self.vm.banners.count > 0 ? 1 : 0;
  105. break;
  106. case 1:// category
  107. return self.vm.categorys.count > 0 ? 1 : 0;
  108. break;
  109. case 2:// tips
  110. return 1;
  111. default:
  112. {
  113. if (self.vm.listData.count < section-3) {
  114. return 0;
  115. }
  116. ASHomeMainListModel *m = self.vm.listData[section-3];
  117. NSInteger showtype = m.showType;
  118. switch (showtype) {
  119. case 1:// best seller
  120. return m.productList.count > 0 ? 1 : 0;
  121. case 2:// new in
  122. return m.productList.count > 0 ? 1 : 0;
  123. case 3://
  124. return m.imgUrl.length > 0 ? 1 : 0;
  125. case 4:// New Arrivals
  126. return m.productList.count > 0 ? 1 : 0;
  127. case 5:// 单图活动
  128. return m.imgUrl.length > 0 ? 1 : 0;
  129. case 6:// shop by looking
  130. return m.titleArr.count > 0 ? 1 : 0;
  131. case 7:// memship
  132. return m.imgUrl.length > 0 ? 1 : 0;
  133. // case 8:// video Active
  134. // return m.videoUrl.length > 0 ? 1 : 0;
  135. // case 9:// You may also like
  136. // return m.productList.count > 0 ? 1 : 0;
  137. // case 10:// today deals
  138. // return m.productList.count > 0 ? 1 : 0;
  139. // case 11: // 带背景色和图片的 cycle cell
  140. // return m.productList.count > 0 ? 1 : 0;
  141. default:
  142. return 0;
  143. }
  144. return 1;
  145. }
  146. }
  147. return 1;
  148. }
  149. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  150. if (indexPath.section == 0) { // banner
  151. ASHomeBannerCell *bcell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeBannerCell" forIndexPath:indexPath];
  152. @weakify(self);
  153. bcell.selectCallBack = ^(NSInteger index, NSString * _Nonnull title, NSString * _Nonnull typeId, id _Nonnull model) {
  154. if (model == nil || ![model isKindOfClass:[ASJumpModel class]]) {
  155. return;
  156. }
  157. ASJumpModel *m = (ASJumpModel *)model;
  158. [weak_self jumpWithModel:m];
  159. };
  160. [bcell showData:self.vm.banners];
  161. return bcell;
  162. }
  163. if (indexPath.section == 1) { // category
  164. ASCategaryListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASCategaryListCell" forIndexPath:indexPath];
  165. cell.arr = self.vm.categorys;
  166. cell.selectCategory = ^(ASJumpModel * _Nonnull model, NSIndexPath * _Nonnull index) {
  167. // TODO: - to productlist
  168. [self jumpWithModel:model];
  169. };
  170. return cell;
  171. }
  172. if (indexPath.section == 2) {
  173. ASHomeTipCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeTipCell" forIndexPath:indexPath];
  174. return cell;
  175. }
  176. if (self.vm.listData.count <= indexPath.section-3) {
  177. return [UITableViewCell new];
  178. }
  179. ASHomeMainListModel *m = self.vm.listData[indexPath.section-3];
  180. __weak typeof(self) weakSelf = self;
  181. switch (m.showType) {
  182. case 1: {// bestSell
  183. ASHomeBestSellCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeBestSellCell" forIndexPath:indexPath];
  184. cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull m) {
  185. [weakSelf goto_WKM_GoodsDetailsC:m.Id];
  186. };
  187. cell.model = m;
  188. return cell;
  189. }
  190. case 2: {
  191. ASHomeNewInCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeNewInCellTableViewCell" forIndexPath:indexPath];
  192. cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull model) {
  193. [weakSelf goto_WKM_GoodsDetailsC: model.Id];
  194. };
  195. cell.addCartBlock = ^(NSInteger i, ASProductBaseModel * _Nonnull model) {
  196. [weakSelf action_GoodsSizeC: model.Id];
  197. };
  198. [cell setData:m];
  199. return cell;
  200. }
  201. case 3: {
  202. ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath];
  203. cell.tapImgBlock = ^{
  204. [weakSelf jumpWithModel:[m toJumpM]];
  205. };
  206. [cell setImgStr:m.imgUrl];
  207. return cell;
  208. }
  209. case 5: {
  210. ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath];
  211. cell.tapImgBlock = ^{
  212. [weakSelf jumpWithModel:[m toJumpM]];
  213. };
  214. [cell setImgStr:m.imgUrl];
  215. return cell;
  216. }
  217. case 7: {
  218. ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath];
  219. cell.tapImgBlock = ^{
  220. [weakSelf jumpWithModel:[m toJumpM]];
  221. };
  222. [cell setImgStr:m.imgUrl];
  223. return cell;
  224. }
  225. case 4: {
  226. ASHomeFlashDealCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeFlashDealCell" forIndexPath:indexPath];
  227. cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull model) {
  228. [weakSelf goto_WKM_GoodsDetailsC:model.Id];
  229. };
  230. cell.proAddCartClick = ^(NSInteger i, ASProductBaseModel * _Nonnull model) {
  231. [weakSelf action_GoodsSizeC:model.Id];
  232. };
  233. cell.model = m;
  234. return cell;
  235. }
  236. case 6: {
  237. ASHomeLookingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeLookingCell" forIndexPath:indexPath];
  238. cell.itemClick = ^(NSInteger i, ASHomeCategoryModel * _Nonnull model) {
  239. [weakSelf jumpWithModel:[model toJumpM]];
  240. };
  241. cell.model = m;
  242. return cell;
  243. }
  244. default:
  245. break;
  246. }
  247. return [UITableViewCell new];
  248. }
  249. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  250. return 40;
  251. }
  252. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  253. return UITableViewAutomaticDimension;
  254. }
  255. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  256. return 0.01;
  257. }
  258. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  259. UIView *footer = [[UIView alloc] initWithFrame:CGRectZero];
  260. footer.backgroundColor = [UIColor clearColor];
  261. return footer;
  262. }
  263. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  264. return 0.01;
  265. }
  266. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  267. UIView *footer = [[UIView alloc] initWithFrame:CGRectZero];
  268. footer.backgroundColor = [UIColor clearColor];
  269. return footer;
  270. }
  271. -(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  272. }
  273. // MARK: - nextViews
  274. - (void)pushToProductList:(NSString *)typeId title:(NSString *)title {
  275. ASProductListViewController *vc = [ASProductListViewController new];
  276. vc.titleStr = title;
  277. [self.navigationController pushViewController:vc animated:true];
  278. }
  279. // MARK: - subVs
  280. -(UITableView *)tableV {
  281. if (!_tableV) {
  282. UITableView *tabV = [[UITableView alloc] initWithFrame:self.view.bounds];
  283. tabV.delegate = self;
  284. tabV.dataSource = self;
  285. [tabV baseSet];
  286. tabV.backgroundColor = UIColor.whiteColor;
  287. tabV.separatorStyle = UITableViewCellSeparatorStyleNone;
  288. [tabV registerClass:[ASHomeBannerCell class] forCellReuseIdentifier:@"ASHomeBannerCell"];
  289. [tabV registerClass:[ASCategaryListCell class] forCellReuseIdentifier:@"ASCategaryListCell"];
  290. [tabV registerClass:[ASHomeTipCell class] forCellReuseIdentifier:@"ASHomeTipCell"];
  291. [tabV registerClass:[ASHomeBestSellCell class] forCellReuseIdentifier:@"ASHomeBestSellCell"];
  292. [tabV registerClass:[ASHomeNewInCellTableViewCell class] forCellReuseIdentifier:@"ASHomeNewInCellTableViewCell"];
  293. [tabV registerClass:[ASHomeImgCell class] forCellReuseIdentifier:@"ASHomeImgCell"];
  294. [tabV registerClass:[ASHomeFlashDealCell class] forCellReuseIdentifier:@"ASHomeFlashDealCell"];
  295. [tabV registerClass:[ASHomeLookingCell class] forCellReuseIdentifier:@"ASHomeLookingCell"];
  296. _tableV = tabV;
  297. }
  298. return _tableV;
  299. }
  300. @end