RQExerciseAnswerItemViewModel.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // RQExerciseAnswerItemViewModel.m
  3. // JSJP
  4. //
  5. // Created by 张嵘 on 2021/8/16.
  6. //
  7. #import "RQExerciseAnswerItemViewModel.h"
  8. @interface RQExerciseAnswerItemViewModel (){
  9. CGFloat _exRowHeight;//记录原始rowHeight
  10. }
  11. @property (nonatomic, readwrite, strong) NSString *answerString;
  12. @property (nonatomic, readwrite, strong) RQYDTJSModel *ydtJSModel;
  13. @property (nonatomic, readwrite, assign) CGFloat skillHeight;
  14. @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
  15. @property (nonatomic, readwrite, strong) NSMutableAttributedString *skillString;
  16. @property (nonatomic, readwrite, strong) QMUIButton *readBtn;
  17. @end
  18. @implementation RQExerciseAnswerItemViewModel
  19. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
  20. if (self = [super init]) {
  21. @weakify(self)
  22. dispatch_async(dispatch_get_main_queue(), ^{
  23. @strongify(self)
  24. NSArray *allOptionsArr = @[@"A",@"B",@"C",@"D"];
  25. NSMutableArray *answerOptionArr = @[].mutableCopy;
  26. for (int i = 0; i < exerciseModel.ydtQuestionModel.AnswerTrue.length; i ++) {
  27. NSRange range;
  28. range.location = i;
  29. range.length = 1;
  30. NSString *indexStr = [exerciseModel.ydtQuestionModel.AnswerTrue substringWithRange:range];
  31. [answerOptionArr addObject:[allOptionsArr objectAtIndex:indexStr.integerValue - 1]];
  32. }
  33. self.ydtJSModel = [RQ_YDT_JS_Question_Module getQuestionJSWithQuestionId:exerciseModel.ydtQuestionModel.ID];
  34. exerciseModel.ydtQuestionModel.explain = self.ydtJSModel.best_explain_new;//2023.9.25
  35. // //ning 2023-5-24 一点通db 解题答案数据有异常-新整jq库进行修正。
  36. // self.ydtJSModel.qb_read_analyse = [YN_YDT_JQ_Question_Module getQuestionKTJQWithQuestionId:exerciseModel.ydtQuestionModel.ID];
  37. //于2023-08-18 年更新db完成-解决此问题
  38. self.answerString = [NSString stringWithFormat:@"答案 %@",[answerOptionArr.mutableCopy componentsJoinedByString:@" "]];
  39. __block CGFloat space = 0.f;
  40. if (RQObjectIsNil(self.ydtJSModel)) {
  41. self.skillHeight = 0.f;
  42. space = 0.f;
  43. } else {
  44. NSString *readText = [[self.ydtJSModel.qb_analyse stringByReplacingOccurrencesOfString:@"【" withString:@""] stringByReplacingOccurrencesOfString:@"】" withString:@""];
  45. if(readText==nil&&exerciseModel.ydtQuestionModel.Type==RQQuestionType_Judgment){//判断题
  46. readText = [NSString stringWithFormat:@"看题目答题:%@",[answerOptionArr.mutableCopy componentsJoinedByString:@" "]];
  47. }
  48. NSArray *knameList = [self getKeyRangeList:self.ydtJSModel.qb_analyse readText:readText];//获取关键字
  49. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] init];
  50. //播放-答案
  51. if (RQ_COMMON_MANAGER.JSJP_APP_VOICE) {//读题-是否开启
  52. CGSize btnSize = CGSizeMake(30, 30);
  53. //读题
  54. QMUIButton *readBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  55. CGFloat ebtnH = 30.f;
  56. CGFloat ebtnW = 30.f;
  57. CGSize ebtnSize = CGSizeMake(ebtnW, ebtnH);
  58. readBtn.size = btnSize;
  59. [readBtn setImage:RQImageNamed(@"播放_pay") forState:UIControlStateNormal];
  60. [readBtn setImage:RQImageNamed(@"播放_suspend") forState:UIControlStateSelected];
  61. [readBtn addTarget:self action:@selector(buttonActionPayVoicedo:) forControlEvents:UIControlEventTouchUpInside];
  62. NSMutableAttributedString *readAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:readBtn
  63. contentMode:UIViewContentModeCenter
  64. attachmentSize:ebtnSize
  65. alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]
  66. alignment:YYTextVerticalAlignmentCenter];
  67. self.readBtn = readBtn;
  68. [text appendAttributedString:readAttachment];
  69. }
  70. NSMutableAttributedString *analyseString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",readText]];
  71. [text appendAttributedString:analyseString];
  72. text.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  73. text.yy_lineSpacing = 8;
  74. text.yy_color = RQ_MAIN_TEXT_COLOR_1;
  75. if(knameList){//替换关键字标红
  76. @try {
  77. for (NSDictionary *item in knameList) {
  78. NSArray *values = item.allValues.firstObject;
  79. NSString *replacementString = item.allKeys.firstObject;
  80. NSRange range = NSMakeRange([values[0] intValue]+1, [values[1] intValue]);
  81. NSDictionary *attributes = @{ NSForegroundColorAttributeName: RGB_COLOR(244, 0, 47),NSFontAttributeName: [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]};
  82. [text replaceCharactersInRange:range withString:replacementString];
  83. [text setAttributes:attributes range:NSMakeRange(range.location, range.length)];
  84. }
  85. } @catch (NSException *exception) {
  86. NSLog(@"替换替换发生错误");
  87. }
  88. }
  89. self.skillString = text;
  90. NSInteger line = RQ_COMMON_MANAGER.APP_SWITCH? 0 : ((RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne)? 0 : ((RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour)? 0 : 1));
  91. if (RQ_USER_MANAGER.tryLookCount>0 && line==1){//试看
  92. line = 0;
  93. }
  94. CGFloat height = (line == 0)? [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel] : RQ_FIT_HORIZONTAL(34.f);
  95. self.skillHeight = height;
  96. space = 6.f;
  97. }
  98. _exRowHeight = [_answerString heightForFont:[UIFont systemFontOfSize:19] width:RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2)] + (8 * 2) + (16 * 2) + space;
  99. });
  100. }
  101. return self;
  102. }
  103. //重写 skillHeight get 方法
  104. - (CGFloat)skillHeight{
  105. CGFloat skillH = _skillHeight;
  106. NSInteger line = RQ_COMMON_MANAGER.APP_SWITCH? 0 : ((RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne)? 0 : ((RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour)? 0 : 1));
  107. if (RQ_USER_MANAGER.tryLookCount>0 && line==1){//试看
  108. line = 0;
  109. }
  110. if(line==1) skillH = RQ_FIT_HORIZONTAL(34.f);
  111. return skillH;
  112. }
  113. //重写 rowHeight get 方法
  114. - (CGFloat)rowHeight{
  115. CGFloat rowH = _exRowHeight + self.skillHeight;
  116. NSInteger line = RQ_COMMON_MANAGER.APP_SWITCH? 0 : ((RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne)? 0 : ((RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour)? 0 : 1));
  117. if (RQ_USER_MANAGER.tryLookCount>0 && line==1){//试看
  118. rowH = rowH + 40.f;
  119. }
  120. return rowH;
  121. }
  122. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  123. lb.attributedText = attributedStr;
  124. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2), CGFLOAT_MAX);
  125. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  126. lb.textLayout = layout;
  127. CGFloat introHeight = layout.textBoundingSize.height+10;
  128. return introHeight;
  129. }
  130. - (YYLabel *)yyLabel {
  131. if (!_yyLabel) {
  132. _yyLabel = [[YYLabel alloc] init];
  133. _yyLabel.userInteractionEnabled = YES;
  134. }
  135. return _yyLabel;
  136. }
  137. - (NSString *)itemClassName {
  138. return @"RQExerciseAnswerCell";
  139. }
  140. #pragma mark -事件
  141. - (void)buttonActionPayVoicedo:(UIButton *)btn
  142. {
  143. if (RQ_COMMON_MANAGER.JSJP_APP_VOICE) {//读题-是否开启
  144. //是vip
  145. NSInteger line = RQ_COMMON_MANAGER.APP_SWITCH? 0 : ((RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne)? 0 : ((RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour)? 0 : 1));
  146. if (RQ_USER_MANAGER.tryLookCount>0 && line==1){//试看
  147. line = 0;
  148. }
  149. if (line==1) {
  150. ShowMsg(@"请购买会员,才能读解题技巧。");
  151. return;
  152. }
  153. }
  154. btn.userInteractionEnabled = NO;
  155. btn.selected = !btn.isSelected;
  156. if(btn.selected){//播放
  157. if (NY_VOICE_MANAGER.state == NYVoiceType_Pause){
  158. [NY_VOICE_MANAGER resumePayVoiceActionUrl];//恢复
  159. }else {
  160. [NY_VOICE_MANAGER stopPayVoiceActionUrl];
  161. NSString *url = self.ydtJSModel.qb_skill_audio_link;
  162. WeakSelf(weakSelf)
  163. [NY_VOICE_MANAGER startPayVoiceActionUrl:url completedBlock:^(NYVoiceType voiceType) {
  164. switch (voiceType) {
  165. case NYVoiceType_Start:{
  166. weakSelf.readBtn.selected = YES;
  167. }break;
  168. case NYVoiceType_Wait:{//完成-等待
  169. weakSelf.readBtn.selected = NO;
  170. }break;
  171. case NYVoiceType_Resume:{//恢复
  172. weakSelf.readBtn.selected = YES;
  173. }break;
  174. case NYVoiceType_Pause:{//暂停
  175. weakSelf.readBtn.selected = NO;
  176. }break;
  177. case NYVoiceType_Stop:{//停止
  178. weakSelf.readBtn.selected = NO;
  179. }break;
  180. default:
  181. break;
  182. }
  183. }];
  184. }
  185. }else {//暂停
  186. [NY_VOICE_MANAGER pausePayVoiceActionUrl];
  187. }
  188. //防止频繁点击 1 秒点一次
  189. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  190. self.readBtn.userInteractionEnabled = YES;
  191. });
  192. }
  193. @end