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

#import "OnlineLearningDetailViewController.h"
#import "VideoHelperViewController.h"
#import "VideoListViewController.h"
#import "VideoDetailViewController.h"
13 14 15
#import "CustomWKWebViewController.h"
#import "LearningCompleteViewController.h"
#import "AssessmentViewController.h"
16 17


18
@interface OnlineLearningDetailViewController ()<SelectStudyItemDelegate,VideoPlayerDelegate,BeginAssessmentDelegate,InspectionStateDelegate>
19 20

/**
21
 学习数据
22
 */
23 24
@property (nonatomic,strong) StudyTaskResponse *studyResult;
@property (nonatomic,strong) WYPopoverController *settingsPopoverController;
25

26 27 28 29
@end

@implementation OnlineLearningDetailViewController

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#pragma mark -渲染完成
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

#pragma mark -视图即将消失
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
49 50 51 52 53 54 55
    //记录播放时间
    VideoHelperViewController *videoVc = [self.childViewControllers firstObject];
    if (videoVc.learningItem) {
        double currentTime = [videoVc getCurrentPlayingTime];
        double totalTime = videoVc.playerItem.duration.value/videoVc.playerItem.duration.timescale;
        [self switchVideoRecordPlayTime:videoVc.learningItem.attachment withPlayTime:currentTime withPlayPercent:currentTime/totalTime];
    }
56
    [videoVc stopPlay];
57 58
}

59 60 61 62
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addChildViewController];
63
    [self listeningHomeButton];
64
    [self getStudyItemDetailAction];
65 66
}

67
#pragma mark - 添加子控制器
68 69 70 71
- (void)addChildViewController
{
    //播放窗口
    VideoHelperViewController *videoWindow = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"VideoHelperViewController"];
72
    videoWindow.delegate = self;
73 74
    [self addChildViewController:videoWindow];
    [self.view addSubview:videoWindow.view];
75 76 77 78
    //播放简介
    VideoDetailViewController *playDetail = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"VideoDetailViewController"];
    [self addChildViewController:playDetail];
    [self.view addSubview:playDetail.view];
79 80
    //播放列表
    VideoListViewController *playList = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"VideoListViewController"];
81
    playList.studyItemTitle = self.studyTypeEntity.name;
82
    playList.delegate = self;
83
    videoWindow.progressDelegate = playList;
84 85
    [self addChildViewController:playList];
    [self.view addSubview:playList.view];
86
    __weak typeof(videoWindow) weakVideWindow = videoWindow;
87 88 89 90
    //全屏
    WS(weakSelf);
    [videoWindow setZoomButtonClickBlock:^(BOOL boolValue) {
        if (boolValue) {
91
            [SHARED_APPDELEGATE.window addSubview:weakVideWindow.view];
92 93 94 95 96 97 98 99 100 101 102 103
            [UIView animateWithDuration:0.2 animations:^{
                weakVideWindow.view.frame = [UIScreen mainScreen].bounds;
                weakVideWindow.playerLayer.frame = [UIScreen mainScreen].bounds;
            }];
        }else{
            [weakSelf.view insertSubview:weakVideWindow.view atIndex:0];
            [UIView animateWithDuration:0.2 animations:^{
                weakVideWindow.view.frame = CGRectMake(0, NavigationHeight, ScreenWidth*2/3, ScreenHeight/2);
                weakVideWindow.playerLayer.frame = CGRectMake(0, 0, ScreenWidth*2/3, ScreenHeight/2);
            }completion:nil];
        }
    }];
104
    videoWindow.view.frame = CGRectMake(0,NavigationHeight, ScreenWidth*2/3, ScreenHeight/2);
105
    playList.view.frame = CGRectMake(0, 64,ScreenWidth, ScreenHeight-64);
106
    playDetail.view.frame = CGRectMake(0, ScreenHeight/2+NavigationHeight, ScreenWidth*2/3, ScreenHeight/2-NavigationHeight);
107 108
}

109 110 111 112
#pragma mark - 监听Home键
- (void)listeningHomeButton
{
    //监听是否触发home键挂起程序.
113
    [Notification addObserver:self selector:@selector(applicationWillResignActive:)name:UIApplicationWillResignActiveNotification object:nil];
114
    //监听是否重新进入程序程序.
115
    [Notification addObserver:self selector:@selector(applicationDidBecomeActive:)name:UIApplicationDidBecomeActiveNotification object:nil];
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}

#pragma mark - 进入后台
- (void)applicationWillResignActive:(NSNotification *)notification
{
    printf("触发home按下\n");
    //记录播放时间
    VideoHelperViewController *videoVc = [self.childViewControllers firstObject];
    if (videoVc.learningItem) {
        double currentTime = [videoVc getCurrentPlayingTime];
        double totalTime = videoVc.playerItem.duration.value/videoVc.playerItem.duration.timescale;
        [self switchVideoRecordPlayTime:videoVc.learningItem.attachment withPlayTime:currentTime withPlayPercent:currentTime/totalTime];
    }
}

#pragma mark - 进入前台
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
    printf("重新进来后响应\n");
}
136

137 138 139 140
#pragma mark - 获取学习项详情
- (void)getStudyItemDetailAction
{
    WS(weakSelf);
141 142
    StudyTaskCondition *studyListModel = [[StudyTaskCondition alloc]init];
    studyListModel.employeeIdEquals = [Shoppersmanager manager].Shoppers.employee.fid;
143
    studyListModel.typeEquals = self.studyTypeEntity.fid;
144 145 146 147
    DataPage *page = [[DataPage alloc] init];
    page.page = ONE;
    page.rows = 9999;
    studyListModel.page = page;
148
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
149
    [HTTP networkRequestWithURL:SERVERREQUESTURL(STUDYLIST) withRequestType:ZERO withParameter:studyListModel withReturnValueBlock:^(id returnValue) {
150
        
151
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
152
        if (RESULT(returnValue)) {
153 154
            weakSelf.studyResult = [[StudyTaskResponse alloc]initWithDictionary:returnValue[@"data"] error:nil];
            [weakSelf transferData:weakSelf.studyResult];
155
        }else {
156
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
157
        }
158
        
159
    }withFailureBlock:^(NSError *error) {
160
        [XBLoadingView hideHUDViewWithDefault];
161
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
162 163 164
    }];
}

165 166 167
#pragma mark - 选中学习项
- (void)seleStudyItemCellIndex:(NSIndexPath *)indexPath
{
168 169 170 171 172 173
    //判断学习列表是否处于全屏状态,第一次选中是需要取消全屏状态
    VideoListViewController *studyListVC = self.childViewControllers[2];
    if (studyListVC.view.width == ScreenWidth) {
        [studyListVC narrowAnimation];
    }
    VideoDetailViewController *studyDetailVc = self.childViewControllers[1];
174
    studyDetailVc.indexPath = indexPath;
175
    WS(weakSelf);
176 177 178
    VideoHelperViewController *videoVc = [self.childViewControllers firstObject];
    RsStudyTask *studyEntity = self.studyResult.studyEntity[indexPath.section];
    CustomStudyEntity *studyList = studyEntity.studyTasks[indexPath.row];
179
    videoVc.indexPath = indexPath;
180
    [studyListVC selectedIndexPath:indexPath withIsQualified:[studyList.examResult boolValue]];
181
    if ([studyList.attachment.fileUrl rangeOfString:@".mp4"].location != NSNotFound) {
182 183 184 185 186 187
        //判断是否是第一次播放
        if (videoVc.learningItem) {
            double currentTime = [videoVc getCurrentPlayingTime];
            double totalTime = videoVc.playerItem.duration.value/videoVc.playerItem.duration.timescale;
            [self switchVideoRecordPlayTime:videoVc.learningItem.attachment withPlayTime:currentTime withPlayPercent:currentTime/totalTime];
        }
188 189 190 191
        videoVc.learningItem = studyList;
    }else if ([studyList.attachment.fileUrl rangeOfString:@".ppt"].location != NSNotFound) {
        
        [videoVc resetPlayer];
192 193 194 195 196 197
        CustomWKWebViewController *pptVc = [[CustomWKWebViewController alloc]init];
        pptVc.urlString = studyList.attachment.fileUrl;
        pptVc.type = Study;
        pptVc.indexPath = indexPath;
        videoVc.learningItem = nil;
        [pptVc setScrollViewEndBottomBlock:^(NSIndexPath *indexPath){
198
            [weakSelf dismissViewControllerAnimated:YES completion:^{
199
                [weakSelf videoPlayFinish:studyList withIndexPath:indexPath];
200 201
            }];
        }];
202
        [self presentViewController:pptVc animated:YES completion:nil];
203 204 205
    }
}

206 207 208 209 210 211 212 213 214
#pragma mark - 切换视频播放记录播放时间
- (void)switchVideoRecordPlayTime:(TOAttachmentEntity *)attachment withPlayTime:(double)playTime withPlayPercent:(double)percent
{
    TOAttachmentPlayEntity *playTimeEntity = [[TOAttachmentPlayEntity alloc] init];
    playTimeEntity.fid = attachment.entityId;
    playTimeEntity.attachmentId = attachment.fid;
    playTimeEntity.employeeId = [Shoppersmanager manager].Shoppers.employee.fid;
    playTimeEntity.playTime = [NSString stringWithFormat:@"%.0lf",playTime];
    playTimeEntity.playPercent = [NSString stringWithFormat:@"%.2lf",percent];
曹云霄's avatar
曹云霄 committed
215
    [HTTP networkRequestWithURL:SERVERREQUESTURL(ATTACHMENTPLAYTIME) withRequestType:ZERO withParameter:playTimeEntity withReturnValueBlock:^(id returnValue) {
216
        
曹云霄's avatar
曹云霄 committed
217
        if (RESULT(returnValue)) {
218 219 220 221 222 223
            attachment.playTime = playTimeEntity.playTime;
            attachment.playPercent = playTimeEntity.playPercent;
        }else {
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
        }
        
224
    } withFailureBlock:^(NSError *error) {
225 226 227 228 229
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


230
#pragma mark - 视频播放完成
231
- (void)videoPlayFinish:(CustomStudyEntity *)studyEntity withIndexPath:(NSIndexPath *)indexPath
232
{
233 234 235 236 237 238
    WS(weakSelf);
    [self studyFinishGetIntegral:^(NSString *integral) {
        LearningCompleteViewController *assessmentVc = [[[weakSelf class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"LearningCompleteViewController"];
        assessmentVc.delegate = weakSelf;
        assessmentVc.studyEntity = studyEntity;
        assessmentVc.integralString = integral;
239
        assessmentVc.indexPath = indexPath;
240 241 242 243 244 245 246 247
        assessmentVc.preferredContentSize = CGSizeMake(520, 450);
        weakSelf.settingsPopoverController = [[WYPopoverController alloc] initWithContentViewController:assessmentVc];
        weakSelf.settingsPopoverController.wantsDefaultContentAppearance = NO;
        weakSelf.settingsPopoverController.theme.fillBottomColor = [UIColor clearColor];
        weakSelf.settingsPopoverController.theme.fillTopColor = [UIColor clearColor];
        weakSelf.settingsPopoverController.theme.glossShadowColor = [UIColor clearColor];
        [weakSelf.settingsPopoverController presentPopoverAsDialogAnimated:YES
                                                               options:WYPopoverAnimationOptionFadeWithScale];
248
    }withStudyEntity:studyEntity];
249 250 251
}

#pragma mark - 学习完成获取积分
252
- (void)studyFinishGetIntegral:(void(^)(NSString *integral))getIntegral withStudyEntity:(CustomStudyEntity *)studuEntity
253
{
254 255
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
256
    [XBLoadingView showHUDViewWithDefault];
257
    // 任务一,学习项完成
曹云霄's avatar
曹云霄 committed
258
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(FINISHSTUDY),studuEntity.fid,[Shoppersmanager manager].Shoppers.employee.fid] withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
259
        dispatch_group_leave(group);
曹云霄's avatar
曹云霄 committed
260
        if (RESULT(returnValue)) {
261 262 263 264 265

        }else {
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
        }
        
266
    } withFailureBlock:^(NSError *error) {
267 268 269 270 271
        dispatch_group_leave(group);
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
    // 任务二,获得学习积分
    dispatch_group_enter(group);
曹云霄's avatar
曹云霄 committed
272
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(GETINTEGRAL),STUDYFINISH] withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
273 274
        
        dispatch_group_leave(group);
曹云霄's avatar
曹云霄 committed
275
        if (RESULT(returnValue)) {
276
            if (getIntegral) {
277
                getIntegral(returnValue[@"data"]);
278 279 280 281 282
            }
        }else {
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
        }
        
283
    } withFailureBlock:^(NSError *error) {
284
        dispatch_group_leave(group);
285 286
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
287 288 289 290
    // 全部任务完成
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [XBLoadingView hideHUDViewWithDefault];
    });
291 292 293
}

#pragma mark - 开始考核
294
- (void)beginAssessment:(CustomStudyEntity *)studyEntity withIndexPath:(NSIndexPath *)indexPath
295 296
{
    WS(weakSelf);
297 298 299 300 301 302 303
    VideoHelperViewController *videoWindow = self.childViewControllers[0];
    if (videoWindow.view.height == ScreenHeight) {
        [self.view insertSubview:videoWindow.view atIndex:0];
        [UIView animateWithDuration:0.2 animations:^{
            videoWindow.view.frame = CGRectMake(0, NavigationHeight, ScreenWidth*2/3, ScreenHeight/2);
            videoWindow.playerLayer.frame = CGRectMake(0, 0, ScreenWidth*2/3, ScreenHeight/2);
        }completion:^(BOOL finished) {
304
            [weakSelf assessment:studyEntity.fid withIndexPath:indexPath];
305
        }];
306
    }else {
307
        [weakSelf assessment:studyEntity.fid withIndexPath:indexPath];
308
    }
309 310
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
#pragma mark - 考核
- (void)assessment:(NSString *)taskId withIndexPath:(NSIndexPath *)indexPath
{
    WS(weakSelf);
    [self.settingsPopoverController dismissPopoverAnimated:YES completion:^{
        AssessmentViewController *assessmentVc = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"AssessmentViewController"];
        assessmentVc.taskId = taskId;
        assessmentVc.indexPath = indexPath;
        assessmentVc.delegate = weakSelf;
        [weakSelf.navigationController pushViewController:assessmentVc animated:YES];
    }];
}

#pragma mark --- <InspectionStateDelegate>考核状态
#pragma mark -再学一遍
- (void)learningAgainButtonClick:(NSIndexPath *)indexPath
{
328
    VideoListViewController *studyListVC = self.childViewControllers[2];
329 330 331 332 333 334 335
    [studyListVC selectedIndexPath:indexPath withIsQualified:NO];
    [self seleStudyItemCellIndex:indexPath];
}

#pragma mark -考核完成
- (void)inspectionThroughAction:(NSIndexPath *)indexPath
{
336
    VideoListViewController *studyListVC = self.childViewControllers[2];
337 338 339 340 341 342 343 344 345
    [studyListVC selectedIndexPath:indexPath withIsQualified:YES];
}

#pragma mark - <VideoPlayerDelegate>
- (void)isFirstPLayOrPPTPlay:(NSIndexPath *)indexPath
{
    [self seleStudyItemCellIndex:indexPath];
}

346 347 348
#pragma mark - 赋值
- (void)transferData:(StudyTaskResponse *)result
{
349
    if (result.studyEntity.count == 0) {
350 351 352
        [XBLoadingView showHUDViewWithText:@"学习内容为空"];
        [self.navigationController popViewControllerAnimated:YES];
        return;
353
    }
354
    VideoListViewController *studyListVC = self.childViewControllers[2];
355
    studyListVC.datasArray = [NSMutableArray arrayWithArray:result.studyEntity];
356
    VideoDetailViewController *studyDetailsVC = self.childViewControllers[1];
357 358 359 360
    studyDetailsVC.datasArray = result.studyEntity;
    studyDetailsVC.indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
}

361

362
@end