RQExercisExplainItemViewModel.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // RQExercisExplainItemViewModel.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2022/7/3.
  6. //
  7. #import "RQExercisExplainItemViewModel.h"
  8. @interface RQExercisExplainItemViewModel ()
  9. @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel;
  10. @property (nonatomic, readwrite, strong) RQExplainModel *explainModel;
  11. @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
  12. @property (nonatomic, readwrite, strong) NSMutableAttributedString *explainString;
  13. @end
  14. @implementation RQExercisExplainItemViewModel
  15. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
  16. if (self = [super init]) {
  17. @weakify(self)
  18. self.exerciseModel = exerciseModel;
  19. self.explainModel = [RQ_QUESTION_DB_MANAGER getExplainWithExerciseModel:exerciseModel];
  20. CGFloat headerHeight = 16.f + 26.f + 16.f + 18.f;
  21. CGFloat videoHeight = 0;
  22. CGFloat textHeight = 0;
  23. CGFloat footerHeight = 16.f;
  24. if (!RQObjectIsNil(self.explainModel)) {
  25. if (RQStringIsNotEmpty(self.explainModel.short_video_url)) {
  26. videoHeight = 8.f + RQ_FIT_HORIZONTAL(184.f);
  27. self.operation = ^{
  28. @strongify(self)
  29. RQExplainVideoViewModel *explainVideoViewModel = [[RQExplainVideoViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{RQViewModelUtilKey : self.explainModel }];
  30. [RQ_APPDELEGATE.services pushViewModel:explainVideoViewModel animated:YES];
  31. };
  32. }
  33. if (RQStringIsNotEmpty(exerciseModel.explain_js)) {
  34. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:exerciseModel.explain_js];
  35. text.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  36. text.yy_lineSpacing = 8;
  37. text.yy_color = RQ_MAIN_TEXT_COLOR_1;
  38. self.explainString = text;
  39. CGFloat height = [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel];
  40. textHeight = 24.f + RQ_FIT_HORIZONTAL(30.f) + 16.f + height;
  41. }
  42. }
  43. self.rowHeight = headerHeight + videoHeight + textHeight + footerHeight;
  44. }
  45. return self;
  46. }
  47. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  48. lb.attributedText = attributedStr;
  49. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2), CGFLOAT_MAX);
  50. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  51. lb.textLayout = layout;
  52. CGFloat introHeight = layout.textBoundingSize.height;
  53. return introHeight;
  54. }
  55. - (YYLabel *)yyLabel {
  56. if (!_yyLabel) {
  57. _yyLabel = [[YYLabel alloc] init];
  58. }
  59. return _yyLabel;
  60. }
  61. - (NSString *)itemClassName {
  62. return @"RQExercisExplainCell";
  63. }
  64. @end