// // MusicRoomViewController.m // KulexiuForTeacher // // Created by 王智 on 2022/6/14. // #import "MusicRoomViewController.h" #import "MusicRoomDetailModel.h" #import "MusicRoomCourseInfoCell.h" #import "AccompanyDetailBottomView.h" #import "RecordCheckManager.h" #import "KSPremissionAlert.h" #import "OnlineClassManager.h" #import "AccompanyArrangeCell.h" #import "MusicRoomStudentCell.h" #import "AccompanyAlertView.h" #import "KSGroupConversationController.h" #import "HomeworkDetailViewController.h" @interface MusicRoomViewController () @property (nonatomic, assign) NSInteger joinRoomBeforeTime; // 上课开始时间 @property (nonatomic, assign) NSInteger quitRomeEndTime; // 下课截止时间 @property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) MusicRoomDetailModel *detailModel; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) OnlineClassManager *classManager; @property (nonatomic, strong) AccompanyDetailBottomView *bottomView; @property (nonatomic, strong) AccompanyAlertView *alertView; @property (nonatomic, strong) NSMutableArray *studentArray; @end @implementation MusicRoomViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self allocTitle:@"课程详情"]; [self configUI]; } - (void)configUI { [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); make.bottom.mas_equalTo(self.view.mas_bottom).offset(-iPhoneXSafeBottomMargin); }]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self requestCourseInfoMessage]; } - (void)requestCourseInfoMessage { [self requestStudentList]; [self requestRoomConfig]; } - (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:@"pianoStartTime"]; self.quitRomeEndTime = [result ks_integerValueForKey:@"pianoEndTime"]; } else { [self MBPShow:MESSAGEKEY]; } } faliure:^(NSError * _Nonnull error) { }]; } - (void)requestStudentList { [self showhud]; [KSNetworkingManager musicRoomDetailRequest:KS_POST courseId:self.courseId success:^(NSDictionary * _Nonnull dic) { [self removehub]; if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) { self.detailModel = [[MusicRoomDetailModel alloc] initWithDictionary:[dic ks_dictionaryValueForKey:@"data"]]; self.studentArray = [NSMutableArray arrayWithArray:self.detailModel.studentHomeworkList.records]; [self evaluateBottomButtonStatus]; } else { [self MBPShow:MESSAGEKEY]; } [self.tableView reloadData]; } faliure:^(NSError * _Nonnull error) { [self removehub]; }]; } - (void)evaluateBottomButtonStatus { if ([self.detailModel.courseStatus isEqualToString:@"COMPLETE"]) { [self.bottomView.sureButton setTitle:@"已结束" forState:UIControlStateNormal]; [self.bottomView.sureButton setBackgroundColor:HexRGB(0xd5d5d5)]; } else if ([self.detailModel.courseStatus isEqualToString:@"ING"]) { [self.bottomView.sureButton setTitle:@"进入教室" forState:UIControlStateNormal]; [self.bottomView.sureButton setBackgroundColor:THEMECOLOR]; } else { [self.bottomView.sureButton setTitle:@"进入教室" forState:UIControlStateNormal]; [self.bottomView.sureButton setBackgroundColor:THEMECOLOR]; } } #pragma mark ----- tablb data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0 || section == 1) { return 1; } else { return self.studentArray.count; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { MusicRoomCourseInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicRoomCourseInfoCell"]; MJWeakSelf; [cell configWithSource:self.detailModel chatAction:^(NSString * _Nonnull targetId) { [weakSelf chatAction:targetId]; }]; return cell; } else if (indexPath.section == 1) { AccompanyArrangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccompanyArrangeCell"]; BOOL hasArrange = [self.detailModel.decorateHomework isEqualToString:@"1"] ? YES : NO; MJWeakSelf; [cell configWithHomeworkContent:self.detailModel.content hasArrangeHomework:hasArrange callback:^{ [weakSelf arrangeHomework]; }]; return cell; } else { BOOL hasArrange = [self.detailModel.decorateHomework isEqualToString:@"1"] ? YES : NO; Records *studentModel = self.studentArray[indexPath.row]; MusicRoomStudentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicRoomStudentCell"]; [cell configWithStudentInfo:studentModel hasArrangeHomework:hasArrange]; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { // 学生作业 if ([self.detailModel.decorateHomework isEqualToString:@"0"]) { [self MBPShow:@"您还未布置作业"]; return; } Records *studentModel = self.studentArray[indexPath.row]; HomeworkDetailViewController *detailCtrl = [[HomeworkDetailViewController alloc] init]; detailCtrl.courseId = self.detailModel.courseId; detailCtrl.studentId = studentModel.studentId; [self.navigationController pushViewController:detailCtrl animated:YES]; } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return CGFLOAT_MIN; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } - (void)chatAction:(NSString *)targetId { if ([NSString isEmptyString:targetId]) { [self MBPShow:@"报名未结束,暂无群组"]; return; } TUIChatConversationModel *model = [[TUIChatConversationModel alloc] init]; model.groupID = targetId; KSGroupConversationController *ctrl = [[KSGroupConversationController alloc] init]; ctrl.conversation = model; [self.navigationController pushViewController:ctrl animated:YES]; } - (void)arrangeHomework { if (![self.detailModel.courseStatus isEqualToString:@"COMPLETE"]) { [self MBPShow:@"课程结束之后才可以布置作业哦~"]; 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 { [self showhud]; [KSNetworkingManager homeworkDecorateRequest:KS_POST content:homeworkContent courseScheduleId:self.courseId success:^(NSDictionary * _Nonnull dic) { [self removehub]; if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) { [self MBPShow:@"布置成功"]; [self requestCourseInfoMessage]; } else { [self MBPShow:MESSAGEKEY]; } } faliure:^(NSError * _Nonnull error) { [self removehub]; }]; } #pragma mark ---- lazying - (OnlineClassManager *)classManager { if (!_classManager) { _classManager = [[OnlineClassManager alloc] init]; } return _classManager; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; _tableView.backgroundColor = [UIColor clearColor]; _tableView.rowHeight = UITableViewAutomaticDimension; _tableView.estimatedRowHeight = 136.0f; [_tableView registerNib:[UINib nibWithNibName:@"MusicRoomCourseInfoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MusicRoomCourseInfoCell"]; [_tableView registerNib:[UINib nibWithNibName:@"AccompanyArrangeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AccompanyArrangeCell"]; [_tableView registerNib:[UINib nibWithNibName:@"MusicRoomStudentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MusicRoomStudentCell"]; self.bottomView.frame = CGRectMake(0, 0, kScreenWidth, 70); _tableView.tableFooterView = self.bottomView; } return _tableView; } - (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.detailModel.startTime]; NSDate *endDate = [dateFormatter dateFromString:self.detailModel.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) { [self MBPShow:@"该课程已结束"]; } else { NSString *tipsString = [NSString stringWithFormat:@"课程还未开始,请在上课前%zd分钟进入", self.joinRoomBeforeTime]; [self MBPShow:tipsString]; } } // 加入房间 - (void)joinClassRoom { // 加入房间前判断摄像头和麦克风逻辑 PREMISSIONTYPE micEnable = [RecordCheckManager checkPermissionShowAlert:NO showInView:nil]; PREMISSIONTYPE cameraEnable = [RecordCheckManager checkCameraPremissionAvaiable:NO showInView:nil]; if (micEnable == PREMISSIONTYPE_YES && cameraEnable == PREMISSIONTYPE_YES) { // 进入教室 // 判断是否进行课前检测 [self.classManager joinRoomWithId:self.courseId subjectName:self.detailModel.subjectName classEndTime:self.detailModel.endTime inViewController:self]; } else { if (micEnable == PREMISSIONTYPE_NO && cameraEnable == PREMISSIONTYPE_NO) { // 如果麦克风权限和摄像头权限都没有 [self showAlertWithMessage:@"请开启相机和麦克风访问权限" type:CHECKDEVICETYPE_BOTH]; } else if (micEnable == PREMISSIONTYPE_NO) { // 如果没有麦克风权限 [self showAlertWithMessage:@"请开启麦克风访问权限" type:CHECKDEVICETYPE_MIC]; } else if (cameraEnable == PREMISSIONTYPE_NO) { // 如果没有摄像头权限 [self showAlertWithMessage:@"请开启相机访问权限" type:CHECKDEVICETYPE_CAMREA]; } } } - (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