RQExerciseOptionItemViewModel.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // RQExerciseOptionItemViewModel.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2021/8/12.
  6. //
  7. #import "RQExerciseOptionItemViewModel.h"
  8. @interface RQExerciseOptionItemViewModel ()
  9. @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel;
  10. @property (nonatomic, readwrite, assign) RQExerciseOptionItemType exerciseOptionItemType;
  11. @property (nonatomic, readwrite, strong) NSMutableAttributedString *optsString;
  12. @property (nonatomic, readwrite, assign) RQExerciseType exerciseType;
  13. @end
  14. @implementation RQExerciseOptionItemViewModel
  15. /// init
  16. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel opts:(NSString *)opts RQExerciseOptionItemType:(RQExerciseOptionItemType)exerciseOptionItemType exerciseType:(RQExerciseType)exerciseType {
  17. if (self = [super init]) {
  18. self.exerciseModel = exerciseModel;
  19. self.exerciseOptionItemType = exerciseOptionItemType;
  20. self.exerciseType = exerciseType;
  21. opts = [opts containsString:@"×"]? @"错误" : ([opts containsString:@"√"]? @"正确" : opts);
  22. self.optsString = [[NSMutableAttributedString alloc] initWithString:opts];
  23. UIColor *noKeywordColor = RQ_MAIN_LINE_COLOR_1;
  24. switch (exerciseOptionItemType) {
  25. case RQExerciseOptionItemType_Correct_CorrectOption:
  26. case RQExerciseOptionItemType_Error_CorrectOption:{
  27. noKeywordColor = RQ_MAIN_TEXT_COLOR_GREEN;
  28. }
  29. break;
  30. case RQExerciseOptionItemType_Correct_ErrorOption:
  31. case RQExerciseOptionItemType_Error_ErrorOption:{
  32. noKeywordColor = RQ_MAIN_TEXT_COLOR_RED;
  33. }
  34. break;
  35. case RQExerciseOptionItemType_Multiple_Primary: {
  36. noKeywordColor = RQ_MAIN_COLOR;
  37. }
  38. break;
  39. default: {
  40. noKeywordColor = RQ_MAIN_TEXT_COLOR_1;
  41. }
  42. break;
  43. }
  44. if (RQStringIsNotEmpty(exerciseModel.answerkeyword) && exerciseModel.isShowSkillExplanation) {
  45. [[[exerciseModel.answerkeyword componentsSeparatedByString:@"-"].rac_sequence.signal deliverOnMainThread] subscribeNext:^(NSString * _Nullable answerkeyword) {
  46. NSRange targetRange = NSMakeRange(0, opts.length);
  47. NSRange range = targetRange;
  48. [_optsString addAttribute:NSForegroundColorAttributeName value:noKeywordColor range:range];
  49. while (true) {
  50. range = [opts rangeOfString:answerkeyword options:NSLiteralSearch range:targetRange];
  51. if (range.location != NSNotFound) {
  52. targetRange = NSMakeRange(NSMaxRange(range), opts.length-NSMaxRange(range));
  53. [_optsString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize + 2] range:range];
  54. [_optsString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize] range:targetRange];
  55. [_optsString addAttribute:NSForegroundColorAttributeName value:(exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered)? noKeywordColor : RQ_MAIN_TEXT_COLOR_RED range:range];
  56. [_optsString addAttribute:NSForegroundColorAttributeName value:noKeywordColor range:targetRange];
  57. } else {
  58. break;
  59. }
  60. }
  61. } completed:^{
  62. }];
  63. } else {
  64. [_optsString addAttribute:NSForegroundColorAttributeName value:noKeywordColor range:NSMakeRange(0, opts.length)];
  65. }
  66. }
  67. return self;
  68. }
  69. - (NSString *)itemClassName {
  70. return @"RQExerciseOptionCell";
  71. }
  72. @end