RQExerciseAnswerItemViewModel.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. NSInteger line = ((RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne)? 0 : ((RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour)? 0 : 1));
  92. int tryCount = RQ_USER_MANAGER.tryLookCount;
  93. if(RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation){//背题模式
  94. tryCount = 0;
  95. }
  96. if (tryCount>0 && line==1){//试看
  97. line = 0;
  98. }
  99. CGFloat height = (line == 0)? [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel] : RQ_FIT_HORIZONTAL(34.f);
  100. self.skillHeight = height;
  101. space = 6.f;
  102. }
  103. _exRowHeight = [_answerString heightForFont:[UIFont systemFontOfSize:19] width:RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2)] + (8 * 2) + (16 * 2) + space;
  104. });
  105. }
  106. return self;
  107. }
  108. //重写 skillHeight get 方法
  109. - (CGFloat)skillHeight{
  110. CGFloat skillH = _skillHeight;
  111. // 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));
  112. NSInteger line = ((RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne)? 0 : ((RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour)? 0 : 1));
  113. int tryCount = RQ_USER_MANAGER.tryLookCount;
  114. if(RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation){//背题模式
  115. tryCount = 0;
  116. }
  117. if (tryCount>0 && line==1){//试看
  118. line = 0;
  119. }
  120. if(line==1) skillH = RQ_FIT_HORIZONTAL(34.f);
  121. return skillH;
  122. }
  123. //重写 rowHeight get 方法
  124. - (CGFloat)rowHeight{
  125. CGFloat rowH = _exRowHeight + self.skillHeight;
  126. // 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));
  127. NSInteger line = ((RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne)? 0 : ((RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour)? 0 : 1));
  128. int tryCount = RQ_USER_MANAGER.tryLookCount;
  129. if(RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation){//背题模式
  130. tryCount = 0;
  131. }
  132. if (tryCount>0 && line==1){//试看
  133. rowH = rowH + 40.f;
  134. }
  135. return rowH;
  136. }
  137. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  138. lb.attributedText = attributedStr;
  139. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2), CGFLOAT_MAX);
  140. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  141. lb.textLayout = layout;
  142. CGFloat introHeight = layout.textBoundingSize.height+10;
  143. return introHeight;
  144. }
  145. - (YYLabel *)yyLabel {
  146. if (!_yyLabel) {
  147. _yyLabel = [[YYLabel alloc] init];
  148. _yyLabel.userInteractionEnabled = YES;
  149. }
  150. return _yyLabel;
  151. }
  152. - (NSString *)itemClassName {
  153. return @"RQExerciseAnswerCell";
  154. }
  155. #pragma mark -事件
  156. - (void)buttonActionPayVoicedo:(UIButton *)btn
  157. {
  158. if (RQ_COMMON_MANAGER.JSJP_APP_VOICE) {//读题-是否开启
  159. //是vip
  160. // 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));
  161. NSInteger line = ((RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne)? 0 : ((RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour)? 0 : 1));
  162. int tryCount = RQ_USER_MANAGER.tryLookCount;
  163. if(RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation){//背题模式
  164. tryCount = 0;
  165. }
  166. if (tryCount>0 && line==1){//试看
  167. line = 0;
  168. }
  169. if (line==1) {
  170. ShowMsg(@"请购买会员,才能读解题技巧。");
  171. return;
  172. }
  173. }
  174. btn.userInteractionEnabled = NO;
  175. btn.selected = !btn.isSelected;
  176. if(btn.selected){//播放
  177. if (NY_VOICE_MANAGER.state == NYVoiceType_Pause){
  178. [NY_VOICE_MANAGER resumePayVoiceActionUrl];//恢复
  179. }else {
  180. [NY_VOICE_MANAGER stopPayVoiceActionUrl];
  181. NSString *url = self.ydtJSModel.qb_skill_audio_link;
  182. WeakSelf(weakSelf)
  183. [NY_VOICE_MANAGER startPayVoiceActionUrl:url completedBlock:^(NYVoiceType voiceType) {
  184. switch (voiceType) {
  185. case NYVoiceType_Start:{
  186. weakSelf.readBtn.selected = YES;
  187. }break;
  188. case NYVoiceType_Wait:{//完成-等待
  189. weakSelf.readBtn.selected = NO;
  190. }break;
  191. case NYVoiceType_Resume:{//恢复
  192. weakSelf.readBtn.selected = YES;
  193. }break;
  194. case NYVoiceType_Pause:{//暂停
  195. weakSelf.readBtn.selected = NO;
  196. }break;
  197. case NYVoiceType_Stop:{//停止
  198. weakSelf.readBtn.selected = NO;
  199. }break;
  200. default:
  201. break;
  202. }
  203. }];
  204. }
  205. }else {//暂停
  206. [NY_VOICE_MANAGER pausePayVoiceActionUrl];
  207. }
  208. //防止频繁点击 1 秒点一次
  209. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  210. self.readBtn.userInteractionEnabled = YES;
  211. });
  212. }
  213. @end