ASHomeViewController.m 13 KB

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