YMAccountBalanceRechargeItemView.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // YMAccountBalanceRechargeItemView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/28.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMAccountBalanceRechargeItemView.h"
  9. #import "YMAccountBalanceViewModel.h"
  10. #import "YMAccountBalanceRechargeItemCell.h"
  11. #import "CHTCollectionViewWaterfallLayout.h"
  12. @interface YMAccountBalanceRechargeItemView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,CHTCollectionViewDelegateWaterfallLayout>
  13. /// 账户余额VM
  14. @property (nonatomic, strong) YMAccountBalanceViewModel *viewModel;
  15. /// 充值项目提示标签
  16. @property (nonatomic, strong) UILabel *rechargeItemTipsLb;
  17. @property (nonatomic, strong) UIImageView *barnertView;
  18. /// 充值项目排版
  19. @property (nonatomic, strong) CHTCollectionViewWaterfallLayout *rechargeItemLayout;
  20. /// 充值项目容器列表
  21. @property (nonatomic, strong) UICollectionView *rechargeItemCollectionView;
  22. @end
  23. @implementation YMAccountBalanceRechargeItemView
  24. - (void)ym_setupViews{
  25. self.backgroundColor = UIColor.clearColor;
  26. [self addSubview:self.barnertView];
  27. [self addSubview:self.rechargeItemCollectionView];
  28. [self setNeedsUpdateConstraints];
  29. [self updateConstraintsIfNeeded];
  30. }
  31. - (void)updateConstraints{
  32. // [self.rechargeItemTipsLb mas_makeConstraints:^(MASConstraintMaker *make) {
  33. // make.top.equalTo(self).offset(adapt(10));
  34. // make.left.equalTo(self).offset(adapt(10));
  35. // }];
  36. [self.barnertView mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.top.equalTo(self).offset(adapt(16));
  38. make.left.equalTo(self);
  39. make.right.equalTo(self);
  40. //make.height.mas_equalTo(adapt(58));
  41. make.height.mas_equalTo(adapt(0.1));
  42. }];
  43. self.barnertView.hidden = YES;
  44. [self.rechargeItemCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.top.equalTo(self.barnertView.mas_bottom).offset(adapt(16));
  46. make.left.equalTo(self);
  47. make.right.equalTo(self);
  48. make.bottom.equalTo(self).offset(adapt(-10));
  49. }];
  50. [super updateConstraints];
  51. }
  52. - (void)ym_bindViewModel:(YMAccountBalanceViewModel *)viewModel{
  53. if (!viewModel) {
  54. return;
  55. }
  56. _viewModel = viewModel;
  57. @weakify(self)
  58. [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) {
  59. @strongify(self)
  60. [self.rechargeItemCollectionView reloadData];
  61. [self.rechargeItemCollectionView layoutIfNeeded];
  62. //刷新高度
  63. CGFloat rechargeItemCollectionViewHeight = self.rechargeItemCollectionView.collectionViewLayout.collectionViewContentSize.height;
  64. [self.rechargeItemCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
  65. make.height.mas_equalTo(rechargeItemCollectionViewHeight);
  66. }];
  67. ///默认选中
  68. if (self.viewModel.rechargeItemDataArray.count > 0) {
  69. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  70. [self.rechargeItemCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  71. [self collectionView:self.rechargeItemCollectionView didSelectItemAtIndexPath:indexPath];
  72. }
  73. }];
  74. }
  75. #pragma mark - UICollectionViewDataSource
  76. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  77. return 1;
  78. }
  79. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  80. return self.viewModel.rechargeItemDataArray.count;
  81. }
  82. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  83. YMAccountBalanceRechargeItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMAccountBalanceRechargeItemCell class]) forIndexPath:indexPath];
  84. [cell ym_bindViewModel:self.viewModel.rechargeItemDataArray[indexPath.item]];
  85. return cell;
  86. }
  87. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  88. UICollectionReusableView *reusableView = nil;
  89. reusableView.backgroundColor = [UIColor clearColor];
  90. return reusableView;
  91. }
  92. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  93. return CGSizeZero;
  94. }
  95. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
  96. return CGSizeZero;
  97. }
  98. #pragma mark - UICollectionViewDelegate
  99. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  100. [self.viewModel.selectedRechargeItemSubject sendNext:@(indexPath.item)];
  101. }
  102. #pragma mark - CHTCollectionViewDelegateWaterfallLayout
  103. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  104. return CGSizeMake(adapt(110), adapt(82));
  105. }
  106. - (UILabel *)rechargeItemTipsLb{
  107. if (!_rechargeItemTipsLb) {
  108. _rechargeItemTipsLb = [[UILabel alloc]init];
  109. _rechargeItemTipsLb.font = LCFont(11);
  110. _rechargeItemTipsLb.textColor = HexColorFromRGB(0x000000);
  111. _rechargeItemTipsLb.textAlignment = NSTextAlignmentLeft;
  112. _rechargeItemTipsLb.text = @"请选择充值金币:";
  113. }
  114. return _rechargeItemTipsLb;
  115. }
  116. - (UIImageView *)barnertView{
  117. if (!_barnertView) {
  118. _barnertView = [[UIImageView alloc]init];
  119. _barnertView.image = ImageByName(@"ym_account_barner_bg");
  120. _barnertView.userInteractionEnabled = true;
  121. WS(weakSelf)
  122. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
  123. [_barnertView addGestureRecognizer:tap];
  124. [[[tap rac_gestureSignal] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  125. [weakSelf.viewModel clickRechargeImageView];
  126. }];
  127. }
  128. return _barnertView;
  129. }
  130. - (CHTCollectionViewWaterfallLayout *)rechargeItemLayout{
  131. if (!_rechargeItemLayout) {
  132. _rechargeItemLayout = [[CHTCollectionViewWaterfallLayout alloc] init];
  133. _rechargeItemLayout.columnCount = 3;
  134. _rechargeItemLayout.minimumColumnSpacing = adapt(10);
  135. _rechargeItemLayout.minimumInteritemSpacing = adapt(10);
  136. }
  137. return _rechargeItemLayout;
  138. }
  139. - (UICollectionView *)rechargeItemCollectionView{
  140. if (!_rechargeItemCollectionView) {
  141. _rechargeItemCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.rechargeItemLayout];
  142. _rechargeItemCollectionView.delegate = self;
  143. _rechargeItemCollectionView.dataSource = self;
  144. _rechargeItemCollectionView.showsVerticalScrollIndicator = NO;
  145. _rechargeItemCollectionView.showsHorizontalScrollIndicator = NO;
  146. _rechargeItemCollectionView.backgroundColor = UIColor.clearColor;
  147. [_rechargeItemCollectionView registerClass:[YMAccountBalanceRechargeItemCell class] forCellWithReuseIdentifier:NSStringFromClass([YMAccountBalanceRechargeItemCell class])];
  148. }
  149. return _rechargeItemCollectionView;
  150. }
  151. @end