RQExerciseAnswerItemViewModel.m 14 KB

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