NYQuestionContentView.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_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];
  202. NSString *ydt_id = [NSString stringWithFormat:@"%ld",(long)self.exerciseModel.ydtQuestionModel.ID];
  203. [RQ_YDTQuestion_Module.errorRemoveArr addObject:ydt_id];
  204. // [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  205. // [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];
  206. // } error:^(NSError * _Nullable error) {
  207. //
  208. // }];
  209. }
  210. } else {//做错提示
  211. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  212. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"1"];
  213. [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];
  214. // [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  215. // [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];
  216. // } error:^(NSError * _Nullable error) {
  217. // }];
  218. }
  219. }
  220. //选项
  221. - (void)optionsBtnClickdo:(UIButton*)btn{
  222. if(self.exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered)return;//已答题 不可用
  223. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  224. btn.selected = !btn.selected;
  225. }else{
  226. if(self.current_btn){
  227. self.current_btn.selected = NO;
  228. }
  229. btn.selected = !btn.selected;
  230. }
  231. self.current_btn = btn;
  232. NSString *optString = @"";
  233. 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) {
  234. return RQStringIsNotEmpty(optStr);
  235. }].toArray;
  236. if([btn.currentTitle isEqualToString:@"A"]){
  237. optString = allOptionsArr[0];
  238. }else if([btn.currentTitle isEqualToString:@"B"]){
  239. optString = allOptionsArr[1];
  240. }else if([btn.currentTitle isEqualToString:@"C"]){
  241. optString = allOptionsArr[2];
  242. }else if([btn.currentTitle isEqualToString:@"D"]){
  243. optString = allOptionsArr[3];
  244. }
  245. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  246. if (![self.exerciseModel.userAnswer containsObject:optString]) {
  247. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  248. [userAnswerArr addObject:optString];
  249. self.exerciseModel.userAnswer = userAnswerArr.copy;
  250. } else {
  251. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  252. [userAnswerArr removeObject:optString];
  253. self.exerciseModel.userAnswer = userAnswerArr.copy;
  254. }
  255. [self showselAnswerlabel];
  256. } else {
  257. self.exerciseModel.userAnswer = @[optString].mutableCopy;
  258. [self showselAnswerlabel];
  259. [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];//记录用户做的题目
  260. if (self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue == [allOptionsArr indexOfObject:optString] + 1) {//做对自动下一题
  261. self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct;
  262. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"0"];
  263. if (RQ_Exercise_Module.isRightAutoRemoveError) {
  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. NSString *ydt_id = [NSString stringWithFormat:@"%ld",(long)self.exerciseModel.ydtQuestionModel.ID];
  266. [RQ_YDTQuestion_Module.errorRemoveArr addObject:ydt_id];
  267. // [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId: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. } else {//做错提示
  274. self.exerciseModel.answerResultsType = RQAnswerResultsType_Error;
  275. [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"1"];
  276. [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];
  277. // [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) {
  278. // [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];
  279. // } error:^(NSError * _Nullable error) {
  280. // }];
  281. }
  282. }
  283. }
  284. //显示用户选择答案
  285. - (void)showselAnswerlabel{
  286. NSMutableString *answer_str = [NSMutableString string];
  287. if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题
  288. NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy;
  289. [userAnswerArr removeObject:@"预选Option"];
  290. self.exerciseModel.userAnswer = userAnswerArr.copy;
  291. }
  292. if(self.exerciseModel.userAnswer.count>0&&![self.exerciseModel.userAnswer containsObject:@"预选Option"]){
  293. for (NSString *optString in self.exerciseModel.userAnswer) {
  294. if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) {
  295. [answer_str appendString:@"A,"];
  296. } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) {
  297. [answer_str appendString:@"B,"];
  298. } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) {
  299. [answer_str appendString:@"C,"];
  300. } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) {
  301. [answer_str appendString:@"D,"];
  302. }
  303. }
  304. [answer_str deleteCharactersInRange:NSMakeRange(answer_str.length - 1, 1)];
  305. }
  306. self.selAnswerlabel.text = answer_str;
  307. self.exerciseModel.userAnswerStr = answer_str;
  308. }
  309. @end