ASHomeViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.moreClick = ^(ASHomeMainListModel * _Nonnull m) {
  188. [weakSelf pushToProductList:m.title typeid:m.typeId];
  189. };
  190. cell.model = m;
  191. return cell;
  192. }
  193. case 2: {
  194. ASHomeNewInCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeNewInCellTableViewCell" forIndexPath:indexPath];
  195. cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull model) {
  196. [weakSelf goto_WKM_GoodsDetailsC: model.Id];
  197. };
  198. cell.addCartBlock = ^(NSInteger i, ASProductBaseModel * _Nonnull model) {
  199. [weakSelf action_GoodsSizeC: model.Id];
  200. };
  201. cell.moreClick = ^(ASHomeMainListModel * _Nonnull m) {
  202. [weakSelf pushToProductList:m.title typeid:m.typeId];
  203. };
  204. [cell setData:m];
  205. return cell;
  206. }
  207. case 3: {
  208. ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath];
  209. cell.tapImgBlock = ^{
  210. [weakSelf jumpWithModel:[m toJumpM]];
  211. };
  212. [cell setImgStr:m.imgUrl];
  213. return cell;
  214. }
  215. case 5: {
  216. ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath];
  217. cell.tapImgBlock = ^{
  218. [weakSelf jumpWithModel:[m toJumpM]];
  219. };
  220. [cell setImgStr:m.imgUrl];
  221. return cell;
  222. }
  223. case 7: {
  224. ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath];
  225. cell.tapImgBlock = ^{
  226. [weakSelf jumpWithModel:[m toJumpM]];
  227. };
  228. [cell setImgStr:m.imgUrl];
  229. return cell;
  230. }
  231. case 4: {
  232. ASHomeFlashDealCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeFlashDealCell" forIndexPath:indexPath];
  233. cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull model) {
  234. [weakSelf goto_WKM_GoodsDetailsC:model.Id];
  235. };
  236. cell.proAddCartClick = ^(NSInteger i, ASProductBaseModel * _Nonnull model) {
  237. [weakSelf action_GoodsSizeC:model.Id];
  238. };
  239. cell.moreClick = ^(ASHomeMainListModel * _Nonnull m) {
  240. [weakSelf pushToProductList:m.title typeid:m.typeId];
  241. };
  242. cell.model = m;
  243. return cell;
  244. }
  245. case 6: {
  246. ASHomeLookingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeLookingCell" forIndexPath:indexPath];
  247. cell.itemClick = ^(NSInteger i, ASHomeCategoryModel * _Nonnull model) {
  248. [weakSelf jumpWithModel:[model toJumpM]];
  249. };
  250. cell.model = m;
  251. return cell;
  252. }
  253. default:
  254. break;
  255. }
  256. return [UITableViewCell new];
  257. }
  258. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  259. return 40;
  260. }
  261. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  262. return UITableViewAutomaticDimension;
  263. }
  264. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  265. return 0.01;
  266. }
  267. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  268. UIView *footer = [[UIView alloc] initWithFrame:CGRectZero];
  269. footer.backgroundColor = [UIColor clearColor];
  270. return footer;
  271. }
  272. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  273. return 0.01;
  274. }
  275. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  276. UIView *footer = [[UIView alloc] initWithFrame:CGRectZero];
  277. footer.backgroundColor = [UIColor clearColor];
  278. return footer;
  279. }
  280. -(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  281. }
  282. // MARK: - nextViews
  283. - (void)pushToProductList:(NSString *)typeId title:(NSString *)title {
  284. ASProductListViewController *vc = [ASProductListViewController new];
  285. vc.titleStr = title;
  286. [self.navigationController pushViewController:vc animated:true];
  287. }
  288. // MARK: - subVs
  289. -(UITableView *)tableV {
  290. if (!_tableV) {
  291. UITableView *tabV = [[UITableView alloc] initWithFrame:self.view.bounds];
  292. tabV.delegate = self;
  293. tabV.dataSource = self;
  294. [tabV baseSet];
  295. tabV.backgroundColor = UIColor.whiteColor;
  296. tabV.separatorStyle = UITableViewCellSeparatorStyleNone;
  297. [tabV registerClass:[ASHomeBannerCell class] forCellReuseIdentifier:@"ASHomeBannerCell"];
  298. [tabV registerClass:[ASCategaryListCell class] forCellReuseIdentifier:@"ASCategaryListCell"];
  299. [tabV registerClass:[ASHomeTipCell class] forCellReuseIdentifier:@"ASHomeTipCell"];
  300. [tabV registerClass:[ASHomeBestSellCell class] forCellReuseIdentifier:@"ASHomeBestSellCell"];
  301. [tabV registerClass:[ASHomeNewInCellTableViewCell class] forCellReuseIdentifier:@"ASHomeNewInCellTableViewCell"];
  302. [tabV registerClass:[ASHomeImgCell class] forCellReuseIdentifier:@"ASHomeImgCell"];
  303. [tabV registerClass:[ASHomeFlashDealCell class] forCellReuseIdentifier:@"ASHomeFlashDealCell"];
  304. [tabV registerClass:[ASHomeLookingCell class] forCellReuseIdentifier:@"ASHomeLookingCell"];
  305. _tableV = tabV;
  306. }
  307. return _tableV;
  308. }
  309. @end