// // RQExercisExplainItemViewModel.m // SDJK // // Created by 张嵘 on 2022/7/3. // #import "RQExercisExplainItemViewModel.h" @interface RQExercisExplainItemViewModel () @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel; @property (nonatomic, readwrite, strong) RQExplainModel *explainModel; @property (nonatomic, readwrite, strong) YYLabel *yyLabel; @property (nonatomic, readwrite, strong) NSMutableAttributedString *explainString; @end @implementation RQExercisExplainItemViewModel - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel { if (self = [super init]) { @weakify(self) self.exerciseModel = exerciseModel; self.explainModel = [RQ_QUESTION_DB_MANAGER getExplainWithExerciseModel:exerciseModel]; CGFloat headerHeight = 16.f + 26.f + 16.f + 18.f; CGFloat videoHeight = 0; CGFloat textHeight = 0; CGFloat footerHeight = 16.f; if (!RQObjectIsNil(self.explainModel)) { if (RQStringIsNotEmpty(self.explainModel.short_video_url)) { videoHeight = 8.f + RQ_FIT_HORIZONTAL(184.f); self.operation = ^{ @strongify(self) RQExplainVideoViewModel *explainVideoViewModel = [[RQExplainVideoViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{RQViewModelUtilKey : self.explainModel }]; [RQ_APPDELEGATE.services pushViewModel:explainVideoViewModel animated:YES]; }; } if (RQStringIsNotEmpty(exerciseModel.explain_js)) { NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:exerciseModel.explain_js]; text.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]; text.yy_lineSpacing = 8; text.yy_color = RQ_MAIN_TEXT_COLOR_1; self.explainString = text; CGFloat height = [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel]; textHeight = 24.f + RQ_FIT_HORIZONTAL(30.f) + 16.f + height; } } self.rowHeight = headerHeight + videoHeight + textHeight + footerHeight; } return self; } - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb { lb.attributedText = attributedStr; CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - (8 * 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; } - (NSString *)itemClassName { return @"RQExercisExplainCell"; } @end