RQProfilePracticeCell.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // RQProfilePracticeCell.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2022/4/27.
  6. // Copyright © 2022 JCZ. All rights reserved.
  7. //
  8. #import "RQProfilePracticeCell.h"
  9. @interface RQProfilePracticeCell () <RQSwitchDelegate>
  10. @property (nonatomic, readwrite, strong) RQProfilePracticeItemViewModel *viewModel;
  11. @property (nonatomic, readwrite, strong) RQSwitch *rqSwitch;
  12. @property (weak, nonatomic) IBOutlet UILabel *leftTitleLabel;
  13. @property (nonatomic, readwrite, assign) RQSwitchState switchState;
  14. @property (weak, nonatomic) IBOutlet UILabel *carTypeLabel;
  15. @property (weak, nonatomic) IBOutlet UIStackView *chooseCarTypeView;
  16. @property (weak, nonatomic) IBOutlet UILabel *questionAllNumLabel;
  17. @property (weak, nonatomic) IBOutlet UILabel *hasDoneQuestionNumLabel;
  18. @property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
  19. @property (weak, nonatomic) IBOutlet UILabel *countLabel;
  20. @property (weak, nonatomic) IBOutlet UIStackView *upScoreView;
  21. @end
  22. @implementation RQProfilePracticeCell
  23. #pragma mark - PublicMethods
  24. + (instancetype)cellWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath {
  25. static NSString *ID = @"RQProfilePracticeCell";
  26. [collectionView registerNib:[UINib nibWithNibName:ID bundle:nil] forCellWithReuseIdentifier:ID];
  27. RQProfilePracticeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
  28. if (!cell) cell = [self rq_viewFromXib];
  29. [cell.contentView addSubview:cell.rqSwitch];
  30. return cell;
  31. }
  32. - (void)bindViewModel:(RQProfilePracticeItemViewModel *)viewModel {
  33. self.viewModel = viewModel;
  34. }
  35. #pragma mark - SystemMethods
  36. - (void)awakeFromNib {
  37. [super awakeFromNib];
  38. @weakify(self)
  39. [_chooseCarTypeView setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  40. [RQ_APPDELEGATE.services pushViewModel:[[RQChooseCarTypeViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:nil] animated:YES];
  41. }];
  42. RAC(_upScoreView, hidden) = [[[RACObserve(RQ_COMMON_MANAGER, APP_SWITCH) takeUntil:self.rac_prepareForReuseSignal] deliverOnMainThread] distinctUntilChanged];
  43. [_upScoreView setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  44. [RQ_VIP_Module isVipWithSubject:0 complete:^(BOOL isVip) {
  45. if (RQ_VIP_Module.isVip) {
  46. [RQ_VIP_Module gotoVipCenter];
  47. } else {
  48. [RQ_VIP_Module gotoBuyVipWithVipPageType:RQVIPPageType_Full];
  49. }
  50. }];
  51. }];
  52. RAC(self.carTypeLabel, text) = [[RACObserve(RQ_YDTQuestion_Module, carType) map:^id _Nullable(id _Nullable value) {
  53. RQHomePageCarType carType = [value integerValue];
  54. return [RQ_YDTQuestion_Module getCarTypeCNNameWithCarType:carType];
  55. }] takeUntil:self.rac_prepareForReuseSignal];
  56. RAC(self.leftTitleLabel, hidden) = [[[RACObserve(RQ_YDTQuestion_Module, carType) deliverOnMainThread] takeUntil:self.rac_prepareForReuseSignal] map:^id _Nullable(id _Nullable value) {
  57. RQHomePageCarType carType = [value integerValue];
  58. BOOL needHide = (carType == RQHomePageCarType_Car || carType == RQHomePageCarType_Bus || carType == RQHomePageCarType_Truck || carType == RQHomePageCarType_Motorcycle);
  59. return @(needHide);
  60. }];
  61. RAC(self.rqSwitch, hidden) = [[[RACObserve(RQ_YDTQuestion_Module, carType) deliverOnMainThread] takeUntil:self.rac_prepareForReuseSignal] map:^id _Nullable(id _Nullable value) {
  62. RQHomePageCarType carType = [value integerValue];
  63. BOOL needHide = (carType == RQHomePageCarType_Car || carType == RQHomePageCarType_Bus || carType == RQHomePageCarType_Truck || carType == RQHomePageCarType_Motorcycle);
  64. return @(!needHide);
  65. }];
  66. RAC(self.questionAllNumLabel, text) = [[RACSignal combineLatest:@[RACObserve(RQ_YDTQuestion_Module, carType), [RACObserve(self, switchState) distinctUntilChanged]] reduce:^id (NSNumber *carType, NSNumber *switchState) {
  67. BOOL isSubjectOne = switchState.boolValue;
  68. NSInteger allCount = [RQ_YDTQuestion_Module getQuestionNumWithWithSubject:isSubjectOne? RQHomePageSubjectType_SubjectOne : RQHomePageSubjectType_SubjectFour exerciseType:RQExerciseType_Sequential];
  69. return [NSString stringWithFormat:@"%ld",allCount];
  70. }] takeUntil:self.rac_prepareForReuseSignal];
  71. RAC(self.hasDoneQuestionNumLabel, text) = [[RACSignal combineLatest:@[RACObserve(RQ_YDTQuestion_Module, carType), [RACObserve(self, switchState) distinctUntilChanged], RACObserve(RQ_YDT_USER_Question_Module, doNum)] reduce:^id (NSNumber *carType, NSNumber *switchState, NSNumber *doNum) {
  72. BOOL isSubjectOne = switchState.boolValue;
  73. NSInteger hasDoneCount = [RQ_YDT_USER_Question_Module getDoQuestionNumWithCarType:RQ_YDTQuestion_Module.carType subject:isSubjectOne? RQHomePageSubjectType_SubjectOne : RQHomePageSubjectType_SubjectFour];
  74. return [NSString qmui_stringWithNSInteger:hasDoneCount];
  75. }] takeUntil:self.rac_prepareForReuseSignal];
  76. RAC(self.scoreLabel, text) = [[RACSignal combineLatest:@[RACObserve(RQ_COMMON_MANAGER, examResultOneListArr), RACObserve(RQ_COMMON_MANAGER, examResultFourListArr), [RACObserve(self, switchState) distinctUntilChanged]] reduce:^id (NSArray *examResultOneListArr, NSArray *examResultFourListArr, NSNumber *switchState) {
  77. BOOL isSubjectOne = switchState.boolValue;
  78. NSArray *arr;
  79. if (isSubjectOne) {
  80. arr = examResultOneListArr;
  81. } else {
  82. arr = examResultFourListArr;
  83. }
  84. float avg = [[[arr.rac_sequence.signal map:^id _Nullable(RQExamResultModel *examResultModel) {
  85. return examResultModel.score;
  86. }].toArray valueForKeyPath:@"@avg.floatValue"] floatValue];
  87. NSString *score = [NSString qmui_stringWithCGFloat:avg decimal:0];
  88. return score;
  89. }] takeUntil:self.rac_willDeallocSignal];
  90. RAC(self.countLabel, text) = [[RACSignal combineLatest:@[RACObserve(RQ_COMMON_MANAGER, examResultOneListArr), RACObserve(RQ_COMMON_MANAGER, examResultFourListArr), [RACObserve(self, switchState) distinctUntilChanged]] reduce:^id (NSArray *examResultOneListArr, NSArray *examResultFourListArr, NSNumber *switchState) {
  91. BOOL isSubjectOne = switchState.boolValue;
  92. NSArray *arr;
  93. if (isSubjectOne) {
  94. arr = examResultOneListArr;
  95. } else {
  96. arr = examResultFourListArr;
  97. }
  98. NSString *count = [NSString qmui_stringWithNSInteger:arr.count];
  99. return [NSString stringWithFormat:@"近%@次",count];
  100. }] takeUntil:self.rac_willDeallocSignal];
  101. self.switchState = ![self.rqSwitch getSwitchState];
  102. [[RACObserve(RQ_Profile_Module, profileSubject) takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id _Nullable x) {
  103. switch (RQ_Profile_Module.profileSubject) {
  104. case 0:
  105. self.rqSwitch.thumbOnTintColor = RQ_VIP_Module.isSubject1Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  106. self.rqSwitch.thumbOffTintColor = RQ_VIP_Module.isSubject1Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  107. self.rqSwitch.thumbDisabledTintColor = RQ_VIP_Module.isSubject1Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  108. break;
  109. case 1:
  110. self.rqSwitch.thumbOnTintColor = RQ_VIP_Module.isSubject2Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  111. self.rqSwitch.thumbOffTintColor = RQ_VIP_Module.isSubject2Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  112. self.rqSwitch.thumbDisabledTintColor = RQ_VIP_Module.isSubject2Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  113. break;
  114. case 2:
  115. self.rqSwitch.thumbOnTintColor = RQ_VIP_Module.isSubject3Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  116. self.rqSwitch.thumbOffTintColor = RQ_VIP_Module.isSubject3Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  117. self.rqSwitch.thumbDisabledTintColor = RQ_VIP_Module.isSubject3Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  118. break;
  119. case 3:
  120. self.rqSwitch.thumbOnTintColor = RQ_VIP_Module.isSubject4Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  121. self.rqSwitch.thumbOffTintColor = RQ_VIP_Module.isSubject4Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  122. self.rqSwitch.thumbDisabledTintColor = RQ_VIP_Module.isSubject4Vip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  123. break;
  124. case 4:
  125. self.rqSwitch.thumbOnTintColor = RQ_VIP_Module.isSubjectAllVip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  126. self.rqSwitch.thumbOffTintColor = RQ_VIP_Module.isSubjectAllVip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  127. self.rqSwitch.thumbDisabledTintColor = RQ_VIP_Module.isSubjectAllVip? RQColorFromHexString(@"#8950C5") : RQColorFromHexString(@"#498EF5");
  128. break;
  129. default:
  130. break;
  131. }
  132. self.rqSwitch.switchThumb.backgroundColor = self.rqSwitch.thumbOnTintColor;
  133. self.rqSwitch.track.backgroundColor = self.rqSwitch.trackOnTintColor;
  134. }];
  135. }
  136. #pragma mark - RQSwitchDelegate
  137. - (void)switchStateChanged:(RQSwitchState)currentState {
  138. self.switchState = currentState;
  139. }
  140. #pragma mark - LazyLoad
  141. - (RQSwitch *)rqSwitch {
  142. if (!_rqSwitch) {
  143. _rqSwitch = [[RQSwitch alloc] initWithSize:RQSwitchSizeSubject style:RQSwitchStyleSubject state:RQSwitchStateOn];
  144. _rqSwitch.delegate = self;
  145. _rqSwitch.center = CGPointMake(RQ_FIT_HORIZONTAL(105.f) / 2.f + 16, RQ_FIT_HORIZONTAL(25.f) / 2.f + 16);
  146. }
  147. return _rqSwitch;
  148. }
  149. @end