NYQuestionContentView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.userAnswer.count>0){//已答题-选中
  133. if(![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. if(userAnswerArr.count==0){
  184. ShowMsg(@"请选择您的答案");
  185. return;
  186. }
  187. self.exerciseModel.userAnswer = userAnswerArr.copy;
  188. NSString *userAnswerIndexStr = [[[userAnswerArr.rac_sequence.signal map:^id _Nullable(NSString *optStr) {
  189. return @([allOptionsArr indexOfObject:optStr] + 1);
  190. }].toArray arraySortASC] componentsJoinedByString:@""];
  191. [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];//记录用户做的题目
  192. if (userAnswerIndexStr.integerValue == self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue) {
  193. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  194. //做对自动下一题
  195. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"0"];
  196. if (RQ_Exercise_Module.isRightAutoRemoveError) {
  197. [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  198. [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];
  199. } error:^(NSError * _Nullable error) {
  200. }];
  201. }
  202. } else {//做错提示
  203. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  204. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"1"];
  205. [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  206. [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];
  207. } error:^(NSError * _Nullable error) {
  208. }];
  209. }
  210. }
  211. //选项
  212. - (void)optionsBtnClickdo:(UIButton*)btn{
  213. if(self.exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered)return;//已答题 不可用
  214. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  215. btn.selected = !btn.selected;
  216. }else{
  217. if(self.current_btn){
  218. self.current_btn.selected = NO;
  219. }
  220. btn.selected = !btn.selected;
  221. }
  222. self.current_btn = btn;
  223. NSString *optString = @"";
  224. 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) {
  225. return RQStringIsNotEmpty(optStr);
  226. }].toArray;
  227. if([btn.currentTitle isEqualToString:@"A"]){
  228. optString = allOptionsArr[0];
  229. }else if([btn.currentTitle isEqualToString:@"B"]){
  230. optString = allOptionsArr[1];
  231. }else if([btn.currentTitle isEqualToString:@"C"]){
  232. optString = allOptionsArr[2];
  233. }else if([btn.currentTitle isEqualToString:@"D"]){
  234. optString = allOptionsArr[3];
  235. }
  236. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  237. if (![self.exerciseModel.userAnswer containsObject:optString]) {
  238. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  239. [userAnswerArr addObject:optString];
  240. self.exerciseModel.userAnswer = userAnswerArr.copy;
  241. } else {
  242. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  243. [userAnswerArr removeObject:optString];
  244. self.exerciseModel.userAnswer = userAnswerArr.copy;
  245. }
  246. [self showselAnswerlabel];
  247. } else {
  248. self.exerciseModel.userAnswer = @[optString].mutableCopy;
  249. [self showselAnswerlabel];
  250. [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];//记录用户做的题目
  251. if (self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue == [allOptionsArr indexOfObject:optString] + 1) {//做对自动下一题
  252. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  253. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"0"];
  254. if (RQ_Exercise_Module.isRightAutoRemoveError) {
  255. [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  256. [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];
  257. } error:^(NSError * _Nullable error) {
  258. }];
  259. }
  260. } else {//做错提示
  261. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  262. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"1"];
  263. [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  264. [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];
  265. } error:^(NSError * _Nullable error) {
  266. }];
  267. }
  268. }
  269. }
  270. //显示用户选择答案
  271. - (void)showselAnswerlabel{
  272. NSMutableString *answer_str = [NSMutableString string];
  273. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  274. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  275. [userAnswerArr removeObject:@"预选Option"];
  276. self.exerciseModel.userAnswer = userAnswerArr.copy;
  277. }
  278. if(self.exerciseModel.userAnswer.count>0&&![self.exerciseModel.userAnswer containsObject:@"预选Option"]){
  279. for (NSString *optString in self.exerciseModel.userAnswer) {
  280. if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) {
  281. [answer_str appendString:@"A,"];
  282. } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) {
  283. [answer_str appendString:@"B,"];
  284. } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) {
  285. [answer_str appendString:@"C,"];
  286. } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) {
  287. [answer_str appendString:@"D,"];
  288. }
  289. }
  290. [answer_str deleteCharactersInRange:NSMakeRange(answer_str.length - 1, 1)];
  291. }
  292. self.selAnswerlabel.text = answer_str;
  293. self.exerciseModel.userAnswerStr = answer_str;
  294. }
  295. @end