// // RQExerciseOptionItemViewModel.m // JSJP // // Created by 张嵘 on 2021/8/12. // #import "RQExerciseOptionItemViewModel.h" @interface RQExerciseOptionItemViewModel () @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel; @property (nonatomic, readwrite, assign) RQExerciseOptionItemType exerciseOptionItemType; @property (nonatomic, readwrite, strong) NSMutableAttributedString *optsString; @property (nonatomic, readwrite, strong) UIColor *btnBgColor; //@property (nonatomic, readwrite, assign) RQExerciseType exerciseType; @property (nonatomic, readwrite, strong) YYLabel *yyLabel; @property (nonatomic, readwrite, assign) CGFloat labelHeight; @end @implementation RQExerciseOptionItemViewModel /// init - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel allOptionsArr:(NSArray *)allOptionsArr optString:(NSString *)optString; { if (self = [super init]) { @weakify(self) self.exerciseModel = exerciseModel; self.optsString = [[NSMutableAttributedString alloc] initWithString:optString]; self.optsString.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]; self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE; self.labelHeight = [self getMessageHeightWithAttributedStr:self.optsString andLabel:self.yyLabel]; self.rowHeight = 10.f + ((self.labelHeight < RQ_FIT_HORIZONTAL(30.f)? RQ_FIT_HORIZONTAL(30.f) : self.labelHeight)) + 10.f; 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) { return RQStringIsNotEmpty(optStr); }].toArray; NSMutableArray *answerOptionArr = @[].mutableCopy; for (int i = 0; i < self.exerciseModel.ydtQuestionModel.AnswerTrue.length; i ++) { NSRange range; range.location = i; range.length = 1; NSString *indexStr = [self.exerciseModel.ydtQuestionModel.AnswerTrue substringWithRange:range]; [answerOptionArr addObject:[allOptionsArr objectAtIndex:indexStr.integerValue - 1]]; } if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) { self.title = @"A"; } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) { self.title = @"B"; } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) { self.title = @"C"; } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) { self.title = @"D"; } [[RACObserve(self.exerciseModel, userAnswer) deliverOnMainThread] subscribeNext:^(id _Nullable x) { @strongify(self) if (self.exerciseModel.ydtQuestionModel.Type == 3) { NSArray *userAnswerArr = (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? answerOptionArr.copy : self.exerciseModel.userAnswer; // NSArray *userAnswerArr = self.exerciseModel.userAnswer; if ([userAnswerArr containsObject:@"预选Option"]) { if ([userAnswerArr containsObject:optString]) { [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_COLOR range:NSMakeRange(0, optString.length)]; self.btnBgColor = RQ_MAIN_COLOR; } else { [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)]; self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE; } } else { self.operation = ^{}; if ([userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) { [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)]; self.title = @""; self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN; if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) { self.exerciseModel.answerResultsType = (self.exerciseModel.answerResultsType == RQAnswerResultsType_Error)? RQAnswerResultsType_Error : RQAnswerResultsType_Correct; } } else if ([userAnswerArr containsObject:optString] && ![answerOptionArr containsObject:optString]) { [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)]; self.title = @""; self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED; if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) { self.exerciseModel.answerResultsType = RQAnswerResultsType_Error; } } else if (![userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) { [_optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)]; self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN; if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) { self.exerciseModel.answerResultsType = RQAnswerResultsType_Error; } } else { [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)]; self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE; } } } else { NSString *answerOptionStr = allOptionsArr[self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue - 1]; NSArray *userAnswerArr = (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? answerOptionArr.copy : self.exerciseModel.userAnswer; // NSArray *userAnswerArr = self.exerciseModel.userAnswer; if (userAnswerArr.count > 0) { self.operation = ^{}; if ([answerOptionStr isEqualToString:optString]) { /// 正确选项 [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)]; if ([userAnswerArr.firstObject isEqualToString:optString]) { self.title = @""; if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) { self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct; } } self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN; } else { /// 错误选项 if ([userAnswerArr containsObject:optString]) { if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) { self.exerciseModel.answerResultsType = RQAnswerResultsType_Error; [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)]; self.title = @""; self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED; } else { [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)]; self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE; } } else { [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)]; self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE; } } } else { [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)]; self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE; } } }]; [[[RACObserve(RQ_Exercise_Module, exerciseFontSize) takeUntil:self.rac_willDeallocSignal] deliverOnMainThread] subscribeNext:^(id _Nullable x) { @strongify(self) self.optsString.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]; self.optsString = self.optsString; self.labelHeight = [self getMessageHeightWithAttributedStr:self.optsString andLabel:self.yyLabel]; }]; 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) { return @(10.f + ((labelHeight.floatValue < RQ_FIT_HORIZONTAL(30.f)? RQ_FIT_HORIZONTAL(30.f) : labelHeight.floatValue)) + 10.f); }] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal]; //答题-block self.operation = (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? ^{} : ^{ @strongify(self) if (RQ_USER_MANAGER.tryLookCount > 0) {//试看次数 RQ_USER_MANAGER.tryLookCount = RQ_USER_MANAGER.tryLookCount - 1; } if (self.exerciseModel.ydtQuestionModel.Type == 3) { if (![self.exerciseModel.userAnswer containsObject:optString]) { NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy; [userAnswerArr addObject:optString]; self.exerciseModel.userAnswer = userAnswerArr.copy; } else { NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy; [userAnswerArr removeObject:optString]; self.exerciseModel.userAnswer = userAnswerArr.copy; } } else { self.exerciseModel.userAnswer = @[optString].mutableCopy; // [RQ_YDTQuestion_Module saveHistoryQuestionNumWithQuestionId:exerciseModel.ydtQuestionModel.num]; if (exerciseModel.ydtQuestionModel.AnswerTrue.integerValue == [allOptionsArr indexOfObject:optString] + 1) { [RQNotificationCenter postNotificationName:RQAutoJumpNextNotification object:nil]; if (RQ_Exercise_Module.isRightAutoRemoveError) { [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId:exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) { [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]; [RQNotificationCenter postNotificationName:RQCancelWrongNotification object:nil]; } error:^(NSError * _Nullable error) { }]; } } else { [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) { [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]; } error:^(NSError * _Nullable error) { }]; } } }; } return self; } - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb { lb.attributedText = attributedStr; CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 3) - RQ_FIT_HORIZONTAL(30.f), CGFLOAT_MAX); YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr]; lb.textLayout = layout; CGFloat introHeight = layout.textBoundingSize.height; return introHeight + ((RQ_Exercise_Module.exerciseFontSize - 16) * 8.f); } - (YYLabel *)yyLabel { if (!_yyLabel) { _yyLabel = [[YYLabel alloc] init]; } return _yyLabel; } - (NSString *)itemClassName { return @"RQExerciseOptionCell"; } @end