RQExerciseQuestionItemViewModel.m 17 KB

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