BreakThroughViewController.m 10.2 KB
Newer Older
1
//
2
//  BreakThroughViewController.m
3 4
//  Lighting
//
5 6
//  Created by 曹云霄 on 2016/11/28.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
7 8
//

9 10
#import "BreakThroughViewController.h"
#import "InstructionsViewController.h"
11
#import "AnswerViewController.h"
12 13 14
#import "BreakThroughFinishViewController.h"
#import "BreakThroughTableViewCell.h"
#import "StartBreakThroughViewController.h"
15

16
@interface BreakThroughViewController ()<WYPopoverControllerDelegate,DismissDelegate,UITableViewDelegate,UITableViewDataSource,CompeteDelegate,StartBreakThroughDelegate>
17 18 19

@property (nonatomic,strong) WYPopoverController *popover;

20 21 22 23
/**
 闯关数据
 */
@property (nonatomic,strong) PassLevelResponse *emigratedResponse;
24 25 26 27 28
/**
 查询model
 */
@property (nonatomic,strong) PassLevelCondition *queryModel;
/**
29
 数据源
30 31 32
 */
@property (nonatomic,strong) NSMutableArray *datasArray;

33 34 35 36 37 38
/**
 选中关卡
 */
@property (nonatomic,assign) NSInteger selectedIndex;


39 40
@end

41 42
@implementation BreakThroughViewController

43 44 45 46 47 48 49 50 51 52 53

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self uiConfigAction];
    [self setUpRefreshAction];
}

#pragma mark -UI
- (void)uiConfigAction
{
54
    self.emigratedTableView.backgroundView = [[UIImageView alloc] initWithImage:TCImage(@"confirmBack")];
55 56 57 58 59 60 61
}

#pragma mark -设置刷新
- (void)setUpRefreshAction
{
    WS(weakSelf);
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
62 63
        weakSelf.queryModel.page.page = ONE;
        [weakSelf.emigratedTableView.mj_footer resetNoMoreData];
64 65 66 67
        [weakSelf getThroughHistoryDatasAction:YES];
    }];
    headerRefresh.stateLabel.hidden = YES;
    headerRefresh.lastUpdatedTimeLabel.hidden = YES;
68 69 70
    self.emigratedTableView.mj_header =headerRefresh;
    [self.emigratedTableView.mj_header beginRefreshing];
    self.emigratedTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
71
        if (++ weakSelf.queryModel.page.page > weakSelf.emigratedResponse.totalpages) {
72
            [weakSelf.emigratedTableView.mj_footer endRefreshingWithNoMoreData];
73 74 75 76 77
        }else
        {
            [weakSelf getThroughHistoryDatasAction:NO];
        }
    }];
78
    self.emigratedTableView.mj_footer.automaticallyHidden = YES;
79 80 81 82 83 84 85 86 87 88
}

#pragma mark - 获取闯关信息
- (void)getThroughHistoryDatasAction:(BOOL)isRefresh
{
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];;
    [HTTP networkRequestWithURL:SERVERREQUESTURL(THROUGHLIST) withRequestType:ZERO withParameter:self.queryModel withReturnValueBlock:^(id returnValue) {
        
        [XBLoadingView hideHUDViewWithDefault];
89
        [weakSelf endRefreshingForTableView:weakSelf.emigratedTableView];
90 91 92 93 94 95
        if (RESULT(returnValue)) {
            if (isRefresh) {
                [weakSelf.datasArray removeAllObjects];
            }
            weakSelf.emigratedResponse = [[PassLevelResponse alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
            [weakSelf.datasArray addObjectsFromArray:weakSelf.emigratedResponse.passLevelEntity];
96
            [weakSelf.emigratedTableView reloadData];
97 98 99 100 101 102 103 104 105 106
        }else {
            [XBLoadingView hideHUDViewWithDefault];
        }
        
    }withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


107 108
#pragma mark -<UITableViewDelegate,UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
109 110 111 112
{
    return self.datasArray.count;
}

113
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
114
{
115 116 117
    BreakThroughTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BreakThroughTableViewCell" forIndexPath:indexPath];
    cell.entity = self.datasArray[indexPath.row];
    return cell;
118 119
}

120
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
121
{
122 123
    self.selectedIndex = indexPath.row;
    TOPassLevelEntity *entity = self.datasArray[indexPath.item];
124 125 126 127 128 129
    [self judgePlanState:entity];
}

#pragma mark -判断任务状态
- (void)judgePlanState:(TOPassLevelEntity *)entity
{
130
    if ([entity.passResult isEqualToString:@"0"] && !entity.residue) {//失败
131
        
132
    }else if ([entity.passResult isEqualToString:@"1"]){//成功
133 134
        WS(weakSelf);
        [self getPasslevelResult:entity finish:^(TOPassLevelResultEntity *resultEntity,NSString *integration) {
135
            BreakThroughFinishViewController *resultVc = [[[weakSelf class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"BreakThroughFinishViewController"];
136 137 138 139 140 141 142
            resultVc.delegate = weakSelf;
            resultVc.state = [resultEntity.passResult integerValue];
            resultVc.score = [resultEntity.grade stringValue];
            resultVc.integral = integration;
            resultVc.preferredContentSize = CGSizeMake(520, 400);
            [weakSelf showPopoverView:resultVc];
        }];
143 144 145 146 147 148
    }else if ([entity.passResult isEqualToString:@"2"]){//提交
        
    }else {//未闯关
        StartBreakThroughViewController *startBreakThrough = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"StartBreakThroughViewController"];
        startBreakThrough.opportunityNumber = [NSString stringWithFormat:@"%ld",entity.residue];
        startBreakThrough.delegate = self;
149 150
        startBreakThrough.opportunityNumber = entity.residue;
        startBreakThrough.passResult = entity.passResult;
151 152
        startBreakThrough.preferredContentSize = CGSizeMake(520, 400);
        [self showPopoverView:startBreakThrough];
153 154 155 156 157 158 159 160 161 162 163
    }
}

#pragma mark -获取闯关成功信息
- (void)getPasslevelResult:(TOPassLevelEntity *)entity finish:(void(^)(TOPassLevelResultEntity *entity,NSString *integral))result
{
    __block TOPassLevelResultEntity *resulrEntity;
    __block NSString *integral = nil;
    [XBLoadingView showHUDViewWithDefault];
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
164
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(PASSLEVERESULT),entity.fid] withRequestType:GET withParameter:nil withReturnValueBlock:^(id returnValue) {
165 166 167 168 169 170
        
        dispatch_group_leave(group);
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            resulrEntity = [[TOPassLevelResultEntity alloc] initWithDictionary:RESPONSE(response) error:nil];
        }else {
171
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
172 173 174 175 176 177 178 179 180 181 182 183
        }
        
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
    dispatch_group_enter(group);
    NSString *url = [NSString stringWithFormat:SERVERREQUESTURL(GETINTEGRAL),RECRUITTHROUGH];
    [HTTP networkWithDictionaryRequestWithURL:url withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
        dispatch_group_leave(group);
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            
184
              integral = [NSString stringWithFormat:@"+%@",RESPONSE(returnValue)];
185 186
            
        }else {
187
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
188 189 190 191 192 193 194 195 196 197
        }
        
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        result(resulrEntity,integral);
    });
}

198 199 200 201 202 203 204 205

#pragma mark - <DismissDelegate>
#pragma mark - 做题完成
- (void)dismissController:(BOOL)animationed
{
    [self.popover dismissPopoverAnimated:animationed];
}

206
#pragma mark - 闯关结束
207 208 209 210
- (void)emigratedFinish:(NSString *)passResult
{
    TOPassLevelEntity *entity = self.datasArray[self.selectedIndex];
    entity.passResult = passResult;
211 212 213 214 215 216 217
    //闯关失败
    if ([passResult isEqualToString:@"0"]) {
        //扣除一次闯关机会
        if (entity.residue > 0) {
            entity.residue -= 1;
        }
    }
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    [self.emigratedTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.selectedIndex inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark - 完成
- (void)finished
{
    [self.popover dismissPopoverAnimated:YES];
}

#pragma mark -<StartBreakThroughDelegate>
- (void)startAnswerTheQuestions
{
    TOPassLevelEntity *entity = self.datasArray[self.selectedIndex];
    AnswerViewController *answer = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"AnswerViewController"];
    answer.delegate = self;
    answer.entity = entity;
    answer.preferredContentSize = CGSizeMake(520, 400);
    [self showPopoverView:answer];
}

- (void)closeController
{
    [self.popover dismissPopoverAnimated:YES];
}

#pragma mark - 闯关说明
- (IBAction)instructionsButtonClickAction:(UIButton *)sender {
    
    InstructionsViewController *instruction = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"InstructionsViewController"];
    WS(weakSelf);
    [instruction setDismissSyntonyBlock:^{
        [weakSelf.popover dismissPopoverAnimated:YES];
    }];
    instruction.preferredContentSize = CGSizeMake(ScreenWidth/2, ScreenHeight/2);
    [self showPopoverView:instruction];
}

255 256 257 258 259 260 261 262 263 264 265
#pragma mark - 弹出框
- (void)showPopoverView:(BaseViewController *)controller
{
    self.popover = [[WYPopoverController alloc] initWithContentViewController:controller];
    self.popover.theme.fillBottomColor = [UIColor clearColor];
    self.popover.theme.fillTopColor = [UIColor clearColor];
    self.popover.theme.glossShadowColor = [UIColor clearColor];
    self.popover.delegate = self;
    [self.popover presentPopoverAsDialogAnimated:YES options:WYPopoverAnimationOptionFadeWithScale];
}

266 267
#pragma mark - 点击空白禁止收起
- (BOOL)popoverControllerShouldDismissPopover:(WYPopoverController *)popoverController
268
{
269
    return NO;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
}

#pragma mark -lazy
- (NSMutableArray *)datasArray
{
    if (!_datasArray) {
        _datasArray = [NSMutableArray array];
    }
    return _datasArray;
}

- (PassLevelCondition *)queryModel
{
    if (!_queryModel) {
        _queryModel = [[PassLevelCondition alloc] init];
        _queryModel.validEquals = YES;
        DataPage *page = [[DataPage alloc]init];
        page.page = ONE;
        page.rows = KROWS;
        _queryModel.page = page;
    }
    return _queryModel;
}







@end