RQExerciseQuestionItemViewModel.m 18 KB

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