RQExerciseQuestionItemViewModel.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 ([RQ_YDTQuestion_Module isNewRuleQuestionWithQuestionId:exerciseModel.ydtQuestionModel.ID]) {
  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 - 3 fotState:UIControlStateNormal];
  177. [newBtn setBackgroundImage:[UIImage qmui_imageWithColor:RQColorFromHexString(@"#9C5FED") 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. NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:questionStr];
  187. attrString.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  188. attrString.yy_color = RQ_MAIN_TEXT_COLOR_1;
  189. // if (RQStringIsNotEmpty(exerciseModel.titlekeyword) && exerciseModel.isShowSkillExplanation) {
  190. // [[[exerciseModel.titlekeyword componentsSeparatedByString:@"-"].rac_sequence.signal deliverOnMainThread] subscribeNext:^(NSString * _Nullable skillkeywordStr) {
  191. // NSRange range = [questionStr rangeOfString:skillkeywordStr];
  192. // [attrString yy_setColor:RQ_MAIN_TEXT_COLOR_RED range:range];
  193. // [attrString yy_setFont:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize + 2] range:range];
  194. // }];
  195. // }
  196. // NSRange range = [string rangeOfString:@"《遵守实验室须知》"];
  197. // [attrString yy_setColor:[UIColor colorWithHexString:@"#19ADA9"] range:range];
  198. //
  199. // [attrString yy_setTextHighlightRange:range
  200. // color:[UIColor colorWithHexString:@"#19ADA9"]
  201. // backgroundColor:[UIColor blueColor]
  202. // tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
  203. // [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
  204. // }];
  205. [testText appendAttributedString: attrString];
  206. if (RQ_COMMON_MANAGER.JSJP_APP_VOICE) {//读题-是否开启
  207. //读题
  208. QMUIButton *readBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  209. CGFloat ebtnH = 20.f;
  210. CGFloat ebtnW = 58.f;
  211. CGSize ebtnSize = CGSizeMake(ebtnW, ebtnH);
  212. readBtn.size = btnSize;
  213. [readBtn setImage:RQImageNamed(@"读题pay") forState:UIControlStateNormal];
  214. [readBtn setImage:RQImageNamed(@"读题pause") forState:UIControlStateSelected];
  215. [readBtn addTarget:self action:@selector(buttonActionPayVoicedo:) forControlEvents:UIControlEventTouchUpInside];
  216. NSMutableAttributedString *readAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:readBtn
  217. contentMode:UIViewContentModeCenter
  218. attachmentSize:ebtnSize
  219. alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]
  220. alignment:YYTextVerticalAlignmentCenter];
  221. self.readBtn = readBtn;
  222. [testText appendAttributedString:self.spaceAttachment];
  223. [testText appendAttributedString:readAttachment];
  224. }
  225. [text appendAttributedString:testText];
  226. // [text appendAttributedString:[self padding]];
  227. text.yy_lineSpacing = 8;
  228. return text;
  229. }
  230. /**
  231. * 获取lb的高度
  232. * @param mess lb.text
  233. * @param lb (YYLabel *)label
  234. * @return lb的高度
  235. */
  236. - (CGFloat)getMessageHeight:(NSString *)mess andLabel:(YYLabel *)lb {
  237. NSMutableAttributedString *introText = [[NSMutableAttributedString alloc] initWithString:mess];
  238. introText.yy_font = [UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  239. introText.yy_lineSpacing = 8;
  240. lb.attributedText = introText;
  241. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2), CGFLOAT_MAX);
  242. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:introText];
  243. lb.textLayout = layout;
  244. CGFloat introHeight = layout.textBoundingSize.height;
  245. return introHeight;
  246. }
  247. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  248. lb.attributedText = attributedStr;
  249. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2), CGFLOAT_MAX);
  250. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  251. lb.textLayout = layout;
  252. CGFloat introHeight = layout.textBoundingSize.height;
  253. return introHeight;
  254. }
  255. - (YYLabel *)yyLabel {
  256. if (!_yyLabel) {
  257. _yyLabel = [[YYLabel alloc] init];
  258. }
  259. return _yyLabel;
  260. }
  261. - (NSMutableAttributedString *)spaceAttachment {
  262. if (!_spaceAttachment) {
  263. UIView *spaceView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 4, 25)];
  264. _spaceAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:spaceView contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(4, 25) alignToFont:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize] alignment:YYTextVerticalAlignmentCenter];
  265. }
  266. return _spaceAttachment;
  267. }
  268. - (NSString *)itemClassName {
  269. return @"RQExerciseQuestionCell";
  270. }
  271. #pragma mark -事件
  272. - (void)buttonActionPayVoicedo:(UIButton *)btn
  273. {
  274. btn.userInteractionEnabled = NO;
  275. btn.selected = !btn.isSelected;
  276. if(btn.selected){//播放
  277. if (NY_VOICE_MANAGER.state == NYVoiceType_Pause){
  278. [NY_VOICE_MANAGER resumePayVoiceActionUrl];//恢复
  279. }else {
  280. // self.exerciseModel.ydtQuestionModel.Question
  281. NSString *Question = self.exerciseModel.ydtQuestionModel.Question;
  282. WeakSelf(weakSelf)
  283. NY_VOICE_MANAGER.paytext = Question;
  284. [NY_VOICE_MANAGER stopPayVoiceActionUrl];
  285. NSString *url = self.exerciseModel.ydtQuestionModel.question_audio;
  286. [NY_VOICE_MANAGER startPayVoiceActionUrl:url completedBlock:^(NYVoiceType voiceType) {
  287. switch (voiceType) {
  288. case NYVoiceType_Start:{
  289. if (![weakSelf.exerciseModel.userAnswer containsObject:@"预选Option"]&&
  290. weakSelf.exerciseModel.userAnswer.count) {//未答题
  291. weakSelf.readCount++;
  292. }
  293. weakSelf.readBtn.selected = YES;
  294. }break;
  295. case NYVoiceType_Wait:{//完成-等待
  296. if ([weakSelf.exerciseModel.userAnswer containsObject:@"预选Option"]||
  297. weakSelf.exerciseModel.userAnswer.count==0) {//未答题
  298. weakSelf.readBtn.selected = NO;
  299. }else{//已答题
  300. if(weakSelf.readCount==2){
  301. weakSelf.readBtn.selected = NO;
  302. }else if(weakSelf.readCount==1){
  303. if(RQStringIsNotEmpty(weakSelf.exerciseModel.ydtQuestionModel.answer_audio)){
  304. //播放选项
  305. [NY_VOICE_MANAGER startPayVoiceActionUrl:weakSelf.exerciseModel.ydtQuestionModel.answer_audio];
  306. }else{
  307. weakSelf.readBtn.selected = NO;
  308. }
  309. }
  310. }
  311. }break;
  312. case NYVoiceType_Resume:{//恢复
  313. weakSelf.readBtn.selected = YES;
  314. }break;
  315. case NYVoiceType_Pause:{//暂停
  316. weakSelf.readBtn.selected = NO;
  317. }break;
  318. case NYVoiceType_Stop:{//停止
  319. weakSelf.readBtn.selected = NO;
  320. }break;
  321. default:
  322. break;
  323. }
  324. }];
  325. }
  326. }else {//暂停
  327. [NY_VOICE_MANAGER pausePayVoiceActionUrl];
  328. }
  329. //防止频繁点击 1 秒点一次
  330. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  331. self.readBtn.userInteractionEnabled = YES;
  332. });
  333. }
  334. @end