MOSignInCollectionViewCell.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // MOSignInCollectionViewCell.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2024/9/10.
  6. //
  7. #import "MOSignInCollectionViewCell.h"
  8. #import "MOSignInGiftCell.h"
  9. #import "MOWebViewController.h"
  10. @interface MOSignInCollectionViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
  11. @property (weak, nonatomic) IBOutlet UIView *bgView;
  12. @property (weak, nonatomic) IBOutlet UILabel *titleLab;
  13. @property (weak, nonatomic) IBOutlet UILabel *dayNumLab;
  14. @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
  15. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewWidth;
  16. @property (weak, nonatomic) IBOutlet UILabel *tipLab;
  17. @property (weak, nonatomic) IBOutlet UIButton *signInBtn;
  18. /** 数据源 */
  19. @property (nonatomic, strong) NSMutableArray *dataArr;
  20. @end
  21. @implementation MOSignInCollectionViewCell
  22. - (void)awakeFromNib {
  23. [super awakeFromNib];
  24. // Initialization code
  25. [self.signInBtn setBackgroundColor:kBaseBtnBgColor];
  26. self.signInBtn.layer.cornerRadius = 46.0 / 2.0;
  27. self.signInBtn.layer.masksToBounds = YES;
  28. self.titleLab.textColor = kBaseBtnBgColor;
  29. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
  30. flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  31. CGFloat width = 66.0;
  32. CGFloat height = 74.0;
  33. flow.itemSize = CGSizeMake(width, height);
  34. flow.minimumLineSpacing = 5.0;//行间距
  35. flow.minimumInteritemSpacing = 0.0;//列间距
  36. [self.collectionView setCollectionViewLayout:flow];
  37. self.collectionView.delegate = self;
  38. self.collectionView.dataSource = self;
  39. self.collectionView.backgroundColor = [UIColor clearColor];
  40. [self.collectionView registerNib:[UINib nibWithNibName:@"MOSignInGiftCell" bundle:nil] forCellWithReuseIdentifier:MOSignInGiftCell_ID];
  41. self.titleLab.text = NSLocalString(@"mimo_sign_in_title");
  42. self.dayNumLab.text = [NSString stringWithFormat:NSLocalString(@"mimo_sign_in_for_day"),0];
  43. self.tipLab.text = NSLocalString(@"mimo_sign_in_tip_two");
  44. [self.signInBtn setTitle:NSLocalString(@"mimo_sign_in_title") forState:UIControlStateNormal];
  45. self.titleLab.font = [MOTextTools getTheFontWithSize:24.0 AndFontName:kNormalTitleFontStr];
  46. [self.signInBtn setFont:[MOTextTools getTheFontWithSize:16.0 AndFontName:kNormalContentFontStr]];
  47. self.tipLab.font = [MOTextTools getTheFontWithSize:12.0 AndFontName:kNormalContentFontStr];
  48. self.dayNumLab.font = [MOTextTools getTheFontWithSize:16.0 AndFontName:kNormalContentFontStr];
  49. }
  50. - (IBAction)signInBtnClick:(id)sender {
  51. if(self.baseData.sign){
  52. UIViewController *currentVC = [MOTools currentViewController];
  53. MOWebViewController *vc = [[MOWebViewController alloc] init];
  54. NSString *urlStr = [NSString stringWithFormat:@"%@/#/taskCenter?tagName=dailyTasks",kNetPath_Web_Base];
  55. vc.webUrl = [NSURL URLWithString:urlStr];
  56. vc.hidesBottomBarWhenPushed = YES;
  57. [currentVC.navigationController pushViewController:vc animated:YES];
  58. self.needViewDismissBlock ? self.needViewDismissBlock() : nil;
  59. //已签到
  60. return;
  61. }
  62. else
  63. {
  64. //未签到
  65. [self toSignInAction];
  66. }
  67. }
  68. - (void)toSignInAction{
  69. WEAKSELF
  70. [kHttpManager toSubmitTheSignTaskAboutSignWithParams:nil andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  71. if(kCode_Success){
  72. // MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]);
  73. MOSignBaseData *model = [MOSignBaseData modelObjectWithDictionary:data[@"data"]];
  74. weakSelf.baseData.continuous = model.continuous;
  75. weakSelf.baseData.signPrizes = model.signPrizes;
  76. weakSelf.baseData.sign = YES;
  77. weakSelf.viewDataBlock ? weakSelf.viewDataBlock(weakSelf.baseData) : nil;
  78. [weakSelf updataAllUIWithProperty];
  79. //设置签到时间
  80. [MOTools setTimeOfShowSignDevice:[NSDate date]];
  81. }
  82. else{
  83. MOLogV(@"toGetTheSignTaskInfoWithParams 接口报错了");
  84. kShowNetError(data)
  85. }
  86. }];
  87. }
  88. #pragma mark UICollectionViewDelegate,UICollectionViewDataSource
  89. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  90. {
  91. return 1;
  92. }
  93. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  94. {
  95. return self.dataArr.count;
  96. }
  97. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  98. MOSignPrizes *model = self.dataArr[indexPath.row];
  99. MOSignInGiftCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MOSignInGiftCell_ID forIndexPath:indexPath];
  100. cell.isGet = self.baseData.sign;
  101. cell.cellModel = model;
  102. return cell;
  103. }
  104. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  105. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  106. }
  107. - (void)updataAllUIWithProperty{
  108. self.dataArr = [self.baseData.signPrizes mutableCopy];
  109. CGFloat theMaxWidth = 296.0 - 25.0 * 2.0;
  110. CGFloat theNeedWidth = self.dataArr.count * (66.0 + 5.0);
  111. if(theNeedWidth > theMaxWidth){
  112. theNeedWidth = theMaxWidth;
  113. }
  114. self.collectionViewWidth.constant = theNeedWidth;
  115. [self.collectionView reloadData];
  116. NSString *signNumDayStr = [NSString stringWithFormat:NSLocalString(@"mimo_sign_in_for_day"),self.baseData.continuous];
  117. NSMutableAttributedString *plainText = [self colorfulStringWith:signNumDayStr];
  118. self.dayNumLab.attributedText = plainText;
  119. if(self.baseData.sign){
  120. [self.signInBtn setTitle:NSLocalString(@"mimo_sign_in_more_btn") forState:UIControlStateNormal];
  121. }
  122. else{
  123. [self.signInBtn setTitle:NSLocalString(@"mimo_sign_in_title") forState:UIControlStateNormal];
  124. }
  125. }
  126. - (NSMutableAttributedString *)colorfulStringWith:(NSString *)contentStr{
  127. NSString *numStr = [NSString stringWithFormat:@"%.f",self.baseData.continuous];
  128. // 创建属性字符串,整体颜色为白色
  129. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:@{NSFontAttributeName:[MOTextTools getTheFontWithSize:16.0 AndFontName:kNormalContentFontStr],NSForegroundColorAttributeName:[MOTools colorWithHexString:@"#282828" alpha:1.0]}];
  130. NSRange redRange = [contentStr rangeOfString:numStr];
  131. if(redRange.location != NSNotFound){
  132. // 将最后三个字符的颜色设置为红色
  133. [attributedString addAttribute:NSForegroundColorAttributeName value:kBaseBtnBgColor range:redRange];
  134. }
  135. return attributedString;
  136. }
  137. - (void)toGetTheSignInData{
  138. if(!self.baseData){
  139. WEAKSELF
  140. [kHttpManager toGetTheSignTaskInfoWithParams:nil andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  141. if(kCode_Success){
  142. // MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]);
  143. MOSignBaseData *baseData = [MOSignBaseData modelObjectWithDictionary:data[@"data"]];
  144. weakSelf.baseData = baseData;
  145. weakSelf.viewDataBlock ? weakSelf.viewDataBlock(weakSelf.baseData) : nil;
  146. [weakSelf updataAllUIWithProperty];
  147. }
  148. else{
  149. MOLogV(@"toGetTheSignTaskInfoWithParams 接口报错了");
  150. kShowNetError(data)
  151. }
  152. }];
  153. }
  154. else{
  155. [self updataAllUIWithProperty];
  156. }
  157. }
  158. - (NSMutableArray *)dataArr{
  159. if(!_dataArr){
  160. _dataArr = [NSMutableArray array];
  161. }
  162. return _dataArr;
  163. }
  164. @end