NYExerciseOptionItemViewModel.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // NYExerciseOptionItemViewModel.m
  3. // jiaPei
  4. //
  5. // Created by Ning.ge on 2024/10/31.
  6. // Copyright © 2024 JCZ. All rights reserved.
  7. //
  8. #import "NYExerciseOptionItemViewModel.h"
  9. @interface NYExerciseOptionItemViewModel ()
  10. @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel;
  11. @property (nonatomic, readwrite, assign) RQExerciseOptionItemType exerciseOptionItemType;
  12. @property (nonatomic, readwrite, strong) NSMutableAttributedString *optsString;
  13. @property (nonatomic, readwrite, strong) UIColor *btnBgColor;
  14. //@property (nonatomic, readwrite, assign) RQExerciseType exerciseType;
  15. @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
  16. @property (nonatomic, readwrite, assign) CGFloat labelHeight;
  17. @end
  18. @implementation NYExerciseOptionItemViewModel
  19. /// init
  20. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel allOptionsArr:(NSArray *)allOptionsArr optString:(NSString *)optString; {
  21. if (self = [super init]) {
  22. @weakify(self)
  23. self.exerciseModel = exerciseModel;
  24. if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) {
  25. self.title = @"A";
  26. } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) {
  27. self.title = @"B";
  28. } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) {
  29. self.title = @"C";
  30. } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) {
  31. self.title = @"D";
  32. }
  33. optString = [NSString stringWithFormat:@"%@.%@",self.title,optString];
  34. self.optsString = [[NSMutableAttributedString alloc] initWithString:optString];
  35. self.optsString.yy_font = [UIFont systemFontOfSize:16];
  36. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  37. self.labelHeight = [self getMessageHeightWithAttributedStr:self.optsString andLabel:self.yyLabel];
  38. self.rowHeight = self.labelHeight ;
  39. 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) {
  40. return RQStringIsNotEmpty(optStr);
  41. }].toArray;
  42. NSMutableArray *answerOptionArr = @[].mutableCopy;
  43. for (int i = 0; i < self.exerciseModel.ydtQuestionModel.AnswerTrue.length; i ++) {
  44. NSRange range;
  45. range.location = i;
  46. range.length = 1;
  47. NSString *indexStr = [self.exerciseModel.ydtQuestionModel.AnswerTrue substringWithRange:range];
  48. [answerOptionArr addObject:[allOptionsArr objectAtIndex:indexStr.integerValue - 1]];
  49. }
  50. [[RACObserve(self.exerciseModel, userAnswerRecitation) deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  51. @strongify(self)
  52. NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  53. if(RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation){//背题模式
  54. userAnswerArr = self.exerciseModel.userAnswerRecitation;
  55. }
  56. if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) {
  57. self.title = @"A";
  58. } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) {
  59. self.title = @"B";
  60. } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) {
  61. self.title = @"C";
  62. } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) {
  63. self.title = @"D";
  64. }
  65. if (self.exerciseModel.ydtQuestionModel.Type == 3) {
  66. // NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  67. if ([userAnswerArr containsObject:@"预选Option"]) {
  68. if ([userAnswerArr containsObject:optString]) {
  69. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_COLOR range:NSMakeRange(0, optString.length)];
  70. self.btnBgColor = RQ_MAIN_COLOR;
  71. } else {
  72. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  73. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  74. }
  75. } else {
  76. if ([userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) {
  77. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  78. self.title = @"";
  79. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  80. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  81. self.exerciseModel.answerResultsType = (self.exerciseModel.answerResultsType == RQAnswerResultsType_Error)? RQAnswerResultsType_Error : RQAnswerResultsType_Correct;
  82. }
  83. } else if ([userAnswerArr containsObject:optString] && ![answerOptionArr containsObject:optString]) {
  84. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)];
  85. self.title = @"";
  86. self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED;
  87. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  88. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  89. }
  90. } else if (![userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) {
  91. [_optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  92. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  93. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  94. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  95. }
  96. } else {
  97. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  98. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  99. }
  100. }
  101. } else {
  102. NSString *answerOptionStr = allOptionsArr[self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue - 1];
  103. // NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  104. if (userAnswerArr.count > 0) {
  105. if ([answerOptionStr isEqualToString:optString]) {
  106. /// 正确选项
  107. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  108. if ([userAnswerArr.firstObject isEqualToString:optString]) {
  109. self.title = @"";
  110. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  111. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  112. }
  113. }
  114. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  115. } else {
  116. /// 错误选项
  117. if ([userAnswerArr containsObject:optString]) {
  118. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  119. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  120. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)];
  121. self.title = @"";
  122. self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED;
  123. } else {
  124. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  125. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  126. }
  127. } else {
  128. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  129. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  130. }
  131. }
  132. } else {
  133. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  134. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  135. }
  136. }
  137. }];
  138. [[RACObserve(self.exerciseModel, userAnswer) deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  139. @strongify(self)
  140. if (self.exerciseModel.ydtQuestionModel.Type == 3) {
  141. NSArray *userAnswerArr = (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? answerOptionArr.copy : self.exerciseModel.userAnswer;
  142. // NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  143. if ([userAnswerArr containsObject:@"预选Option"]) {
  144. if ([userAnswerArr containsObject:optString]) {
  145. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_COLOR range:NSMakeRange(0, optString.length)];
  146. self.btnBgColor = RQ_MAIN_COLOR;
  147. } else {
  148. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  149. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  150. }
  151. } else {
  152. //self.operation = ^{};
  153. if ([userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) {
  154. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  155. self.title = @"";
  156. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  157. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  158. self.exerciseModel.answerResultsType = (self.exerciseModel.answerResultsType == RQAnswerResultsType_Error)? RQAnswerResultsType_Error : RQAnswerResultsType_Correct;
  159. }
  160. } else if ([userAnswerArr containsObject:optString] && ![answerOptionArr containsObject:optString]) {
  161. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)];
  162. self.title = @"";
  163. self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED;
  164. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  165. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  166. }
  167. } else if (![userAnswerArr containsObject:optString] && [answerOptionArr containsObject:optString]) {
  168. [_optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  169. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  170. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  171. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  172. }
  173. } else {
  174. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  175. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  176. }
  177. }
  178. } else {
  179. NSString *answerOptionStr = allOptionsArr[self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue - 1];
  180. NSArray *userAnswerArr = (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation)? answerOptionArr.copy : self.exerciseModel.userAnswer;
  181. // NSArray *userAnswerArr = self.exerciseModel.userAnswer;
  182. if (userAnswerArr.count > 0) {
  183. //self.operation = ^{};
  184. if ([answerOptionStr isEqualToString:optString]) {
  185. /// 正确选项
  186. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_GREEN range:NSMakeRange(0, optString.length)];
  187. if ([userAnswerArr.firstObject isEqualToString:optString]) {
  188. self.title = @"";
  189. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  190. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  191. }
  192. }
  193. self.btnBgColor = RQ_MAIN_TEXT_COLOR_GREEN;
  194. } else {
  195. /// 错误选项
  196. if ([userAnswerArr containsObject:optString]) {
  197. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Recitation) {
  198. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  199. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:NSMakeRange(0, optString.length)];
  200. self.title = @"";
  201. self.btnBgColor = RQ_MAIN_TEXT_COLOR_RED;
  202. } else {
  203. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  204. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  205. }
  206. } else {
  207. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  208. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  209. }
  210. }
  211. } else {
  212. [self.optsString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_1 range:NSMakeRange(0, optString.length)];
  213. self.btnBgColor = RQ_MAIN_TEXT_COLOR_WHITE;
  214. }
  215. }
  216. }];
  217. [[[RACObserve(RQ_Exercise_Module, exerciseFontSize) takeUntil:self.rac_willDeallocSignal] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  218. @strongify(self)
  219. self.optsString.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  220. self.optsString = self.optsString;
  221. self.labelHeight = [self getMessageHeightWithAttributedStr:self.optsString andLabel:self.yyLabel];
  222. }];
  223. 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) {
  224. return @( labelHeight.floatValue);
  225. }] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal];
  226. //答题-block
  227. self.operation = ^{
  228. };
  229. }
  230. return self;
  231. }
  232. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  233. lb.attributedText = attributedStr;
  234. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH -131 -225 - (16 * 4)-40 - RQ_FIT_HORIZONTAL(30.f), CGFLOAT_MAX);
  235. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  236. lb.textLayout = layout;
  237. CGFloat introHeight = layout.textBoundingSize.height;
  238. return introHeight + ((RQ_Exercise_Module.exerciseFontSize - 16) * 8.f);
  239. }
  240. - (YYLabel *)yyLabel {
  241. if (!_yyLabel) {
  242. _yyLabel = [[YYLabel alloc] init];
  243. }
  244. return _yyLabel;
  245. }
  246. - (NSString *)itemClassName {
  247. return @"NYExerciseOptionCell";
  248. }
  249. @end