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

535 lines
19 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"
#import "MaskTestResult.h"
#import "GiGaUserDefault.h"
#import "GiGaUserLoginVC.h"
#import "MaskTestResultVC.h"
@interface GiGaQuestionVC ()<QeustionViewDelegate>
@property (nonatomic,weak) MaskQuestionView *questionView;
@property (nonatomic,assign) NSInteger page;
@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;
[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{
//没有tabview 界面 直接 上移动安全 导航高度 (设计稿 -64 或 -88
CGFloat navH = SAFE_NAV_HEIGHT;
UIButton *numberBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[numberBtn setBackgroundImage:[UIImage imageNamed:@"result_logo"] 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);
titleLabel.numberOfLines = 2;
//titleLabel.backgroundColor = [UIColor redColor];
self.questionlabel = titleLabel;
//[titleLabel sizeToFit];
[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 = 3;
descLabel.textAlignment = NSTextAlignmentCenter;
self.descLabel = descLabel;
[descLabel sizeToFit];
[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 -navH);
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(-20);
make.height.mas_equalTo(40);
}];
[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(-50);
}];
//测试题
[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) {
if (self.view.frame.size.height <=504) {
make.top.mas_equalTo(self.view.mas_top).offset(504 - navH);
}else{
make.top.mas_equalTo(self.view.mas_top).offset(529 - navH);
}
make.size.mas_equalTo(CGSizeMake(126, 40));
make.left.mas_equalTo(self.view.mas_centerX).offset(- 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) {
if (self.view.frame.size.height <=504) {
make.top.mas_equalTo(self.view.mas_top).offset(504 - navH);
}else{
make.top.mas_equalTo(self.view.mas_top).offset(529 - navH);
}
make.size.mas_equalTo(CGSizeMake(97, 40));
make.left.mas_equalTo(self.view.mas_centerX).offset(-10-97);
}];
if (_page == 1) {
self.preBtn.hidden = YES;
}
[self reloadViews];
}
-(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 - SAFE_NAV_HEIGHT, self.view.frame.size.width,212) style:style titles:titles current:_currentModel];
NSLog(@"_currentModel--ID:%d",_currentModel.questionId);
questionView.delegate = self;
[self.view addSubview:questionView];
self.questionView = questionView;
}
- (void)sliderChangeValueShouldChoiceModelAt:(int)index slider:(float)value
{
if (index > _currentModel.answerList.count) {
return;
}
//答案整型非范围 设计稿范围展示!!
NSLog(@"答案下标:%d",index);
//返回时查看之前选中答案时使用
_currentModel.slidervalue = value;
//选中记录 修改答案默认下标 最终答案选择根据defaultFlag 1 时为选中答案
[self changeAnswerDefaultflag:index];
}
- (void)yesOrNoChoiceModelAt:(int)index{
_currentModel.slidervalue = index;
[self changeAnswerDefaultflag:index];
}
-(void)changeAnswerDefaultflag:(int)index{
NSArray *temparr = [NSArray arrayWithArray:_currentModel.answerList];
[temparr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
QustionAnswer *model = obj;
if ((int)index == idx) {
model.defaultFlag = 1;
}else{
model.defaultFlag = 0;
}
}];
}
-(void)loadRequestQuestions{
NSDictionary *params = @{};
GiGaBaseAPiRequest *request = [GiGaBaseAPiRequest initWithRequestPath:kApiSkinTestQuestionList method:RequestPostMethod parms:params];
[self.view makeToastActivity:CSToastPositionCenter];
[request requstDataWithResult:^(GiGaAPIResult *result) {
[self.view hideToastActivity];
if (result.success) {
NSLog(@"result:%@",result.dic);
NSArray *list = result.dic[@"list"];
if (list.count==0 || [list isKindOfClass: [NSNull class]]) {
GIGA_ShowToast(@"");
return ;
}
for (NSDictionary *answerDic in list) {
ExercisesModel *model = [[ExercisesModel alloc] initWithDictionary:answerDic error:nil];
model.slidervalue = 0;
if (model.optionsStyle == 2) {
//1是 0否
NSArray *answerlsit = [NSArray arrayWithArray:model.answerList];
[answerlsit enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
QustionAnswer *answer = obj;
if (answer.defaultFlag == 1) {
model.slidervalue = (float)idx;
}
}];
}else{
NSArray *answerlsit = [NSArray arrayWithArray:model.answerList];
[answerlsit enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
QustionAnswer *answer = obj;
if (answer.defaultFlag == 1) {
model.slidervalue = (answerlsit.count -1 -idx) / (answerlsit.count - 1);
//GILog(@"model.slidervalue%.2f",model.slidervalue);
}
}];
}
[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{
//刷新问题相关数据
if (_page == self.questonArray.count) {
NSAttributedString *nextTitle = [[NSAttributedString alloc] initWithString:@"完成" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTBOLD size:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
[_nextBtn setAttributedTitle:nextTitle forState:UIControlStateNormal];
}else{
NSAttributedString *nextTitle = [[NSAttributedString alloc] initWithString:@"下一题" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTBOLD size:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
[_nextBtn setAttributedTitle:nextTitle 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;
if (self.view.frame.size.height <= 504) {
[self.nextBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view.mas_top).offset(504 -SAFE_NAV_HEIGHT);
make.size.mas_equalTo(CGSizeMake(126, 40));
make.left.mas_equalTo(self.view.mas_centerX).offset(10);
}];
}else{
[self.nextBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view.mas_top).offset(529 -SAFE_NAV_HEIGHT);
make.size.mas_equalTo(CGSizeMake(126, 40));
make.left.mas_equalTo(self.view.mas_centerX).offset(10);
}];
}
}else{
[self.nextBtn mas_updateConstraints:^(MASConstraintMaker *make) {
if (self.view.frame.size.height <= 504) {
make.top.mas_equalTo(self.view.mas_top).offset(504 -SAFE_NAV_HEIGHT);
}else{
make.top.mas_equalTo(self.view.mas_top).offset(529 -SAFE_NAV_HEIGHT);
}
make.size.mas_equalTo(CGSizeMake(126, 40));
make.left.mas_equalTo(self.view.mas_centerX).offset(-63);
}];
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{
[self userTestEnd];
}
-(void)userTestEnd{
BOOL isUserLogin = [GiGaUserDefault isUserLogin];
if (isUserLogin) {
[self getTestResult];
}else{
[self jxt_showAlertWithTitle:@"对不起您还没有登录" message:@"请登录后查看肤质测试结果" appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
alertMaker.addActionCancelTitle(@"返回首页");
alertMaker.addActionDestructiveTitle(@"登录");
} actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
if (buttonIndex == 0) {
[self.navigationController popViewControllerAnimated:YES];
}else{
GiGaUserLoginVC *userlogInVC= [[GiGaUserLoginVC alloc] init];
GiGaBaseNavViewController *baseNav = [[GiGaBaseNavViewController alloc] initWithRootViewController:userlogInVC];
[self presentViewController:baseNav animated:YES completion:nil];
}
}];
}
}
//获取结果
-(void)getTestResult{
self.nextBtn.userInteractionEnabled = NO;
NSDictionary *params = @{@"singleChoiceList":self.answerArray};
[self.view makeToastActivity:CSToastPositionCenter];
NSString *url = [NSString stringWithFormat:@"%@%@",[GiGaServerConfig getMainUrl],kAiSkinTestResult];
[GiGaNetManager userbodyRequest:url params:params completionHandler:^(NSURLResponse *response, NSDictionary *resDic, NSError * _Nullable error) {
self.nextBtn.userInteractionEnabled = YES;
[self.view hideToastActivity];
GiGaAPIResult *result = [[GiGaAPIResult alloc] initWithDictionary:resDic];
if (result.success) {
MaskTestResult* mode = [[MaskTestResult alloc] initWithDictionary:resDic error:nil];
NSUInteger minute = [mode.minute integerValue];
NSTimeInterval time = minute * 60;
[GiGaUserDefault savaMaskeTime:time];
[GiGaUserDefault saveUserTestFlag:YES];
NC_POST_NAME_OBJECT(@"testfalgUpdate", nil);
//展示结果
// NSDictionary *dic = @{
// @"dryness":@"0.41",
// @"drynessPercent":@"41%",
// @"mask":@"",
// @"minute":@"19",
// @"oiliness":@"0.59",
// @"oilinessPercent":@"59%"
// };
// MaskTestResult *model =[[MaskTestResult alloc] initWithDictionary:dic error:nil];
MaskTestResultVC *resultVC = [[MaskTestResultVC alloc] init];
resultVC.model = mode;
resultVC.isfromshouYE = YES;
[self.navigationController pushViewController:resultVC animated:YES];
}else{
GIGA_ShowToast(result.message);
}
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;
__block int answerid = 0;
NSArray *answerArr = [NSArray arrayWithArray:_currentModel.answerList];
[answerArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
QustionAnswer *model = obj;
if (model.defaultFlag == 1) {
answerid = model.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]};
NSArray *tempArr= [NSArray arrayWithArray:self.answerArray];
//返回时处理
[tempArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSDictionary *dic = obj;
if ([dic[@"questionId"] intValue] == questionId) {
[self.answerArray removeObject:dic];
}
}];
[self.answerArray addObject:answerDic];
NSLog(@"选择题答案:%@",self.answerArray);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end