RQExerciseOptionItemViewModel.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // RQExerciseOptionItemViewModel.m
  3. // JSJP
  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, strong) UIColor *btnBgColor;
  13. //@property (nonatomic, readwrite, assign) RQExerciseType exerciseType;
  14. @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
  15. @property (nonatomic, readwrite, assign) CGFloat labelHeight;
  16. @end
  17. @implementation RQExerciseOptionItemViewModel
  18. /// init
  19. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel allOptionsArr:(NSArray *)allOptionsArr optString:(NSString *)optString; {
  20. if (self = [super init]) {
  21. @weakify(self)
  22. self.exerciseModel = exerciseModel;
  23. self.optsString = [[NSMutableAttributedString alloc] initWithString:optString];
  24. self.optsString.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  25. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  26. self.labelHeight = [self getMessageHeightWithAttributedStr:self.optsString andLabel:self.yyLabel];
  27. self.rowHeight = 10.f + ((self.labelHeight < RQ_FIT_HORIZONTAL(30.f)? RQ_FIT_HORIZONTAL(30.f) : self.labelHeight)) + 10.f;
  28. NSArray *allOptionsArr = [@[self.exerciseModel.ydtQuestionModel.An1,self.exerciseModel.ydtQuestionModel.An2,self.exerciseModel.ydtQuestionModel.An3,self.exerciseModel.ydtQuestionModel.An4].rac_sequence.signal filter:^BOOL(NSString *optStr) {
  29. return RQStringIsNotEmpty(optStr);
  30. }].toArray;
  31. NSMutableArray *answerOptionArr = @[].mutableCopy;
  32. for (int i = 0; i < self.exerciseModel.ydtQuestionModel.AnswerTrue.length; i ++) {
  33. NSRange range;
  34. range.location = i;
  35. range.length = 1;
  36. NSString *indexStr = [self.exerciseModel.ydtQuestionModel.AnswerTrue substringWithRange:range];
  37. [answerOptionArr addObject:[allOptionsArr objectAtIndex:indexStr.integerValue - 1]];
  38. }
  39. if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) {
  40. self.title = @"A";
  41. } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) {
  42. self.title = @"B";
  43. } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) {
  44. self.title = @"C";
  45. } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) {
  46. self.title = @"D";
  47. }
  48. [[RACObserve(self.exerciseModel, userAnswer) deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  49. @strongify(self)
  50. if (self.exerciseModel.ydtQuestionModel.Type == 3) {
  51. NSArray *userAnswerArr = (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? answerOptionArr.copy : self.exerciseModel.userAnswer;
  52. // NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  53. if ([userAnswerArr containsObject:@"预选Option"]) {
  54. if ([userAnswerArr containsObject:optString]) {
  55. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_COLOR range:NSMakeRange(0, optString.length)];
  56. self.btnBgColor = RQ_MAIN_COLOR;
  57. } else {
  58. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  59. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  60. }
  61. } else {
  62. self.operation = ^{};
  63. if ([userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) {
  64. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  65. self.title = @"";
  66. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  67. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  68. self.exerciseModel.answerResultsType = (self.exerciseModel.answerResultsType == RQAnswerResultsType_Error)? RQAnswerResultsType_Error : RQAnswerResultsType_Correct;
  69. }
  70. } else if ([userAnswerArr containsObject:optString] && ![answerOptionArr containsObject:optString]) {
  71. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)];
  72. self.title = @"";
  73. self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED;
  74. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  75. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  76. }
  77. } else if (![userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) {
  78. [_optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  79. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  80. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  81. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  82. }
  83. } else {
  84. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  85. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  86. }
  87. }
  88. } else {
  89. NSString *answerOptionStr = allOptionsArr[self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue - 1];
  90. NSArray *userAnswerArr = (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? answerOptionArr.copy : self.exerciseModel.userAnswer;
  91. // NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  92. if (userAnswerArr.count > 0) {
  93. self.operation = ^{};
  94. if ([answerOptionStr isEqualToString:optString]) {
  95. /// 正确选项
  96. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  97. if ([userAnswerArr.firstObject isEqualToString:optString]) {
  98. self.title = @"";
  99. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  100. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  101. }
  102. }
  103. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  104. } else {
  105. /// 错误选项
  106. if ([userAnswerArr containsObject:optString]) {
  107. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  108. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  109. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)];
  110. self.title = @"";
  111. self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED;
  112. } else {
  113. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  114. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  115. }
  116. } else {
  117. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  118. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  119. }
  120. }
  121. } else {
  122. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  123. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  124. }
  125. }
  126. }];
  127. [[[RACObserve(RQ_Exercise_Module, exerciseFontSize) takeUntil:self.rac_willDeallocSignal] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  128. @strongify(self)
  129. self.optsString.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  130. self.optsString = self.optsString;
  131. self.labelHeight = [self getMessageHeightWithAttributedStr:self.optsString andLabel:self.yyLabel];
  132. }];
  133. RAC(self, rowHeight) = [[[RACSignal combineLatest:@[[RACObserve(RQ_Exercise_Module, exerciseFontSize) takeUntil:self.rac_willDeallocSignal],[RACObserve(self, labelHeight) takeUntil:self.rac_willDeallocSignal]] reduce:^(NSNumber *exerciseFontSize, NSNumber *labelHeight) {
  134. return @(10.f + ((labelHeight.floatValue < RQ_FIT_HORIZONTAL(30.f)? RQ_FIT_HORIZONTAL(30.f) : labelHeight.floatValue)) + 10.f);
  135. }] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal];
  136. self.operation = (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? ^{} : ^{
  137. @strongify(self)
  138. if (self.exerciseModel.ydtQuestionModel.Type == 3) {
  139. if (![self.exerciseModel.userAnswer containsObject:optString]) {
  140. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  141. [userAnswerArr addObject:optString];
  142. self.exerciseModel.userAnswer = userAnswerArr.copy;
  143. } else {
  144. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  145. [userAnswerArr removeObject:optString];
  146. self.exerciseModel.userAnswer = userAnswerArr.copy;
  147. }
  148. } else {
  149. self.exerciseModel.userAnswer = @[optString].mutableCopy;
  150. // [RQ_YDTQuestion_Module saveHistoryQuestionNumWithQuestionId:exerciseModel.ydtQuestionModel.num];
  151. if (exerciseModel.ydtQuestionModel.AnswerTrue.integerValue == [allOptionsArr indexOfObject:optString] + 1) {
  152. [RQNotificationCenter postNotificationName:RQAutoJumpNextNotification object:nil];
  153. if (RQ_Exercise_Module.isRightAutoRemoveError) {
  154. [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId:exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  155. [RQ_YDT_USER_Question_Module doQuestionWithID:exerciseModel.ydtQuestionModel.ID myAnswer:[allOptionsArr indexOfObject:optString] + 1 trueAnswer:exerciseModel.ydtQuestionModel.AnswerTrue.integerValue carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQExerciseType_Sequential];
  156. [RQNotificationCenter postNotificationName:RQCancelWrongNotification object:nil];
  157. } error:^(NSError * _Nullable error) {
  158. }];
  159. }
  160. } else {
  161. [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  162. [RQ_YDT_USER_Question_Module doQuestionWithID:exerciseModel.ydtQuestionModel.ID myAnswer:[allOptionsArr indexOfObject:optString] + 1 trueAnswer:exerciseModel.ydtQuestionModel.AnswerTrue.integerValue carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQExerciseType_Sequential];
  163. } error:^(NSError * _Nullable error) {
  164. }];
  165. }
  166. }
  167. };
  168. }
  169. return self;
  170. }
  171. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  172. lb.attributedText = attributedStr;
  173. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 3) - RQ_FIT_HORIZONTAL(30.f), CGFLOAT_MAX);
  174. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  175. lb.textLayout = layout;
  176. CGFloat introHeight = layout.textBoundingSize.height;
  177. return introHeight + ((RQ_Exercise_Module.exerciseFontSize - 16) * 8.f);
  178. }
  179. - (YYLabel *)yyLabel {
  180. if (!_yyLabel) {
  181. _yyLabel = [[YYLabel alloc] init];
  182. }
  183. return _yyLabel;
  184. }
  185. - (NSString *)itemClassName {
  186. return @"RQExerciseOptionCell";
  187. }
  188. @end