RQExerciseQuestionItemViewModel.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. //
  2. // RQExerciseQuestionItemViewModel.m
  3. // JSJP
  4. //
  5. // Created by 张嵘 on 2021/8/16.
  6. //
  7. #import "RQExerciseQuestionItemViewModel.h"
  8. #import "payRequsestHandler.h"
  9. @interface RQExerciseQuestionItemViewModel ()
  10. @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel;
  11. @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
  12. @property (nonatomic, readwrite, strong) NSString *typeString;
  13. @property (nonatomic, readwrite, strong) NSString *newString;
  14. @property (nonatomic, readwrite, strong) NSMutableAttributedString *qusetionString;
  15. @property (nonatomic, readwrite, assign) CGFloat labelHeight;
  16. @property (nonatomic, readwrite, assign) CGFloat imageHeight;
  17. @property (nonatomic, readwrite, strong) NSString *imageString;
  18. @property (nonatomic, readwrite, strong) NSString *videoString;
  19. @property (nonatomic, readwrite, strong) NSMutableAttributedString *spaceAttachment;
  20. @property (nonatomic, readwrite, strong) QMUIButton *readBtn;
  21. @property (nonatomic,assign) int readCount;
  22. @end
  23. @implementation RQExerciseQuestionItemViewModel
  24. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
  25. if (self = [super init]) {
  26. @weakify(self)
  27. self.readCount = 0;
  28. self.exerciseModel = exerciseModel;
  29. dispatch_async(dispatch_get_main_queue(), ^{
  30. @strongify(self)
  31. self.qusetionString = [self dealQuestionStrWithRQExerciseModel:exerciseModel];
  32. self.labelHeight = [self getMessageHeightWithAttributedStr:self.qusetionString andLabel:self.yyLabel];
  33. });
  34. if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.media_url)) {
  35. if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.sinaimg)) {
  36. self.imageString = exerciseModel.ydtQuestionModel.media_url;
  37. self.videoString = @"";
  38. } else if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.video_url)) {
  39. self.imageString = @"";
  40. self.videoString = exerciseModel.ydtQuestionModel.media_url;
  41. } else {
  42. self.imageString = @"";
  43. self.videoString = @"";
  44. }
  45. } else {
  46. self.imageString = @"";
  47. self.videoString = @"";
  48. }
  49. if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.sinaimg) || RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.media_url)) {
  50. if (!RQObjectIsNil(exerciseModel.ydtQuestionModel.media_content)) {
  51. NSString *imageStr = [exerciseModel.ydtQuestionModel.media_content base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  52. UIImage *sinaImage = [UIImage sd_imageWithData:[NSData dataWithBase64EncodedString:imageStr] scale:1 firstFrameOnly:YES];
  53. exerciseModel.ydtQuestionModel.sinaImage = sinaImage;
  54. } else {
  55. NSString *media_name;
  56. if(RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.sinaimg)){//不为空
  57. media_name = exerciseModel.ydtQuestionModel.sinaimg;
  58. self.imageString = exerciseModel.ydtQuestionModel.sinaimg;
  59. }else{
  60. if ([exerciseModel.ydtQuestionModel.media_url containsString:@"https://"]) {
  61. media_name = [exerciseModel.ydtQuestionModel.media_url componentsSeparatedByString:@"/"].lastObject;
  62. }
  63. }
  64. NSData *mediaData = [RQ_YDTQuestion_Module getMediaDataWithMediaName:media_name];
  65. if (mediaData) {
  66. NSString *imageStr = [mediaData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  67. UIImage *sinaImage = [UIImage sd_imageWithData:[NSData dataWithBase64EncodedString:imageStr] scale:1 firstFrameOnly:YES];
  68. exerciseModel.ydtQuestionModel.sinaImage = sinaImage;
  69. }
  70. }
  71. } else if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.video_url) && RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.media_url)) {
  72. NSString *media_name;
  73. if ([exerciseModel.ydtQuestionModel.video_url containsString:@"https://"]) {
  74. media_name = [exerciseModel.ydtQuestionModel.video_url componentsSeparatedByString:@"/"].lastObject;
  75. } else {
  76. media_name = exerciseModel.ydtQuestionModel.video_url;
  77. }
  78. NSURL *video_filePath_Url;
  79. if (!RQObjectIsNil(exerciseModel.ydtQuestionModel.media_content) && !RQ_COMMON_MANAGER.JSJP_Is_Online_Media_Video) {
  80. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  81. NSString *documentsDirectory = [paths objectAtIndex:0];
  82. NSString *path = [documentsDirectory stringByAppendingPathComponent:media_name];
  83. [[NSFileManager defaultManager] createFileAtPath:path contents:exerciseModel.ydtQuestionModel.media_content attributes:nil];
  84. video_filePath_Url = [NSURL fileURLWithPath:path];
  85. }
  86. exerciseModel.ydtQuestionModel.video_filePath_Url = video_filePath_Url;
  87. }
  88. if (RQStringIsNotEmpty(self.imageString)) {
  89. __block CGSize imageSize = CGSizeMake(RQ_SCREEN_WIDTH - 32.f, (RQ_SCREEN_WIDTH - 32.f) * (175.f / 345.f));
  90. if (exerciseModel.ydtQuestionModel.sinaImage) {
  91. imageSize = exerciseModel.ydtQuestionModel.sinaImage.size;
  92. } else {
  93. NSString *cacheKeyStr = [[YYWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:self.imageString]];
  94. UIImage *cacheImage = [[YYImageCache sharedCache] getImageForKey:cacheKeyStr];
  95. if (cacheImage) {
  96. imageSize = cacheImage.size;
  97. } else {
  98. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  99. @strongify(self)
  100. // 处理耗时操作的代码块...
  101. imageSize = [UIImage getImageSizeWithURL:self.imageString];
  102. //通知主线程刷新
  103. dispatch_async(dispatch_get_main_queue(), ^{
  104. @strongify(self)
  105. //回调或者说是通知主线程刷新,
  106. if (imageSize.width == 0 || imageSize.height == 0) {
  107. self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (175.f / 345.f);
  108. } else {
  109. self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (imageSize.height / imageSize.width);
  110. }
  111. });
  112. });
  113. }
  114. }
  115. if (imageSize.width == 0 || imageSize.height == 0) {
  116. self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (175.f / 345.f);
  117. } else {
  118. self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (imageSize.height / imageSize.width);
  119. }
  120. } else if (RQStringIsNotEmpty(self.videoString)) {
  121. self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (175.f / 345.f);
  122. }
  123. self.rowHeight = 16.f + self.labelHeight + ((self.imageHeight == 0)? 0 : (16.f + self.imageHeight)) + 16.f;
  124. [[[RACObserve(RQ_Exercise_Module, exerciseFontSize) takeUntil:self.rac_willDeallocSignal] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  125. @strongify(self)
  126. self.qusetionString = [self dealQuestionStrWithRQExerciseModel:exerciseModel];
  127. self.labelHeight = [self getMessageHeightWithAttributedStr:self.qusetionString andLabel:self.yyLabel];
  128. }];
  129. RAC(self, rowHeight) = [[RACSignal combineLatest:@[[RACObserve(RQ_Exercise_Module, exerciseFontSize) takeUntil:self.rac_willDeallocSignal],[RACObserve(self, imageHeight) takeUntil:self.rac_willDeallocSignal],[RACObserve(self, labelHeight) takeUntil:self.rac_willDeallocSignal]] reduce:^(NSNumber *exerciseFontSize, NSNumber *imageHeight, NSNumber *labelHeight) {
  130. return @(16.f + labelHeight.floatValue + ((imageHeight.floatValue == 0)? 0 : (16.f + imageHeight.floatValue)) + 16.f);
  131. }] deliverOnMainThread] ;
  132. }
  133. return self;
  134. }
  135. - (NSString *)newString {
  136. return @"新规题";
  137. }
  138. - (NSMutableAttributedString *)dealQuestionStrWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
  139. switch (exerciseModel.ydtQuestionModel.Type) {
  140. case 1:
  141. self.typeString = @"判断题";
  142. break;
  143. case 2:
  144. self.typeString = @"单选题";
  145. break;
  146. case 3:
  147. self.typeString = @"多选题";
  148. break;
  149. default:
  150. self.typeString = @"";
  151. break;
  152. }
  153. NSString *questionStr = [NSString stringWithFormat:@"%ld、%@",(long)exerciseModel.ydtQuestionModel.num + 1 ,exerciseModel.ydtQuestionModel.Question];
  154. NSMutableAttributedString *text = [NSMutableAttributedString new];
  155. NSMutableAttributedString *testText = [NSMutableAttributedString new];
  156. NSMutableAttributedString *typeAttachment = nil;
  157. QMUIButton *typeBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  158. CGFloat btnHeight = (25.f / 17.f) * RQ_Exercise_Module.exerciseFontSize;
  159. CGFloat btnWeight = (60.f / 25.f) * btnHeight;
  160. CGFloat btnCornerRadius = btnHeight / 2.f;
  161. CGSize btnSize = CGSizeMake(btnWeight, btnHeight);
  162. typeBtn.size = btnSize;
  163. [typeBtn setTitle:self.typeString textColor:RQ_MAIN_TEXT_COLOR_WHITE Font:RQ_Exercise_Module.exerciseFontSize - 3 fotState:UIControlStateNormal];
  164. [typeBtn setBackgroundImage:[UIImage qmui_imageWithColor:RQ_MAIN_COLOR size:btnSize cornerRadiusArray:@[@(btnCornerRadius), @(btnCornerRadius), @0, @(btnCornerRadius)]] forState:UIControlStateNormal];
  165. typeAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:typeBtn
  166. contentMode:UIViewContentModeCenter
  167. attachmentSize:btnSize
  168. alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]
  169. alignment:YYTextVerticalAlignmentCenter];
  170. [testText appendAttributedString:typeAttachment];
  171. [testText appendAttributedString:self.spaceAttachment];
  172. if(exerciseModel.isLocalityLX){//是本地练习
  173. NSMutableAttributedString *newAttachment = nil;
  174. QMUIButton *newBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  175. newBtn.size = btnSize;
  176. [newBtn setTitle:@"本市考题" textColor:RQ_MAIN_TEXT_COLOR_WHITE Font:RQ_Exercise_Module.exerciseFontSize - 6 fotState:UIControlStateNormal];
  177. [newBtn setBackgroundImage:[UIImage qmui_imageWithColor:RQColor(30, 222, 91) size:btnSize cornerRadiusArray:@[@(btnCornerRadius), @(btnCornerRadius), @(btnCornerRadius), @(btnCornerRadius)]] forState:UIControlStateNormal];
  178. newAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:newBtn
  179. contentMode:UIViewContentModeCenter
  180. attachmentSize:btnSize
  181. alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]
  182. alignment:YYTextVerticalAlignmentCenter];
  183. [testText appendAttributedString:newAttachment];
  184. [testText appendAttributedString:self.spaceAttachment];
  185. }
  186. if ([RQ_YDTQuestion_Module isNewRuleQuestionWithQuestionId:exerciseModel.ydtQuestionModel.ID]) {
  187. NSMutableAttributedString *newAttachment = nil;
  188. QMUIButton *newBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  189. newBtn.size = btnSize;
  190. [newBtn setTitle:@"新规题" textColor:RQ_MAIN_TEXT_COLOR_WHITE Font:RQ_Exercise_Module.exerciseFontSize - 3 fotState:UIControlStateNormal];
  191. [newBtn setBackgroundImage:[UIImage qmui_imageWithColor:RQColorFromHexString(@"#9C5FED") size:btnSize cornerRadiusArray:@[@(btnCornerRadius), @(btnCornerRadius), @(btnCornerRadius), @(btnCornerRadius)]] forState:UIControlStateNormal];
  192. newAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:newBtn
  193. contentMode:UIViewContentModeCenter
  194. attachmentSize:btnSize
  195. alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]
  196. alignment:YYTextVerticalAlignmentCenter];
  197. [testText appendAttributedString:newAttachment];
  198. [testText appendAttributedString:self.spaceAttachment];
  199. }
  200. NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:questionStr];
  201. attrString.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  202. attrString.yy_color = RQ_MAIN_TEXT_COLOR_1;
  203. // if (RQStringIsNotEmpty(exerciseModel.titlekeyword) && exerciseModel.isShowSkillExplanation) {
  204. // [[[exerciseModel.titlekeyword componentsSeparatedByString:@"-"].rac_sequence.signal deliverOnMainThread] subscribeNext:^(NSString * _Nullable skillkeywordStr) {
  205. // NSRange range = [questionStr rangeOfString:skillkeywordStr];
  206. // [attrString yy_setColor:RQ_MAIN_TEXT_COLOR_RED range:range];
  207. // [attrString yy_setFont:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize + 2] range:range];
  208. // }];
  209. // }
  210. // NSRange range = [string rangeOfString:@"《遵守实验室须知》"];
  211. // [attrString yy_setColor:[UIColor colorWithHexString:@"#19ADA9"] range:range];
  212. //
  213. // [attrString yy_setTextHighlightRange:range
  214. // color:[UIColor colorWithHexString:@"#19ADA9"]
  215. // backgroundColor:[UIColor blueColor]
  216. // tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
  217. // [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
  218. // }];
  219. [testText appendAttributedString: attrString];
  220. if (RQ_COMMON_MANAGER.JSJP_APP_VOICE) {//读题-是否开启
  221. //读题
  222. QMUIButton *readBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  223. CGFloat ebtnH = 20.f;
  224. CGFloat ebtnW = 58.f;
  225. CGSize ebtnSize = CGSizeMake(ebtnW, ebtnH);
  226. readBtn.size = btnSize;
  227. [readBtn setImage:RQImageNamed(@"读题pay") forState:UIControlStateNormal];
  228. [readBtn setImage:RQImageNamed(@"读题pause") forState:UIControlStateSelected];
  229. [readBtn addTarget:self action:@selector(buttonActionPayVoicedo:) forControlEvents:UIControlEventTouchUpInside];
  230. NSMutableAttributedString *readAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:readBtn
  231. contentMode:UIViewContentModeCenter
  232. attachmentSize:ebtnSize
  233. alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]
  234. alignment:YYTextVerticalAlignmentCenter];
  235. self.readBtn = readBtn;
  236. [testText appendAttributedString:self.spaceAttachment];
  237. [testText appendAttributedString:readAttachment];
  238. }
  239. [text appendAttributedString:testText];
  240. // [text appendAttributedString:[self padding]];
  241. text.yy_lineSpacing = 8;
  242. return text;
  243. }
  244. /**
  245. * 获取lb的高度
  246. * @param mess lb.text
  247. * @param lb (YYLabel *)label
  248. * @return lb的高度
  249. */
  250. - (CGFloat)getMessageHeight:(NSString *)mess andLabel:(YYLabel *)lb {
  251. NSMutableAttributedString *introText = [[NSMutableAttributedString alloc] initWithString:mess];
  252. introText.yy_font = [UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  253. introText.yy_lineSpacing = 8;
  254. lb.attributedText = introText;
  255. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2), CGFLOAT_MAX);
  256. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:introText];
  257. lb.textLayout = layout;
  258. CGFloat introHeight = layout.textBoundingSize.height;
  259. return introHeight;
  260. }
  261. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  262. lb.attributedText = attributedStr;
  263. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2), CGFLOAT_MAX);
  264. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  265. lb.textLayout = layout;
  266. CGFloat introHeight = layout.textBoundingSize.height;
  267. return introHeight;
  268. }
  269. - (YYLabel *)yyLabel {
  270. if (!_yyLabel) {
  271. _yyLabel = [[YYLabel alloc] init];
  272. }
  273. return _yyLabel;
  274. }
  275. - (NSMutableAttributedString *)spaceAttachment {
  276. if (!_spaceAttachment) {
  277. UIView *spaceView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 4, 25)];
  278. _spaceAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:spaceView contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(4, 25) alignToFont:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize] alignment:YYTextVerticalAlignmentCenter];
  279. }
  280. return _spaceAttachment;
  281. }
  282. - (NSString *)itemClassName {
  283. return @"RQExerciseQuestionCell";
  284. }
  285. #pragma mark -事件
  286. - (void)buttonActionPayVoicedo:(UIButton *)btn
  287. {
  288. btn.userInteractionEnabled = NO;
  289. btn.selected = !btn.isSelected;
  290. if(btn.selected){//播放
  291. if (NY_VOICE_MANAGER.state == NYVoiceType_Pause){
  292. [NY_VOICE_MANAGER resumePayVoiceActionUrl];//恢复
  293. }else {
  294. // self.exerciseModel.ydtQuestionModel.Question
  295. NSString *Question = self.exerciseModel.ydtQuestionModel.Question;
  296. WeakSelf(weakSelf)
  297. NY_VOICE_MANAGER.paytext = Question;
  298. [NY_VOICE_MANAGER stopPayVoiceActionUrl];
  299. NSString *url = self.exerciseModel.ydtQuestionModel.question_audio;
  300. [NY_VOICE_MANAGER startPayVoiceActionUrl:url completedBlock:^(NYVoiceType voiceType) {
  301. switch (voiceType) {
  302. case NYVoiceType_Start:{
  303. if (![weakSelf.exerciseModel.userAnswer containsObject:@"预选Option"]&&
  304. weakSelf.exerciseModel.userAnswer.count) {//未答题
  305. weakSelf.readCount++;
  306. }
  307. weakSelf.readBtn.selected = YES;
  308. }break;
  309. case NYVoiceType_Wait:{//完成-等待
  310. if ([weakSelf.exerciseModel.userAnswer containsObject:@"预选Option"]||
  311. weakSelf.exerciseModel.userAnswer.count==0) {//未答题
  312. weakSelf.readBtn.selected = NO;
  313. }else{//已答题
  314. if(weakSelf.readCount==2){
  315. weakSelf.readBtn.selected = NO;
  316. }else if(weakSelf.readCount==1){
  317. if(RQStringIsNotEmpty(weakSelf.exerciseModel.ydtQuestionModel.answer_audio)){
  318. //播放选项
  319. [NY_VOICE_MANAGER startPayVoiceActionUrl:weakSelf.exerciseModel.ydtQuestionModel.answer_audio];
  320. }else{
  321. weakSelf.readBtn.selected = NO;
  322. }
  323. }
  324. }
  325. }break;
  326. case NYVoiceType_Resume:{//恢复
  327. weakSelf.readBtn.selected = YES;
  328. }break;
  329. case NYVoiceType_Pause:{//暂停
  330. weakSelf.readBtn.selected = NO;
  331. }break;
  332. case NYVoiceType_Stop:{//停止
  333. weakSelf.readBtn.selected = NO;
  334. }break;
  335. default:
  336. break;
  337. }
  338. }];
  339. }
  340. }else {//暂停
  341. [NY_VOICE_MANAGER pausePayVoiceActionUrl];
  342. }
  343. //防止频繁点击 1 秒点一次
  344. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  345. self.readBtn.userInteractionEnabled = YES;
  346. });
  347. }
  348. @end