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

#import "EmigratedMainViewController.h"
#import "ThroughHistoryView.h"
#import "InstructionsViewController.h"
12
#import "AnswerViewController.h"
13

曹云霄's avatar
曹云霄 committed
14
@interface EmigratedMainViewController ()<WYPopoverControllerDelegate,DismissDelegate>
15 16 17

@property (nonatomic,strong) WYPopoverController *popover;

18 19 20 21 22
/**
 闯关数据
 */
@property (nonatomic,strong) PassLevelResponse *emigratedResponse;

23 24 25 26
@end

@implementation EmigratedMainViewController

27

28 29 30 31 32 33 34 35 36 37
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getThroughHistoryDatasAction];
}

#pragma mark - 获取闯关信息
- (void)getThroughHistoryDatasAction
{
    WS(weakSelf);
38
    PassLevelCondition *emigrated = [[PassLevelCondition alloc]init];
39
    emigrated.validEquals = YES;
40 41 42 43
    DataPage *page = [[DataPage alloc]init];
    page.page = ONE;
    page.rows = KROWS;
    emigrated.page = page;
44
    [XBLoadingView showHUDViewWithDefault];;
曹云霄's avatar
曹云霄 committed
45
    [HTTP networkRequestWithURL:SERVERREQUESTURL(THROUGHLIST) withRequestType:ZERO withParameter:emigrated withReturnValueBlock:^(id returnValue) {
46
        
47
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
48
        if (RESULT(returnValue)) {
49
            weakSelf.emigratedResponse = [[PassLevelResponse alloc]initWithDictionary:returnValue[@"data"] error:nil];
50
            [weakSelf setUpEmigratedItem];
51
        }else {
52
            [XBLoadingView hideHUDViewWithDefault];
53
        }
54
        
55
    }withFailureBlock:^(NSError *error) {
56
        [XBLoadingView hideHUDViewWithDefault];
57
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
58 59 60 61 62 63
    }];
}

#pragma mark - 开始
- (IBAction)beginButtonClickAction:(UIButton *)sender {
    
64 65 66 67 68 69 70 71 72 73 74 75 76
    CGFloat width = 100;
    CGFloat interval = 30;
    self.throughHistoryBackScrollView.contentSize = CGSizeMake(self.emigratedResponse.passLevelEntity.count*width+(self.emigratedResponse.passLevelEntity.count-1)*interval, 0);
    WS(weakSelf);
    [UIView animateWithDuration:0.2 animations:^{
        sender.alpha = 0;
        weakSelf.throughHistoryBackScrollView.alpha = 1;
    }];
}

#pragma mark - 设置闯关项
- (void)setUpEmigratedItem
{
77 78 79
    CGFloat width = 100;
    CGFloat height = 120;
    CGFloat interval = 30;
80 81
    for (int i=0; i<self.emigratedResponse.passLevelEntity.count; i++) {
        TOPassLevelEntity *entity = self.emigratedResponse.passLevelEntity[i];
82
        ThroughHistoryView *throughView = [ThroughHistoryView initializeView];
83
        throughView.tag = i;
84
        if ([[self class] isBlankString:entity.passResult]) {
85
            [throughView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectApplicableAction:)]];
86
        }
87 88
        throughView.titleLabel.text = entity.title;
        throughView.endDateLabel.text = [[entity.endDate componentsSeparatedByString:@" "] firstObject];
89
        throughView.frame = CGRectMake(i*width+i*interval, 0, width, height);
90
        throughView.stateImageView.image = [self emigratedState:entity.passResult];
91 92 93 94
        [self.throughHistoryBackScrollView addSubview:throughView];
    }
}

95 96 97 98
#pragma mark - 选择相应的期数
- (void)selectApplicableAction:(UITapGestureRecognizer *)sender
{
    TOPassLevelEntity *entity = self.emigratedResponse.passLevelEntity[sender.view.tag];
99 100 101 102 103 104 105 106
    //判断闯关任务是否过期
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormat dateFromString:entity.endDate];
    NSDate *date2 = [date dateByAddingTimeInterval:8 * 60 * 60];//手动增加8小时
    int result = [self compareOneDay:[NSDate date] withAnotherDay:date2];
    if (result == ONE) {
107 108
        [XBLoadingView showHUDViewWithText:@"已过期"];return;
    }
109
    AnswerViewController *answer = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"AnswerViewController"];
曹云霄's avatar
曹云霄 committed
110
    answer.delegate = self;
111 112 113 114 115
    answer.passLevelId = entity.fid;
    answer.preferredContentSize = CGSizeMake(520, 400);
    [self showPopoverView:answer];
}

116
#pragma mark - <DismissDelegate>
曹云霄's avatar
曹云霄 committed
117
#pragma mark - 做题完成
118
- (void)dismissController:(BOOL)animationed
曹云霄's avatar
曹云霄 committed
119
{
120
    [self.popover dismissPopoverAnimated:animationed];
121 122 123 124 125 126
}

#pragma mark - 闯关完成
- (void)emigratedFinish
{
    [self getThroughHistoryDatasAction];
曹云霄's avatar
曹云霄 committed
127 128
}

129 130 131 132 133 134 135 136 137
#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);
138 139 140 141 142 143 144
    [self showPopoverView:instruction];
}

#pragma mark - 弹出框
- (void)showPopoverView:(BaseViewController *)controller
{
    self.popover = [[WYPopoverController alloc] initWithContentViewController:controller];
145 146 147
    self.popover.theme.fillBottomColor = [UIColor clearColor];
    self.popover.theme.fillTopColor = [UIColor clearColor];
    self.popover.theme.glossShadowColor = [UIColor clearColor];
148
    self.popover.delegate = self;
149 150 151
    [self.popover presentPopoverAsDialogAnimated:YES options:WYPopoverAnimationOptionFadeWithScale];
}

152 153 154 155 156
#pragma mark - 点击空白禁止收起
- (BOOL)popoverControllerShouldDismissPopover:(WYPopoverController *)popoverController
{
    return NO;
}
157 158

@end