NYQuestionContentView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // NYQuestionContentView.m
  3. // jiaPei
  4. //
  5. // Created by Ning.ge on 2024/10/30.
  6. // Copyright © 2024 JCZ. All rights reserved.
  7. //
  8. #import "NYQuestionContentView.h"
  9. @interface NYQuestionContentView ()
  10. @property (nonatomic,weak) UIView *bottom_tools;
  11. @property (weak, nonatomic) UILabel *titleLabel;
  12. @property (weak, nonatomic) UILabel *selAnswerlabel;
  13. @property (nonatomic,weak) UIView *answer_view;
  14. @property (nonatomic,weak) RQExerciseModel *exerciseModel;
  15. @property (nonatomic,strong) UIButton *current_btn;
  16. @end
  17. @implementation NYQuestionContentView
  18. #pragma mark --UI
  19. - (instancetype)initWithFrame:(CGRect)frame
  20. {
  21. self = [super initWithFrame:frame];
  22. if (self) {
  23. [self setupUI];
  24. }
  25. return self;
  26. }
  27. - (instancetype)initWithCoder:(NSCoder *)decoder
  28. {
  29. self = [super initWithCoder:decoder];
  30. if (self) {
  31. [self setupUI];
  32. }
  33. return self;
  34. }
  35. - (void)setupUI
  36. {
  37. [self bottomUI];
  38. _questionContentViewModel = [[NYQuestionContentViewModel alloc] init];
  39. RQTableView *tableView = [[RQTableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  40. tableView.backgroundColor = [UIColor qmui_colorWithHexString:@"#F5F6F8"];
  41. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  42. // set delegate and dataSource
  43. tableView.delegate = _questionContentViewModel;
  44. tableView.dataSource = _questionContentViewModel;
  45. [self addSubview:tableView];
  46. self.me_tableView = tableView;
  47. // 注册cell
  48. [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
  49. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.top.mas_equalTo(self);
  51. make.bottom.mas_equalTo(self.bottom_tools.mas_top);
  52. make.left.mas_equalTo(self).offset(0);
  53. make.right.mas_equalTo(self).offset(0);
  54. }];
  55. }
  56. - (void)bottomUI{
  57. UIView *bottom_tools = [[UIView alloc] init];
  58. bottom_tools.backgroundColor = [UIColor qmui_colorWithHexString:@"#F5F6F8"];
  59. [self addSubview:bottom_tools];
  60. self.bottom_tools = bottom_tools;
  61. [bottom_tools mas_makeConstraints:^(MASConstraintMaker *make) {
  62. make.bottom.mas_equalTo(self);
  63. make.left.mas_equalTo(self).offset(0);
  64. make.right.mas_equalTo(self).offset(0);
  65. make.height.mas_equalTo(30);
  66. }];
  67. UILabel *titleLabel = [[UILabel alloc] init];
  68. titleLabel.backgroundColor = UIColor.clearColor;
  69. titleLabel.text = @"您选择的答案:";
  70. titleLabel.font = [UIFont boldSystemFontOfSize:14.f];
  71. titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#2B6BCC"];
  72. [bottom_tools addSubview:titleLabel];
  73. self.titleLabel = titleLabel;
  74. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.top.mas_equalTo(bottom_tools);
  76. make.left.mas_equalTo(bottom_tools).offset(9);
  77. make.height.mas_equalTo(30);
  78. make.width.mas_equalTo(98);
  79. }];
  80. UILabel *selAnswerlabel = [[UILabel alloc] init];
  81. selAnswerlabel.backgroundColor = UIColor.clearColor;
  82. selAnswerlabel.text = @"";
  83. selAnswerlabel.font = [UIFont systemFontOfSize:14];
  84. selAnswerlabel.textColor = [UIColor qmui_colorWithHexString:@"#BC322D"];
  85. [bottom_tools addSubview:selAnswerlabel];
  86. self.selAnswerlabel = selAnswerlabel;
  87. [selAnswerlabel mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.top.mas_equalTo(bottom_tools);
  89. make.left.mas_equalTo(titleLabel.mas_right).offset(9);
  90. make.height.mas_equalTo(30);
  91. make.width.mas_equalTo(80);
  92. }];
  93. UIView *answer_view = [[UIView alloc] init];
  94. answer_view.backgroundColor = UIColor.clearColor;
  95. [bottom_tools addSubview:answer_view];
  96. self.answer_view = answer_view;
  97. [answer_view mas_makeConstraints:^(MASConstraintMaker *make) {
  98. make.bottom.top.mas_equalTo(bottom_tools);
  99. make.right.mas_equalTo(bottom_tools).offset(0);
  100. make.width.mas_equalTo(180);
  101. }];
  102. }
  103. - (void)readLodData:(RQExerciseModel *)exerciseModel{
  104. self.exerciseModel = exerciseModel;
  105. [_questionContentViewModel loadQuestionContentView:exerciseModel tableView:self.me_tableView ];
  106. //选项
  107. if(_questionContentViewModel.dataSource.count>1){
  108. [self.answer_view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  109. NSArray<NYExerciseOptionItemViewModel*> *allOptionsArr = ((RQCommonGroupViewModel*)_questionContentViewModel.dataSource[1]).itemViewModels;
  110. [self showselAnswerlabel];
  111. NSMutableArray *btn_list = [NSMutableArray array];
  112. if(allOptionsArr.count>0){
  113. CGFloat buttonWidth = 25;
  114. CGFloat spacing = 10;
  115. for (int i = 0; i < allOptionsArr.count; i++) {
  116. NYExerciseOptionItemViewModel *item = allOptionsArr[i];
  117. NSString *title = item.title;
  118. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  119. button.layer.borderWidth = 1.f;
  120. button.layer.borderColor = [UIColor qmui_colorWithHexString:@"#3D3C38"].CGColor;
  121. button.frame = CGRectMake(i * (buttonWidth + spacing), 2.5, buttonWidth, buttonWidth);
  122. button.titleLabel.font = [UIFont systemFontOfSize:14];
  123. [button setTitle:title forState:UIControlStateNormal];
  124. [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
  125. [button setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
  126. [button setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor size:CGSizeMake(100, 100)] forState:UIControlStateNormal];
  127. [button setBackgroundImage:[UIImage imageWithColor:UIColorHex(0x3169CE) size:CGSizeMake(100, 100)] forState:UIControlStateSelected];
  128. [button addTarget:self action:@selector(optionsBtnClickdo:) forControlEvents:UIControlEventTouchUpInside];
  129. [self.answer_view addSubview:button];
  130. [btn_list addObject:button];
  131. }
  132. if(self.exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered){//已答题-选中
  133. if(self.exerciseModel.userAnswer.count>0&&![self.exerciseModel.userAnswer containsObject:@"预选Option"]){
  134. for (NSString *optString in self.exerciseModel.userAnswer) {
  135. NSString *userTitle = @"";
  136. if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) {
  137. userTitle = @"A";
  138. } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) {
  139. userTitle = @"B";
  140. } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) {
  141. userTitle = @"C";
  142. } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) {
  143. userTitle = @"D";
  144. }
  145. for (UIButton *btn in btn_list) {
  146. NSLog(@"btnT =%@ uT= %@,",btn.currentTitle,userTitle);
  147. if([userTitle isEqualToString:btn.currentTitle]){
  148. btn.selected = YES;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  155. //添加一个确认按钮
  156. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  157. button.tag = 99;
  158. button.layer.borderWidth = 1.f;
  159. button.layer.borderColor = [UIColor qmui_colorWithHexString:@"#3D3C38"].CGColor;
  160. button.frame = CGRectMake(4 * (buttonWidth + spacing), 2.5, buttonWidth+10, buttonWidth);
  161. button.titleLabel.font = [UIFont systemFontOfSize:12];
  162. [button setTitle:@"确认" forState:UIControlStateNormal];
  163. [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
  164. [button setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
  165. [button setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor size:CGSizeMake(100, 100)] forState:UIControlStateNormal];
  166. [button setBackgroundImage:[UIImage imageWithColor:UIColorHex(0x3169CE) size:CGSizeMake(100, 100)] forState:UIControlStateSelected];
  167. [button addTarget:self action:@selector(affirmBtnClickdo:) forControlEvents:UIControlEventTouchUpInside];
  168. [self.answer_view addSubview:button];
  169. }
  170. }
  171. }
  172. }
  173. //确认
  174. - (void)affirmBtnClickdo:(UIButton*)btn{
  175. if(self.exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered)return;//已答题 不可用
  176. 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) {
  177. return RQStringIsNotEmpty(optStr);
  178. }].toArray;
  179. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  180. if ([self.exerciseModel.userAnswer containsObject:@"预选Option"]) {
  181. [userAnswerArr removeObject:@"预选Option"];
  182. }
  183. self.exerciseModel.userAnswer = userAnswerArr.copy;
  184. NSString *userAnswerIndexStr = [[[userAnswerArr.rac_sequence.signal map:^id _Nullable(NSString *optStr) {
  185. return @([allOptionsArr indexOfObject:optStr] + 1);
  186. }].toArray arraySortASC] componentsJoinedByString:@""];
  187. [NYTools saveExerciseModel:self.exerciseModel userKey:RQ_USER_MANAGER.currentUser.outId carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQ_Exercise_Module.currentExerciseType];//记录用户做的题目
  188. if (userAnswerIndexStr.integerValue == self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue) {
  189. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  190. //做对自动下一题
  191. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"0"];
  192. if (RQ_Exercise_Module.isRightAutoRemoveError) {
  193. [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  194. [RQ_YDT_USER_Question_Module doQuestionWithID:self.exerciseModel.ydtQuestionModel.ID myAnswer:userAnswerIndexStr.integerValue trueAnswer:self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQExerciseType_Sequential];
  195. } error:^(NSError * _Nullable error) {
  196. }];
  197. }
  198. } else {//做错提示
  199. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  200. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"1"];
  201. [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  202. [RQ_YDT_USER_Question_Module doQuestionWithID:self.exerciseModel.ydtQuestionModel.ID myAnswer:userAnswerIndexStr.integerValue trueAnswer:self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQExerciseType_Sequential];
  203. } error:^(NSError * _Nullable error) {
  204. }];
  205. }
  206. }
  207. //选项
  208. - (void)optionsBtnClickdo:(UIButton*)btn{
  209. if(self.exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered)return;//已答题 不可用
  210. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  211. btn.selected = !btn.selected;
  212. }else{
  213. if(self.current_btn){
  214. self.current_btn.selected = NO;
  215. }
  216. btn.selected = !btn.selected;
  217. }
  218. self.current_btn = btn;
  219. NSString *optString = @"";
  220. 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) {
  221. return RQStringIsNotEmpty(optStr);
  222. }].toArray;
  223. if([btn.currentTitle isEqualToString:@"A"]){
  224. optString = allOptionsArr[0];
  225. }else if([btn.currentTitle isEqualToString:@"B"]){
  226. optString = allOptionsArr[1];
  227. }else if([btn.currentTitle isEqualToString:@"C"]){
  228. optString = allOptionsArr[2];
  229. }else if([btn.currentTitle isEqualToString:@"D"]){
  230. optString = allOptionsArr[3];
  231. }
  232. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  233. if (![self.exerciseModel.userAnswer containsObject:optString]) {
  234. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  235. [userAnswerArr addObject:optString];
  236. self.exerciseModel.userAnswer = userAnswerArr.copy;
  237. } else {
  238. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  239. [userAnswerArr removeObject:optString];
  240. self.exerciseModel.userAnswer = userAnswerArr.copy;
  241. }
  242. [self showselAnswerlabel];
  243. } else {
  244. self.exerciseModel.userAnswer = @[optString].mutableCopy;
  245. [self showselAnswerlabel];
  246. [NYTools saveExerciseModel:self.exerciseModel userKey:RQ_USER_MANAGER.currentUser.outId carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQ_Exercise_Module.currentExerciseType];//记录用户做的题目
  247. if (self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue == [allOptionsArr indexOfObject:optString] + 1) {//做对自动下一题
  248. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  249. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"0"];
  250. if (RQ_Exercise_Module.isRightAutoRemoveError) {
  251. [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  252. [RQ_YDT_USER_Question_Module doQuestionWithID:self.exerciseModel.ydtQuestionModel.ID myAnswer:[allOptionsArr indexOfObject:optString] + 1 trueAnswer:self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQExerciseType_Sequential];
  253. } error:^(NSError * _Nullable error) {
  254. }];
  255. }
  256. } else {//做错提示
  257. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  258. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"1"];
  259. [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  260. [RQ_YDT_USER_Question_Module doQuestionWithID:self.exerciseModel.ydtQuestionModel.ID myAnswer:[allOptionsArr indexOfObject:optString] + 1 trueAnswer:self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQExerciseType_Sequential];
  261. } error:^(NSError * _Nullable error) {
  262. }];
  263. }
  264. }
  265. }
  266. //显示用户选择答案
  267. - (void)showselAnswerlabel{
  268. NSMutableString *answer_str = [NSMutableString string];
  269. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  270. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  271. [userAnswerArr removeObject:@"预选Option"];
  272. self.exerciseModel.userAnswer = userAnswerArr.copy;
  273. }
  274. if(self.exerciseModel.userAnswer.count>0&&![self.exerciseModel.userAnswer containsObject:@"预选Option"]){
  275. for (NSString *optString in self.exerciseModel.userAnswer) {
  276. if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) {
  277. [answer_str appendString:@"A,"];
  278. } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) {
  279. [answer_str appendString:@"B,"];
  280. } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) {
  281. [answer_str appendString:@"C,"];
  282. } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) {
  283. [answer_str appendString:@"D,"];
  284. }
  285. }
  286. [answer_str deleteCharactersInRange:NSMakeRange(answer_str.length - 1, 1)];
  287. }
  288. self.selAnswerlabel.text = answer_str;
  289. self.exerciseModel.userAnswerStr = answer_str;
  290. }
  291. @end