ASHomeViewController.m 12 KB

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