ASHomeViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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) NSArray <ASHomeMainListModel*>*listArr;
  25. @property (nonatomic, strong, nullable) ASHomeActiveView *activeV;
  26. @end
  27. @implementation ASHomeViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. self.vm = [ASHomeViewModel new];
  31. self.topVm = [ASProductListViewModel new];
  32. __block typeof(self) wSelf = self;
  33. [self ucHomeStyle:^{
  34. // TODO: 跳转搜索模块
  35. }];
  36. [self addSubV];
  37. [self demoData];
  38. [self.tableV.mj_header beginRefreshing];
  39. [self updateTopLinkData];
  40. }
  41. - (void)demoData {
  42. _listArr = [ASHomeMainListModel homeDemoDatas];
  43. }
  44. - (void)getTopLinkData {
  45. __weak typeof(self) weakSelf = self;
  46. [self.topVm getTopLinkList:^{
  47. [weakSelf updateTopLinkData];
  48. }];
  49. }
  50. - (void)getHomeTopData {
  51. __weak typeof(self) weakSelf = self;
  52. [self.vm getHomeTopData:^{
  53. [weakSelf.tableV reloadData];
  54. }];
  55. }
  56. - (void)updateTopLinkData {
  57. if (self.topVm.topLinks.count > 0) {
  58. self.activeV = [[ASHomeActiveView alloc] initWithFrame:CGRectZero];
  59. [self.activeV setTapTopBlock:^(KWSearchHotKeyModel * _Nullable m) {
  60. NSLog(@"---toplink:-%@--", m.code);
  61. }];
  62. self.tableV.tableHeaderView = self.activeV;
  63. [self.activeV setTopData:self.topVm.topLinks];
  64. } else {
  65. self.activeV.tapBlock = nil;
  66. self.activeV = nil;
  67. self.tableV.tableHeaderView = nil;
  68. }
  69. }
  70. - (void)addSubV {
  71. [self.view addSubview:self.tableV];
  72. [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.top.equalTo(self.customNavBar.mas_bottom);
  74. make.leading.trailing.equalTo(self.view);
  75. make.bottom.equalTo(self.view);
  76. }];
  77. __weak typeof(self) weakSelf = self;
  78. self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  79. [weakSelf getTopLinkData];
  80. [weakSelf getHomeTopData];
  81. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1), dispatch_get_main_queue(), ^{
  82. [self.tableV.mj_header endRefreshing];
  83. [self.tableV reloadData];
  84. });
  85. }];
  86. }
  87. // MARK: - UITableViewDelegate
  88. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  89. {
  90. if (indexPath.section == 0) { // banner
  91. return;
  92. }
  93. }
  94. // MARK: - UITableViewDataSource
  95. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  96. {
  97. return self.listArr.count + 2;
  98. }
  99. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  100. {
  101. switch (section) {
  102. case 0:// banner
  103. return self.vm.banners.count > 0 ? 1 : 0;
  104. break;
  105. case 1:// category
  106. return self.vm.categorys.count > 0 ? 1 : 0;
  107. break;
  108. default:
  109. return 1;
  110. }
  111. return 1;
  112. }
  113. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  114. if (indexPath.section == 0) { // banner
  115. ASHomeBannerCell *bcell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeBannerCell" forIndexPath:indexPath];
  116. @weakify(self);
  117. bcell.selectCallBack = ^(NSInteger index, NSString * _Nonnull title, NSString * _Nonnull typeId, id _Nonnull model) {
  118. if (model == nil || ![model isKindOfClass:[ASJumpModel class]]) {
  119. return;
  120. }
  121. ASJumpModel *m = (ASJumpModel *)model;
  122. [weak_self jumpWithModel:m];
  123. };
  124. [bcell showData:self.vm.banners];
  125. return bcell;
  126. }
  127. if (indexPath.section == 1) { // category
  128. ASCategaryListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASCategaryListCell" forIndexPath:indexPath];
  129. cell.arr = self.vm.categorys;
  130. cell.selectCategory = ^(ASJumpModel * _Nonnull model, NSIndexPath * _Nonnull index) {
  131. // TODO: - to productlist
  132. [self jumpWithModel:model];
  133. };
  134. return cell;
  135. }
  136. if (self.listArr.count <= indexPath.row) {
  137. return [UITableViewCell new];
  138. }
  139. ASHomeMainListModel *m = self.listArr[indexPath.section-1];
  140. switch (m.showType) {
  141. case 1: { // 分类
  142. }
  143. case 2: { // tips
  144. ASHomeTipCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeTipCell" forIndexPath:indexPath];
  145. return cell;
  146. }
  147. case 3: {// bestSell
  148. ASHomeBestSellCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeBestSellCell" forIndexPath:indexPath];
  149. cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull m) {
  150. };
  151. cell.model = m;
  152. return cell;
  153. }
  154. case 4: {
  155. ASHomeNewInCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeNewInCellTableViewCell" forIndexPath:indexPath];
  156. [cell setData:m];
  157. return cell;
  158. }
  159. case 5: {
  160. ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath];
  161. cell.tapImgBlock = ^{
  162. // TODO: -
  163. };
  164. [cell setImgStr:m.imgUrl];
  165. return cell;
  166. }
  167. case 6: {
  168. ASHomeFlashDealCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeFlashDealCell" forIndexPath:indexPath];
  169. cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull m) {
  170. };
  171. cell.proAddCartClick = ^(NSInteger i, ASProductBaseModel * _Nonnull m) {
  172. };
  173. cell.model = m;
  174. return cell;
  175. }
  176. case 7: {
  177. ASHomeLookingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeLookingCell" forIndexPath:indexPath];
  178. cell.itemClick = ^(NSInteger i, ASHomeCategoryModel * _Nonnull m) {
  179. };
  180. cell.model = m;
  181. return cell;
  182. }
  183. default:
  184. break;
  185. }
  186. return [UITableViewCell new];
  187. }
  188. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  189. return 40;
  190. }
  191. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  192. return UITableViewAutomaticDimension;
  193. }
  194. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  195. return 0.01;
  196. }
  197. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  198. UIView *footer = [[UIView alloc] initWithFrame:CGRectZero];
  199. footer.backgroundColor = [UIColor clearColor];
  200. return footer;
  201. }
  202. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  203. return 0.01;
  204. }
  205. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  206. UIView *footer = [[UIView alloc] initWithFrame:CGRectZero];
  207. footer.backgroundColor = [UIColor clearColor];
  208. return footer;
  209. }
  210. -(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  211. }
  212. // MARK: - nextViews
  213. - (void)pushToProductList:(NSString *)typeId title:(NSString *)title {
  214. ASProductListViewController *vc = [ASProductListViewController new];
  215. vc.titleStr = title;
  216. [self.navigationController pushViewController:vc animated:true];
  217. }
  218. // MARK: - subVs
  219. -(UITableView *)tableV {
  220. if (!_tableV) {
  221. UITableView *tabV = [[UITableView alloc] initWithFrame:self.view.bounds];
  222. tabV.delegate = self;
  223. tabV.dataSource = self;
  224. [tabV baseSet];
  225. tabV.backgroundColor = UIColor.whiteColor;
  226. tabV.separatorStyle = UITableViewCellSeparatorStyleNone;
  227. [tabV registerClass:[ASHomeBannerCell class] forCellReuseIdentifier:@"ASHomeBannerCell"];
  228. [tabV registerClass:[ASCategaryListCell class] forCellReuseIdentifier:@"ASCategaryListCell"];
  229. [tabV registerClass:[ASHomeTipCell class] forCellReuseIdentifier:@"ASHomeTipCell"];
  230. [tabV registerClass:[ASHomeBestSellCell class] forCellReuseIdentifier:@"ASHomeBestSellCell"];
  231. [tabV registerClass:[ASHomeNewInCellTableViewCell class] forCellReuseIdentifier:@"ASHomeNewInCellTableViewCell"];
  232. [tabV registerClass:[ASHomeImgCell class] forCellReuseIdentifier:@"ASHomeImgCell"];
  233. [tabV registerClass:[ASHomeFlashDealCell class] forCellReuseIdentifier:@"ASHomeFlashDealCell"];
  234. [tabV registerClass:[ASHomeLookingCell class] forCellReuseIdentifier:@"ASHomeLookingCell"];
  235. _tableV = tabV;
  236. }
  237. return _tableV;
  238. }
  239. @end