// // YMPersonalPageGiftWallView.m // MSYOUPAI // // Created by YoMi on 2024/3/14. // Copyright © 2024 MS. All rights reserved. // #import "YMPersonalPageGiftWallView.h" #import "YMPersonalPageViewModel.h" #import "YMPersonalPageGiftWallCell.h" #import "CHTCollectionViewWaterfallLayout.h" @interface YMPersonalPageGiftWallView () /// 个人主页VM @property (nonatomic, strong) YMPersonalPageViewModel *viewModel; /// 个人主页礼物墙标签 @property (nonatomic, strong) UILabel *personalPageGiftWallLb; /// 动态排版 @property (nonatomic, strong) CHTCollectionViewWaterfallLayout *giftWallLayout; /// 动态容器列表 @property (nonatomic, strong) UICollectionView *giftWallCollectionView; @end @implementation YMPersonalPageGiftWallView - (void)ym_setupViews{ [self addSubview:self.personalPageGiftWallLb]; [self addSubview:self.giftWallCollectionView]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.personalPageGiftWallLb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(adapt(10)); make.left.equalTo(self).offset(adapt(20)); }]; [self.giftWallCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.personalPageGiftWallLb.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; @weakify(self) [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) { @strongify(self) [self.giftWallCollectionView reloadData]; [self.giftWallCollectionView layoutIfNeeded]; //刷新高度 CGFloat giftWallCollectionViewHeight = self.giftWallCollectionView.collectionViewLayout.collectionViewContentSize.height; [self.giftWallCollectionView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(giftWallCollectionViewHeight); }]; }]; } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.viewModel.giftWallDataArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YMPersonalPageGiftWallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMPersonalPageGiftWallCell class]) forIndexPath:indexPath]; [cell ym_bindViewModel:self.viewModel.giftWallDataArray[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(60), adapt(100)); } - (UILabel *)personalPageGiftWallLb{ if (!_personalPageGiftWallLb) { _personalPageGiftWallLb = [[UILabel alloc]init]; _personalPageGiftWallLb.font = LCBoldFont(17); _personalPageGiftWallLb.textColor = HexColorFromRGB(0x000000); _personalPageGiftWallLb.textAlignment = NSTextAlignmentLeft; _personalPageGiftWallLb.text = @"TA的礼物"; } return _personalPageGiftWallLb; } - (CHTCollectionViewWaterfallLayout *)giftWallLayout{ if (!_giftWallLayout) { _giftWallLayout = [[CHTCollectionViewWaterfallLayout alloc] init]; _giftWallLayout.columnCount = 5; _giftWallLayout.sectionInset = UIEdgeInsetsZero; } return _giftWallLayout; } - (UICollectionView *)giftWallCollectionView{ if (!_giftWallCollectionView) { _giftWallCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.giftWallLayout]; _giftWallCollectionView.delegate = self; _giftWallCollectionView.dataSource = self; _giftWallCollectionView.userInteractionEnabled = NO; _giftWallCollectionView.showsVerticalScrollIndicator = NO; _giftWallCollectionView.showsHorizontalScrollIndicator = NO; _giftWallCollectionView.backgroundColor = UIColor.whiteColor; [_giftWallCollectionView registerClass:[YMPersonalPageGiftWallCell class] forCellWithReuseIdentifier:NSStringFromClass([YMPersonalPageGiftWallCell class])]; } return _giftWallCollectionView; } @end