GiGaMaskTime/GIGA/Modules/Mask/Exercises/Controller/GiGaQuestionVC.m

362 lines
12 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// GiGaQuestionVC.m
// GIGA
//
// Created by lianxiang on 2018/8/23.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaQuestionVC.h"
#import "Masonry.h"
#import "GiGaBaseAPiRequest.h"
#import "GiGaNetManager.h"
#import "GIGaQuestionSlider.h"
#import "MaskQuestionView.h"
#import "ExercisesModel.h"
#import "QustionAnswer.h"
#import "GiGaServerConfig.h"
@interface GiGaQuestionVC ()<QeustionViewDelegate>
@property (nonatomic,weak) MaskQuestionView *questionView;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,assign) NSInteger curentAnswerIdex;
@property (nonatomic,strong) NSMutableArray *questonArray;
@property (nonatomic,strong) NSMutableArray *answerArray;
@property (nonatomic,strong) ExercisesModel *currentModel;
@property (nonatomic,strong) UILabel *questionlabel;
@property (nonatomic,strong) UILabel *descLabel;
@property (nonatomic,strong) UIButton *numberBtn;
@property (nonatomic,strong) UIButton *nextBtn;
@property (nonatomic,strong) UIButton *preBtn;
@end
@implementation GiGaQuestionVC
- (void)viewDidLoad {
[self addNavTitile:@"测试肤质"];
[super viewDidLoad];
// self.title = @"测试肤质";
_page = 1;
//默认
_curentAnswerIdex = 0;
//
[self loadRequestQuestions];
}
-(NSMutableArray *)questonArray{
if (!_questonArray) {
_questonArray = [[NSMutableArray alloc] init];
}
return _questonArray;
}
//questionId answerid
- (NSMutableArray *)answerArray{
if (!_answerArray) {
_answerArray = [[NSMutableArray alloc] init];
}
return _answerArray;
}
-(void)initUI{
UIButton *numberBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[numberBtn setBackgroundImage:[UIImage imageNamed:@"ceshi_number"] forState:UIControlStateNormal];
NSAttributedString *numbertitle = [[NSAttributedString alloc] initWithString:@"1" attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
[numberBtn setAttributedTitle:numbertitle forState:UIControlStateNormal];
self.numberBtn = numberBtn;
[self.view addSubview:numberBtn];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = @"悄悄地告诉你小优的年龄区间是?";
titleLabel.font = [UIFont fontWithName:GIGA_FONTBOLD size:16];
titleLabel.textColor = GIGARGB(63, 63, 63, 1);
self.questionlabel = titleLabel;
[self.view addSubview:titleLabel];
UILabel *descLabel = [[UILabel alloc] init];
descLabel.text = @"精准的选择,让小优更好的为您挑选最符合您的肤质的面膜";
descLabel.font = [UIFont fontWithName:GIGA_FONTNAME size:12];
descLabel.textColor = GIGARGB(208, 208, 208, 1);
descLabel.numberOfLines = 0;
descLabel.textAlignment = NSTextAlignmentCenter;
self.descLabel = descLabel;
[self.view addSubview:descLabel];
[numberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(31, 31));
make.top.mas_equalTo(self.view.mas_top).offset(112 + PhoneX_TopMargin);
make.left.mas_equalTo(self.view.mas_left).offset(46);
}];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(numberBtn.mas_right).offset(12);
make.centerY.mas_equalTo(numberBtn.mas_centerY);
make.right.mas_equalTo(self.view.mas_right).offset(-53);
}];
[descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(numberBtn.mas_right).offset(12);
make.top.mas_equalTo(titleLabel.mas_bottom).offset(20);
make.right.mas_equalTo(self.view.mas_right).offset(-89);
}];
//测试题
[self creatQustionView];
//下一题
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
nextBtn.backgroundColor = GIGARGB(181, 14, 14, 1);
[nextBtn addTarget:self action:@selector(nextBtnAction) forControlEvents:UIControlEventTouchUpInside];
NSAttributedString *nextTitle = [[NSAttributedString alloc] initWithString:@"下一题" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTBOLD size:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
[nextBtn setAttributedTitle:nextTitle forState:UIControlStateNormal];
nextBtn.layer.masksToBounds = YES;
nextBtn.layer.cornerRadius = 2;
self.nextBtn = nextBtn;
[self.view addSubview:nextBtn];
[nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view.mas_top).offset(529);
make.size.mas_equalTo(CGSizeMake(126, 40));
make.left.mas_equalTo(self.view.mas_left).offset(self.view.frame.size.width /2 - 126 /2);
}];
//上一题
UIButton *preBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[preBtn addTarget:self action:@selector(preBtnAction) forControlEvents:UIControlEventTouchUpInside];
NSAttributedString *preTitle = [[NSAttributedString alloc] initWithString:@"上一题" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTBOLD size:18],NSForegroundColorAttributeName:GIGARGB(181, 14, 14, 1)}];
[preBtn setAttributedTitle:preTitle forState:UIControlStateNormal];
preBtn.layer.borderWidth = 1;
preBtn.layer.borderColor = GIGARGB(181, 14, 14, 1).CGColor;
preBtn.layer.masksToBounds = YES;
preBtn.layer.cornerRadius = 2;
self.preBtn = preBtn;
[self.view addSubview:self.preBtn];
[self.preBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view.mas_left).offset(69);
make.top.mas_equalTo(self.view.mas_top).offset(529);
make.size.mas_equalTo(CGSizeMake(97, 40));
}];
if (_page == 1) {
self.preBtn.hidden = YES;
}
}
-(void)creatQustionView{
if (!_currentModel&&!_currentModel.optionsStyle) {
return;
}
if (_currentModel.optionsStyle == 1) {
[self creatAnswerView:MaskQuetionViewStleSliderNormal];
}else if (_currentModel.optionsStyle == 2){
[self creatAnswerView:MaskQuetionViewStleYesOrNO];
}else if (_currentModel.optionsStyle == 3 ){
[self creatAnswerView:MaskQuetionViewStyleSliderShowIndicator];
}else if (_currentModel.optionsStyle == 4){
[self creatAnswerView:MaskQuetionViewStleSliderGradient];
}
}
-(void)creatAnswerView:(MaskQuetionViewStyle)style{
//选择题view
NSArray *titles = @[@"title0",@"title1",@"title2",@"title3",@"title4"];
NSMutableArray *tit = [NSMutableArray new];
NSArray *answerList1 = _currentModel.answerList;
for (QustionAnswer *mode in answerList1){
//[tit insertObject:mode.answer atIndex:0];
[tit addObject:mode.answer];
}
titles = tit;
if (self.questionView) {
[self.questionView removeFromSuperview];
self.questionView = nil;
}
[self.view layoutIfNeeded];
MaskQuestionView *questionView = [[MaskQuestionView alloc] initQuestionView:CGRectMake(0,248, self.view.frame.size.width,212) style:style titles:titles];
questionView.answerList = _currentModel.answerList;
NSLog(@"_currentModel--ID:%d",_currentModel.questionId);
questionView.delegate = self;
[self.view addSubview:questionView];
self.questionView = questionView;
}
- (void)sliderChangeValueShouldChoiceModelAt:(NSInteger)index
{
NSLog(@"答案下标:%ld",(long)index);
_curentAnswerIdex = index;
}
- (void)yesOrNoChoiceModelAt:(NSInteger)index{
_curentAnswerIdex = index;
}
-(void)loadRequestQuestions{
NSDictionary *params = @{};
GiGaBaseAPiRequest *request = [GiGaBaseAPiRequest initWithRequestPath:@"activity/maskquestion/v1/list" method:RequestPostMethod parms:params];
[self.view makeToastActivity:CSToastPositionCenter];
[request requstDataWithResult:^(GiGaAPIResult *result) {
if (result.success) {
[self.view hideToastActivity];
NSLog(@"result:%@",result.dic);
NSArray *list = result.dic[@"list"];
for (NSDictionary *answerDic in list) {
ExercisesModel *model = [[ExercisesModel alloc] initWithDictionary:answerDic error:nil];
[self.questonArray addObject:model];
}
self.currentModel = self.questonArray[0];
[self initUI];
}
}];
}
#pragma mark 获取当前题目信息
-(void)getQuestionsInfo{
_currentModel = _questonArray[_page - 1];
[self reloadViews];
}
#pragma mark 刷新题目
-(void)reloadViews{
//刷新问题相关数据
NSAttributedString *numbertitle = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld",(long)_page] attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
[_numberBtn setAttributedTitle:numbertitle forState:UIControlStateNormal];
self.questionlabel.text = [NSString stringWithFormat:@"%@",_currentModel.question];
self.descLabel.text = _currentModel.remark;
//
[self creatQustionView];
//next pre
[self.view layoutIfNeeded];
if (_page > 1) {
self.preBtn.hidden = NO;
[self.nextBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view.mas_top).offset(529);
make.size.mas_equalTo(CGSizeMake(126, 40));
make.left.mas_equalTo(self.view.mas_left).offset(self.view.frame.size.width /2 + 10);
}];
}else{
[self.nextBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view.mas_top).offset(529);
make.size.mas_equalTo(CGSizeMake(126, 40));
make.left.mas_equalTo(self.view.mas_left).offset(self.view.frame.size.width /2 - 126 /2);
}];
self.preBtn.hidden = YES;
}
}
-(void)nextBtnAction{
[self saveProgress];
if (_page == self.questonArray.count ) {
GIGA_ShowToast(@"没有了");
[self submitAnswer];
return;
}
_page ++ ;
//获取题目信息
[self getQuestionsInfo];
//self.nextBtn.enabled = _page == [_currentModel.totalCount integerValue] ? NO : YES;
}
-(void)submitAnswer{
NSDictionary *params = @{@"singleChoiceList":self.answerArray};
[self.view makeToastActivity:CSToastPositionCenter];
NSString *url = [NSString stringWithFormat:@"%@activity/maskquestion/v1/result",[GiGaServerConfig getMainUrl]];
[GiGaNetManager userbodyRequest:url params:params completionHandler:^(NSURLResponse *response, NSDictionary *resDic, NSError * _Nullable error) {
[self.view hideToastActivity];
GiGaAPIResult *result = [[GiGaAPIResult alloc] initWithDictionary:resDic];
GILog(@"**答案提交***\n code:%ld\n message:%@\n *******\n dic:%@",result.code,result.message,result.dic);
}];
}
-(void)preBtnAction{
_page -- ;
//获取题目信息
[self getQuestionsInfo];
}
#pragma mark -做题记录
-(void)saveProgress{
int questionId = _currentModel.questionId;
QustionAnswer *curentAnswer = _currentModel.answerList[_curentAnswerIdex];
int answerid = curentAnswer.answer_id;
GILog(@"问题ID%d\n 角标id(答案号)%d\n 题号:%ld",questionId,answerid,(long)_page);
NSDictionary *answerDic = @{@"questionId":[NSString stringWithFormat:@"%d",questionId],@"answerId":[NSString stringWithFormat:@"%d",answerid]};
for (NSDictionary *dic in self.answerArray) {
// 返回时覆盖
if (questionId == [dic[@"questionId"] intValue]) {
//先删除再添加 上传测试结果顺序任意
[self.answerArray removeObject:dic];
}
}
[self.answerArray addObject:answerDic];
NSLog(@"选择题答案:%@",self.answerArray);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end