YMHomeChildController.m 8.8 KB

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