YMPersonalPageDynamicView.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // YMPersonalPageDynamicView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/3.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMPersonalPageDynamicView.h"
  9. #import "YMPersonalPageViewModel.h"
  10. #import "YMPersonalPageDynamicCell.h"
  11. #import "CHTCollectionViewWaterfallLayout.h"
  12. @interface YMPersonalPageDynamicView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,CHTCollectionViewDelegateWaterfallLayout>
  13. /// 个人主页VM
  14. @property (nonatomic, strong) YMPersonalPageViewModel *viewModel;
  15. /// 个人主页动态标签
  16. @property (nonatomic, strong) UILabel *personalPageDynamicLb;
  17. /// 个人主页动态数量标签
  18. @property (nonatomic, strong) UILabel *personalPageDynamicNumberLb;
  19. /// 查看全部按钮
  20. @property (nonatomic, strong) UIButton *seeAllBtn;
  21. /// 动态排版
  22. @property (nonatomic, strong) CHTCollectionViewWaterfallLayout *dynamicLayout;
  23. /// 动态容器列表
  24. @property (nonatomic, strong) UICollectionView *dynamicCollectionView;
  25. @end
  26. @implementation YMPersonalPageDynamicView
  27. - (void)ym_setupViews{
  28. [self addSubview:self.personalPageDynamicLb];
  29. [self addSubview:self.personalPageDynamicNumberLb];
  30. [self addSubview:self.seeAllBtn];
  31. [self addSubview:self.dynamicCollectionView];
  32. [self setNeedsUpdateConstraints];
  33. [self updateConstraintsIfNeeded];
  34. }
  35. - (void)updateConstraints{
  36. [self.personalPageDynamicLb mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.top.equalTo(self).offset(adapt(10));
  38. make.left.equalTo(self).offset(adapt(20));
  39. }];
  40. [self.personalPageDynamicNumberLb mas_makeConstraints:^(MASConstraintMaker *make) {
  41. make.centerY.equalTo(self.personalPageDynamicLb.mas_centerY);
  42. make.left.equalTo(self.personalPageDynamicLb.mas_right).offset(adapt(5));
  43. }];
  44. [self.seeAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.centerY.equalTo(self.personalPageDynamicLb.mas_centerY);
  46. make.right.equalTo(self).offset(adapt(-20));
  47. }];
  48. [self.dynamicCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.top.equalTo(self.personalPageDynamicLb.mas_bottom).offset(adapt(20));
  50. make.left.equalTo(self).offset(adapt(20));
  51. make.right.equalTo(self).offset(adapt(-20));
  52. make.bottom.equalTo(self).offset(adapt(-10));
  53. }];
  54. [super updateConstraints];
  55. }
  56. - (void)ym_bindViewModel:(YMPersonalPageViewModel *)viewModel{
  57. if (!viewModel) {
  58. return;
  59. }
  60. _viewModel = viewModel;
  61. RAC(self.personalPageDynamicNumberLb, text) = RACObserve(self.viewModel, dynamicNumber);
  62. @weakify(self)
  63. [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) {
  64. @strongify(self)
  65. [self.dynamicCollectionView reloadData];
  66. [self.dynamicCollectionView layoutIfNeeded];
  67. //刷新高度
  68. CGFloat dynamicCollectionViewHeight = self.dynamicCollectionView.collectionViewLayout.collectionViewContentSize.height;
  69. [self.dynamicCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
  70. make.height.mas_equalTo(dynamicCollectionViewHeight);
  71. }];
  72. }];
  73. }
  74. #pragma mark - UICollectionViewDataSource
  75. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  76. return 1;
  77. }
  78. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  79. return self.viewModel.dynamicDataArray.count > 5 ? 5 : self.viewModel.dynamicDataArray.count;
  80. }
  81. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  82. YMPersonalPageDynamicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMPersonalPageDynamicCell class]) forIndexPath:indexPath];
  83. [cell ym_bindViewModel:self.viewModel.dynamicDataArray[indexPath.item]];
  84. return cell;
  85. }
  86. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  87. UICollectionReusableView *reusableView = nil;
  88. reusableView.backgroundColor = [UIColor clearColor];
  89. return reusableView;
  90. }
  91. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  92. return CGSizeZero;
  93. }
  94. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
  95. return CGSizeZero;
  96. }
  97. #pragma mark - UICollectionViewDelegate
  98. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  99. }
  100. #pragma mark - CHTCollectionViewDelegateWaterfallLayout
  101. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  102. return CGSizeMake(adapt(67), adapt(67));
  103. }
  104. - (UILabel *)personalPageDynamicLb{
  105. if (!_personalPageDynamicLb) {
  106. _personalPageDynamicLb = [[UILabel alloc]init];
  107. _personalPageDynamicLb.font = LCBoldFont(17);
  108. _personalPageDynamicLb.textColor = HexColorFromRGB(0x000000);
  109. _personalPageDynamicLb.textAlignment = NSTextAlignmentLeft;
  110. _personalPageDynamicLb.text = @"TA的动态";
  111. }
  112. return _personalPageDynamicLb;
  113. }
  114. - (UILabel *)personalPageDynamicNumberLb{
  115. if (!_personalPageDynamicNumberLb) {
  116. _personalPageDynamicNumberLb = [[UILabel alloc]init];
  117. _personalPageDynamicNumberLb.font = LCFont(11);
  118. _personalPageDynamicNumberLb.textColor = HexColorFromRGB(0x737373);
  119. _personalPageDynamicNumberLb.textAlignment = NSTextAlignmentLeft;
  120. _personalPageDynamicNumberLb.text = @"(**)";
  121. }
  122. return _personalPageDynamicNumberLb;
  123. }
  124. - (UIButton *)seeAllBtn{
  125. if (!_seeAllBtn) {
  126. _seeAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  127. _seeAllBtn.titleLabel.font = LCFont(11);
  128. [_seeAllBtn setTitleColor:HexColorFromRGB(0x737373) forState:UIControlStateNormal];
  129. [_seeAllBtn setTitle:@"查看更多" forState:UIControlStateNormal];
  130. [_seeAllBtn setImage:ImageByName(@"ym_personal_page_arrow_icon") forState:UIControlStateNormal];
  131. _seeAllBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  132. [_seeAllBtn setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
  133. CGFloat margin = 3;
  134. _seeAllBtn.imageEdgeInsets = UIEdgeInsetsMake(0, margin, 0, -margin);
  135. _seeAllBtn.userInteractionEnabled = NO;
  136. }
  137. return _seeAllBtn;
  138. }
  139. - (CHTCollectionViewWaterfallLayout *)dynamicLayout{
  140. if (!_dynamicLayout) {
  141. _dynamicLayout = [[CHTCollectionViewWaterfallLayout alloc] init];
  142. _dynamicLayout.columnCount = 5;
  143. _dynamicLayout.sectionInset = UIEdgeInsetsZero;
  144. }
  145. return _dynamicLayout;
  146. }
  147. - (UICollectionView *)dynamicCollectionView{
  148. if (!_dynamicCollectionView) {
  149. _dynamicCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.dynamicLayout];
  150. _dynamicCollectionView.delegate = self;
  151. _dynamicCollectionView.dataSource = self;
  152. _dynamicCollectionView.userInteractionEnabled = NO;
  153. _dynamicCollectionView.showsVerticalScrollIndicator = NO;
  154. _dynamicCollectionView.showsHorizontalScrollIndicator = NO;
  155. _dynamicCollectionView.backgroundColor = UIColor.whiteColor;
  156. [_dynamicCollectionView registerClass:[YMPersonalPageDynamicCell class] forCellWithReuseIdentifier:NSStringFromClass([YMPersonalPageDynamicCell class])];
  157. }
  158. return _dynamicCollectionView;
  159. }
  160. @end