// // YMPersonalPageDynamicView.m // MSYOUPAI // // Created by YoMi on 2024/3/3. // Copyright © 2024 MS. All rights reserved. // #import "YMPersonalPageDynamicView.h" #import "YMPersonalPageViewModel.h" #import "YMPersonalPageDynamicCell.h" #import "CHTCollectionViewWaterfallLayout.h" @interface YMPersonalPageDynamicView () /// 个人主页VM @property (nonatomic, strong) YMPersonalPageViewModel *viewModel; /// 个人主页动态标签 @property (nonatomic, strong) UILabel *personalPageDynamicLb; /// 个人主页动态数量标签 @property (nonatomic, strong) UILabel *personalPageDynamicNumberLb; /// 查看全部按钮 @property (nonatomic, strong) UIButton *seeAllBtn; /// 动态排版 @property (nonatomic, strong) CHTCollectionViewWaterfallLayout *dynamicLayout; /// 动态容器列表 @property (nonatomic, strong) UICollectionView *dynamicCollectionView; @end @implementation YMPersonalPageDynamicView - (void)ym_setupViews{ [self addSubview:self.personalPageDynamicLb]; [self addSubview:self.personalPageDynamicNumberLb]; [self addSubview:self.seeAllBtn]; [self addSubview:self.dynamicCollectionView]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.personalPageDynamicLb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(adapt(10)); make.left.equalTo(self).offset(adapt(20)); }]; [self.personalPageDynamicNumberLb mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.personalPageDynamicLb.mas_centerY); make.left.equalTo(self.personalPageDynamicLb.mas_right).offset(adapt(5)); }]; [self.seeAllBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.personalPageDynamicLb.mas_centerY); make.right.equalTo(self).offset(adapt(-20)); }]; [self.dynamicCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.personalPageDynamicLb.mas_bottom).offset(adapt(20)); make.left.equalTo(self).offset(adapt(20)); make.right.equalTo(self).offset(adapt(-20)); make.bottom.equalTo(self).offset(adapt(-10)); }]; [super updateConstraints]; } - (void)ym_bindViewModel:(YMPersonalPageViewModel *)viewModel{ if (!viewModel) { return; } _viewModel = viewModel; RAC(self.personalPageDynamicNumberLb, text) = RACObserve(self.viewModel, dynamicNumber); @weakify(self) [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) { @strongify(self) [self.dynamicCollectionView reloadData]; [self.dynamicCollectionView layoutIfNeeded]; //刷新高度 CGFloat dynamicCollectionViewHeight = self.dynamicCollectionView.collectionViewLayout.collectionViewContentSize.height; [self.dynamicCollectionView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(dynamicCollectionViewHeight); }]; }]; } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.viewModel.dynamicDataArray.count > 5 ? 5 : self.viewModel.dynamicDataArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YMPersonalPageDynamicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMPersonalPageDynamicCell class]) forIndexPath:indexPath]; [cell ym_bindViewModel:self.viewModel.dynamicDataArray[indexPath.item]]; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ UICollectionReusableView *reusableView = nil; reusableView.backgroundColor = [UIColor clearColor]; return reusableView; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ return CGSizeZero; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{ return CGSizeZero; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ } #pragma mark - CHTCollectionViewDelegateWaterfallLayout - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(adapt(67), adapt(67)); } - (UILabel *)personalPageDynamicLb{ if (!_personalPageDynamicLb) { _personalPageDynamicLb = [[UILabel alloc]init]; _personalPageDynamicLb.font = LCBoldFont(17); _personalPageDynamicLb.textColor = HexColorFromRGB(0x000000); _personalPageDynamicLb.textAlignment = NSTextAlignmentLeft; _personalPageDynamicLb.text = @"TA的动态"; } return _personalPageDynamicLb; } - (UILabel *)personalPageDynamicNumberLb{ if (!_personalPageDynamicNumberLb) { _personalPageDynamicNumberLb = [[UILabel alloc]init]; _personalPageDynamicNumberLb.font = LCFont(11); _personalPageDynamicNumberLb.textColor = HexColorFromRGB(0x737373); _personalPageDynamicNumberLb.textAlignment = NSTextAlignmentLeft; _personalPageDynamicNumberLb.text = @"(**)"; } return _personalPageDynamicNumberLb; } - (UIButton *)seeAllBtn{ if (!_seeAllBtn) { _seeAllBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _seeAllBtn.titleLabel.font = LCFont(11); [_seeAllBtn setTitleColor:HexColorFromRGB(0x737373) forState:UIControlStateNormal]; [_seeAllBtn setTitle:@"查看更多" forState:UIControlStateNormal]; [_seeAllBtn setImage:ImageByName(@"ym_personal_page_arrow_icon") forState:UIControlStateNormal]; _seeAllBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; [_seeAllBtn setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft]; CGFloat margin = 3; _seeAllBtn.imageEdgeInsets = UIEdgeInsetsMake(0, margin, 0, -margin); _seeAllBtn.userInteractionEnabled = NO; } return _seeAllBtn; } - (CHTCollectionViewWaterfallLayout *)dynamicLayout{ if (!_dynamicLayout) { _dynamicLayout = [[CHTCollectionViewWaterfallLayout alloc] init]; _dynamicLayout.columnCount = 5; _dynamicLayout.sectionInset = UIEdgeInsetsZero; } return _dynamicLayout; } - (UICollectionView *)dynamicCollectionView{ if (!_dynamicCollectionView) { _dynamicCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.dynamicLayout]; _dynamicCollectionView.delegate = self; _dynamicCollectionView.dataSource = self; _dynamicCollectionView.userInteractionEnabled = NO; _dynamicCollectionView.showsVerticalScrollIndicator = NO; _dynamicCollectionView.showsHorizontalScrollIndicator = NO; _dynamicCollectionView.backgroundColor = UIColor.whiteColor; [_dynamicCollectionView registerClass:[YMPersonalPageDynamicCell class] forCellWithReuseIdentifier:NSStringFromClass([YMPersonalPageDynamicCell class])]; } return _dynamicCollectionView; } @end