// // RQExerciseQuestionItemViewModel.m // JSJP // // Created by 张嵘 on 2021/8/16. // #import "RQExerciseQuestionItemViewModel.h" #import "payRequsestHandler.h" @interface RQExerciseQuestionItemViewModel () @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel; @property (nonatomic, readwrite, strong) YYLabel *yyLabel; @property (nonatomic, readwrite, strong) NSString *typeString; @property (nonatomic, readwrite, strong) NSString *newString; @property (nonatomic, readwrite, strong) NSMutableAttributedString *qusetionString; @property (nonatomic, readwrite, assign) CGFloat labelHeight; @property (nonatomic, readwrite, assign) CGFloat imageHeight; @property (nonatomic, readwrite, strong) NSString *imageString; @property (nonatomic, readwrite, strong) NSString *videoString; @property (nonatomic, readwrite, strong) NSMutableAttributedString *spaceAttachment; @property (nonatomic, readwrite, strong) QMUIButton *readBtn; @end @implementation RQExerciseQuestionItemViewModel - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel { if (self = [super init]) { @weakify(self) self.exerciseModel = exerciseModel; dispatch_async(dispatch_get_main_queue(), ^{ @strongify(self) self.qusetionString = [self dealQuestionStrWithRQExerciseModel:exerciseModel]; self.labelHeight = [self getMessageHeightWithAttributedStr:self.qusetionString andLabel:self.yyLabel]; }); if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.media_url)) { if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.sinaimg)) { self.imageString = exerciseModel.ydtQuestionModel.media_url; self.videoString = @""; } else if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.video_url)) { self.imageString = @""; self.videoString = exerciseModel.ydtQuestionModel.media_url; } else { self.imageString = @""; self.videoString = @""; } } else { self.imageString = @""; self.videoString = @""; } if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.sinaimg) && RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.media_url)) { if (!RQObjectIsNil(exerciseModel.ydtQuestionModel.media_content)) { NSString *imageStr = [exerciseModel.ydtQuestionModel.media_content base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; UIImage *sinaImage = [UIImage sd_imageWithData:[NSData dataWithBase64EncodedString:imageStr] scale:1 firstFrameOnly:YES]; exerciseModel.ydtQuestionModel.sinaImage = sinaImage; } else { NSString *media_name; if ([exerciseModel.ydtQuestionModel.media_url containsString:@"https://"]) { media_name = [exerciseModel.ydtQuestionModel.media_url componentsSeparatedByString:@"/"].lastObject; } NSData *mediaData = [RQ_YDTQuestion_Module getMediaDataWithMediaName:media_name]; if (mediaData) { NSString *imageStr = [mediaData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; UIImage *sinaImage = [UIImage sd_imageWithData:[NSData dataWithBase64EncodedString:imageStr] scale:1 firstFrameOnly:YES]; exerciseModel.ydtQuestionModel.sinaImage = sinaImage; } } } else if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.video_url) && RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.media_url)) { NSString *media_name; if ([exerciseModel.ydtQuestionModel.video_url containsString:@"https://"]) { media_name = [exerciseModel.ydtQuestionModel.video_url componentsSeparatedByString:@"/"].lastObject; } else { media_name = exerciseModel.ydtQuestionModel.video_url; } NSURL *video_filePath_Url; if (!RQObjectIsNil(exerciseModel.ydtQuestionModel.media_content) && !RQ_COMMON_MANAGER.JSJP_Is_Online_Media_Video) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:media_name]; [[NSFileManager defaultManager] createFileAtPath:path contents:exerciseModel.ydtQuestionModel.media_content attributes:nil]; video_filePath_Url = [NSURL fileURLWithPath:path]; } exerciseModel.ydtQuestionModel.video_filePath_Url = video_filePath_Url; } if (RQStringIsNotEmpty(self.imageString)) { __block CGSize imageSize = CGSizeMake(RQ_SCREEN_WIDTH - 32.f, (RQ_SCREEN_WIDTH - 32.f) * (175.f / 345.f)); if (exerciseModel.ydtQuestionModel.sinaImage) { imageSize = exerciseModel.ydtQuestionModel.sinaImage.size; } else { NSString *cacheKeyStr = [[YYWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:self.imageString]]; UIImage *cacheImage = [[YYImageCache sharedCache] getImageForKey:cacheKeyStr]; if (cacheImage) { imageSize = cacheImage.size; } else { dispatch_async(dispatch_get_global_queue(0, 0), ^{ @strongify(self) // 处理耗时操作的代码块... imageSize = [UIImage getImageSizeWithURL:self.imageString]; //通知主线程刷新 dispatch_async(dispatch_get_main_queue(), ^{ @strongify(self) //回调或者说是通知主线程刷新, if (imageSize.width == 0 || imageSize.height == 0) { self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (175.f / 345.f); } else { self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (imageSize.height / imageSize.width); } }); }); } } if (imageSize.width == 0 || imageSize.height == 0) { self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (175.f / 345.f); } else { self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (imageSize.height / imageSize.width); } } else if (RQStringIsNotEmpty(self.videoString)) { self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (175.f / 345.f); } self.rowHeight = 16.f + self.labelHeight + ((self.imageHeight == 0)? 0 : (16.f + self.imageHeight)) + 16.f; [[[RACObserve(RQ_Exercise_Module, exerciseFontSize) takeUntil:self.rac_willDeallocSignal] deliverOnMainThread] subscribeNext:^(id _Nullable x) { @strongify(self) self.qusetionString = [self dealQuestionStrWithRQExerciseModel:exerciseModel]; self.labelHeight = [self getMessageHeightWithAttributedStr:self.qusetionString andLabel:self.yyLabel]; }]; 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) { return @(16.f + labelHeight.floatValue + ((imageHeight.floatValue == 0)? 0 : (16.f + imageHeight.floatValue)) + 16.f); }] deliverOnMainThread] ; } return self; } - (NSString *)newString { return @"新规题"; } - (NSMutableAttributedString *)dealQuestionStrWithRQExerciseModel:(RQExerciseModel *)exerciseModel { switch (exerciseModel.ydtQuestionModel.Type) { case 1: self.typeString = @"判断题"; break; case 2: self.typeString = @"单选题"; break; case 3: self.typeString = @"多选题"; break; default: self.typeString = @""; break; } NSString *questionStr = [NSString stringWithFormat:@"%ld、%@",(long)exerciseModel.ydtQuestionModel.num + 1 ,exerciseModel.ydtQuestionModel.Question]; NSMutableAttributedString *text = [NSMutableAttributedString new]; NSMutableAttributedString *testText = [NSMutableAttributedString new]; NSMutableAttributedString *typeAttachment = nil; QMUIButton *typeBtn = [QMUIButton buttonWithType:UIButtonTypeCustom]; CGFloat btnHeight = (25.f / 17.f) * RQ_Exercise_Module.exerciseFontSize; CGFloat btnWeight = (60.f / 25.f) * btnHeight; CGFloat btnCornerRadius = btnHeight / 2.f; CGSize btnSize = CGSizeMake(btnWeight, btnHeight); typeBtn.size = btnSize; [typeBtn setTitle:self.typeString textColor:RQ_MAIN_TEXT_COLOR_WHITE Font:RQ_Exercise_Module.exerciseFontSize - 3 fotState:UIControlStateNormal]; [typeBtn setBackgroundImage:[UIImage qmui_imageWithColor:RQ_MAIN_COLOR size:btnSize cornerRadiusArray:@[@(btnCornerRadius), @(btnCornerRadius), @0, @(btnCornerRadius)]] forState:UIControlStateNormal]; typeAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:typeBtn contentMode:UIViewContentModeCenter attachmentSize:btnSize alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize] alignment:YYTextVerticalAlignmentCenter]; [testText appendAttributedString:typeAttachment]; [testText appendAttributedString:self.spaceAttachment]; if ([RQ_YDTQuestion_Module isNewRuleQuestionWithQuestionId:exerciseModel.ydtQuestionModel.ID]) { NSMutableAttributedString *newAttachment = nil; QMUIButton *newBtn = [QMUIButton buttonWithType:UIButtonTypeCustom]; newBtn.size = btnSize; [newBtn setTitle:@"新规题" textColor:RQ_MAIN_TEXT_COLOR_WHITE Font:RQ_Exercise_Module.exerciseFontSize - 3 fotState:UIControlStateNormal]; [newBtn setBackgroundImage:[UIImage qmui_imageWithColor:RQColorFromHexString(@"#9C5FED") size:btnSize cornerRadiusArray:@[@(btnCornerRadius), @(btnCornerRadius), @(btnCornerRadius), @(btnCornerRadius)]] forState:UIControlStateNormal]; newAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:newBtn contentMode:UIViewContentModeCenter attachmentSize:btnSize alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize] alignment:YYTextVerticalAlignmentCenter]; [testText appendAttributedString:newAttachment]; [testText appendAttributedString:self.spaceAttachment]; } NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:questionStr]; attrString.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]; attrString.yy_color = RQ_MAIN_TEXT_COLOR_1; // if (RQStringIsNotEmpty(exerciseModel.titlekeyword) && exerciseModel.isShowSkillExplanation) { // [[[exerciseModel.titlekeyword componentsSeparatedByString:@"-"].rac_sequence.signal deliverOnMainThread] subscribeNext:^(NSString * _Nullable skillkeywordStr) { // NSRange range = [questionStr rangeOfString:skillkeywordStr]; // [attrString yy_setColor:RQ_MAIN_TEXT_COLOR_RED range:range]; // [attrString yy_setFont:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize + 2] range:range]; // }]; // } // NSRange range = [string rangeOfString:@"《遵守实验室须知》"]; // [attrString yy_setColor:[UIColor colorWithHexString:@"#19ADA9"] range:range]; // // [attrString yy_setTextHighlightRange:range // color:[UIColor colorWithHexString:@"#19ADA9"] // backgroundColor:[UIColor blueColor] // tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) { // [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]]; // }]; [testText appendAttributedString: attrString]; //读题 QMUIButton *readBtn = [QMUIButton buttonWithType:UIButtonTypeCustom]; CGFloat ebtnH = 20.f; CGFloat ebtnW = 58.f; CGSize ebtnSize = CGSizeMake(ebtnW, ebtnH); readBtn.size = btnSize; [readBtn setImage:RQImageNamed(@"读题pay") forState:UIControlStateNormal]; [readBtn setImage:RQImageNamed(@"读题pause") forState:UIControlStateSelected]; [readBtn addTarget:self action:@selector(buttonActionPayVoicedo:) forControlEvents:UIControlEventTouchUpInside]; NSMutableAttributedString *readAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:readBtn contentMode:UIViewContentModeCenter attachmentSize:ebtnSize alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize] alignment:YYTextVerticalAlignmentCenter]; self.readBtn = readBtn; [testText appendAttributedString:self.spaceAttachment]; [testText appendAttributedString:readAttachment]; [text appendAttributedString:testText]; // [text appendAttributedString:[self padding]]; text.yy_lineSpacing = 8; return text; } /** * 获取lb的高度 * @param mess lb.text * @param lb (YYLabel *)label * @return lb的高度 */ - (CGFloat)getMessageHeight:(NSString *)mess andLabel:(YYLabel *)lb { NSMutableAttributedString *introText = [[NSMutableAttributedString alloc] initWithString:mess]; introText.yy_font = [UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize]; introText.yy_lineSpacing = 8; lb.attributedText = introText; CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2), CGFLOAT_MAX); YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:introText]; lb.textLayout = layout; CGFloat introHeight = layout.textBoundingSize.height; return introHeight; } - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb { lb.attributedText = attributedStr; CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2), CGFLOAT_MAX); YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr]; lb.textLayout = layout; CGFloat introHeight = layout.textBoundingSize.height; return introHeight; } - (YYLabel *)yyLabel { if (!_yyLabel) { _yyLabel = [[YYLabel alloc] init]; } return _yyLabel; } - (NSMutableAttributedString *)spaceAttachment { if (!_spaceAttachment) { UIView *spaceView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 4, 25)]; _spaceAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:spaceView contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(4, 25) alignToFont:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize] alignment:YYTextVerticalAlignmentCenter]; } return _spaceAttachment; } - (NSString *)itemClassName { return @"RQExerciseQuestionCell"; } #pragma mark -事件 - (void)buttonActionPayVoicedo:(UIButton *)btn { btn.selected = !btn.isSelected; if(btn.selected){//播放 if (NY_VOICE_MANAGER.state == NYVoiceType_Pause){ [NY_VOICE_MANAGER resumePayVoiceAction];//恢复 }else { NSString *Question = self.exerciseModel.ydtQuestionModel.Question; @weakify(self) [NY_VOICE_MANAGER startPayVoiceActionText:Question completedBlock:^(NYVoiceType voiceType) { switch (voiceType) { case NYVoiceType_Start:{ self.readBtn.selected = YES; }break; case NYVoiceType_Wait:{//完成-等待 self.readBtn.selected = NO; }break; case NYVoiceType_Resume:{//恢复 self.readBtn.selected = YES; }break; case NYVoiceType_Pause:{//暂停 self.readBtn.selected = NO; }break; case NYVoiceType_Stop:{//停止 self.readBtn.selected = NO; }break; default: break; } }]; } }else {//暂停 [NY_VOICE_MANAGER pausePayVoiceAction]; } } @end