SimulationExamRecordController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. //
  2. // SimulationExamRecordController.m
  3. // MusicGradeExam
  4. //
  5. // Created by Kyle on 2020/9/21.
  6. // Copyright © 2020 DayaMusic. All rights reserved.
  7. //
  8. #import "SimulationExamRecordController.h"
  9. #import "RecordBodyView.h"
  10. #import "RecordListCell.h"
  11. #import "RecordBottomView.h"
  12. #import "RecordTipsView.h"
  13. #import "KSMediaManager.h"
  14. #import "WMPlayer.h"
  15. #import "SongModel.h"
  16. #import "KSGuideMaskView.h"
  17. #import "KSNormalAlertView.h"
  18. @interface SimulationExamRecordController ()<UITableViewDelegate, UITableViewDataSource,WMPlayerDelegate>
  19. {
  20. WMPlayer *_wmPlayer;
  21. CGRect _playerFrame;
  22. }
  23. @property (nonatomic, assign) BOOL isRatation;
  24. @property (nonatomic, strong) UIView *bgView;
  25. @property (nonatomic, strong) RecordBodyView *topView;
  26. @property (nonatomic, strong) RecordTipsView *tipsView;
  27. @property (nonatomic, strong) UITableView *tableView;
  28. @property (nonatomic, strong) RecordBottomView *bottomView;
  29. @property (nonatomic, strong) NSMutableArray *songArray;
  30. @property (nonatomic, strong) KSMediaManager *mediaManager;
  31. @property (strong, nonatomic) MBProgressHUD *HUD;
  32. @property (nonatomic, strong) NSMutableArray *fileUrlArray;
  33. @property (nonatomic, assign) BOOL hasShowTips;
  34. @end
  35. @implementation SimulationExamRecordController
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. // Do any additional setup after loading the view.
  39. self.ks_prefersNavigationBarHidden = YES;
  40. [self configUI];
  41. [self evaluateSource];
  42. }
  43. - (void)viewWillAppear:(BOOL)animated {
  44. [super viewWillAppear:animated];
  45. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  46. }
  47. - (void)viewDidDisappear:(BOOL)animated {
  48. [super viewDidDisappear:animated];
  49. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  50. if(@available(iOS 13.0, *)){
  51. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDarkContent;
  52. }
  53. }
  54. - (void)evaluateSource {
  55. if (self.sourceModel == nil) {
  56. return;
  57. }
  58. [self.topView configTime:[NSString stringWithFormat:@"%.0f", self.sourceModel.subTime]];
  59. [self.tipsView configWithEndTime:self.sourceModel.examEndTime];
  60. NSData *songDate = [self.sourceModel.songJson dataUsingEncoding:NSUTF8StringEncoding];
  61. NSArray *listArray = [NSJSONSerialization JSONObjectWithData:songDate options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
  62. NSMutableArray *songList = [NSMutableArray array];
  63. for (NSDictionary *parm in listArray) {
  64. SongModel *model = [[SongModel alloc] initWithDictionary:parm];
  65. [songList addObject:model];
  66. }
  67. self.songArray = [NSMutableArray arrayWithArray:songList];
  68. for (NSInteger i = 0; i < self.songArray.count; i++) {
  69. SongModel *model = self.songArray[i];
  70. NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, model.songName];
  71. NSDictionary *fileMessage = UserDefault(fileKey);
  72. if (fileMessage) {
  73. NSString *remoteUrl = [fileMessage stringValueForKey:@"remoteUrl"];
  74. [self.fileUrlArray addObject:remoteUrl];
  75. }
  76. }
  77. [self checkSubmitButtonEnable];
  78. [self.tableView reloadData];
  79. }
  80. - (void)configUI {
  81. [self.view addSubview:self.topView];
  82. [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.top.left.right.mas_equalTo(self.view);
  84. make.height.mas_equalTo(self.view.mas_width).multipliedBy(79.0 / 207).offset(11);
  85. }];
  86. [self.view addSubview:self.bottomView];
  87. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.left.right.mas_equalTo(self.view);
  89. make.bottom.mas_equalTo(self.view.mas_bottomMargin);
  90. make.height.mas_equalTo(90);
  91. }];
  92. [self.view addSubview:self.tableView];
  93. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.left.right.mas_equalTo(self.view);
  95. make.top.mas_equalTo(self.topView.mas_bottom);
  96. make.bottom.mas_equalTo(self.bottomView.mas_top);
  97. }];
  98. }
  99. - (void)viewDidAppear:(BOOL)animated {
  100. [super viewDidAppear:animated];
  101. if (_hasShowTips == NO) {
  102. [self addIntroduceView];
  103. }
  104. }
  105. - (void)addIntroduceView {
  106. _hasShowTips = YES;
  107. CGRect rect1 = [self.tableView convertRect:self.tableView.tableHeaderView.frame toView:[UIApplication sharedApplication].keyWindow];
  108. rect1.origin.y -= 50;
  109. rect1.size.height += 50;
  110. UIBezierPath *pathOne = [UIBezierPath bezierPathWithRect:rect1];
  111. CGRect rect2 = rect1;
  112. rect2.size = CGSizeMake(kScreenWidth, 130);
  113. rect2.origin.y = CGRectGetMaxY(rect1);
  114. UIBezierPath *pathSecond = [UIBezierPath bezierPathWithRect:rect2];
  115. NSArray *tipsArray = @[@"请在录播时间内完成视频上传", @"点击上传曲目录播视频"];
  116. NSArray *bezierArray = @[pathOne,pathSecond];
  117. KSGuideMaskView *guideView = [[KSGuideMaskView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
  118. [guideView addTips:tipsArray transparentRect:bezierArray shaperLayerIndex:-1];
  119. [guideView showMaskViewInView:nil];
  120. }
  121. #pragma mark ----- table data source
  122. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  123. return self.songArray.count;
  124. }
  125. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  126. SongModel *model = self.songArray[indexPath.row];
  127. RecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecordListCell"];
  128. MJWeakSelf;
  129. [cell configCellWithSource:model examRegistrationId:self.examRegistrationId operationCallback:^(RECORDTYPE type, NSString *fileKey) {
  130. [weakSelf opreationCellType:type fileKey:fileKey];
  131. }];
  132. return cell;
  133. }
  134. - (void)opreationCellType:(RECORDTYPE)type fileKey:(NSString *)fileKey {
  135. if (type == RECORDTYPE_RECORD) { // 录像
  136. self.mediaManager = [[KSMediaManager alloc] init];
  137. self.mediaManager.mediaType = MEDIATYPE_VIDEO;
  138. self.mediaManager.maxPhotoNumber = 1;
  139. self.mediaManager.baseCtrl = self;
  140. self.mediaManager.maxDuration = self.sourceModel.singleSongRecordMinutes * 60;
  141. MJWeakSelf;
  142. [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
  143. NSLog(@"%@", videoUrl);
  144. // 不上传回调
  145. [weakSelf refreshView:videoUrl fileKey:fileKey];
  146. }];
  147. [self.mediaManager takePhoto];
  148. }
  149. else { // 删除
  150. [self deleteFileWithKey:fileKey];
  151. }
  152. }
  153. - (void)refreshView:(NSString *)videoUrl fileKey:(NSString *)fileKey {
  154. NSString *fileUrl = videoUrl;
  155. NSString *fileName = [[videoUrl componentsSeparatedByString:@"/"] lastObject];
  156. // 保存文件路径
  157. NSDictionary *parm = @{@"localFileUrl":videoUrl,
  158. @"fileName": fileName};
  159. UserDefaultSet(parm, fileKey);
  160. [self.tableView reloadData];
  161. [self.fileUrlArray addObject:fileUrl];
  162. [self checkSubmitButtonEnable];
  163. }
  164. - (void)deleteFileWithKey:(NSString *)fileKey {
  165. NSDictionary *parm = UserDefault(fileKey);
  166. NSString *localUrl = [parm stringValueForKey:@"localFileUrl"];
  167. [self removeVideoWithPath:localUrl];
  168. UserDefaultRemoveObjectForKey(fileKey);
  169. [self.fileUrlArray removeObject:localUrl];
  170. [self checkSubmitButtonEnable];
  171. [self.tableView reloadData];
  172. }
  173. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  174. RecordListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  175. if (cell.fileMessage) {
  176. // 播放
  177. NSString *localUrl = [cell.fileMessage stringValueForKey:@"localFileUrl"];
  178. if ([[NSFileManager defaultManager] fileExistsAtPath:localUrl]) {
  179. [self playVideoWithUrl:[NSURL fileURLWithPath:localUrl]];
  180. }
  181. else {
  182. NSString *remoteUrl = [cell.fileMessage stringValueForKey:@"remoteUrl"];
  183. [self playVideoWithUrl:[NSURL URLWithString:remoteUrl]];
  184. }
  185. }
  186. }
  187. - (void)checkSubmitButtonEnable {
  188. if (self.fileUrlArray.count == 0) {
  189. self.bottomView.finishButton.userInteractionEnabled = NO;
  190. [self.bottomView.finishButton setBackgroundImage:[UIImage imageNamed:@"button_unable"] forState:UIControlStateNormal];
  191. }
  192. else {
  193. self.bottomView.finishButton.userInteractionEnabled = YES;
  194. [self.bottomView.finishButton setBackgroundImage:[UIImage imageNamed:@"button_nomal"] forState:UIControlStateNormal];
  195. [self.bottomView.finishButton setBackgroundImage:[UIImage imageNamed:@"button_highlight"] forState:UIControlStateHighlighted];
  196. }
  197. }
  198. #pragma mark --- lazying
  199. - (RecordBodyView *)topView {
  200. if (!_topView) {
  201. _topView = [RecordBodyView shareInstance];
  202. MJWeakSelf;
  203. [_topView topviewAction:^(RECORDTOPACTION action) {
  204. [weakSelf backOrEndAction:action];
  205. }];
  206. }
  207. return _topView;
  208. }
  209. - (void)backOrEndAction:(RECORDTOPACTION)action {
  210. if (action == RECORDTOPACTION_BACK) {
  211. [self.navigationController popViewControllerAnimated:YES];
  212. }
  213. else { // 时间结束
  214. MJWeakSelf;
  215. if (self.fileUrlArray.count == 0) {
  216. [self KSShowMsg:@"考试结束" promptCompletion:^{
  217. [weakSelf.navigationController popViewControllerAnimated:YES];
  218. }];
  219. }
  220. else {
  221. [KSNormalAlertView ks_showAlertWithTitle:@"考试时间已结束,是否提交?" leftTitle:@"取消" rightTitle:@"确定" cancel:^{
  222. [weakSelf removeAllFile];
  223. [weakSelf.navigationController popViewControllerAnimated:YES];
  224. } confirm:^{
  225. [weakSelf submitService];
  226. }];
  227. }
  228. }
  229. }
  230. - (RecordTipsView *)tipsView {
  231. if (!_tipsView) {
  232. _tipsView = [RecordTipsView shareInstance];
  233. }
  234. return _tipsView;
  235. }
  236. - (UITableView *)tableView {
  237. if (!_tableView) {
  238. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  239. _tableView.delegate = self;
  240. _tableView.dataSource = self;
  241. _tableView.rowHeight = 130;
  242. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  243. _tableView.backgroundColor = HexRGB(0xf5f5f5);
  244. _tableView.showsVerticalScrollIndicator = NO;
  245. [_tableView registerNib:[UINib nibWithNibName:@"RecordListCell" bundle:nil] forCellReuseIdentifier:@"RecordListCell"];
  246. _tableView.tableHeaderView = self.tipsView;
  247. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
  248. bottomView.backgroundColor = HexRGB(0xf3f4f8);
  249. _tableView.tableFooterView = bottomView;
  250. }
  251. return _tableView;
  252. }
  253. - (RecordBottomView *)bottomView {
  254. if (!_bottomView) {
  255. _bottomView = [RecordBottomView shareInstance];
  256. _topView.topTitleLabel.text = @"录播模拟考试";
  257. MJWeakSelf;
  258. [_bottomView submitMediaAction:^{
  259. [weakSelf submitAction];
  260. }];
  261. }
  262. return _bottomView;
  263. }
  264. - (void)submitAction {
  265. // 是否确认提交
  266. MJWeakSelf;
  267. [KSNormalAlertView ks_showAlertWithTitle:@"请确认已上传所有录播曲目" leftTitle:@"取消" rightTitle:@"确认" cancel:^{
  268. } confirm:^{
  269. [weakSelf submitService];
  270. }];
  271. }
  272. // 完成考试
  273. - (void)submitService {
  274. [self removeAllFile];
  275. MJWeakSelf;
  276. [self KSShowMsg:@"提交成功" promptCompletion:^{
  277. // 移除所有缓存
  278. UserDefaultRemoveObjectForKey(@"SimulationRecordEndTime");
  279. [weakSelf.topView stopDurationTimer];
  280. [weakSelf.navigationController popViewControllerAnimated:YES];
  281. }];
  282. }
  283. - (void)removeAllFile {
  284. for (NSInteger i = 0; i < self.songArray.count; i++) {
  285. SongModel *model = self.songArray[i];
  286. NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, model.songName];
  287. NSDictionary *fileMessage = UserDefault(fileKey);
  288. if (fileMessage) {
  289. NSString *localUrl = [fileMessage stringValueForKey:@"localFileUrl"];
  290. UserDefaultRemoveObjectForKey(fileKey);
  291. [self removeVideoWithPath:localUrl];
  292. [self.fileUrlArray removeObject:localUrl];
  293. }
  294. }
  295. }
  296. - (void)removeVideoWithPath:(NSString *)videoUrl {
  297. NSFileManager *fileMamager = [NSFileManager defaultManager];
  298. if ([fileMamager fileExistsAtPath:videoUrl]) {
  299. [fileMamager removeItemAtPath:videoUrl error:nil];
  300. }
  301. }
  302. - (NSMutableArray *)fileUrlArray {
  303. if (!_fileUrlArray) {
  304. _fileUrlArray = [NSMutableArray arrayWithCapacity:self.songArray.count];
  305. }
  306. return _fileUrlArray;
  307. }
  308. #pragma mark ------- 播放文件
  309. - (void)playVideoWithUrl:(NSURL *)fileUrl {
  310. _playerFrame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin);
  311. _wmPlayer = [[WMPlayer alloc] initWithFrame:_playerFrame];
  312. WMPlayerModel *playModel = [[WMPlayerModel alloc] init];
  313. playModel.videoURL = fileUrl;
  314. _wmPlayer.playerModel = playModel;
  315. _wmPlayer.delegate = self;
  316. _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
  317. _bgView.backgroundColor = [UIColor blackColor];
  318. [[UIApplication sharedApplication].keyWindow addSubview:_bgView];
  319. [[UIApplication sharedApplication].keyWindow addSubview:_wmPlayer];
  320. [[UIApplication sharedApplication].keyWindow bringSubviewToFront:_wmPlayer];
  321. [_wmPlayer play];
  322. }
  323. - (void)wmplayer:(WMPlayer *)wmplayer clickedCloseButton:(UIButton *)backBtn {
  324. [wmplayer removePlayer];
  325. [_bgView removeFromSuperview];
  326. [self setNeedsStatusBarAppearanceUpdate];
  327. }
  328. - (void)wmplayer:(WMPlayer *)wmplayer clickedFullScreenButton:(UIButton *)fullScreenBtn {
  329. self.isRatation = !self.isRatation;
  330. if (self.isRatation) {
  331. [wmplayer removeFromSuperview];
  332. [UIView animateWithDuration:1.0f animations:^{
  333. wmplayer.transform = CGAffineTransformMakeRotation(M_PI_2);
  334. } completion:^(BOOL finished) {
  335. wmplayer.frame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin);
  336. [[UIApplication sharedApplication].keyWindow addSubview:wmplayer];
  337. [[UIApplication sharedApplication].keyWindow bringSubviewToFront:wmplayer];
  338. }];
  339. }
  340. else {
  341. [wmplayer removeFromSuperview];
  342. [UIView animateWithDuration:1.0f animations:^{
  343. // 复原
  344. wmplayer.transform = CGAffineTransformIdentity;
  345. } completion:^(BOOL finished) {
  346. wmplayer.frame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin);
  347. [[UIApplication sharedApplication].keyWindow addSubview:wmplayer];
  348. [[UIApplication sharedApplication].keyWindow bringSubviewToFront:wmplayer];
  349. }];
  350. }
  351. }
  352. - (void)dealloc {
  353. [self.topView stopDurationTimer];
  354. }
  355. /*
  356. #pragma mark - Navigation
  357. // In a storyboard-based application, you will often want to do a little preparation before navigation
  358. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  359. // Get the new view controller using [segue destinationViewController].
  360. // Pass the selected object to the new view controller.
  361. }
  362. */
  363. @end