NYQuestionContentView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. if(iPhone6){
  101. make.width.mas_equalTo(130);
  102. }else{
  103. make.width.mas_equalTo(180);
  104. }
  105. }];
  106. }
  107. - (void)readLodData:(RQExerciseModel *)exerciseModel{
  108. self.exerciseModel = exerciseModel;
  109. [_questionContentViewModel loadQuestionContentView:exerciseModel tableView:self.me_tableView ];
  110. //选项
  111. if(_questionContentViewModel.dataSource.count>1){
  112. [self.answer_view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  113. NSArray<NYExerciseOptionItemViewModel*> *allOptionsArr = ((RQCommonGroupViewModel*)_questionContentViewModel.dataSource[1]).itemViewModels;
  114. [self showselAnswerlabel];
  115. NSMutableArray *btn_list = [NSMutableArray array];
  116. if(allOptionsArr.count>0){
  117. CGFloat buttonWidth = 25;
  118. CGFloat spacing = 10;
  119. for (int i = 0; i < allOptionsArr.count; i++) {
  120. NYExerciseOptionItemViewModel *item = allOptionsArr[i];
  121. NSString *title = item.title;
  122. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  123. button.layer.borderWidth = 1.f;
  124. button.layer.borderColor = [UIColor qmui_colorWithHexString:@"#3D3C38"].CGColor;
  125. button.frame = CGRectMake(i * (buttonWidth + spacing), 2.5, buttonWidth, buttonWidth);
  126. button.titleLabel.font = [UIFont systemFontOfSize:14];
  127. [button setTitle:title forState:UIControlStateNormal];
  128. [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
  129. [button setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
  130. [button setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor size:CGSizeMake(100, 100)] forState:UIControlStateNormal];
  131. [button setBackgroundImage:[UIImage imageWithColor:UIColorHex(0x3169CE) size:CGSizeMake(100, 100)] forState:UIControlStateSelected];
  132. [button addTarget:self action:@selector(optionsBtnClickdo:) forControlEvents:UIControlEventTouchUpInside];
  133. [self.answer_view addSubview:button];
  134. [btn_list addObject:button];
  135. }
  136. if(self.exerciseModel.userAnswer.count>0){//已答题-选中
  137. if(![self.exerciseModel.userAnswer containsObject:@"预选Option"]){
  138. for (NSString *optString in self.exerciseModel.userAnswer) {
  139. NSString *userTitle = @"";
  140. if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) {
  141. userTitle = @"A";
  142. } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) {
  143. userTitle = @"B";
  144. } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) {
  145. userTitle = @"C";
  146. } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) {
  147. userTitle = @"D";
  148. }
  149. for (UIButton *btn in btn_list) {
  150. NSLog(@"btnT =%@ uT= %@,",btn.currentTitle,userTitle);
  151. if([userTitle isEqualToString:btn.currentTitle]){
  152. btn.selected = YES;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  159. //添加一个确认按钮
  160. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  161. button.tag = 99;
  162. button.layer.borderWidth = 1.f;
  163. button.layer.borderColor = [UIColor qmui_colorWithHexString:@"#3D3C38"].CGColor;
  164. button.frame = CGRectMake(4 * (buttonWidth + spacing), 2.5, buttonWidth+10, buttonWidth);
  165. button.titleLabel.font = [UIFont systemFontOfSize:12];
  166. [button setTitle:@"确认" forState:UIControlStateNormal];
  167. [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
  168. [button setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
  169. [button setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor size:CGSizeMake(100, 100)] forState:UIControlStateNormal];
  170. [button setBackgroundImage:[UIImage imageWithColor:UIColorHex(0x3169CE) size:CGSizeMake(100, 100)] forState:UIControlStateSelected];
  171. [button addTarget:self action:@selector(affirmBtnClickdo:) forControlEvents:UIControlEventTouchUpInside];
  172. [self.answer_view addSubview:button];
  173. }
  174. }
  175. }
  176. }
  177. //确认
  178. - (void)affirmBtnClickdo:(UIButton*)btn{
  179. if(self.exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered)return;//已答题 不可用
  180. 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) {
  181. return RQStringIsNotEmpty(optStr);
  182. }].toArray;
  183. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  184. if ([self.exerciseModel.userAnswer containsObject:@"预选Option"]) {
  185. [userAnswerArr removeObject:@"预选Option"];
  186. }
  187. if(userAnswerArr.count==0){
  188. ShowMsg(@"请选择您的答案");
  189. return;
  190. }
  191. self.exerciseModel.userAnswer = userAnswerArr.copy;
  192. NSString *userAnswerIndexStr = [[[userAnswerArr.rac_sequence.signal map:^id _Nullable(NSString *optStr) {
  193. return @([allOptionsArr indexOfObject:optStr] + 1);
  194. }].toArray arraySortASC] componentsJoinedByString:@""];
  195. [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];//记录用户做的题目
  196. if (userAnswerIndexStr.integerValue == self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue) {
  197. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  198. //做对自动下一题
  199. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"0"];
  200. if (RQ_Exercise_Module.isRightAutoRemoveError) {
  201. [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId: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. } else {//做错提示
  207. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  208. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"1"];
  209. [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  210. [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];
  211. } error:^(NSError * _Nullable error) {
  212. }];
  213. }
  214. }
  215. //选项
  216. - (void)optionsBtnClickdo:(UIButton*)btn{
  217. if(self.exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered)return;//已答题 不可用
  218. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  219. btn.selected = !btn.selected;
  220. }else{
  221. if(self.current_btn){
  222. self.current_btn.selected = NO;
  223. }
  224. btn.selected = !btn.selected;
  225. }
  226. self.current_btn = btn;
  227. NSString *optString = @"";
  228. 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) {
  229. return RQStringIsNotEmpty(optStr);
  230. }].toArray;
  231. if([btn.currentTitle isEqualToString:@"A"]){
  232. optString = allOptionsArr[0];
  233. }else if([btn.currentTitle isEqualToString:@"B"]){
  234. optString = allOptionsArr[1];
  235. }else if([btn.currentTitle isEqualToString:@"C"]){
  236. optString = allOptionsArr[2];
  237. }else if([btn.currentTitle isEqualToString:@"D"]){
  238. optString = allOptionsArr[3];
  239. }
  240. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  241. if (![self.exerciseModel.userAnswer containsObject:optString]) {
  242. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  243. [userAnswerArr addObject:optString];
  244. self.exerciseModel.userAnswer = userAnswerArr.copy;
  245. } else {
  246. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  247. [userAnswerArr removeObject:optString];
  248. self.exerciseModel.userAnswer = userAnswerArr.copy;
  249. }
  250. [self showselAnswerlabel];
  251. } else {
  252. self.exerciseModel.userAnswer = @[optString].mutableCopy;
  253. [self showselAnswerlabel];
  254. [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];//记录用户做的题目
  255. if (self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue == [allOptionsArr indexOfObject:optString] + 1) {//做对自动下一题
  256. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  257. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"0"];
  258. if (RQ_Exercise_Module.isRightAutoRemoveError) {
  259. [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId: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. } else {//做错提示
  265. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  266. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"1"];
  267. [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  268. [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];
  269. } error:^(NSError * _Nullable error) {
  270. }];
  271. }
  272. }
  273. }
  274. //显示用户选择答案
  275. - (void)showselAnswerlabel{
  276. NSMutableString *answer_str = [NSMutableString string];
  277. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  278. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  279. [userAnswerArr removeObject:@"预选Option"];
  280. self.exerciseModel.userAnswer = userAnswerArr.copy;
  281. }
  282. if(self.exerciseModel.userAnswer.count>0&&![self.exerciseModel.userAnswer containsObject:@"预选Option"]){
  283. for (NSString *optString in self.exerciseModel.userAnswer) {
  284. if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) {
  285. [answer_str appendString:@"A,"];
  286. } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) {
  287. [answer_str appendString:@"B,"];
  288. } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) {
  289. [answer_str appendString:@"C,"];
  290. } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) {
  291. [answer_str appendString:@"D,"];
  292. }
  293. }
  294. [answer_str deleteCharactersInRange:NSMakeRange(answer_str.length - 1, 1)];
  295. }
  296. self.selAnswerlabel.text = answer_str;
  297. self.exerciseModel.userAnswerStr = answer_str;
  298. }
  299. @end