YMHomePageListViewController.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // YMHomePageListViewController.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/11.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMHomePageListViewController.h"
  9. #import "YMHomePageListViewModel.h"
  10. #import "YMChatNotificationViewModel.h"
  11. #import "YMHomePageListCell.h"
  12. #import "CHTCollectionViewWaterfallLayout.h"
  13. @interface YMHomePageListViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,CHTCollectionViewDelegateWaterfallLayout>
  14. /// 滚动视图回调
  15. @property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
  16. /// 列表VM
  17. @property (nonatomic, strong) YMHomePageListViewModel *viewModel;
  18. /// 内容排版
  19. @property (nonatomic, strong) CHTCollectionViewWaterfallLayout *contentLayout;
  20. /// 容器列表
  21. @property (nonatomic, strong) UICollectionView *contentCollectionView;
  22. @end
  23. @implementation YMHomePageListViewController
  24. @dynamic viewModel;
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. [self setNavHidden:YES];
  28. [OCNotificationCenter addObserver:self selector:@selector(refreshHomeData:) name:REFRESH_HOME_NOTIFICATION object:nil];
  29. YMEmptyView *empty = [YMEmptyView emptyActionViewWithImage:ImageByName(@"ym_common_no_data_icon") titleStr:@"开启“个性化推荐”后可启用推荐列表" detailStr:@"" btnTitleStr:@"立即开启" btnClickBlock:^{
  30. YMChatNotificationViewModel *chatNotificationVM = [[YMChatNotificationViewModel alloc]initWithParams:@{}];
  31. [YMRouter openURL:stringFormat(@"%@%@", YM_ROUTER_URL_PREFIX, YM_ROUTER_CHAT_NOTIFICATION) withUserInfo:@{
  32. RouterViewModel:chatNotificationVM
  33. } completion:nil];
  34. }];
  35. empty.contentViewOffset = adapt(-80);
  36. empty.imageSize = kEmptyViewSize;
  37. empty.titleLabFont = LCFont(13);
  38. empty.titleLabTextColor = HexColorFromRGB(0x9b9b9b);
  39. empty.actionBtnCornerRadius = adapt(10);
  40. empty.actionBtnTitleColor = HexColorFromRGB(0xFFFFFF);
  41. empty.actionBtnBackGroundColor = HexColorFromRGB(0xfd7bc5);
  42. self.view.ym_emptyView = empty;
  43. self.view.ym_emptyView.autoShowEmptyView = NO;
  44. }
  45. - (void)viewWillAppear:(BOOL)animated{
  46. [super viewWillAppear:animated];
  47. [self homePageIsHidePersonalizedRecommendationTips];
  48. }
  49. - (void)ym_setupViews {
  50. [self.view addSubview:self.contentCollectionView];
  51. [self.view setNeedsUpdateConstraints];
  52. [self.view updateConstraintsIfNeeded];
  53. }
  54. - (void)updateViewConstraints {
  55. [self.contentCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.top.equalTo(self.view);
  57. make.left.equalTo(self.view);
  58. make.right.equalTo(self.view);
  59. make.bottom.equalTo(self.view).offset(-TabbarHeight);
  60. }];
  61. [super updateViewConstraints];
  62. }
  63. - (void)ym_bindViewModel {
  64. [self headerRefreshing];
  65. @weakify(self)
  66. [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) {
  67. @strongify(self)
  68. if (self.refreshBlock) {
  69. self.refreshBlock();
  70. }
  71. [self.contentCollectionView reloadData];
  72. switch ([result intValue]) {
  73. case YMHeaderRefresh_HasMoreData:
  74. case YMHeaderRefresh_HasNoMoreData:
  75. {
  76. [self.contentCollectionView.mj_footer endRefreshing];
  77. }
  78. break;
  79. case YMFooterRefresh_HasMoreData:
  80. {
  81. [self.contentCollectionView.mj_footer endRefreshing];
  82. if (self.contentCollectionView.mj_footer == self.noLoadMoreFooter) {
  83. self.contentCollectionView.mj_footer = self.loadMoreFooter;
  84. }
  85. }
  86. break;
  87. case YMFooterRefresh_HasNoMoreData:
  88. {
  89. [self.contentCollectionView.mj_footer endRefreshing];
  90. if (self.contentCollectionView.mj_footer == self.loadMoreFooter) {
  91. self.contentCollectionView.mj_footer = self.noLoadMoreFooter;
  92. }
  93. }
  94. break;
  95. case YMRefreshError: {
  96. [self.contentCollectionView.mj_footer endRefreshing];
  97. }
  98. break;
  99. default:
  100. break;
  101. }
  102. [self.contentCollectionView ym_endLoading];
  103. }];
  104. }
  105. #pragma mark - UICollectionViewDataSource
  106. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  107. return 1;
  108. }
  109. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  110. return self.viewModel.listDataArray.count;
  111. }
  112. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  113. YMHomePageListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMHomePageListCell class]) forIndexPath:indexPath];
  114. YMHomePageListCellViewModel *cellVM = self.viewModel.listDataArray[indexPath.item];
  115. cellVM.jxCategoryPageIndex = self.pageIndex;
  116. cellVM.jxPageTitle = self.jxPageTitle;
  117. [cellVM updateWithPageIndex];
  118. @weakify(self)
  119. cellVM.changeAccostStatusBlock = ^{
  120. @strongify(self)
  121. [UIView performWithoutAnimation:^{
  122. [self.contentCollectionView reloadItemsAtIndexPaths:@[indexPath]];
  123. }];
  124. };
  125. [cell ym_bindViewModel:cellVM];
  126. return cell;
  127. }
  128. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  129. UICollectionReusableView *reusableView = nil;
  130. reusableView.backgroundColor = [UIColor clearColor];
  131. return reusableView;
  132. }
  133. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  134. return CGSizeZero;
  135. }
  136. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
  137. return CGSizeZero;
  138. }
  139. #pragma mark - UICollectionViewDelegate
  140. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  141. if (self.viewModel.listDataArray[indexPath.item].userId != 0) {
  142. [self.viewModel.gotoPersonalPageSubject sendNext:@(self.viewModel.listDataArray[indexPath.item].userId)];
  143. }
  144. }
  145. #pragma mark - CHTCollectionViewDelegateWaterfallLayout
  146. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  147. return CGSizeMake(adapt(170), adapt(220));
  148. }
  149. ///首页是否隐藏个性化推荐提示
  150. - (void)homePageIsHidePersonalizedRecommendationTips{
  151. if ([[OCUserDefaults objectForKey:kIS_HIDE_PERSONALIZED_RECOMMENDATION_TIPS] boolValue]) {
  152. self.contentCollectionView.hidden = NO;
  153. [self.view ym_hideEmptyView];
  154. } else {
  155. self.contentCollectionView.hidden = YES;
  156. [self.view ym_showEmptyView];
  157. }
  158. }
  159. - (void)refreshHomeData:(NSNotification *)notice{
  160. [self headerRefreshing];
  161. }
  162. - (void)headerRefreshing{
  163. [self.contentCollectionView ym_startLoading];
  164. self.viewModel.currentPage = 1;
  165. [self.viewModel getHomePageListData];
  166. }
  167. - (void)footerRefreshing{
  168. [self.contentCollectionView ym_startLoading];
  169. self.viewModel.currentPage++;
  170. [self.viewModel getHomePageListData];
  171. }
  172. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  173. !self.scrollCallback ?: self.scrollCallback(scrollView);
  174. }
  175. #pragma mark - JXPagingViewListViewDelegate
  176. - (UIView *)listView {
  177. return self.view;
  178. }
  179. - (UIScrollView *)listScrollView {
  180. return self.contentCollectionView;
  181. }
  182. - (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
  183. self.scrollCallback = callback;
  184. }
  185. - (void)listWillAppear {
  186. // NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
  187. }
  188. - (void)listDidAppear {
  189. // NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
  190. }
  191. - (void)listWillDisappear {
  192. // NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
  193. }
  194. - (void)listDidDisappear {
  195. // NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
  196. }
  197. - (CHTCollectionViewWaterfallLayout *)contentLayout{
  198. if (!_contentLayout) {
  199. _contentLayout = [[CHTCollectionViewWaterfallLayout alloc] init];
  200. _contentLayout.columnCount = 2;
  201. _contentLayout.sectionInset = UIEdgeInsetsMake(adapt(5), adapt(5), adapt(5), adapt(5));
  202. }
  203. return _contentLayout;
  204. }
  205. - (UICollectionView *)contentCollectionView{
  206. if (!_contentCollectionView) {
  207. _contentCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.contentLayout];
  208. _contentCollectionView.delegate = self;
  209. _contentCollectionView.dataSource = self;
  210. _contentCollectionView.showsVerticalScrollIndicator = NO;
  211. _contentCollectionView.showsHorizontalScrollIndicator = NO;
  212. _contentCollectionView.backgroundColor = UIColor.whiteColor;
  213. [_contentCollectionView registerClass:[YMHomePageListCell class] forCellWithReuseIdentifier:NSStringFromClass([YMHomePageListCell class])];
  214. _contentCollectionView.mj_footer = self.noLoadMoreFooter;
  215. YMEmptyView *empty = [YMEmptyView emptyViewWithImageStr:@"ym_common_no_data_icon" titleStr:@"暂无数据" detailStr:@""];
  216. empty.imageSize = kEmptyViewSize;
  217. _contentCollectionView.ym_emptyView = empty;
  218. _contentCollectionView.ym_emptyView.autoShowEmptyView = NO;
  219. }
  220. return _contentCollectionView;
  221. }
  222. @end