// // NYExerciseQuestionItemViewModel.m // jiaPei // // Created by Ning.ge on 2024/10/31. // Copyright © 2024 JCZ. All rights reserved. // #import "NYExerciseQuestionItemViewModel.h" #import "payRequsestHandler.h" @interface NYExerciseQuestionItemViewModel () @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; @property (nonatomic,assign) int readCount; @end @implementation NYExerciseQuestionItemViewModel - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel { if (self = [super init]) { @weakify(self) self.readCount = 0; 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(RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.sinaimg)){//不为空 media_name = exerciseModel.ydtQuestionModel.sinaimg; self.imageString = exerciseModel.ydtQuestionModel.sinaimg; }else{ 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; } self.imageHeight = 0; 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 { NSString *questionStr = [NSString stringWithFormat:@"%ld、%@",(long)exerciseModel.ydtQuestionModel.num + 1 ,exerciseModel.ydtQuestionModel.Question]; NSMutableAttributedString *text = [NSMutableAttributedString new]; NSMutableAttributedString *testText = [NSMutableAttributedString new]; [testText appendAttributedString:self.spaceAttachment]; NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:questionStr]; attrString.yy_font = [UIFont systemFontOfSize:16]; attrString.yy_color = RQ_MAIN_TEXT_COLOR_1; [testText appendAttributedString: attrString]; [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:16]; introText.yy_lineSpacing = 8; lb.attributedText = introText; CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH-131 -225 - (16 * 4)-40, 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-131 -225 - (16 * 4)-40, 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:16] alignment:YYTextVerticalAlignmentCenter]; } return _spaceAttachment; } - (NSString *)itemClassName { return @"NYExerciseQuestionCell"; } @end