// // YMMemberRenewalItemView.m // MSYOUPAI // // Created by YoMi on 2024/2/27. // Copyright © 2024 MS. All rights reserved. // #import "YMMemberRenewalItemView.h" #import "YMMemberCenterViewModel.h" #import "YMMemberRenewalItemCell.h" @interface YMMemberRenewalItemView () /// 会员中心VM @property (nonatomic, strong) YMMemberCenterViewModel *viewModel; /// 续费项目排版 @property (nonatomic, strong) UICollectionViewFlowLayout *renewalItemLayout; /// 续费项目容器列表 @property (nonatomic, strong) UICollectionView *renewalItemCollectionView; @end @implementation YMMemberRenewalItemView - (void)ym_setupViews{ [self addSubview:self.renewalItemCollectionView]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.renewalItemCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self); make.left.equalTo(self); make.right.equalTo(self); make.bottom.equalTo(self); }]; [super updateConstraints]; } - (void)ym_bindViewModel:(YMMemberCenterViewModel *)viewModel{ if (!viewModel) { return; } _viewModel = viewModel; @weakify(self) [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) { @strongify(self) [self.renewalItemCollectionView reloadData]; [self.renewalItemCollectionView layoutIfNeeded]; //刷新高度 // CGFloat renewalItemCollectionViewHeight = self.renewalItemCollectionView.collectionViewLayout.collectionViewContentSize.height; // [self.renewalItemCollectionView mas_updateConstraints:^(MASConstraintMaker *make) { // make.height.mas_equalTo(renewalItemCollectionViewHeight); // }]; // ///默认选中 if (self.viewModel.renewalItemDataArray.count > 0) { /// 如果数组大于等于2判断是0下标还是1下标 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.viewModel.renewalItemDataArray.count >= 2 ? 1 : 0 inSection:0]; [self.renewalItemCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; [self collectionView:self.renewalItemCollectionView didSelectItemAtIndexPath:indexPath]; } }]; } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.viewModel.renewalItemDataArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YMMemberRenewalItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMMemberRenewalItemCell class]) forIndexPath:indexPath]; [cell ym_bindViewModel:self.viewModel.renewalItemDataArray[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{ [self.viewModel.selectedRechargeItemSubject sendNext:@(indexPath.item)]; } #pragma mark - CHTCollectionViewDelegateWaterfallLayout - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(adapt(108), adapt(130)); } - (UICollectionViewFlowLayout *)renewalItemLayout{ if (!_renewalItemLayout) { _renewalItemLayout = [[UICollectionViewFlowLayout alloc] init]; _renewalItemLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _renewalItemLayout.minimumLineSpacing = (kScreenWidth - adapt(108*3) - adapt(20))/2; _renewalItemLayout.minimumInteritemSpacing = adapt(0); } return _renewalItemLayout; } - (UICollectionView *)renewalItemCollectionView{ if (!_renewalItemCollectionView) { _renewalItemCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.renewalItemLayout]; _renewalItemCollectionView.delegate = self; _renewalItemCollectionView.dataSource = self; _renewalItemCollectionView.showsVerticalScrollIndicator = NO; _renewalItemCollectionView.showsHorizontalScrollIndicator = NO; _renewalItemCollectionView.backgroundColor = UIColor.clearColor; [_renewalItemCollectionView registerClass:[YMMemberRenewalItemCell class] forCellWithReuseIdentifier:NSStringFromClass([YMMemberRenewalItemCell class])]; } return _renewalItemCollectionView; } @end