RQExerciseOptionItemViewModel.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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, userAnswerRecitation) deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  49. @strongify(self)
  50. NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  51. if(RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation){//背题模式
  52. userAnswerArr = self.exerciseModel.userAnswerRecitation;
  53. }
  54. if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) {
  55. self.title = @"A";
  56. } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) {
  57. self.title = @"B";
  58. } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) {
  59. self.title = @"C";
  60. } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) {
  61. self.title = @"D";
  62. }
  63. if (self.exerciseModel.ydtQuestionModel.Type == 3) {
  64. // NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  65. if ([userAnswerArr containsObject:@"预选Option"]) {
  66. if ([userAnswerArr containsObject:optString]) {
  67. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_COLOR range:NSMakeRange(0, optString.length)];
  68. self.btnBgColor = RQ_MAIN_COLOR;
  69. } else {
  70. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  71. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  72. }
  73. } else {
  74. if ([userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) {
  75. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  76. self.title = @"";
  77. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  78. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  79. self.exerciseModel.answerResultsType = (self.exerciseModel.answerResultsType == RQAnswerResultsType_Error)? RQAnswerResultsType_Error : RQAnswerResultsType_Correct;
  80. }
  81. } else if ([userAnswerArr containsObject:optString] && ![answerOptionArr containsObject:optString]) {
  82. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)];
  83. self.title = @"";
  84. self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED;
  85. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  86. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  87. }
  88. } else if (![userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) {
  89. [_optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  90. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  91. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  92. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  93. }
  94. } else {
  95. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  96. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  97. }
  98. }
  99. } else {
  100. NSString *answerOptionStr = allOptionsArr[self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue - 1];
  101. // NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  102. if (userAnswerArr.count > 0) {
  103. if ([answerOptionStr isEqualToString:optString]) {
  104. /// 正确选项
  105. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  106. if ([userAnswerArr.firstObject isEqualToString:optString]) {
  107. self.title = @"";
  108. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  109. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  110. }
  111. }
  112. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  113. } else {
  114. /// 错误选项
  115. if ([userAnswerArr containsObject:optString]) {
  116. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  117. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  118. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)];
  119. self.title = @"";
  120. self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED;
  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. } else {
  126. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  127. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  128. }
  129. }
  130. } else {
  131. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  132. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  133. }
  134. }
  135. }];
  136. [[RACObserve(self.exerciseModel, userAnswer) deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  137. @strongify(self)
  138. if (self.exerciseModel.ydtQuestionModel.Type == 3) {
  139. NSArray *userAnswerArr = (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? answerOptionArr.copy : self.exerciseModel.userAnswer;
  140. // NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  141. if ([userAnswerArr containsObject:@"预选Option"]) {
  142. if ([userAnswerArr containsObject:optString]) {
  143. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_COLOR range:NSMakeRange(0, optString.length)];
  144. self.btnBgColor = RQ_MAIN_COLOR;
  145. } else {
  146. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  147. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  148. }
  149. } else {
  150. //self.operation = ^{};
  151. if ([userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) {
  152. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  153. self.title = @"";
  154. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  155. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  156. self.exerciseModel.answerResultsType = (self.exerciseModel.answerResultsType == RQAnswerResultsType_Error)? RQAnswerResultsType_Error : RQAnswerResultsType_Correct;
  157. }
  158. } else if ([userAnswerArr containsObject:optString] && ![answerOptionArr containsObject:optString]) {
  159. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)];
  160. self.title = @"";
  161. self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED;
  162. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  163. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  164. }
  165. } else if (![userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) {
  166. [_optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  167. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  168. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  169. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  170. }
  171. } else {
  172. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  173. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  174. }
  175. }
  176. } else {
  177. NSString *answerOptionStr = allOptionsArr[self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue - 1];
  178. NSArray *userAnswerArr = (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? answerOptionArr.copy : self.exerciseModel.userAnswer;
  179. // NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  180. if (userAnswerArr.count > 0) {
  181. //self.operation = ^{};
  182. if ([answerOptionStr isEqualToString:optString]) {
  183. /// 正确选项
  184. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  185. if ([userAnswerArr.firstObject isEqualToString:optString]) {
  186. self.title = @"";
  187. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  188. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  189. }
  190. }
  191. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  192. } else {
  193. /// 错误选项
  194. if ([userAnswerArr containsObject:optString]) {
  195. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  196. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  197. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)];
  198. self.title = @"";
  199. self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED;
  200. } else {
  201. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  202. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  203. }
  204. } else {
  205. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  206. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  207. }
  208. }
  209. } else {
  210. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  211. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  212. }
  213. }
  214. }];
  215. [[[RACObserve(RQ_Exercise_Module, exerciseFontSize) takeUntil:self.rac_willDeallocSignal] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  216. @strongify(self)
  217. self.optsString.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  218. self.optsString = self.optsString;
  219. self.labelHeight = [self getMessageHeightWithAttributedStr:self.optsString andLabel:self.yyLabel];
  220. }];
  221. 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) {
  222. return @(10.f + ((labelHeight.floatValue < RQ_FIT_HORIZONTAL(30.f)? RQ_FIT_HORIZONTAL(30.f) : labelHeight.floatValue)) + 10.f);
  223. }] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal];
  224. //答题-block
  225. //(RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? ^{} :
  226. self.operation = ^{
  227. if(RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)return;
  228. @strongify(self)
  229. NSLog(@"RQExerciseType_xxxx----operation");
  230. if(self.exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered)return;//已答题 不可用
  231. if (RQ_USER_MANAGER.tryLookCount > 0) {//试看次数
  232. RQ_USER_MANAGER.tryLookCount = RQ_USER_MANAGER.tryLookCount - 1;
  233. }
  234. if (self.exerciseModel.ydtQuestionModel.Type == 3) {
  235. if (![self.exerciseModel.userAnswer containsObject:optString]) {
  236. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  237. [userAnswerArr addObject:optString];
  238. self.exerciseModel.userAnswer = userAnswerArr.copy;
  239. } else {
  240. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  241. [userAnswerArr removeObject:optString];
  242. self.exerciseModel.userAnswer = userAnswerArr.copy;
  243. }
  244. } else {
  245. self.exerciseModel.userAnswer = @[optString].mutableCopy;
  246. // [RQ_YDTQuestion_Module saveHistoryQuestionNumWithQuestionId:exerciseModel.ydtQuestionModel.num];
  247. [NYTools saveExerciseModel:exerciseModel userKey:RQ_USER_MANAGER.currentUser.outId carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQ_Exercise_Module.currentExerciseType];//记录用户做的题目
  248. if (exerciseModel.ydtQuestionModel.AnswerTrue.integerValue == [allOptionsArr indexOfObject:optString] + 1) {
  249. [RQNotificationCenter postNotificationName:RQAutoJumpNextNotification object:nil];
  250. if (RQ_Exercise_Module.isRightAutoRemoveError) {
  251. [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId:exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  252. [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];
  253. [RQNotificationCenter postNotificationName:RQCancelWrongNotification object:nil];
  254. } error:^(NSError * _Nullable error) {
  255. }];
  256. }
  257. } else {
  258. [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  259. [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];
  260. } error:^(NSError * _Nullable error) {
  261. }];
  262. }
  263. }
  264. };
  265. }
  266. return self;
  267. }
  268. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  269. lb.attributedText = attributedStr;
  270. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - RQ_FIT_HORIZONTAL(30.f), CGFLOAT_MAX);
  271. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  272. lb.textLayout = layout;
  273. CGFloat introHeight = layout.textBoundingSize.height;
  274. return introHeight + ((RQ_Exercise_Module.exerciseFontSize - 16) * 8.f);
  275. }
  276. - (YYLabel *)yyLabel {
  277. if (!_yyLabel) {
  278. _yyLabel = [[YYLabel alloc] init];
  279. }
  280. return _yyLabel;
  281. }
  282. - (NSString *)itemClassName {
  283. return @"RQExerciseOptionCell";
  284. }
  285. @end