// // AccompanyDetailViewController.m // KulexiuForTeacher // // Created by 王智 on 2022/4/6. // #import "AccompanyDetailViewController.h" #import "AccompanyNavView.h" #import "AccompanyAlertView.h" #import "AccompanyCourseInfoCell.h" #import "AccompanyEvaluateCell.h" #import "AccompanyStudentEvaCell.h" #import "AccompanyArrangeCell.h" #import "AccompanyHomeworkCell.h" #import "AccompanyRemarkCell.h" #import "HomeworkDetailModel.h" #import "EvaluateDetailModel.h" #import "AccompanyDetailBottomView.h" #import "KSChatConversationViewController.h" #import #import #import "KSPremissionAlert.h" #import "OnlineClassManager.h" @interface AccompanyDetailViewController () { WMPlayer *_wmPlayer; CGRect _playerFrame; } @property (nonatomic, strong) UIView *bgView; @property (nonatomic, assign) BOOL isRatation; @property (nonatomic, strong) AccompanyNavView *navView; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) HomeworkDetailModel *homeworkModel; @property (nonatomic, strong) EvaluateDetailModel *evaluateModel; @property (nonatomic, strong) AccompanyDetailBottomView *bottomView; @property (nonatomic, strong) AccompanyAlertView *alertView; @property (nonatomic, strong) NSMutableArray *fileArray; @property (nonatomic, strong) OnlineClassManager *classManager; @end @implementation AccompanyDetailViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.ks_prefersNavigationBarHidden = YES; [self configUI]; } - (void)requestCourseInfoMessage { [self requestHomeworkMessage]; [self requestEvaluateMessage]; [self requestRoomConfig]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self requestCourseInfoMessage]; } - (void)requestRoomConfig { [KSNetworkingManager selectRoomConfigRequest:KS_GET success:^(NSDictionary * _Nonnull dic) { if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) { NSDictionary *result = [dic ks_dictionaryValueForKey:@"data"]; self.joinRoomBeforeTime = [result ks_integerValueForKey:@"practiceStartTime"]; self.quitRomeEndTime = [result ks_integerValueForKey:@"practiceEndTime"]; } else { [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY]; } } faliure:^(NSError * _Nonnull error) { }]; } - (void)requestHomeworkMessage { [KSNetworkingManager homeworkDetailRequest:KS_GET courseId:self.courseId studentId:self.studentId success:^(NSDictionary * _Nonnull dic) { if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) { self.homeworkModel = [[HomeworkDetailModel alloc] initWithDictionary:[dic ks_dictionaryValueForKey:@"data"]]; if (self.homeworkModel.submitHomework == 1) { if (![NSString isEmptyString:self.homeworkModel.studentAttachments]) { self.fileArray = [NSMutableArray arrayWithArray:[self.homeworkModel.studentAttachments componentsSeparatedByString:@","]]; } else { self.fileArray = [NSMutableArray array]; } } else { self.fileArray = [NSMutableArray array]; } } else { [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY]; } [self evaluateViewDisplay]; [self.tableView reloadData]; } faliure:^(NSError * _Nonnull error) { }]; } - (void)requestEvaluateMessage { [KSNetworkingManager selectRepliedRequest:KS_POST courseGroupId:self.courseGroupId courseScheduleId:self.courseId studentId:self.studentId success:^(NSDictionary * _Nonnull dic) { if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) { self.evaluateModel = [[EvaluateDetailModel alloc] initWithDictionary:[dic ks_dictionaryValueForKey:@"data"]]; } else { [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY]; } [self.tableView reloadData]; } faliure:^(NSError * _Nonnull error) { }]; } - (void)configUI { [self.scrollView removeFromSuperview]; [self.view addSubview:self.navView]; CGFloat height = [self.navView getViewHeight]; [self.navView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.mas_equalTo(self.view); make.height.mas_equalTo(height); }]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.top.mas_equalTo(self.view.mas_top).offset(kNaviBarHeight); make.bottom.mas_equalTo(self.view.mas_bottom).offset(-iPhoneXSafeBottomMargin); }]; } - (void)evaluateViewDisplay { if ([self.homeworkModel.courseStatus isEqualToString:@"COMPLETE"]) { self.tableView.tableFooterView = [UIView new]; } else { self.tableView.tableFooterView = self.bottomView; self.bottomView.sureButton.userInteractionEnabled = YES; [self.bottomView.sureButton setTitle:@"进入教室" forState:UIControlStateNormal]; [self.bottomView.sureButton setBackgroundColor:THEMECOLOR]; [self.bottomView.sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; } } - (void)evaluateWithStatusLabel:(UILabel *)statusLabel { if ([self.homeworkModel.courseStatus isEqualToString:@"COMPLETE"]) { statusLabel.text = @"已结束"; statusLabel.textColor = HexRGB(0x999999); } else if ([self.homeworkModel.courseStatus isEqualToString:@"ING"]) { statusLabel.text = @"进行中"; statusLabel.textColor = THEMECOLOR; } else { statusLabel.text = @"未开始"; statusLabel.textColor = HexRGB(0xff802c); } } #pragma mark --- table data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 6; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { AccompanyCourseInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccompanyCourseInfoCell"]; [self evaluateWithStatusLabel:cell.statusLabel]; [cell configWithStartTime:self.homeworkModel.startTime endTime:self.homeworkModel.endTime studentAvatar:self.homeworkModel.studentAvatar studentName:self.homeworkModel.studentName studentId:self.homeworkModel.studentId studentSubject:self.homeworkModel.subjectName]; cell.hideChatButton = NO; MJWeakSelf; [cell chatCalkback:^{ [weakSelf chatAction]; }]; return cell; } else if (indexPath.row == 1) { AccompanyEvaluateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccompanyEvaluateCell"]; NSString *message = self.evaluateModel.teacherReplied; BOOL hasEvaluate = [NSString isEmptyString:self.evaluateModel.teacherReplied] ? NO : YES; MJWeakSelf; [cell configWithEvaluateMessage:message hasEvaluate:hasEvaluate callback:^{ [weakSelf evaluateCourse]; }]; return cell; } else if (indexPath.row == 2) { AccompanyStudentEvaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccompanyStudentEvaCell"]; NSString *message = self.evaluateModel.studentReplied; BOOL hasEvaluate = [NSString isEmptyString:self.evaluateModel.studentReplied] ? NO : YES; [cell configWithEvaluateMessage:message starNum:self.evaluateModel.score hasEvaluate:hasEvaluate]; return cell; } else if (indexPath.row == 3) { AccompanyArrangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccompanyArrangeCell"]; BOOL hasArrange = self.homeworkModel.decorateHomework == 1 ? YES : NO; MJWeakSelf; [cell configWithHomeworkContent:self.homeworkModel.content hasArrangeHomework:hasArrange callback:^{ [weakSelf arrangeHomework]; }]; return cell; } else if (indexPath.row == 4) { BOOL hasSubmitHomework = self.homeworkModel.submitHomework == 1 ? YES : NO; BOOL isExpired = self.homeworkModel.homeworkExpire == 1 ? YES : NO; AccompanyHomeworkCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccompanyHomeworkCell"]; MJWeakSelf; [cell configWithAttachmentArray:self.fileArray hasSubmit:hasSubmitHomework isExpired:isExpired callback:^(NSInteger viewIndex) { [weakSelf playVideoIndex:viewIndex]; }]; return cell; } else { AccompanyRemarkCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccompanyRemarkCell"]; BOOL hasEvaluate = self.homeworkModel.reviewHomework == 1? YES : NO; MJWeakSelf; [cell configWithRemarkMessage:self.homeworkModel.teacherReplied hasEvaluate:hasEvaluate callback:^{ [weakSelf evaluateHomework]; }]; return cell; } } - (void)chatAction { if (self.homeworkModel && ![NSString isEmptyString:self.homeworkModel.imUserId]) { TUIChatConversationModel *model = [[TUIChatConversationModel alloc] init]; model.userID = self.homeworkModel.imUserId; KSChatConversationViewController *ctrl = [[KSChatConversationViewController alloc] init]; ctrl.conversation = model; [self.navigationController pushViewController:ctrl animated:YES]; } } - (void)evaluateCourse { if (![self.homeworkModel.courseStatus isEqualToString:@"COMPLETE"]) { [LOADING_MANAGER MBShowAUTOHidingInWindow:@"课程结束之后才可以评价哦~"]; return; } self.alertView = [AccompanyAlertView shareInstance]; self.alertView.alertTitle.text = @"评价学员"; self.alertView.tipsLabel.text = @"请输入您对本次课程学员表现的评价"; self.alertView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight); [self.alertView showInView:[UIApplication sharedApplication].keyWindow showStarView:NO]; MJWeakSelf; [self.alertView sureCallback:^(NSString * _Nonnull content, NSInteger starNum) { [weakSelf evaluateAction:content]; }]; } - (void)evaluateAction:(NSString *)content { if ([NSString isEmptyString:content]) { [LOADING_MANAGER MBShowAUTOHidingInWindow:@"请输入评价内容"]; return; } [LOADING_MANAGER showHUD]; [KSNetworkingManager teacherCourseRepliedRequest:KS_POST courseScheduleId:self.courseId courseGroupId:self.courseGroupId studentId:self.studentId teacherReplied:content success:^(NSDictionary * _Nonnull dic) { [LOADING_MANAGER removeHUD]; if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) { [LOADING_MANAGER MBShowAUTOHidingInWindow:@"评价成功"]; [self requestCourseInfoMessage]; } else { [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY]; } } faliure:^(NSError * _Nonnull error) { [LOADING_MANAGER removeHUD]; }]; } - (void)arrangeHomework { if (![self.homeworkModel.courseStatus isEqualToString:@"COMPLETE"]) { [LOADING_MANAGER MBShowAUTOHidingInWindow:@"课程结束之后才可以布置作业哦~"]; return; } self.alertView = [AccompanyAlertView shareInstance]; self.alertView.alertTitle.text = @"布置作业"; self.alertView.tipsLabel.text = @"请输入本次课程作业内容"; self.alertView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight); [self.alertView showInView:[UIApplication sharedApplication].keyWindow showStarView:NO]; MJWeakSelf; [self.alertView sureCallback:^(NSString * _Nonnull content, NSInteger starNum) { [weakSelf arrangeAction:content]; }]; } - (void)arrangeAction:(NSString *)homeworkContent { [LOADING_MANAGER showHUD]; [KSNetworkingManager homeworkDecorateRequest:KS_POST content:homeworkContent courseScheduleId:self.courseId success:^(NSDictionary * _Nonnull dic) { [LOADING_MANAGER removeHUD]; if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) { [LOADING_MANAGER MBShowAUTOHidingInWindow:@"布置成功"]; [self requestCourseInfoMessage]; } else { [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY]; } } faliure:^(NSError * _Nonnull error) { [LOADING_MANAGER removeHUD]; }]; } // 评价作业 - (void)evaluateHomework { if (self.homeworkModel.decorateHomework == 0) { [LOADING_MANAGER MBShowAUTOHidingInWindow:@"您还未布置作业"]; return; } else if (self.homeworkModel.submitHomework == 0) { [LOADING_MANAGER MBShowAUTOHidingInWindow:@"学生还未提交作业"]; return; } self.alertView = [AccompanyAlertView shareInstance]; self.alertView.alertTitle.text = @"作业点评"; self.alertView.tipsLabel.text = @"请输入您对本次作业的点评"; self.alertView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight); [self.alertView showInView:[UIApplication sharedApplication].keyWindow showStarView:NO]; MJWeakSelf; [self.alertView sureCallback:^(NSString * _Nonnull content, NSInteger starNum) { [weakSelf commentAction:content]; }]; } - (void)commentAction:(NSString *)commentMessage { [LOADING_MANAGER showHUD]; [KSNetworkingManager homeworkReviewRequest:KS_POST courseScheduleId:self.courseId studentId:self.studentId review:commentMessage success:^(NSDictionary * _Nonnull dic) { [LOADING_MANAGER removeHUD]; if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) { [LOADING_MANAGER MBShowAUTOHidingInWindow:@"点评成功"]; [self requestCourseInfoMessage]; } else { [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY]; } } faliure:^(NSError * _Nonnull error) { [LOADING_MANAGER removeHUD]; }]; } - (void)playVideoIndex:(NSInteger)index { if (self.fileArray.count > index) { NSString *fileUrl = self.fileArray[index]; [self playVideoWithUrl:fileUrl]; } } #pragma mark ------ WMPlayer - (void)playVideoWithUrl:(NSString *)fileUrl { fileUrl = [fileUrl getUrlEndcodeString]; _playerFrame = CGRectMake(0, iPhoneXSafeTopMargin, kScreenWidth, kScreenHeight - iPhoneXSafeTopMargin - iPhoneXSafeBottomMargin); _wmPlayer = [[WMPlayer alloc] initWithFrame:_playerFrame]; WMPlayerModel *playModel = [[WMPlayerModel alloc] init]; playModel.videoURL = [NSURL URLWithString:fileUrl]; _wmPlayer.playerModel = playModel; _wmPlayer.delegate = self; _wmPlayer.tintColor = THEMECOLOR; _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]; }]; } } #pragma mark --- lazying - (OnlineClassManager *)classManager { if (!_classManager) { _classManager = [[OnlineClassManager alloc] init]; } return _classManager; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; _tableView.backgroundColor = [UIColor clearColor]; _tableView.rowHeight = UITableViewAutomaticDimension; [_tableView registerNib:[UINib nibWithNibName:@"AccompanyCourseInfoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AccompanyCourseInfoCell"]; [_tableView registerNib:[UINib nibWithNibName:@"AccompanyEvaluateCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AccompanyEvaluateCell"]; [_tableView registerNib:[UINib nibWithNibName:@"AccompanyStudentEvaCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AccompanyStudentEvaCell"]; [_tableView registerNib:[UINib nibWithNibName:@"AccompanyArrangeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AccompanyArrangeCell"]; [_tableView registerNib:[UINib nibWithNibName:@"AccompanyHomeworkCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AccompanyHomeworkCell"]; [_tableView registerNib:[UINib nibWithNibName:@"AccompanyRemarkCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AccompanyRemarkCell"]; _tableView.estimatedRowHeight = 136.0f; self.bottomView.frame = CGRectMake(0, 0, kScreenWidth, 70); _tableView.tableFooterView = self.bottomView; } return _tableView; } - (AccompanyNavView *)navView { if (!_navView) { _navView = [AccompanyNavView shareInstance]; MJWeakSelf; [_navView navCallback:^{ [weakSelf backAction]; }]; } return _navView; } - (AccompanyDetailBottomView *)bottomView { if (!_bottomView) { _bottomView = [AccompanyDetailBottomView shareInstance]; MJWeakSelf; [_bottomView joinroomCallback:^{ [weakSelf tryJoinRoom]; }]; } return _bottomView; } - (void)tryJoinRoom { NSDateFormatter *dateFormatter = [NSObject getDateformatter]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *beginDate = [dateFormatter dateFromString:self.homeworkModel.startTime]; NSDate *endDate = [dateFormatter dateFromString:self.homeworkModel.endTime]; NSDate *currentDate = [NSDate date]; NSTimeInterval beginTimeInterval = [beginDate timeIntervalSinceDate:currentDate]; NSTimeInterval endTimeInterval = [currentDate timeIntervalSinceDate:endDate]; if (beginTimeInterval <= self.joinRoomBeforeTime * 60 && endTimeInterval < 0) { [self joinClassRoom]; } else if (endTimeInterval > 0) { [LOADING_MANAGER MBShowAUTOHidingInWindow:@"该课程已结束"]; } else { NSString *tipsString = [NSString stringWithFormat:@"课程还未开始,请在上课前%zd分钟进入", self.joinRoomBeforeTime]; [LOADING_MANAGER MBShowAUTOHidingInWindow:tipsString]; } } // 加入房间 - (void)joinClassRoom { // 加入房间前判断摄像头和麦克风逻辑 [RecordCheckManager checkCameraPremissionAvaiableCallback:^(PREMISSIONTYPE type) { [self afterCheckCameraCheckMic:type]; }]; } - (void)afterCheckCameraCheckMic:(PREMISSIONTYPE)cameraType { [RecordCheckManager checkMicPermissionAvaiableCallback:^(PREMISSIONTYPE type) { if (type == PREMISSIONTYPE_YES && cameraType == PREMISSIONTYPE_YES) { // 判断是否进行课前检测 [self.classManager joinRoomWithId:self.courseId subjectName:self.homeworkModel.subjectName classEndTime:self.homeworkModel.endTime inViewController:self]; } else { NSString *content = @""; CHECKDEVICETYPE checkType = CHECKDEVICETYPE_BOTH; if (cameraType == PREMISSIONTYPE_NO && type == PREMISSIONTYPE_NO) { content = @"请开启相机和麦克风访问权限"; checkType = CHECKDEVICETYPE_BOTH; } else if (cameraType == PREMISSIONTYPE_NO && type == PREMISSIONTYPE_YES) { content = @"请开启相机访问权限"; checkType = CHECKDEVICETYPE_CAMREA; } else if (cameraType == PREMISSIONTYPE_YES && type == PREMISSIONTYPE_NO) { content = @"请开启麦克风访问权限"; checkType = CHECKDEVICETYPE_MIC; } [self showAlertWithMessage:content type:checkType]; } }]; } - (void)showAlertWithMessage:(NSString *)message type:(CHECKDEVICETYPE)deviceType { [KSPremissionAlert shareInstanceDisplayImage:deviceType message:message showInView:self.view cancel:^{ } confirm:^{ [self openSettingView]; }]; } - (void)openSettingView { if (@available(iOS 10, *)) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil]; } else { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } } /* #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