YMMemberRenewalItemView.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // YMMemberRenewalItemView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/27.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMMemberRenewalItemView.h"
  9. #import "YMMemberCenterViewModel.h"
  10. #import "YMMemberRenewalItemCell.h"
  11. @interface YMMemberRenewalItemView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UICollectionViewDelegateFlowLayout>
  12. /// 会员中心VM
  13. @property (nonatomic, strong) YMMemberCenterViewModel *viewModel;
  14. /// 续费项目排版
  15. @property (nonatomic, strong) UICollectionViewFlowLayout *renewalItemLayout;
  16. /// 续费项目容器列表
  17. @property (nonatomic, strong) UICollectionView *renewalItemCollectionView;
  18. @end
  19. @implementation YMMemberRenewalItemView
  20. - (void)ym_setupViews{
  21. [self addSubview:self.renewalItemCollectionView];
  22. [self setNeedsUpdateConstraints];
  23. [self updateConstraintsIfNeeded];
  24. }
  25. - (void)updateConstraints{
  26. [self.renewalItemCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  27. make.top.equalTo(self);
  28. make.left.equalTo(self);
  29. make.right.equalTo(self);
  30. make.bottom.equalTo(self);
  31. }];
  32. [super updateConstraints];
  33. }
  34. - (void)ym_bindViewModel:(YMMemberCenterViewModel *)viewModel{
  35. if (!viewModel) {
  36. return;
  37. }
  38. _viewModel = viewModel;
  39. @weakify(self)
  40. [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) {
  41. @strongify(self)
  42. [self.renewalItemCollectionView reloadData];
  43. [self.renewalItemCollectionView layoutIfNeeded];
  44. //刷新高度
  45. // CGFloat renewalItemCollectionViewHeight = self.renewalItemCollectionView.collectionViewLayout.collectionViewContentSize.height;
  46. // [self.renewalItemCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
  47. // make.height.mas_equalTo(renewalItemCollectionViewHeight);
  48. // }];
  49. //
  50. ///默认选中
  51. if (self.viewModel.renewalItemDataArray.count > 0) {
  52. /// 如果数组大于等于2判断是0下标还是1下标
  53. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.viewModel.renewalItemDataArray.count >= 2 ? 1 : 0 inSection:0];
  54. [self.renewalItemCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  55. [self collectionView:self.renewalItemCollectionView didSelectItemAtIndexPath:indexPath];
  56. }
  57. }];
  58. }
  59. #pragma mark - UICollectionViewDataSource
  60. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  61. return 1;
  62. }
  63. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  64. return self.viewModel.renewalItemDataArray.count;
  65. }
  66. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  67. YMMemberRenewalItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMMemberRenewalItemCell class]) forIndexPath:indexPath];
  68. [cell ym_bindViewModel:self.viewModel.renewalItemDataArray[indexPath.item]];
  69. return cell;
  70. }
  71. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  72. UICollectionReusableView *reusableView = nil;
  73. reusableView.backgroundColor = [UIColor clearColor];
  74. return reusableView;
  75. }
  76. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  77. return CGSizeZero;
  78. }
  79. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
  80. return CGSizeZero;
  81. }
  82. #pragma mark - UICollectionViewDelegate
  83. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  84. [self.viewModel.selectedRechargeItemSubject sendNext:@(indexPath.item)];
  85. }
  86. #pragma mark - CHTCollectionViewDelegateWaterfallLayout
  87. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  88. return CGSizeMake(adapt(108), adapt(130));
  89. }
  90. - (UICollectionViewFlowLayout *)renewalItemLayout{
  91. if (!_renewalItemLayout) {
  92. _renewalItemLayout = [[UICollectionViewFlowLayout alloc] init];
  93. _renewalItemLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  94. _renewalItemLayout.minimumLineSpacing = (kScreenWidth - adapt(108*3) - adapt(20))/2;
  95. _renewalItemLayout.minimumInteritemSpacing = adapt(0);
  96. }
  97. return _renewalItemLayout;
  98. }
  99. - (UICollectionView *)renewalItemCollectionView{
  100. if (!_renewalItemCollectionView) {
  101. _renewalItemCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.renewalItemLayout];
  102. _renewalItemCollectionView.delegate = self;
  103. _renewalItemCollectionView.dataSource = self;
  104. _renewalItemCollectionView.showsVerticalScrollIndicator = NO;
  105. _renewalItemCollectionView.showsHorizontalScrollIndicator = NO;
  106. _renewalItemCollectionView.backgroundColor = UIColor.clearColor;
  107. [_renewalItemCollectionView registerClass:[YMMemberRenewalItemCell class] forCellWithReuseIdentifier:NSStringFromClass([YMMemberRenewalItemCell class])];
  108. }
  109. return _renewalItemCollectionView;
  110. }
  111. @end