// // SimulationExamRecordController.m // MusicGradeExam // // Created by Kyle on 2020/9/21. // Copyright © 2020 DayaMusic. All rights reserved. // #import "SimulationExamRecordController.h" #import "RecordBodyView.h" #import "RecordListCell.h" #import "RecordBottomView.h" #import "RecordTipsView.h" #import "KSMediaManager.h" #import "WMPlayer.h" #import "SongModel.h" #import "KSGuideMaskView.h" #import "KSNormalAlertView.h" @interface SimulationExamRecordController () { WMPlayer *_wmPlayer; CGRect _playerFrame; } @property (nonatomic, assign) BOOL isRatation; @property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) RecordBodyView *topView; @property (nonatomic, strong) RecordTipsView *tipsView; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) RecordBottomView *bottomView; @property (nonatomic, strong) NSMutableArray *songArray; @property (nonatomic, strong) KSMediaManager *mediaManager; @property (strong, nonatomic) MBProgressHUD *HUD; @property (nonatomic, strong) NSMutableArray *fileUrlArray; @property (nonatomic, assign) BOOL hasShowTips; @end @implementation SimulationExamRecordController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.ks_prefersNavigationBarHidden = YES; [self configUI]; [self evaluateSource]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; if(@available(iOS 13.0, *)){ [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDarkContent; } } - (void)evaluateSource { if (self.sourceModel == nil) { return; } [self.topView configTime:[NSString stringWithFormat:@"%.0f", self.sourceModel.subTime]]; [self.tipsView configWithEndTime:self.sourceModel.examEndTime]; NSData *songDate = [self.sourceModel.songJson dataUsingEncoding:NSUTF8StringEncoding]; NSArray *listArray = [NSJSONSerialization JSONObjectWithData:songDate options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil]; NSMutableArray *songList = [NSMutableArray array]; for (NSDictionary *parm in listArray) { SongModel *model = [[SongModel alloc] initWithDictionary:parm]; [songList addObject:model]; } self.songArray = [NSMutableArray arrayWithArray:songList]; for (NSInteger i = 0; i < self.songArray.count; i++) { SongModel *model = self.songArray[i]; NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, model.songName]; NSDictionary *fileMessage = UserDefault(fileKey); if (fileMessage) { NSString *remoteUrl = [fileMessage stringValueForKey:@"remoteUrl"]; [self.fileUrlArray addObject:remoteUrl]; } } [self checkSubmitButtonEnable]; [self.tableView reloadData]; } - (void)configUI { [self.view addSubview:self.topView]; [self.topView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.mas_equalTo(self.view); make.height.mas_equalTo(self.view.mas_width).multipliedBy(79.0 / 207).offset(11); }]; [self.view addSubview:self.bottomView]; [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.bottom.mas_equalTo(self.view.mas_bottomMargin); make.height.mas_equalTo(90); }]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.top.mas_equalTo(self.topView.mas_bottom); make.bottom.mas_equalTo(self.bottomView.mas_top); }]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (_hasShowTips == NO) { [self addIntroduceView]; } } - (void)addIntroduceView { _hasShowTips = YES; CGRect rect1 = [self.tableView convertRect:self.tableView.tableHeaderView.frame toView:[UIApplication sharedApplication].keyWindow]; rect1.origin.y -= 50; rect1.size.height += 50; UIBezierPath *pathOne = [UIBezierPath bezierPathWithRect:rect1]; CGRect rect2 = rect1; rect2.size = CGSizeMake(kScreenWidth, 130); rect2.origin.y = CGRectGetMaxY(rect1); UIBezierPath *pathSecond = [UIBezierPath bezierPathWithRect:rect2]; NSArray *tipsArray = @[@"请在录播时间内完成视频上传", @"点击上传曲目录播视频"]; NSArray *bezierArray = @[pathOne,pathSecond]; KSGuideMaskView *guideView = [[KSGuideMaskView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]; [guideView addTips:tipsArray transparentRect:bezierArray shaperLayerIndex:-1]; [guideView showMaskViewInView:nil]; } #pragma mark ----- table data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.songArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SongModel *model = self.songArray[indexPath.row]; RecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecordListCell"]; MJWeakSelf; [cell configCellWithSource:model examRegistrationId:self.examRegistrationId operationCallback:^(RECORDTYPE type, NSString *fileKey) { [weakSelf opreationCellType:type fileKey:fileKey]; }]; return cell; } - (void)opreationCellType:(RECORDTYPE)type fileKey:(NSString *)fileKey { if (type == RECORDTYPE_RECORD) { // 录像 self.mediaManager = [[KSMediaManager alloc] init]; self.mediaManager.mediaType = MEDIATYPE_VIDEO; self.mediaManager.maxPhotoNumber = 1; self.mediaManager.baseCtrl = self; self.mediaManager.maxDuration = self.sourceModel.singleSongRecordMinutes * 60; MJWeakSelf; [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) { NSLog(@"%@", videoUrl); // 不上传回调 [weakSelf refreshView:videoUrl fileKey:fileKey]; }]; [self.mediaManager takePhoto]; } else { // 删除 [self deleteFileWithKey:fileKey]; } } - (void)refreshView:(NSString *)videoUrl fileKey:(NSString *)fileKey { NSString *fileUrl = videoUrl; NSString *fileName = [[videoUrl componentsSeparatedByString:@"/"] lastObject]; // 保存文件路径 NSDictionary *parm = @{@"localFileUrl":videoUrl, @"fileName": fileName}; UserDefaultSet(parm, fileKey); [self.tableView reloadData]; [self.fileUrlArray addObject:fileUrl]; [self checkSubmitButtonEnable]; } - (void)deleteFileWithKey:(NSString *)fileKey { NSDictionary *parm = UserDefault(fileKey); NSString *localUrl = [parm stringValueForKey:@"localFileUrl"]; [self removeVideoWithPath:localUrl]; UserDefaultRemoveObjectForKey(fileKey); [self.fileUrlArray removeObject:localUrl]; [self checkSubmitButtonEnable]; [self.tableView reloadData]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { RecordListCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.fileMessage) { // 播放 NSString *localUrl = [cell.fileMessage stringValueForKey:@"localFileUrl"]; if ([[NSFileManager defaultManager] fileExistsAtPath:localUrl]) { [self playVideoWithUrl:[NSURL fileURLWithPath:localUrl]]; } else { NSString *remoteUrl = [cell.fileMessage stringValueForKey:@"remoteUrl"]; [self playVideoWithUrl:[NSURL URLWithString:remoteUrl]]; } } } - (void)checkSubmitButtonEnable { if (self.fileUrlArray.count == 0) { self.bottomView.finishButton.userInteractionEnabled = NO; [self.bottomView.finishButton setBackgroundImage:[UIImage imageNamed:@"button_unable"] forState:UIControlStateNormal]; } else { self.bottomView.finishButton.userInteractionEnabled = YES; [self.bottomView.finishButton setBackgroundImage:[UIImage imageNamed:@"button_nomal"] forState:UIControlStateNormal]; [self.bottomView.finishButton setBackgroundImage:[UIImage imageNamed:@"button_highlight"] forState:UIControlStateHighlighted]; } } #pragma mark --- lazying - (RecordBodyView *)topView { if (!_topView) { _topView = [RecordBodyView shareInstance]; MJWeakSelf; [_topView topviewAction:^(RECORDTOPACTION action) { [weakSelf backOrEndAction:action]; }]; } return _topView; } - (void)backOrEndAction:(RECORDTOPACTION)action { if (action == RECORDTOPACTION_BACK) { [self.navigationController popViewControllerAnimated:YES]; } else { // 时间结束 MJWeakSelf; if (self.fileUrlArray.count == 0) { [self KSShowMsg:@"考试结束" promptCompletion:^{ [weakSelf.navigationController popViewControllerAnimated:YES]; }]; } else { [KSNormalAlertView ks_showAlertWithTitle:@"考试时间已结束,是否提交?" leftTitle:@"取消" rightTitle:@"确定" cancel:^{ [weakSelf removeAllFile]; [weakSelf.navigationController popViewControllerAnimated:YES]; } confirm:^{ [weakSelf submitService]; }]; } } } - (RecordTipsView *)tipsView { if (!_tipsView) { _tipsView = [RecordTipsView shareInstance]; } return _tipsView; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.rowHeight = 130; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = HexRGB(0xf5f5f5); _tableView.showsVerticalScrollIndicator = NO; [_tableView registerNib:[UINib nibWithNibName:@"RecordListCell" bundle:nil] forCellReuseIdentifier:@"RecordListCell"]; _tableView.tableHeaderView = self.tipsView; UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)]; bottomView.backgroundColor = HexRGB(0xf3f4f8); _tableView.tableFooterView = bottomView; } return _tableView; } - (RecordBottomView *)bottomView { if (!_bottomView) { _bottomView = [RecordBottomView shareInstance]; _topView.topTitleLabel.text = @"录播模拟考试"; MJWeakSelf; [_bottomView submitMediaAction:^{ [weakSelf submitAction]; }]; } return _bottomView; } - (void)submitAction { // 是否确认提交 MJWeakSelf; [KSNormalAlertView ks_showAlertWithTitle:@"请确认已上传所有录播曲目" leftTitle:@"取消" rightTitle:@"确认" cancel:^{ } confirm:^{ [weakSelf submitService]; }]; } // 完成考试 - (void)submitService { [self removeAllFile]; MJWeakSelf; [self KSShowMsg:@"提交成功" promptCompletion:^{ // 移除所有缓存 UserDefaultRemoveObjectForKey(@"SimulationRecordEndTime"); [weakSelf.topView stopDurationTimer]; [weakSelf.navigationController popViewControllerAnimated:YES]; }]; } - (void)removeAllFile { for (NSInteger i = 0; i < self.songArray.count; i++) { SongModel *model = self.songArray[i]; NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, model.songName]; NSDictionary *fileMessage = UserDefault(fileKey); if (fileMessage) { NSString *localUrl = [fileMessage stringValueForKey:@"localFileUrl"]; UserDefaultRemoveObjectForKey(fileKey); [self removeVideoWithPath:localUrl]; [self.fileUrlArray removeObject:localUrl]; } } } - (void)removeVideoWithPath:(NSString *)videoUrl { NSFileManager *fileMamager = [NSFileManager defaultManager]; if ([fileMamager fileExistsAtPath:videoUrl]) { [fileMamager removeItemAtPath:videoUrl error:nil]; } } - (NSMutableArray *)fileUrlArray { if (!_fileUrlArray) { _fileUrlArray = [NSMutableArray arrayWithCapacity:self.songArray.count]; } return _fileUrlArray; } #pragma mark ------- 播放文件 - (void)playVideoWithUrl:(NSURL *)fileUrl { _playerFrame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin); _wmPlayer = [[WMPlayer alloc] initWithFrame:_playerFrame]; WMPlayerModel *playModel = [[WMPlayerModel alloc] init]; playModel.videoURL = fileUrl; _wmPlayer.playerModel = playModel; _wmPlayer.delegate = self; _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]; _bgView.backgroundColor = [UIColor blackColor]; [[UIApplication sharedApplication].keyWindow addSubview:_bgView]; [[UIApplication sharedApplication].keyWindow addSubview:_wmPlayer]; [[UIApplication sharedApplication].keyWindow bringSubviewToFront:_wmPlayer]; [_wmPlayer play]; } - (void)wmplayer:(WMPlayer *)wmplayer clickedCloseButton:(UIButton *)backBtn { [wmplayer removePlayer]; [_bgView removeFromSuperview]; [self setNeedsStatusBarAppearanceUpdate]; } - (void)wmplayer:(WMPlayer *)wmplayer clickedFullScreenButton:(UIButton *)fullScreenBtn { self.isRatation = !self.isRatation; if (self.isRatation) { [wmplayer removeFromSuperview]; [UIView animateWithDuration:1.0f animations:^{ wmplayer.transform = CGAffineTransformMakeRotation(M_PI_2); } completion:^(BOOL finished) { wmplayer.frame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin); [[UIApplication sharedApplication].keyWindow addSubview:wmplayer]; [[UIApplication sharedApplication].keyWindow bringSubviewToFront:wmplayer]; }]; } else { [wmplayer removeFromSuperview]; [UIView animateWithDuration:1.0f animations:^{ // 复原 wmplayer.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { wmplayer.frame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin); [[UIApplication sharedApplication].keyWindow addSubview:wmplayer]; [[UIApplication sharedApplication].keyWindow bringSubviewToFront:wmplayer]; }]; } } - (void)dealloc { [self.topView stopDurationTimer]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end