// // NYExerciseOptionItemViewModel.m // jiaPei // // Created by Ning.ge on 2024/10/31. // Copyright © 2024 JCZ. All rights reserved. // #import "NYExerciseOptionItemViewModel.h" @interface NYExerciseOptionItemViewModel () @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 NYExerciseOptionItemViewModel /// init - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel allOptionsArr:(NSArray *)allOptionsArr optString:(NSString *)optString; { if (self = [super init]) { @weakify(self) self.exerciseModel = exerciseModel; 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"; } optString = [NSString stringWithFormat:@"%@.%@",self.title,optString]; self.optsString = [[NSMutableAttributedString alloc] initWithString:optString]; self.optsString.yy_font = [UIFont systemFontOfSize:16]; self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE; self.labelHeight = [self getMessageHeightWithAttributedStr:self.optsString andLabel:self.yyLabel]; self.rowHeight = self.labelHeight ; 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]]; } [[RACObserve(self.exerciseModel, userAnswerRecitation) deliverOnMainThread] subscribeNext:^(id _Nullable x) { @strongify(self) NSArray *userAnswerArr = self.exerciseModel.userAnswer; if(RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation){//背题模式 userAnswerArr = self.exerciseModel.userAnswerRecitation; } 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"; } if (self.exerciseModel.ydtQuestionModel.Type == 3) { // 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 { 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 = self.exerciseModel.userAnswer; if (userAnswerArr.count > 0) { 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(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 @( labelHeight.floatValue); }] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal]; //答题-block self.operation = ^{ }; } return self; } - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb { lb.attributedText = attributedStr; CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH -131 -225 - (16 * 4)-40 - 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 @"NYExerciseOptionCell"; } @end